Repository navigation
Description
This is a tracking issue for the elided_lifetimes_in_associated_constant future-incompatibility lint.
The goal of this page is describe why this change was made and how you can fix code that is affected by it. It also provides a place to ask questions or register a complaint if you feel the change should not be made. For more information on the policy around future-compatibility warnings, see our breaking change policy guidelines.
What
The elided_lifetimes_in_associated_constant lint detects elided lifetimes that became erroneously allowed in associated constants after #97313.
struct Foo<'f>(&'f ());
impl Foo<'_> {
const STR: &str = "hello, world";
}They current desugar to fresh early-bound lifetimes on the parent impl, like:
struct Foo<'f>(&'f ());
impl<'__a> Foo<'_> {
const STR: &'__a str = "hello, world";
}This is in contrast to the way that elided lifetimes are treated in item-level consts (where elided lifetimes are resolved to 'static), and this behavior was also never formally decided -- static-in-const lifetime rules do not apply to associated consts (rust-lang/rfcs#1623 (comment), #38831 (comment)).
Why
It was never decided what to do with elided lifetimes in consts, and it is not clear that the current behavior is optimal here. This is to stop the leaking by making sure existing elided lifetimes are fixed back to using 'static as they were required to before version 1.64, and that new usages of elided lifetimes in consts are not introduced.
How to fix
Replace the elided lifetimes with 'static (or manually introduce or reference another lifetime):
struct Foo;
impl Foo {
const STR: &'static str = "hello, world";
}
Activity
jonathanstrong commentedon Aug 23, 2023
I'm old enough to remember when clippy used to nag me to change
&'static strto&strforconsts. Ugh.tgross35 commentedon Aug 24, 2023
Is it possible to loosen the implicit lifetime to
'staticlifetime for literals at least? This change is kind of churny and doesn't really seem to align with expected behavior.compiler-errors commentedon Aug 24, 2023
@tgross35: that's behavior that @rust-lang/lang needs to stabilize separately. I'll nominate this issue to see if opinions have changed since #38831, but this lint is specifically aimed at making sure people are not relying on behavior that was never formally stabilized.
Question for T-lang: What should we do here?
'_to'staticin associated consts) in the futurefix: nightly warning in server macro for lifetime (#1580)
repi commentedon Aug 29, 2023
The clippy warn-by-default lint
redundant_static_lifetimesconflicts with this warning with latest nightly, so users would have to disable one or the other which is unfortunate.compiler-errors commentedon Aug 29, 2023
@repi: The
redundant_static_lifetimeslint does not trigger on associated consts:rust/src/tools/clippy/clippy_lints/src/redundant_static_lifetimes.rs
Lines 104 to 108 in b2515fa
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a3f5fbfff746ad971f4ca458633990e6
Can you share code where you encountered a conflict between this future-compat warning and the clippy lint?
repi commentedon Aug 29, 2023
dug a bit deeper and I was wrong, this clippy lint indeed doesn't trigger on associated consts. sorry.
was able to resolve all of our lint warnings on this without
clippy::redundant_static_lifetimestriggering.[Code Structure] Fix latest nightly rustc build (rust-lang/rust#115010)
compiler-errors commentedon Aug 31, 2023
Lang team consensus is that we should go with (A.) above:
Since this was inadvertently stabilized and a separate RFC would need to be authored if it's desired to make elision work in associated consts.
See notes here: https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Triage.20meeting.202023-08-29/near/388257641
QuineDot commentedon Sep 1, 2023
Heads up: This lint as currently written accepts code that is not accepted in Rust 1.72 (nor 1.63).
From 1.64 to current stable, anonymous lifetimes were allowed, but they weren't specifically
'static; instead, each one was independent. The independent-ness caused things like nested elided lifetimes and ambiguous trait bounds for trait objects to fail.Under the change which accompanied this lint, elided lifetimes are no longer independent, they are specifically
'static. This allows more things to compile.Example (Rust 1.63 fails; Rust 1.72 fails; nightly succeeds):
Perhaps this will be considered fine since the lint is supposedly going to be
deny, but I thought I should mention it. If the lint doesn't go the way you anticipate, you may be unwittingly opting into a specific behavior ('static).The change also means that contravariant cases that worked from 1.64-1.72 fail on nightly:
But probably that's not ecosystem-breaking like the covariant case is.
More examples that probably add nothing
The last three examples in this section (before "
implHeaders") don't compile in Rust 1.63 (prior to #97313 landing) due to the use of'_. The first example compiles from 1.64 to current stable, but the second two examples did not. All three compile on nightly (with this lint).This example fails on 1.63 due to the
'_indyn Single<'_>. It compiles on 1.72.This example fails on 1.63 due to the
'_indyn Double<'a, '_>. It fails on 1.72 because it considers the elided trait object lifetime ambiguous in the face of two independent lifetimes.This example fails on 1.63 due to the use of
'_and eliding&lifetimes. It fails on 1.72 because some of the anonymous lifetimes aren't considered to be implicitly longer than the reference lifetime. (N.b. the spans are also broken in the error. Comment out theECScase to see better spans on theRECScase.)compiler-errors commentedon Sep 1, 2023
@QuineDot: thanks for the comment.
Yeah, I'm aware of this, and chose to lower to
'staticto avoid additional errors like #114706, but yeah, perhaps we shouldn't allow this. I'll modify the warning code to continue to lower to fresh lifetimes to preserve 1.64-1.73 behavior behind the warning.Rollup merge of rust-lang#115429 - compiler-errors:assoc-ct-lt-fallth…
62 remaining items
niv nixpkgs: update 518fb620 -> ea5a824f
niv nixpkgs: update f71e09a7 -> 2c27ab2e
ensc commentedon Jan 20, 2025
What is the state of this issue? rust stopped to warn about it somewhere between 1.80 and 1.84, and afair there were clippy warnings in some nightly versions which caused me to remove all the
'staticannotations.dtolnay commentedon Jan 20, 2025
This code stopped triggering the future incompatibility lint after #125258:
It continues to be triggered for this code:
As well as this code:
fix: Use explicit 'static lifetime in `define_str_enum`
Remove unneeded `'static` lifetimes on `&str`s in constants (#8698)
Add explicit static lifetime to consts (#2793)
Rollup merge of rust-lang#156508 - oli-obk:boom, r=petrochenkov
Rollup merge of rust-lang#156508 - oli-obk:boom, r=petrochenkov
Rollup merge of rust-lang#156508 - oli-obk:boom, r=petrochenkov
Rollup merge of rust-lang#156508 - oli-obk:boom, r=petrochenkov
Unrolled build for #156508