Repository navigation
Improve suggestion for lifetime preceding type in generics - #163454
rust-bors[bot] merged 2 commits into
Conversation
|
The parser was modified, potentially altering the grammar of (stable) Rust cc @fmease |
|
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:
|
| 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) | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
I wasn't sure whether adding this helper is really worth it, especially since naming is hard
There was a problem hiding this comment.
I think it's fine, there are plenty of helpers like this in the parser :)
| let snapshot = self.create_snapshot_for_diagnostic(); | ||
| let (mutbl, ty) = self.parse_ref_ty_no_leading_ampersand(); | ||
| self.restore_snapshot(snapshot); |
There was a problem hiding this comment.
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
…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)
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.
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>, onlyFoo<&'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 likename: 'a strasname: &'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 aTyKind::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 haveFoo<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.