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

Tracking issue for future-incompatibility lint elided_lifetimes_in_associated_constant #115010

Open
2 of 3 tasks
compiler-errors opened this issue Aug 19, 2023 · 11 comments
Open
2 of 3 tasks
Labels
A-associated-items Area: Associated items (types, constants & functions) A-lifetimes Area: Lifetimes / regions A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-future-incompatibility Category: Future-incompatibility lints C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Milestone

Comments

@compiler-errors
Copy link
Member

compiler-errors commented Aug 19, 2023

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;
impl Foo {
    const STR: &str = "hello, world";
}

They current desugar to fresh early-bound lifetimes on the parent impl, like:

struct Foo;
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";
}

Tracking

@compiler-errors compiler-errors added the C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. label Aug 19, 2023
@jonathanstrong
Copy link

I'm old enough to remember when clippy used to nag me to change &'static str to &str for consts. Ugh.

@tgross35
Copy link
Contributor

Is it possible to loosen the implicit lifetime to 'static lifetime for literals at least? This change is kind of churny and doesn't really seem to align with expected behavior.

@compiler-errors
Copy link
Member Author

compiler-errors commented 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?

  • A. Continue with this lint, accepting churn, with the possibility of relaxing this (i.e. resolving '_ to 'static in associated consts) in the future
  • B. Fast-track stabilization of this extension to static-lifetimes-in-const (RFC 1623), and revert this lint before it makes its way to stable (1.74, so we have approx. 12 weeks to decide as of 2023/08/24)
  • C. Some other compromise in the middle, e.g. stabilizing the static-lifetime elision rule when some other condition is true (i.e. the self type has no other lifetimes)
  • D. Something else :^)

@compiler-errors compiler-errors added the I-lang-nominated Nominated for discussion during a lang team meeting. label Aug 24, 2023
gbj pushed a commit to leptos-rs/leptos that referenced this issue Aug 24, 2023
On the latest lifetime we're getting the following warning in the server
macro:
 warning: `&` without an explicit lifetime name cannot be used here
   --> src/login.rs:19:1
    |
 19 | #[server(Login, "/api")]
    | ^
    |
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #115010 <rust-lang/rust#115010>
    = note: this warning originates in the attribute macro `server` (in Nightly builds, run with -Z macro-backtrace for more info)
@repi
Copy link

repi commented Aug 29, 2023

The clippy warn-by-default lint redundant_static_lifetimes conflicts with this warning with latest nightly, so users would have to disable one or the other which is unfortunate.

@compiler-errors
Copy link
Member Author

compiler-errors commented Aug 29, 2023

@repi: The redundant_static_lifetimes lint does not trigger on associated consts:

if let ItemKind::Const(box ConstItem { ty: ref var_type, .. }) = item.kind {
Self::visit_type(var_type, cx, "constants have by default a `'static` lifetime");
// Don't check associated consts because `'static` cannot be elided on those (issue
// #2438)
}
(and should have been fixed since rust-lang/rust-clippy#2443)

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
Copy link

repi commented 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_lifetimes triggering.

@compiler-errors
Copy link
Member Author

Lang team consensus is that we should go with (A.) above:

A. Continue with this lint, accepting churn, with the possibility of relaxing this (i.e. resolving '_ to 'static in associated consts) in the future

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

@compiler-errors compiler-errors removed the I-lang-nominated Nominated for discussion during a lang team meeting. label Aug 31, 2023
@QuineDot
Copy link

QuineDot commented 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):

struct S;
impl S {
    const C: &&str = &"";
}

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:

struct Contra<'a>(PhantomData<fn(&'a str)>);

struct S;
impl S {
    const C: Contra<'_> = Contra(PhantomData);
}

fn f<'a>() {
    let _: Contra<'a> = S::C;
}

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 "impl Headers") 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 '_ in dyn Single<'_>. It compiles on 1.72.

