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

New "lifetime bound not satisfied" error on associated type #117664

Closed
laurmaedje opened this issue Nov 7, 2023 · 6 comments
Closed

New "lifetime bound not satisfied" error on associated type #117664

laurmaedje opened this issue Nov 7, 2023 · 6 comments
Labels
C-bug Category: This is a bug. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly.

Comments

@laurmaedje
Copy link
Contributor

Code

I tried this code:

pub struct Foo<'a> {
    // We need to override the constraint's lifetime here so that `Tracked` is
    // covariant over the constraint.
    _tracked: Tracked<'a, Self, <Foo<'static> as Validate>::Constraint>,
}

impl<'a> Validate for Foo<'a> {
    type Constraint = Constraint;
}

pub trait Validate {
    type Constraint;
}

pub struct Constraint;

pub struct Tracked<'a, T, C = <T as Validate>::Constraint>
where
    T: Validate + ?Sized,
{
    pub value: &'a T,
    pub constraint: Option<&'a C>,
}

I expected to see this happen: Compiles.

Instead, this happened: Fails with:

error[E0478]: lifetime bound not satisfied
 --> src/lib.rs:9:5
  |
9 |     type Constraint = Constraint;
  |     ^^^^^^^^^^^^^^^
  |
note: lifetime parameter instantiated with the lifetime `'a` as defined here
 --> src/lib.rs:8:6
  |
8 | impl<'a> Validate for Foo<'a> {
  |      ^^
  = note: but lifetime parameter must outlive the static lifetime

For more information about this error, try `rustc --explain E0478`.

The whole setup with the default type parameter that is overridden is required to get Tracked to be covariant over the constraint. The code above is a minimal extract from a usage of comemo in typst. Linked issue: typst/typst#2600

See also this discussion on Zulip for how this setup came to be in the first place: https://rust-lang.zulipchat.com/#narrow/stream/122651-general/topic/.E2.9C.94.20Variance.20with.20associated.20types

Version it worked on

It most recently worked on:

rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: aarch64-apple-darwin
release: 1.73.0
LLVM version: 17.0.2

Version with regression

rustc 1.75.0-nightly (189d6c71f 2023-11-06)
binary: rustc
commit-hash: 189d6c71f3bb6c52113b5639a80839791974fd22
commit-date: 2023-11-06
host: aarch64-apple-darwin
release: 1.75.0-nightly
LLVM version: 17.0.4
@laurmaedje laurmaedje added C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression. labels Nov 7, 2023
@rustbot rustbot added I-prioritize Issue: Indicates that prioritization has been requested for this issue. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 7, 2023
@lqd
Copy link
Member

lqd commented Nov 7, 2023

I believe this is some other fallout from #117131, like #117598, and that it will be fixed by #117542 which should land real soon.

@Finchiedev
Copy link

Running cargo bisect-rustc seems to confirm #117131 is the culprit:

searched nightlies: from nightly-2023-11-03 to nightly-2023-11-07
regressed nightly: nightly-2023-11-04
searched commit range: a2f5f96...1bb6553
regressed commit: 2520ca8

bisected with cargo-bisect-rustc v0.6.7

Host triple: x86_64-unknown-linux-gnu
Reproduce with:

cargo bisect-rustc -- check 

@Jules-Bertholet
Copy link
Contributor

@rustbot label -regression-untriaged regression-from-stable-to-nightly

@compiler-errors

@rustbot rustbot added regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. and removed regression-untriaged Untriaged performance or correctness regression. labels Nov 7, 2023
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 7, 2023
@Finchiedev
Copy link

With #117542 merged, I believe this issue has been resolved? I am no longer able to reproduce the error on the latest nightly (rustc 1.75.0-nightly (edf0b1db0 2023-11-10))

@apiraino
Copy link
Contributor

@laurmaedje can you confirm the above comment? That is, is this issue fixed for you, too? Thanks

@laurmaedje
Copy link
Contributor Author

Yes, it's resolved for me. Thanks!

@apiraino apiraino removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Nov 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly.
Projects
None yet
Development

No branches or pull requests

7 participants