-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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 array indexing when tuple indexing on an array #54292
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3344,6 +3344,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { | |
} | ||
}; | ||
} | ||
ty::Array(ty, _) if ty.is_numeric() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any reason to limit this suggestion to only |
||
let base = self.tcx.hir.node_to_pretty_string(base.id); | ||
let msg = format!("attempting to use tuple indexing on an array; try"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's change the wording to something along the lines of |
||
let suggestion = format!("{}[{}]", base, field); | ||
err.span_suggestion(field.span, &msg, suggestion); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
}, | ||
ty::RawPtr(..) => { | ||
let base = self.tcx.hir.node_to_pretty_string(base.id); | ||
let msg = format!("`{}` is a native pointer; try dereferencing it", base); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// issue #53712: make the error generated by using tuple indexing on an array more specific | ||
|
||
fn main() { | ||
let arr = [10, 20, 30, 40, 50]; | ||
arr.0; | ||
//~^ ERROR no field `0` on type `[{integer}; 5]` [E0609] | ||
//~| HELP attempting to use tuple indexing on an array; try | ||
//~| SUGGESTION arr[0] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
error[E0609]: no field `0` on type `[{integer}; 5]` | ||
--> $DIR/issue-53712.rs:5:9 | ||
| | ||
LL | arr.0; | ||
| ^ help: attempting to use tuple indexing on an array; try: `arr[0]` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0609`. |
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 wonder if you can get the numeric value of
field
and check wether it is smaller thanty::Array(_, len)
. That would give us a great degree of confidence that the suggestion is correct, we could even switch betweenApplicability
levels given that... 🤔