-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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 tuple-parentheses for enum variants #90677
Conversation
(rust-highfive has picked a reviewer for you, use r? to override) |
I would quite like to also handle normal functions with this change, currently the below doesn't get the tuple suggestion: fn f(_: (i32, i32)) {
}
pub fn main() {
f(1, 2);
} This appears to be because |
d4db5a9
to
0b42630
Compare
Can you add a UI test for this? It would go in fn f() -> Option<(i32, i8)> {
Some(1, 2)
//~^ ERROR this enum variant takes 1 argument but 2 arguments were supplied
} |
r? rust-lang/diagnostics |
This comment has been minimized.
This comment has been minimized.
0013b90
to
f58ef0f
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I think we could merge without this change, as it's not that common to have tuple arguments to regular functions. Though I may just be blind here due to my codebases not having this ^^ |
23bb0af
to
ec8a46f
Compare
Luckily there weren't many adjustments to make to handle this too, although I'd agree with you that it's far less common, at least in codebases I've been through too. Let me know what you think - rebased + tidied the commits. |
r? @camelid |
Left some comments. |
If you rebase, I can approve this :) |
No worries, thanks for sticking with me and for the review! I appreciate the guidance :) |
0afa9b2
to
a8bac98
Compare
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.
Thanks again!
@bors r+ |
📌 Commit a8bac98 has been approved by |
…=camelid Suggest tuple-parentheses for enum variants This follows on from rust-lang#86493 / rust-lang#86481, making the parentheses suggestion. To summarise, given the following code: ```rust fn f() -> Option<(i32, i8)> { Some(1, 2) } ``` The current output is: ``` error[E0061]: this enum variant takes 1 argument but 2 arguments were supplied --> b.rs:2:5 | 2 | Some(1, 2) | ^^^^ - - supplied 2 arguments | | | expected 1 argument error: aborting due to previous error For more information about this error, try `rustc --explain E0061`. ``` With this change, `rustc` will now suggest parentheses when: - The callee is expecting a single tuple argument - The number of arguments passed matches the element count in the above tuple - The arguments' types match the tuple's fields ``` error[E0061]: this enum variant takes 1 argument but 2 arguments were supplied --> b.rs:2:5 | 2 | Some(1, 2) | ^^^^ - - supplied 2 arguments | help: use parentheses to construct a tuple | 2 | Some((1, 2)) | + + ```
☀️ Test successful - checks-actions |
error = Some((expected_arg_count, supplied_arg_count, "E0061", sugg_unit)); | ||
|
||
// are we passing elements of a tuple without the tuple parentheses? | ||
let expected_input_tys = if expected_input_tys.is_empty() { |
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.
This is essentially duplicated on line 217.
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.
I guess this is because we need it for the call to suggested_tuple_wrap
?
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.
I think this whole (pre-existing) function could be cleaned up a lot in general. Like, why does this if
(and the one on L217) even need to exist? And why is an empty list used as a sentinel value?
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.
I'm thinking of trying to refactor it myself.
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.
Note that this gets changed quite a bit in #92364
Changing empty list as a sentinel is something orthogonal that I've thought about and worth doing.
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.
Yeah, the empty list led to a lot of confusion during the development of this too - like you say, it being used as a sentinel could be made more explicit (or even removed).
Finished benchmarking commit (e0e70c0): comparison url. Summary: This benchmark run did not return any relevant results. If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf. @rustbot label: -perf-regression |
… r=camelid Suggest 1-tuple parentheses on exprs without existing parens A follow-on from rust-lang#86116, split out from rust-lang#90677. This alters the suggestion to add a trailing comma to create a 1-tuple - previously we would only apply this if the relevant expression was parenthesised. We now make the suggestion regardless of parentheses, which reduces the fragility of the check (w.r.t formatting). e.g. ```rust let a: Option<(i32,)> = Some(3); ``` gets the below suggestion: ```rust let a: Option<(i32,)> = Some((3,)); // ^ ^^ ``` This change also improves the suggestion in other ways, such as by only making the suggestion if the types would match after the suggestion is applied and making the suggestion a multipart suggestion.
… r=camelid Suggest 1-tuple parentheses on exprs without existing parens A follow-on from rust-lang#86116, split out from rust-lang#90677. This alters the suggestion to add a trailing comma to create a 1-tuple - previously we would only apply this if the relevant expression was parenthesised. We now make the suggestion regardless of parentheses, which reduces the fragility of the check (w.r.t formatting). e.g. ```rust let a: Option<(i32,)> = Some(3); ``` gets the below suggestion: ```rust let a: Option<(i32,)> = Some((3,)); // ^ ^^ ``` This change also improves the suggestion in other ways, such as by only making the suggestion if the types would match after the suggestion is applied and making the suggestion a multipart suggestion.
… r=camelid Suggest 1-tuple parentheses on exprs without existing parens A follow-on from rust-lang#86116, split out from rust-lang#90677. This alters the suggestion to add a trailing comma to create a 1-tuple - previously we would only apply this if the relevant expression was parenthesised. We now make the suggestion regardless of parentheses, which reduces the fragility of the check (w.r.t formatting). e.g. ```rust let a: Option<(i32,)> = Some(3); ``` gets the below suggestion: ```rust let a: Option<(i32,)> = Some((3,)); // ^ ^^ ``` This change also improves the suggestion in other ways, such as by only making the suggestion if the types would match after the suggestion is applied and making the suggestion a multipart suggestion.
This follows on from #86493 / #86481, making the parentheses suggestion. To summarise, given the following code:
The current output is:
With this change,
rustc
will now suggest parentheses when: