Skip to content

Tweak tuple indexing suggestion #110096

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

Merged
merged 1 commit into from
Apr 9, 2023
Merged
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
29 changes: 16 additions & 13 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2810,23 +2810,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
"cannot index into a value of type `{base_t}`",
);
// Try to give some advice about indexing tuples.
if let ty::Tuple(..) = base_t.kind() {
if let ty::Tuple(types) = base_t.kind() {
let mut needs_note = true;
// If the index is an integer, we can show the actual
// fixed expression:
if let ExprKind::Lit(ref lit) = idx.kind {
if let ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) = lit.node {
let snip = self.tcx.sess.source_map().span_to_snippet(base.span);
if let Ok(snip) = snip {
err.span_suggestion(
expr.span,
"to access tuple elements, use",
format!("{snip}.{i}"),
Applicability::MachineApplicable,
);
needs_note = false;
}
if let ExprKind::Lit(ref lit) = idx.kind
&& let ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) = lit.node
&& i < types.len().try_into().expect("expected tuple index to be < usize length")
{
let snip = self.tcx.sess.source_map().span_to_snippet(base.span);
if let Ok(snip) = snip {
err.span_suggestion(
expr.span,
"to access tuple elements, use",
format!("{snip}.{i}"),
Applicability::MachineApplicable,
);
needs_note = false;
}
} else if let ExprKind::Path(..) = idx.peel_borrows().kind {
err.span_label(idx.span, "cannot access tuple elements at a variable index");
}
if needs_note {
err.help(
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/index_message.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn main() {
let z = ();
let _ = z[0]; //~ ERROR cannot index into a value of type `()`
let z = (10,);
let _ = z[0]; //~ ERROR cannot index into a value of type `({integer},)`
}
2 changes: 1 addition & 1 deletion tests/ui/index_message.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0608]: cannot index into a value of type `()`
error[E0608]: cannot index into a value of type `({integer},)`
--> $DIR/index_message.rs:3:13
|
LL | let _ = z[0];
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/issues/issue-27842.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ fn main() {
let i = 0_usize;
let _ = tup[i];
//~^ ERROR cannot index into a value of type

// the case where the index is out of bounds
let tup = (10,);
let _ = tup[3];
//~^ ERROR cannot index into a value of type
}
12 changes: 11 additions & 1 deletion tests/ui/issues/issue-27842.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@ error[E0608]: cannot index into a value of type `({integer}, {integer}, {integer
--> $DIR/issue-27842.rs:9:13
|
LL | let _ = tup[i];
| ^^^^-^
| |
| cannot access tuple elements at a variable index
|
= help: to access tuple elements, use tuple indexing syntax (e.g., `tuple.0`)

error[E0608]: cannot index into a value of type `({integer},)`
--> $DIR/issue-27842.rs:14:13
|
LL | let _ = tup[3];
| ^^^^^^
|
= help: to access tuple elements, use tuple indexing syntax (e.g., `tuple.0`)

error: aborting due to 2 previous errors
error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0608`.