Skip to content

Commit eed27ac

Browse files
authored
Rollup merge of rust-lang#110096 - compiler-errors:tweak-tuple-idx-msg, r=Nilstrieb
Tweak tuple indexing suggestion Fixes rust-lang#110091
2 parents 01fcd19 + fbc3457 commit eed27ac

File tree

5 files changed

+35
-17
lines changed

5 files changed

+35
-17
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -2810,23 +2810,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
28102810
"cannot index into a value of type `{base_t}`",
28112811
);
28122812
// Try to give some advice about indexing tuples.
2813-
if let ty::Tuple(..) = base_t.kind() {
2813+
if let ty::Tuple(types) = base_t.kind() {
28142814
let mut needs_note = true;
28152815
// If the index is an integer, we can show the actual
28162816
// fixed expression:
2817-
if let ExprKind::Lit(ref lit) = idx.kind {
2818-
if let ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) = lit.node {
2819-
let snip = self.tcx.sess.source_map().span_to_snippet(base.span);
2820-
if let Ok(snip) = snip {
2821-
err.span_suggestion(
2822-
expr.span,
2823-
"to access tuple elements, use",
2824-
format!("{snip}.{i}"),
2825-
Applicability::MachineApplicable,
2826-
);
2827-
needs_note = false;
2828-
}
2817+
if let ExprKind::Lit(ref lit) = idx.kind
2818+
&& let ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) = lit.node
2819+
&& i < types.len().try_into().expect("expected tuple index to be < usize length")
2820+
{
2821+
let snip = self.tcx.sess.source_map().span_to_snippet(base.span);
2822+
if let Ok(snip) = snip {
2823+
err.span_suggestion(
2824+
expr.span,
2825+
"to access tuple elements, use",
2826+
format!("{snip}.{i}"),
2827+
Applicability::MachineApplicable,
2828+
);
2829+
needs_note = false;
28292830
}
2831+
} else if let ExprKind::Path(..) = idx.peel_borrows().kind {
2832+
err.span_label(idx.span, "cannot access tuple elements at a variable index");
28302833
}
28312834
if needs_note {
28322835
err.help(

tests/ui/index_message.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
fn main() {
2-
let z = ();
3-
let _ = z[0]; //~ ERROR cannot index into a value of type `()`
2+
let z = (10,);
3+
let _ = z[0]; //~ ERROR cannot index into a value of type `({integer},)`
44
}

tests/ui/index_message.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0608]: cannot index into a value of type `()`
1+
error[E0608]: cannot index into a value of type `({integer},)`
22
--> $DIR/index_message.rs:3:13
33
|
44
LL | let _ = z[0];

tests/ui/issues/issue-27842.rs

+5
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ fn main() {
88
let i = 0_usize;
99
let _ = tup[i];
1010
//~^ ERROR cannot index into a value of type
11+
12+
// the case where the index is out of bounds
13+
let tup = (10,);
14+
let _ = tup[3];
15+
//~^ ERROR cannot index into a value of type
1116
}

tests/ui/issues/issue-27842.stderr

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,20 @@ error[E0608]: cannot index into a value of type `({integer}, {integer}, {integer
88
--> $DIR/issue-27842.rs:9:13
99
|
1010
LL | let _ = tup[i];
11+
| ^^^^-^
12+
| |
13+
| cannot access tuple elements at a variable index
14+
|
15+
= help: to access tuple elements, use tuple indexing syntax (e.g., `tuple.0`)
16+
17+
error[E0608]: cannot index into a value of type `({integer},)`
18+
--> $DIR/issue-27842.rs:14:13
19+
|
20+
LL | let _ = tup[3];
1121
| ^^^^^^
1222
|
1323
= help: to access tuple elements, use tuple indexing syntax (e.g., `tuple.0`)
1424

15-
error: aborting due to 2 previous errors
25+
error: aborting due to 3 previous errors
1626

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

0 commit comments

Comments
 (0)