owned this note changed 2 years ago
Published Linked with GitHub

Weak type aliases for Edition 2024

https://github.com/rust-lang/rust/issues/112792

Implied vs explicit where clauses

Currently type Alias<'a, T> = &'a T; errors with feature(lazy_type_alias).

// implied `T: 'a` bound
// everybody likes that you don't need `where T: 'a`
type Alias<'a, T> = &'a T;

// should this imply `T: Clone`?
//
// lcnr says no!
// errs agrees!!!!
// jackh726 does not
struct NeedsClone<T: Clone>(T);
type Alias<T> = NeedsClone<T>;

Analogy

  • a where-clause is required "iff" the field would be required for a struct field
    • type Foo<..> = Bar
    • struct Foo<..> { b: Bar }

Question Niko can't remember

trait Foo<U> { }

impl<'a, T> Foo<&'a T> for T 
{
//.  ----- why *don't* we add a default `T: 'a` here
    type Bar = &'a T;
}

If we require T: Clone

  • Reversal where everything that is linting against T: Clone
    • now you have to ADD bounds in Rust 2024
  • But in future, maybe they become redundant again
  • If we make it implied today, doesn't preclude us from making it required again

Otherwise

If we wanted type Alias<T> = NeedsClone<T> to work, how would we implement it?

  • In the predicates_of(def_id) query, we populate it with the WF requirements from the right-hand side of the type alias
    • WF without normalizing

fun side-effect: adding additional trait bounds to the environment can break stuff, might want to be more clever :)

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=a82011eb51dee31831068b14bd279916

#![feature(lazy_type_alias)]

trait Trait<'a> {}

struct Foo<'a, 'b, T: Trait<'a>, U: Trait<'b>>(&'a (), &'b (), T, U);

type AliasOk<'a, 'b, T> = Foo<'a, 'b, T, T>
where
    for<'c> T: Trait<'c>;
    
type AliasErr<'a, 'b, T> = Foo<'a, 'b, T, T>
where
    T: Trait<'a>,
    T: Trait<'b>;

variance

fmease is working on computing the variance of a weak type alias by computing it from the rhs.

Select a repo