-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Fix invalid suggestions on unit range bindings #97505
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// run-rustfix | ||
|
||
fn main() { | ||
let () = (()..).start; //~ ERROR: mismatched types | ||
let () = (..()).end; //~ ERROR: mismatched types | ||
let () = (..=()).end; //~ ERROR: mismatched types | ||
let () = (()..()).start; //~ ERROR: mismatched types | ||
let () = (()..=()).start(); //~ ERROR: mismatched types | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// run-rustfix | ||
|
||
fn main() { | ||
let () = ()..; //~ ERROR: mismatched types | ||
let () = ..(); //~ ERROR: mismatched types | ||
let () = ..=(); //~ ERROR: mismatched types | ||
let () = ()..(); //~ ERROR: mismatched types | ||
let () = ()..=(); //~ ERROR: mismatched types | ||
} |
78 changes: 78 additions & 0 deletions
78
src/test/ui/range/issue-93122-bind-unit-range-to-unit.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/issue-93122-bind-unit-range-to-unit.rs:4:9 | ||
| | ||
LL | let () = ()..; | ||
| ^^ ---- this expression has type `RangeFrom<()>` | ||
| | | ||
| expected struct `RangeFrom`, found `()` | ||
| | ||
= note: expected struct `RangeFrom<()>` | ||
found unit type `()` | ||
help: you might have meant to use field `start` whose type is `()` | ||
| | ||
LL | let () = (()..).start; | ||
| ~~~~~~~~~~~~ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-93122-bind-unit-range-to-unit.rs:5:9 | ||
| | ||
LL | let () = ..(); | ||
| ^^ ---- this expression has type `RangeTo<()>` | ||
| | | ||
| expected struct `RangeTo`, found `()` | ||
| | ||
= note: expected struct `RangeTo<()>` | ||
found unit type `()` | ||
help: you might have meant to use field `end` whose type is `()` | ||
| | ||
LL | let () = (..()).end; | ||
| ~~~~~~~~~~ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-93122-bind-unit-range-to-unit.rs:6:9 | ||
| | ||
LL | let () = ..=(); | ||
| ^^ ----- this expression has type `RangeToInclusive<()>` | ||
| | | ||
| expected struct `RangeToInclusive`, found `()` | ||
| | ||
= note: expected struct `RangeToInclusive<()>` | ||
found unit type `()` | ||
help: you might have meant to use field `end` whose type is `()` | ||
| | ||
LL | let () = (..=()).end; | ||
| ~~~~~~~~~~~ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-93122-bind-unit-range-to-unit.rs:7:9 | ||
| | ||
LL | let () = ()..(); | ||
| ^^ ------ this expression has type `std::ops::Range<()>` | ||
| | | ||
| expected struct `std::ops::Range`, found `()` | ||
| | ||
= note: expected struct `std::ops::Range<()>` | ||
found unit type `()` | ||
help: you might have meant to use field `start` whose type is `()` | ||
| | ||
LL | let () = (()..()).start; | ||
| ~~~~~~~~~~~~~~ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/issue-93122-bind-unit-range-to-unit.rs:8:9 | ||
| | ||
LL | let () = ()..=(); | ||
| ^^ ------- this expression has type `RangeInclusive<()>` | ||
| | | ||
| expected struct `RangeInclusive`, found `()` | ||
| | ||
= note: expected struct `RangeInclusive<()>` | ||
found unit type `()` | ||
help: you might have meant to use field `start` whose type is `()` | ||
| | ||
LL | let () = (()..=()).start(); | ||
| ~~~~~~~~~~~~~~~~~ | ||
|
||
error: aborting due to 5 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 don't think this is the right approach. I would instead add a
&hir::Expr
(or aHirId
to get the expression back) toPattern
so that we can then call.precedence()
and compare it toExprPrecedence::Field
. This will work regardless of the types. Also, with access to the expression itself, it'd be nice to see if notice these kind of range literals to suggest changing the range to the start or end, instead of constructing the range and then field accessing one of those.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, I've tried to implement your suggestion but it would be a bigger change than I thought. I'm going to ask some questions on Zulip once I get some time to summarize them!
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.
Switching to waiting on author then :) Feel free to request a review when ready, thanks!
@rustbot author