use core::marker::PhantomData;
trait Single<'a>: 'a + Send + Sync {}
struct L<'l, 'm>(&'l str, &'m str);
impl<'a, 'b> L<'a, 'b> {
    const ECS: PhantomData<Box<dyn Single<'_>>> = PhantomData;
    const SCS: PhantomData<Box<dyn Single<'static>>> = PhantomData;

    const S_ECS: PhantomData<Box<dyn Single<'static> + 'static>> = Self::ECS;
    const S_SCS: PhantomData<Box<dyn Single<'static> + 'static>> = Self::SCS;
}

This example fails on 1.63 due to the '_ in dyn Double<'a, '_>. It fails on 1.72 because it considers the elided trait object lifetime ambiguous in the face of two independent lifetimes.

use core::marker::PhantomData;
trait Double<'a, 'b>: 'a + 'b + Send + Sync {}
struct L<'l, 'm>(&'l str, &'m str);
impl<'a, 'b> L<'a, 'b> {
    const EBCD: PhantomData<Box<dyn Double<'a, '_>>> = PhantomData;
}

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 the ECS case to see better spans on the RECS case.)

use core::marker::PhantomData;
trait Single<'a>: 'a + Send + Sync {}
struct R<'l, 'm, 'r>(&'l str, &'m str, &'r ());
impl<'a, 'b, 'r> R<'a, 'b, 'r> where 'a: 'r, 'b: 'r {
    const ECS: PhantomData<&dyn Single<'_>> = PhantomData;
    const RECS: PhantomData<&'r dyn Single<'_>> = PhantomData;
}

@compiler-errors
Copy link
Member Author

@QuineDot: thanks for the comment.

Yeah, I'm aware of this, and chose to lower to 'static to 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.

fmease added a commit to fmease/rust that referenced this issue Sep 2, 2023
…rough, r=cjgillot

Fall through when resolving elided assoc const lifetimes

`@QuineDot` makes a good point in rust-lang#115010 (comment) that we probably should not accept *more* code due to rust-lang#115011 even though that code will eventually become a forbid-warning in a few versions (rust-lang#115010 (comment)).

Fall through when walking thru the `AnonymousWarnToStatic` (renamed to `AnonymousWarn`) rib so that we can resolve as a fresh lifetime like we did before.
fmease added a commit to fmease/rust that referenced this issue Sep 2, 2023
…rough, r=cjgillot

Fall through when resolving elided assoc const lifetimes

``@QuineDot`` makes a good point in rust-lang#115010 (comment) that we probably should not accept *more* code due to rust-lang#115011 even though that code will eventually become a forbid-warning in a few versions (rust-lang#115010 (comment)).

Fall through when walking thru the `AnonymousWarnToStatic` (renamed to `AnonymousWarn`) rib so that we can resolve as a fresh lifetime like we did before.
bors added a commit to rust-lang-ci/rust that referenced this issue Sep 2, 2023
…ugh, r=cjgillot

Fall through when resolving elided assoc const lifetimes

`@QuineDot` makes a good point in rust-lang#115010 (comment) that we probably should not accept *more* code due to rust-lang#115011 even though that code will eventually become a forbid-warning in a few versions (rust-lang#115010 (comment)).

Fall through when walking thru the `AnonymousWarnToStatic` (renamed to `AnonymousWarn`) rib so that we can resolve as a fresh lifetime like we did before.
github-merge-queue bot pushed a commit to bevyengine/bevy that referenced this issue Sep 2, 2023
# Objective

Fix some nightly warnings found by running

`cargo +nightly clippy`

## Solution

