Skip to content

workaround rust invariant type #1951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
HerringtonDarkholme opened this issue Apr 24, 2025 · 0 comments
Closed

workaround rust invariant type #1951

HerringtonDarkholme opened this issue Apr 24, 2025 · 0 comments

Comments

@HerringtonDarkholme
Copy link
Member

HerringtonDarkholme commented Apr 24, 2025

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

@HerringtonDarkholme HerringtonDarkholme closed this as not planned Won't fix, can't repro, duplicate, stale Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant