Skip to content

Variance not computed for concrete GAT references in struct fields #132198

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

Open
branchseer opened this issue Oct 26, 2024 · 1 comment
Open

Variance not computed for concrete GAT references in struct fields #132198

branchseer opened this issue Oct 26, 2024 · 1 comment
Labels
A-GATs Area: Generic associated types (GATs) 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

@branchseer
Copy link

branchseer commented Oct 26, 2024

Similar to #114221 (comment).

I tried this code:

trait Foo {
    type Ref<'a>;
}

struct Bar;
impl Foo for Bar {
    type Ref<'a> = &'a u8;
}

struct Baz<'a>(<Bar as Foo>::Ref<'a>);

fn f<'a>(s: Baz<'static>) { let _: Baz<'a> = s; }

I expected to see this happen: lifetime check passes

Instead, this happened:

error: lifetime may not live long enough
  --> src/main.rs:12:36
   |
12 | fn f<'a>(s: Baz<'static>) { let _: Baz<'a> = s; }
   |      -- lifetime `'a` defined here ^^^^^^^ type annotation requires that `'a` must outlive `'static`
   |
   = note: requirement occurs because of the type `Baz<'_>`, which makes the generic argument `'_` invariant
   = note: the struct `Baz<'a>` is invariant over the parameter `'a`
   = help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance

Although lifetime variance does get computed when the GAT type is directly referenced in the parameter type:

fn f<'a>(s: <Bar as Foo>::Ref<'static>) { let _: <Bar as Foo>::Ref<'a> = s; } // check passes

Edit:

Sames limitation on types:

trait Foo {
    type GAT<T>;
}

struct Bar;
impl Foo for Bar {
    type GAT<T> = T;
}


fn f<'a>(s: <Bar as Foo>::GAT<&'static ()>) { let _:  <Bar as Foo>::GAT<&'a ()> = s; } // this passes the check

struct Baz<T>(<Bar as Foo>::GAT<T>);
fn f1<'a>(s: Baz<&'static ()>) { let _: Baz<&'a ()> = s; } // but this doesn't

Meta

rustc --version --verbose:

rustc 1.84.0-nightly (c1db4dc24 2024-10-25)
binary: rustc
commit-hash: c1db4dc24267a707409c9bf2e67cf3c7323975c8
commit-date: 2024-10-25
host: aarch64-apple-darwin
release: 1.84.0-nightly
LLVM version: 19.1.1
Backtrace

<backtrace>

@branchseer branchseer added the C-bug Category: This is a bug. label Oct 26, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 26, 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) T-types Relevant to the types team, which will review and decide on the PR/issue. A-GATs Area: Generic associated types (GATs) and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 26, 2024
@branchseer branchseer changed the title Lifetime variance not computed for concrete GAT references in struct fields Variance not computed for concrete GAT references in struct fields Nov 10, 2024
@alex
Copy link
Member

alex commented Nov 17, 2024

I've run into the same issue, attempting to use a GAT in a struct that's wrapped with self_cell. A thing I'm not clear about is whether this is simply a limitation of the the existing compiler/borrow checker, or whether its an issue that requires changes to the Rust language.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-GATs Area: Generic associated types (GATs) 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

4 participants