Fix the following warnings:
- [x]
[elided_lifetimes_in_associated_constant](rust-lang/rust#115010)
2219861
- [x]
[unwrap_or_default](https://rust-lang.github.io/rust-clippy/master/index.html#/unwrap_or_default)
32e21c7
- [x]
[needless_pass_by_ref_mut](https://rust-lang.github.io/rust-clippy/master/index.html#/needless_pass_by_ref_mut)
c85d6d4

There is no breaking change, some internal `bevy_ecs` code no longer
uses a few mutable references but I don't think it should cause any
regression or be performance sensitive, but there might be some ECS
magic I'm unaware of that could break because of those changes
RalfJung pushed a commit to RalfJung/miri that referenced this issue Sep 3, 2023
…gillot

Fall through when resolving elided assoc const lifetimes

`@QuineDot` makes a good point in rust-lang/rust#115010 (comment) that we probably should not accept *more* code due to #115011 even though that code will eventually become a forbid-warning in a few versions (rust-lang/rust#115010 (comment)).

Fall through when walking thru the `AnonymousWarnToStatic` (renamed to `AnonymousWarn`) rib so that we can resolve as a fresh lifetime like we did before.
waywardmonkeys added a commit to waywardmonkeys/icu4x that referenced this issue Sep 4, 2023
This is a new warning in an upcoming version of Rust:

```
warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
note: for more information, see issue #115010 <rust-lang/rust#115010>
```
@holly-hacker
Copy link
Contributor

Apologies if I miss something obvious here, I've only been using Rust for a few years and haven't touched its internals, but why can this...

struct Foo;
impl Foo {
    const STR: &str = "hello, world";
}

... not be desugared into this?

struct Foo;
impl Foo {
    const STR: &'static str = "hello, world";
}

Is this some limitation of the compiler, or is this a backwards-incompatible change? If it's backwards-incompatible, isn't it better to change this behavior in a Rust edition?

I find it strange that const fields can have non-'static lifetimes in the first place, so I assume (nearly) nobody is depending on the fact that their associated consts have a lifetime that may be shorter than 'static.

@tgross35
Copy link
Contributor

tgross35 commented Sep 5, 2023

Apologies if I miss something obvious here, I've only been using Rust for a few years and haven't touched its internals, but why can this...

[...]

... not be desugared into this?

[...]

@holly-hacker the problem is that this is loosening bounds from the current behavior, which needs to go through the review process since it is part of a bigger picture. The notes where this was discussed are here: https://hackmd.io/4br69DHGRy2EIm-JY5dTgw#%E2%80%9CTracking-Issue-for-ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT-future-compat-lint%E2%80%9D-rust115010, I think that the RFC linked there illustrate some of the potential issues.

It seems like generally everyone is in agreement that the current behavior is not expected, it's just not yet clear how to make expected and actual behavior align. It might be possible to special-case something for the common cases - but those discussions and decisions will take time (I just opened some discussion on Zulip about this)

Is this some limitation of the compiler, or is this a backwards-incompatible change? If it's backwards-incompatible, isn't it better to change this behavior in a Rust edition?

Lints can be added outside of edition boundaries since they can always be disabled (editions are usually things that aren't configurable)

Hywan added a commit to Hywan/matrix-rust-sdk that referenced this issue Sep 6, 2023
This was previously accepted by the compiler but is being phased out;
it will become a hard error in a future release! See https://github.com/
rust-lang/rust/issues/115010.
gnoliyil pushed a commit to gnoliyil/fuchsia that referenced this issue Jan 27, 2024
This CL adds an explicit 'static lifetime to the a few constant to
unblock the toolchain roll. For more information on this lint, see
rust-lang/rust#115010.

Test: fx build host_x64/gen/src/developer/ffx/config/lib.clippy
Bug: 132466
Change-Id: I0c894b28b9ebfd50257d4811d0993186cba9a23d
Reviewed-on: https://fuchsia-review.googlesource.com/c/fuchsia/+/906953
Reviewed-by: Marie Janssen <jamuraa@google.com>
Fuchsia-Auto-Submit: Mitchell Kember <mkember@google.com>
Commit-Queue: Auto-Submit <auto-submit@fuchsia-infra.iam.gserviceaccount.com>
Reviewed-by: Tyler Mandry <tmandry@google.com>
Reviewed-by: David Koloski <dkoloski@google.com>
zeenix pushed a commit to zeenix/busd that referenced this issue Jan 30, 2024
```
warning: `&` without an explicit lifetime name cannot be used here
  --> src/fdo/dbus.rs:29:21
   |
29 |     pub const PATH: &str = "/org/freedesktop/DBus";
   |                     ^
   |
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, see issue #115010 <rust-lang/rust#115010>
   = note: `#[warn(elided_lifetimes_in_associated_constant)]` on by default
help: use the `'static` lifetime
   |
29 |     pub const PATH: &'static str = "/org/freedesktop/DBus";
   |                      +++++++
```
hoeppnerj pushed a commit to ibm-s390-linux/s390-tools that referenced this issue Feb 2, 2024
Fixes "warning: `&` without an explicit lifetime name cannot be used
here". This warning will become a hard error in "the future". For more
information, see issue #115010 <rust-lang/rust#115010>

Closes: #162
Reviewed-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
takaswie added a commit to alsa-project/snd-firewire-ctl-services that referenced this issue Mar 2, 2024
This commit suppresses the following warning reported by rustc version 1.75.

warning: `&` without an explicit lifetime name cannot be used here
    ...
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #115010 <rust-lang/rust#115010>
    = note: `#[warn(elided_lifetimes_in_associated_constant)]` on by default
help: use the `'static` lifetime
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
takaswie added a commit to alsa-project/snd-firewire-ctl-services that referenced this issue Mar 2, 2024
This commit suppresses the following warning reported by rustc version 1.75.

warning: `&` without an explicit lifetime name cannot be used here
    ...
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #115010 <rust-lang/rust#115010>
    = note: `#[warn(elided_lifetimes_in_associated_constant)]` on by default
help: use the `'static` lifetime
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
takaswie added a commit to alsa-project/snd-firewire-ctl-services that referenced this issue Mar 2, 2024
This commit suppresses the following warning reported by rustc version 1.75.

warning: `&` without an explicit lifetime name cannot be used here
    ...
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #115010 <rust-lang/rust#115010>
    = note: `#[warn(elided_lifetimes_in_associated_constant)]` on by default
help: use the `'static` lifetime
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
takaswie added a commit to alsa-project/snd-firewire-ctl-services that referenced this issue Mar 2, 2024
This commit suppresses the following warning reported by rustc version 1.75.

warning: `&` without an explicit lifetime name cannot be used here
    ...
    = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
    = note: for more information, see issue #115010 <rust-lang/rust#115010>
    = note: `#[warn(elided_lifetimes_in_associated_constant)]` on by default
help: use the `'static` lifetime
Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
maxdeviant added a commit to zed-industries/zed that referenced this issue Mar 2, 2024
This PR removes unneeded `'static` lifetimes on `&str`s stored in
`const` declarations.

This addresses some Clippy lints about
[`redundant_static_lifetimes`](https://rust-lang.github.io/rust-clippy/master/index.html#/redundant_static_lifetimes).

In item-level `const` declarations we can rely on lifetime elision and
use the default `'static` lifetime.

Note that associated constants still require an explicit `'static`
lifetime, as explained in
rust-lang/rust#115010.

Release Notes:

- N/A
ProxBot pushed a commit to proxmox/pve-installer that referenced this issue Jul 4, 2024
This produces as warning that will become a hard-error in the future.
See also: rust-lang/rust#115010

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
ArcticLampyrid added a commit to ArcticLampyrid/print_raster.rs that referenced this issue Aug 13, 2024
@fmease fmease changed the title Tracking Issue for ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT future-compat lint Tracking issue for future-incompatibility lint elided_lifetimes_in_associated_constant Sep 17, 2024
@fmease fmease added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-associated-items Area: Associated items (types, constants & functions) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-future-incompatibility Category: Future-incompatibility lints T-types Relevant to the types team, which will review and decide on the PR/issue. A-lifetimes Area: Lifetimes / regions labels Sep 17, 2024
itepastra added a commit to itepastra/nixpkgs that referenced this issue Nov 5, 2024
github-actions bot pushed a commit to Mic92/nixpkgs that referenced this issue Nov 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-lifetimes Area: Lifetimes / regions A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-future-incompatibility Category: Future-incompatibility lints C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. 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

7 participants