Skip to content

Covariance combined with contravariance is not invariance unless observed. #31200

Description

@eddyb

Id<'a> does not appear to be invariant over 'a if the whole type is inferred (run on playpen):

type Id<'a> = fn(&'a ()) -> &'a ();
fn id<T>(x: T) -> T {x}
fn scope<F: for<'a> FnOnce(Id<'a>)>(f: F) { f(id) }
fn same<T>(_: T, _: T) {}

fn main() {
    // remove ::<Id> to hide error
    scope(|a| scope(|b| same::<Id>(a, b)));

    scope(|a| scope(|b| {
        let mut k: Id = a; // remove : Id to hide error
        k = b;
    }));
}

This testcase was derived from a (misbehaving) invariance check using fn(T) -> T by @bluss.

I'm not sure this is a soundness issue, because the regionck error appears as soon as the lifetime parameter is instantiated, even from trait impls, so only code completely generic over the type compiles.

However, specialization would allow observing Id<'a> through a completely generic function (like same) without explicit bounds, so it might be interesting to see what happens on #30652.

cc @nikomatsakis @arielb1 @aturon

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesArea: Lifetimes / regions

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions