-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Suggest fix for misplaced generic params on fn item #103366 #103478
Suggest fix for misplaced generic params on fn item #103366 #103478
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @TaKO8Ki (or someone else) soon. Please see the contribution instructions for more information. |
This comment has been minimized.
This comment has been minimized.
54a3773
to
97c17e2
Compare
This comment has been minimized.
This comment has been minimized.
Marking this as ready for review - only note is that this does not currently give help/suggestions in the case of a If this PR gets approved, I can look at tackling the refactor to work with unions in future! 🥳 Alternatively if desired, I can look into doing all in the same PR - but I will be unable to dev for the next ~3 months as I'll be on holiday with a very tiny dev machine 😂 |
ping <3 |
Sorry for the late reply. I will review in a day or two. |
Thank you :D
…On Tue, Nov 15, 2022 at 7:00 PM Takayuki Maeda ***@***.***> wrote:
Sorry for the late reply. I will review in a day or two.
—
Reply to this email directly, view it on GitHub
<#103478 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AGVFWMOP34GMGVKIYCCCFXTWIM7KDANCNFSM6AAAAAARM6IPHA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
generic.span.to(self.token.span), | ||
format!("place the generic parameter name after the {ident_name} name"), | ||
format!(" {ident}{snippet}"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use a smaller span maybe like this?
generic.span.to(self.token.span), | |
format!("place the generic parameter name after the {ident_name} name"), | |
format!(" {ident}{snippet}"), | |
self.token.span.shrink_to_hi(), | |
format!("place the generic parameter name after the {ident_name} name"), | |
snippet, |
Example
error: expected identifier, found `<`
--> $DIR/fn-simple.rs:5:3
|
LL | fn<T> id(x: T) -> T { x }
| ^ expected identifier
|
help: place the generic parameter name after the fn name
|
LL | fn id<T>(x: T) -> T { x }
| ~~~
error: aborting due to previous error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The suggestion ends up with
error: expected identifier, found `<`
--> $DIR/fn-simple.rs:5:3
|
LL | fn<T> id(x: T) -> T { x }
| ^ expected identifier
|
help: place the generic parameter name after the fn name
|
LL | fn<T> id<T>(x: T) -> T { x }
| +++
error: aborting due to previous error
notably since we are inserting a suggestion (<T>
) at the end of id
- we don't remove the original <T>
on the right of the fn
.
I looked around in the compiler docs, but couldn't quite see a way to do the insertion as before, but able to highlight a portion of the suggestion. The initial PR does the correct insertion (i.e. remove the invalid <T>
and insert the correct place), but I suppose what we want is the original suggestion, but with only the insertion of <T>
after id
highlighted (and not the removal/id
highlighted).
Thoughts/advice? Let me know if I need to phrase it differently 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TaKO8Ki Just a bump on ^ - In the meantime, I've pushed a revert to the previous span (i.e. the ident and the generic)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For this, you'll need a multipart_suggestion
, which takes a vector of (Span, String)
pairs. In your case, one of the pair would be (self.token.span.shrink_to_hi(), snippet)
that @TaKO8Ki suggested, and a second pair (generics.span, String::new())
to require deletion of the generics.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooh thanks!! I'll have a look 🎉🎉
@rustbot author |
Thanks for the review :)
EDIT: I realise I can probably edit through github directly and see how that goes 😛 |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
--wip-- [skip ci] get the generic text and put it int he suggestion, but suggestion not working on derive subdiagnostic refactor away from derives and use span_suggestion() instead. Show's the correct(?) generic contents, but overwrites the fn name :( x fmt drop commented code and s/todo/fixme get the correct diagnostic for functions, at least x fmt remove some debugs remove format remove debugs remove useless change remove useless change remove legacy approach correct lookahead + error message contains the ident name fmt refactor code tests add tests remoev debug remove comment
Co-authored-by: Takayuki Maeda <takoyaki0316@gmail.com>
4868ad6
to
8292d07
Compare
@rustbot label -S-waiting-on-author +S-waiting-on-review |
@rustbot author |
This comment has been minimized.
This comment has been minimized.
@rustbot label -S-waiting-on-author +S-waiting-on-review |
Sorry for the late review. @bors r+ rollup |
…66_fix, r=TaKO8Ki Suggest fix for misplaced generic params on fn item rust-lang#103366 fixes rust-lang#103366 This still has some work to go, but works for 2/3 of the initial base cases described in #1033366 simple fn: ``` error: expected identifier, found `<` --> shreys/test_1.rs:1:3 | 1 | fn<T> id(x: T) -> T { x } | ^ expected identifier | help: help: place the generic parameter list after the function name: | 1 | fn id<T>(x: T) -> T { x } | ~~~~ ``` Complicated bounds ``` error: expected identifier, found `<` --> spanishpear/test_2.rs:1:3 | 1 | fn<'a, B: 'a + std::ops::Add<Output = u32>> f(_x: B) { } | ^ expected identifier | help: help: place the generic parameter list after the function name: | 1 | fn f<'a, B: 'a + std::ops::Add<Output = u32>>(_x: B) { } | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` Opening a draft PR for comments on approach, particularly I have the following questions: - [x] Is it okay to be using `err.span_suggestion` over struct derives? I struggled to get the initial implementation (particularly the correct suggestion message) on struct derives, although I think given what I've learned since starting, I could attempt re-doing it with that approach. - [x] in the case where the snippet cannot be obtained from a span, is the `help` but no suggestion okay? I think yes (also, when does this case occur?) - [x] are there any red flags for the generalisation of this work for relevant item kinds (i.e. `struct`, `enum`, `trait`, and `union`). My basic testing indicates it does work for those types except the help tip is currently hardcoded to `after the function name` - which should change dependent on the item. - [x] I am planning to not show the suggestion if there is already a `<` after the item identifier, (i.e. if there are already generics, as after a function name per the original issue). Any major objections? - [x] Is the style of error okay? I wasn't sure if there was a way to make it display nicer, or if thats handled by span_suggestion These aren't blocking questions, and I will keep working on: - check if there is a `<` after the ident (and if so, not showing the suggestion) - generalize the help message - figuring out how to write/run/etc ui tests (including reading the docs for them) - logic cleanups
…66_fix, r=TaKO8Ki Suggest fix for misplaced generic params on fn item rust-lang#103366 fixes rust-lang#103366 This still has some work to go, but works for 2/3 of the initial base cases described in #1033366 simple fn: ``` error: expected identifier, found `<` --> shreys/test_1.rs:1:3 | 1 | fn<T> id(x: T) -> T { x } | ^ expected identifier | help: help: place the generic parameter list after the function name: | 1 | fn id<T>(x: T) -> T { x } | ~~~~ ``` Complicated bounds ``` error: expected identifier, found `<` --> spanishpear/test_2.rs:1:3 | 1 | fn<'a, B: 'a + std::ops::Add<Output = u32>> f(_x: B) { } | ^ expected identifier | help: help: place the generic parameter list after the function name: | 1 | fn f<'a, B: 'a + std::ops::Add<Output = u32>>(_x: B) { } | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` Opening a draft PR for comments on approach, particularly I have the following questions: - [x] Is it okay to be using `err.span_suggestion` over struct derives? I struggled to get the initial implementation (particularly the correct suggestion message) on struct derives, although I think given what I've learned since starting, I could attempt re-doing it with that approach. - [x] in the case where the snippet cannot be obtained from a span, is the `help` but no suggestion okay? I think yes (also, when does this case occur?) - [x] are there any red flags for the generalisation of this work for relevant item kinds (i.e. `struct`, `enum`, `trait`, and `union`). My basic testing indicates it does work for those types except the help tip is currently hardcoded to `after the function name` - which should change dependent on the item. - [x] I am planning to not show the suggestion if there is already a `<` after the item identifier, (i.e. if there are already generics, as after a function name per the original issue). Any major objections? - [x] Is the style of error okay? I wasn't sure if there was a way to make it display nicer, or if thats handled by span_suggestion These aren't blocking questions, and I will keep working on: - check if there is a `<` after the ident (and if so, not showing the suggestion) - generalize the help message - figuring out how to write/run/etc ui tests (including reading the docs for them) - logic cleanups
…iaskrgr Rollup of 10 pull requests Successful merges: - rust-lang#103478 ( Suggest fix for misplaced generic params on fn item rust-lang#103366 ) - rust-lang#107739 (Check for overflow in evaluate_canonical_goal) - rust-lang#108003 (Avoid ICE when the generic_span is empty) - rust-lang#108016 ("Basic usage" is redundant for there is just one example) - rust-lang#108023 (Shrink size of array benchmarks) - rust-lang#108024 (add message to update Cargo.toml when x is changed) - rust-lang#108025 (rustdoc: add more tooltips to intra-doc links) - rust-lang#108029 (s/eval_usize/eval_target_usize/ for clarity) - rust-lang#108035 (Avoid using a dead email address as the main email address) - rust-lang#108038 (Remove needless supertrait constraints from Interner projections) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
fixes #103366
This still has some work to go, but works for 2/3 of the initial base cases described in #1033366
simple fn:
Complicated bounds
Opening a draft PR for comments on approach, particularly I have the following questions:
err.span_suggestion
over struct derives? I struggled to get the initial implementation (particularly the correct suggestion message) on struct derives, although I think given what I've learned since starting, I could attempt re-doing it with that approach.help
but no suggestion okay? I think yes (also, when does this case occur?)struct
,enum
,trait
, andunion
). My basic testing indicates it does work for those types except the help tip is currently hardcoded toafter the function name
- which should change dependent on the item.<
after the item identifier, (i.e. if there are already generics, as after a function name per the original issue). Any major objections?These aren't blocking questions, and I will keep working on:
<
after the ident (and if so, not showing the suggestion)