Skip to content

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2010,7 +2010,18 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
{
if let ObligationCauseCode::Pattern { span: Some(span), .. } = *cause.code() {
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
let suggestion = if expected_def.is_struct() {
let suggestion = if exp_found.found.is_unit() {
let exp_str = format!("{:?}", expected_def);

// `RangeInclusive`'s fields are private, we have to call methods instead.
if exp_str.contains("core::ops::RangeInclusive")
|| exp_str.contains("std::ops::RangeInclusive")
{
Comment on lines +2013 to +2019
Copy link
Contributor

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 a HirId to get the expression back) to Pattern so that we can then call .precedence() and compare it to ExprPrecedence::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.

Copy link
Member Author

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!

Copy link
Contributor

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

format!("({}).{}()", snippet, name)
} else {
format!("({}).{}", snippet, name)
}
} else if expected_def.is_struct() {
format!("{}.{}", snippet, name)
} else if expected_def.is_union() {
format!("unsafe {{ {}.{} }}", snippet, name)
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/range/issue-93122-bind-unit-range-to-unit.fixed
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
}
9 changes: 9 additions & 0 deletions src/test/ui/range/issue-93122-bind-unit-range-to-unit.rs
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 src/test/ui/range/issue-93122-bind-unit-range-to-unit.stderr
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`.