Skip to content
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

Why does using associated type cause 'a to be an invariant #129644

Closed
A4-Tacks opened this issue Aug 27, 2024 · 2 comments
Closed

Why does using associated type cause 'a to be an invariant #129644

A4-Tacks opened this issue Aug 27, 2024 · 2 comments
Labels
A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) C-discussion Category: Discussion or questions that doesn't represent real issues. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@A4-Tacks
Copy link

trait Foo { type Ty; }

#[derive(Debug, Clone)]
//struct X<'a, T: Foo>(&'a T);
struct X<'a, T: Foo>(&'a T, T::Ty);
impl<T: Foo> Foo for X<'_, T> {
    type Ty = T::Ty;
}
impl Foo for () {
    type Ty = ();
}

fn f2<'a>(x: X<'a, X<'static, ()>>) -> X<'a, X<'a, ()>> {
    x
}
error: lifetime may not live long enough
  --> src/main.rs:14:5
   |
13 | fn f2<'a>(x: X<'a, X<'static, ()>>) -> X<'a, X<'a, ()>> {
   |       -- lifetime `'a` defined here
14 |     x
   |     ^ returning this value requires that `'a` must outlive `'static`
   |
   = note: requirement occurs because of the type `X<'_, X<'_, ()>>`, which makes the generic argument `X<'_, ()>` inv
ariant
   = note: the struct `X<'a, T>` is invariant over the parameter `T`
   = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance

error: could not compile `test_` (bin "test_") due to 1 previous error

rustc version:

rustc 1.80.0 (051478957 2024-07-21)
binary: rustc
commit-hash: 051478957371ee0084a7c0913941d2a8c4757bb9
commit-date: 2024-07-21
host: aarch64-unknown-linux-gnu
release: 1.80.0
LLVM version: 18.1.7

edition 2021

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 27, 2024
@rust-lang rust-lang deleted a comment Aug 27, 2024
@rust-lang rust-lang deleted a comment Aug 27, 2024
@rust-lang rust-lang deleted a comment Aug 27, 2024
@fmease fmease added C-discussion Category: Discussion or questions that doesn't represent real issues. A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Aug 27, 2024
@fmease
Copy link
Member

fmease commented Aug 27, 2024

T::Ty forces X's T to be invariant because it may evaluate to (almost) arbitrary types which may reference T — and therefore one generally can't make assumptions about the 'concrete variance'. T::Ty may evaluate to e.g. T in which case it's covariant over T (assuming it's for X<'_, T>, T bare) or to e.g. fn(T) in which case it's contravariant over T (same assumption), etc. Hence invariance as a safe choice.

@fmease fmease added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Aug 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html) C-discussion Category: Discussion or questions that doesn't represent real issues. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants
@fmease @rustbot @Noratrieb @A4-Tacks and others