Skip to content

Overly restrictive variance #32

Open
@lcnr

Description

@lcnr

What is this

This is a design document for const generics. Any discussions about its content should be on zulip. The conclusions of these discussions should then be edited back into this issue. Please do not post any comments directly in this issue.

Content

Currently all generic parameters used by constants in the type system are constrained
to be invariant. Looking at this example, that is unnecessary:

struct UsesRef<'a> {
    actual_ref: &'a (),
    in_anon_const: [u8; std::mem::size_of::<&'a ()>()],
}

Here 'a is forced to be invariant, as it is used in the anonymous constant, but
ideally it should be covariant.

How variance can influence CTFE

The most obvious example is TypeId::of which has to differ between for<'a> fn(&'a ()) and fn(&'static ()) and for<'a> fn(fn(&'a ()) and fn(fn(&'static ()).

Higher ranked types act different during candidate selection and can therefore not simply be ignored. We currently have a future compatability lint for this in #56105 which will probably get allowed in the future.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-generic-exprsGeneric const expressionsA-unificationUnifying constants in the type systemC-design-docsCategory: This is part of our design documentationK-behaviorDocument Kind: regarding user visible behaviorP-optionalPriority: not strictly requiredS-blocked

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions