Skip to content

workaround rust invariant type #1951

Closed as not planned
Closed as not planned
@HerringtonDarkholme

Description

@HerringtonDarkholme

Consider this code

struct N<'a>(&'a ());
trait Bound { type Inner; }
impl<'a> Bound for N<'a> { type Inner = (); }
struct Container<T: Bound>(T::Inner);
fn cast<'a>(m: &'a Container<N<'static>>) -> &'a Container<N<'a>> {
  m // error, Container is invariant over T
}

Changing this to below works

struct N<'a>(&'a ());
trait Bound { type Inner; }
impl<'a> Bound for N<'a> { type Inner = (); }
struct Container<
  T: Bound, 
  D = <T as Bound>::Inner
>(T, D);
fn cast<'a>(m: &'a Container<N<'static>>) -> &'a Container<N<'a>> {
  m
}

See

rust-lang/rust#115799

rust-ndarray/ndarray#1480

Rust compiler can be smarter to detect that Innver is covariant but it does not

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions