Skip to content

Infer all anonymous lifetimes in assoc consts as 'static#156508

Open
oli-obk wants to merge 2 commits into
rust-lang:mainfrom
oli-obk:boom
Open

Infer all anonymous lifetimes in assoc consts as 'static#156508
oli-obk wants to merge 2 commits into
rust-lang:mainfrom
oli-obk:boom

Conversation

@oli-obk
Copy link
Copy Markdown
Contributor

@oli-obk oli-obk commented May 12, 2026

View all comments

fixes #115010

This FCW has been on for almost a year, let's actually do the change that T-lang asked for now that there's no breakage to be expected anymore. I first did a crater run checking if turning it into a hard error breaks anything.

There are two cases that would be a hard error (but totally fine by defaulting to 'static), both based on an old version of minio. One is a personal crate on github with no changes in the last year (https://github.com/PapePathe/daaray_kaamil) and the other is https://github.com/gear-tech/gear, which is developed on github actively and does not have this issue anymore (also old minio dependency that was since updated). The crates.io version is 2 years outdated.

Thus we can pretty much conclude we're not going to cause anyone any confusion or subtle bugs by changing the rules. The rules for associated constants' types are now the same as the rules for free constants' types:

We now treat all anonymous lifetimes as 'static, irrespective of whether other lifetimes are in scope or not. This means the following piece of code starts compiling without warnings or errors again.

struct Foo<'a>(&'a ());

impl<'a> Foo<'a> {
    const STR: &str = "hello, world";
}

Unfortunately it also means that

trait Foo {
    const B: S = S { s: &() };
}

struct S<'a> {
    s: &'a (),
}

now has no warning or error (just like the same const as a free constant would have). It used to emit

error[E0726]: implicit elided lifetime not allowed here
  --> $DIR/missing-lifetime-in-assoc-const-type.rs:7:14
   |
LL |     const B: S = S { s: &() };
   |              ^ expected lifetime parameter
   |
help: indicate the anonymous lifetime
   |
LL |     const B: S<'_> = S { s: &() };
   |               ++++

It is very possible to keep this error around, but it seems unfortunate to have different behaviour between const items and assoc const items.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 12, 2026
@oli-obk
Copy link
Copy Markdown
Contributor Author

oli-obk commented May 12, 2026

@bors try

@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request May 12, 2026
Turn elided_lifetimes_in_associated_constant FCW into a hard error
@rust-log-analyzer

This comment has been minimized.

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 12, 2026

☀️ Try build successful (CI)
Build commit: b28cc4e (b28cc4eb48a6415b914e8ad0308be255ff8d73ed, parent: 2aabf3ce0569dd7c8c2d2a27944850056dcf2566)

@oli-obk
Copy link
Copy Markdown
Contributor Author

oli-obk commented May 13, 2026

@craterbot check

@craterbot
Copy link
Copy Markdown
Collaborator

👌 Experiment pr-156508 created and queued.
🤖 Automatically detected try build b28cc4e
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 13, 2026
@rustbot rustbot added the T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue. label May 13, 2026
@rust-bors

This comment has been minimized.

@craterbot
Copy link
Copy Markdown
Collaborator

🚧 Experiment pr-156508 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Copy Markdown
Collaborator

🎉 Experiment pr-156508 is completed!
📊 3 regressed and 10 fixed (931813 total)
📊 5201 spurious results on the retry-regressed-list.txt, consider a retry1 if this is a significant amount.
📰 Open the summary report.

⚠️ If you notice any spurious failure please add them to the denylist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

Footnotes

  1. re-run the experiment with crates=https://crater-reports.s3.amazonaws.com/pr-156508/retry-regressed-list.txt

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels May 23, 2026
@oli-obk
Copy link
Copy Markdown
Contributor Author

oli-obk commented May 26, 2026

@craterbot
Copy link
Copy Markdown
Collaborator

👌 Experiment pr-156508-1 created and queued.
🤖 Automatically detected try build b28cc4e
⚠️ Try build based on commit eaec6ce, but latest commit is a92df8e. Did you forget to make a new try build?
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 26, 2026
@oli-obk oli-obk changed the title Turn elided_lifetimes_in_associated_constant FCW into a hard error Infer all anonymous lifetimes in assoc consts as 'static May 26, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request May 29, 2026
Some cleanups around passing extra lifetime params from the resolver to ast lowering

No functional changes, mostly removing information that was never used at any step.

Possibly interesting to @petrochenkov and @cjgillot

I originally thought this was a necessary cleanup to be able to refactor our `extra_lifetime_params_map` handling, but it turns out that I can also just do rust-lang#156508 which removes all the ugly usage of that side table and makes it possible for that side table to be put into `PerOwnerResolverData`. The changes here still were an improvement on its own, thus this PR.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request May 29, 2026
Some cleanups around passing extra lifetime params from the resolver to ast lowering

No functional changes, mostly removing information that was never used at any step.

