Skip to content

Improve suggestion for lifetime preceding type in generics - #163454

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
tehforsch:fix-lifetime-preceding-type-in-generics
Oct 3, 2026
Merged

rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
tehforsch:fix-lifetime-preceding-type-in-generics

Conversation

@tehforsch

@tehforsch tehforsch commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Closes #59325

Specifically, this fixes cases like the 2nd example in the issue, where a lifetime precedes a type in generic args:
Foo<'static T1> and suggests either adding a reference, i.e. Foo<&'static T1> or a comma, i.e. Foo<'static, T1>.

In a case like Foo<'static mut T1>, only Foo<&'static mut T1> is suggested.

I initially wanted to do this similar to how the same situation is handled outside of generics (see maybe_recover_ref_ty_no_leading_ampersand) where we recover something like name: 'a str as name: &'a str.

However, in generics, both adding a & and adding a , might be appropriate, so recovering by guessing either one of them might cause more, unrelated errors later. I tried constructing a TyKind::Err(_) and continuing to parse the rest of the generic arguments, but again I'd run into errors later during name resolution.

Also finally, technically it's not super useful to suggest adding a comma in a case like Foo<A, 'a B>, since, after applying the suggestion we would have Foo<A, 'a, B> which isn't a valid type.
However I am guessing (from the grammar) that this is valid in the AST and is only rejected by some stage after parsing, so I left the suggestion in.

@rustbot

rustbot commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 28, 2026
@rustbot

rustbot commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project has assigned @JonathanBrouwer (or someone else) to review your changes, you should hear from them (or someone else) within the next two weeks.

Please see the contribution instructions and our LLM policy for more information.

Why was this reviewer chosen?

The reviewer was selected based on:

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

Comment on lines +548 to +557
pub(super) fn parse_ref_ty_no_leading_ampersand(&mut self) -> (Mutability, Option<Box<Ty>>) {
let mutbl = self.parse_mutability();
match self.parse_ty_no_plus() {
Ok(ty) => (mutbl, Some(ty)),
Err(err) => {
err.cancel();
(mutbl, None)
}
}
}

@tehforsch tehforsch Sep 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure whether adding this helper is really worth it, especially since naming is hard

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it's fine, there are plenty of helpers like this in the parser :)

Comment on lines +2298 to +2300
let snapshot = self.create_snapshot_for_diagnostic();
let (mutbl, ty) = self.parse_ref_ty_no_leading_ampersand();
self.restore_snapshot(snapshot);

@tehforsch tehforsch Sep 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure if maybe just doing

let mutbl = self.parse_mutability();
let is_ty = self.check_type();

is enough, since (other than maybe_recover_ref_ty_no_leading_ampersand) we don't actually need the parsed type

View changes since the review

@tehforsch tehforsch changed the title Fix lifetime preceding type in generics Fix suggestion for lifetime preceding type in generics Sep 28, 2026
@tehforsch tehforsch changed the title Fix suggestion for lifetime preceding type in generics Improve suggestion for lifetime preceding type in generics Sep 28, 2026

@JonathanBrouwer JonathanBrouwer left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@rust-bors

rust-bors Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

📌 Commit ca5f540 has been approved by JonathanBrouwer

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 3, 2026
rust-bors Bot pushed a commit that referenced this pull request Oct 3, 2026
…uwer

Rollup of 8 pull requests

Successful merges:

 - #163645 (add `ignore-compare-mode-next-solver` to all next-solver tests)
 - #163663 (reflection: fix ICE when TypeId::info called on ADT)
 - #163454 (Improve suggestion for lifetime preceding type in generics)
 - #163561 (tests: accept noundef in a couple more places)
 - #163633 (core: io: clean `unused_variables` lint under `no_io_statics`)
 - #163681 (Make the tracking issue template better suited for experiments)
 - #163685 (Avoid long iterator chain in all_rust_features)
 - #163690 (refactor: migrate to LLVM-C's `LLVMGetOrInsertFunction`)

Failed merges:

 - #161491 (Rip out old solver coherence)
@rust-bors
rust-bors Bot merged commit 57373cd into rust-lang:main Oct 3, 2026
13 checks passed
@rustbot rustbot added this to the 1.101.0 milestone Oct 3, 2026
rust-bors Bot pushed a commit that referenced this pull request Oct 3, 2026
Rollup merge of #163454 - Tehforsch:fix-lifetime-preceding-type-in-generics, r=JonathanBrouwer

Improve suggestion for lifetime preceding type in generics

Closes #59325

Specifically, this fixes cases like the 2nd example in the issue, where a lifetime precedes a type in generic args:
`Foo<'static T1>` and suggests either adding a reference, i.e. `Foo<&'static T1>` or a comma, i.e. `Foo<'static, T1>`.

In a case like `Foo<'static mut T1>`, only `Foo<&'static mut T1>` is suggested.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggest adding & when a lifetime immediately precedes a type in a generic arg list

3 participants