Closed
Description
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