Possibly interesting to @petrochenkov and @cjgillot

I originally thought this was a necessary cleanup to be able to refactor our `extra_lifetime_params_map` handling, but it turns out that I can also just do rust-lang#156508 which removes all the ugly usage of that side table and makes it possible for that side table to be put into `PerOwnerResolverData`. The changes here still were an improvement on its own, thus this PR.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request May 29, 2026
Some cleanups around passing extra lifetime params from the resolver to ast lowering

No functional changes, mostly removing information that was never used at any step.

Possibly interesting to @petrochenkov and @cjgillot

I originally thought this was a necessary cleanup to be able to refactor our `extra_lifetime_params_map` handling, but it turns out that I can also just do rust-lang#156508 which removes all the ugly usage of that side table and makes it possible for that side table to be put into `PerOwnerResolverData`. The changes here still were an improvement on its own, thus this PR.
rust-timer added a commit that referenced this pull request May 30, 2026
Rollup merge of #156960 - oli-obk:push-rrmqnqvwtmsq, r=tiif

Some cleanups around passing extra lifetime params from the resolver to ast lowering

No functional changes, mostly removing information that was never used at any step.

Possibly interesting to @petrochenkov and @cjgillot

I originally thought this was a necessary cleanup to be able to refactor our `extra_lifetime_params_map` handling, but it turns out that I can also just do #156508 which removes all the ugly usage of that side table and makes it possible for that side table to be put into `PerOwnerResolverData`. The changes here still were an improvement on its own, thus this PR.
@rust-bors

This comment has been minimized.

@craterbot
Copy link
Copy Markdown
Collaborator

🚧 Experiment pr-156508-1 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Copy Markdown
Collaborator

🎉 Experiment pr-156508-1 is completed!
📊 2 regressed and 1 fixed (5012 total)
📊 946 spurious results on the retry-regressed-list.txt, consider a retry1 if this is a significant amount.
📰 Open the summary report.

⚠️ If you notice any spurious failure please add them to the denylist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

Footnotes

  1. re-run the experiment with crates=https://crater-reports.s3.amazonaws.com/pr-156508-1/retry-regressed-list.txt

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Jun 7, 2026
@oli-obk oli-obk marked this pull request as ready for review June 7, 2026 15:38
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 7, 2026

rust-analyzer is developed in its own repository. If possible, consider making this change to rust-lang/rust-analyzer instead.

cc @rust-lang/rust-analyzer

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 7, 2026

r? @JonathanBrouwer

rustbot has assigned @JonathanBrouwer.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 19 candidates

@oli-obk oli-obk added T-types Relevant to the types team, which will review and decide on the PR/issue. and removed T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue. labels Jun 7, 2026
@oli-obk
Copy link
Copy Markdown
Contributor Author

oli-obk commented Jun 7, 2026

@rfcbot fcp merge types

See the main comment of this PR for a description of the changes and rationale

@rust-rfcbot
Copy link
Copy Markdown
Collaborator

rust-rfcbot commented Jun 7, 2026

@oli-obk has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Jun 7, 2026
warn_since: None,
deny_since: None,
},
Lint {
Copy link
Copy Markdown
Contributor

@ChayimFriedman2 ChayimFriedman2 Jun 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is autogenerated, please revert the change.

View changes since the review

@lcnr
Copy link
Copy Markdown
Contributor

lcnr commented Jun 8, 2026

How come

trait Foo {
    const B: S = S { s: &() };
}

struct S<'a> {
    s: &'a (),
}

does not lint because S has an implicitly elided lifetime 🤔 I always forget the adt lifetime elision rules. I think in fn sigs we always require S<'_>, do we have these rules written down somewhere?

@oli-obk
Copy link
Copy Markdown
Contributor Author

oli-obk commented Jun 8, 2026

that's unrelated to this change. The reason it doesn't lint is "we gave up at some point due to too much churn and/or the person driving it lost momentum", so the lint has very random rules where things apply

@lcnr
Copy link
Copy Markdown
Contributor

lcnr commented Jun 8, 2026

Is it? We currently error here and you explicitly state

It is very possible to keep this error around, but it seems unfortunate to have different behaviour between const items and assoc const items.

I would prefer to keep these errors around. For associated consts their lifetimes can very much not be 'static, so having it be more explicit that there are any lifetimes at all feels valuable to me.

I don't have a strong opinion on what the behavior for free constants should be, but for assoc consts I do somewhat care about this

@oli-obk
Copy link
Copy Markdown
Contributor Author

oli-obk commented Jun 8, 2026

The churn was in the ecosystem for elided lifetimes. In this specific case we can indeed just turn it into a hard error, but then it's also a hard error if you use S<'_>.

These are entirely separate lints, but as long as we don't make it conditional on whether lifetimes are in scope, I'm fine with other solutions

I'll add a commit on top of this one so you can see the diff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-types Relevant to the types team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tracking issue for future-incompatibility lint elided_lifetimes_in_associated_constant

8 participants