Skip to content

Commit f17764e

Browse files
authored
Unrolled build for #146585
Rollup merge of #146585 - hkBst:indexing-1, r=jdonszelmann indexing: reword help After looking at #40850, I thought I'd try to improve wording around error E0608 a bit. Hopefully I've succeeded.
2 parents 4b9c62b + 86f2d42 commit f17764e

File tree

6 files changed

+30
-34
lines changed

6 files changed

+30
-34
lines changed
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1-
An attempt to use index on a type which doesn't implement the `std::ops::Index`
2-
trait was performed.
1+
Attempted to index a value whose type doesn't implement the
2+
`std::ops::Index` trait.
33

44
Erroneous code example:
55

66
```compile_fail,E0608
77
0u8[2]; // error: cannot index into a value of type `u8`
88
```
99

10-
To be able to index into a type it needs to implement the `std::ops::Index`
11-
trait. Example:
10+
Only values with types that implement the `std::ops::Index` trait
11+
can be indexed with square brackets. Example:
1212

1313
```
1414
let v: Vec<u8> = vec![0, 1, 2, 3];
1515
1616
// The `Vec` type implements the `Index` trait so you can do:
1717
println!("{}", v[2]);
1818
```
19+
20+
Tuples and structs are indexed with dot (`.`), not with brackets (`[]`),
21+
and tuple element names are their positions:
22+
```ignore(pseudo code)
23+
// this (pseudo code) expression is true for any tuple:
24+
tuple == (tuple.0, tuple.1, ...)
25+
```

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3551,35 +3551,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
35513551
);
35523552
// Try to give some advice about indexing tuples.
35533553
if let ty::Tuple(types) = base_t.kind() {
3554-
let mut needs_note = true;
3555-
// If the index is an integer, we can show the actual
3556-
// fixed expression:
3554+
err.help(
3555+
"tuples are indexed with a dot and a literal index: `tuple.0`, `tuple.1`, etc.",
3556+
);
3557+
// If index is an unsuffixed integer, show the fixed expression:
35573558
if let ExprKind::Lit(lit) = idx.kind
35583559
&& let ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) = lit.node
3559-
&& i.get()
3560-
< types
3561-
.len()
3562-
.try_into()
3563-
.expect("expected tuple index to be < usize length")
3560+
&& i.get() < types.len().try_into().expect("tuple length fits in u128")
35643561
{
35653562
err.span_suggestion(
35663563
brackets_span,
3567-
"to access tuple elements, use",
3564+
format!("to access tuple element `{i}`, use"),
35683565
format!(".{i}"),
35693566
Applicability::MachineApplicable,
35703567
);
3571-
needs_note = false;
3572-
} else if let ExprKind::Path(..) = idx.peel_borrows().kind {
3573-
err.span_label(
3574-
idx.span,
3575-
"cannot access tuple elements at a variable index",
3576-
);
3577-
}
3578-
if needs_note {
3579-
err.help(
3580-
"to access tuple elements, use tuple indexing \
3581-
syntax (e.g., `tuple.0`)",
3582-
);
35833568
}
35843569
}
35853570

tests/ui/indexing/index_message.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0608]: cannot index into a value of type `({integer},)`
22
--> $DIR/index_message.rs:3:14
33
|
44
LL | let _ = z[0];
5-
| ^^^ help: to access tuple elements, use: `.0`
5+
| ^^^ help: to access tuple element `0`, use: `.0`
6+
|
7+
= help: tuples are indexed with a dot and a literal index: `tuple.0`, `tuple.1`, etc.
68

79
error: aborting due to 1 previous error
810

tests/ui/issues/issue-27842.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@ error[E0608]: cannot index into a value of type `({integer}, {integer}, {integer
22
--> $DIR/issue-27842.rs:4:16
33
|
44
LL | let _ = tup[0];
5-
| ^^^ help: to access tuple elements, use: `.0`
5+
| ^^^ help: to access tuple element `0`, use: `.0`
6+
|
7+
= help: tuples are indexed with a dot and a literal index: `tuple.0`, `tuple.1`, etc.
68

79
error[E0608]: cannot index into a value of type `({integer}, {integer}, {integer})`
810
--> $DIR/issue-27842.rs:9:16
911
|
1012
LL | let _ = tup[i];
11-
| ^-^
12-
| |
13-
| cannot access tuple elements at a variable index
13+
| ^^^
1414
|
15-
= help: to access tuple elements, use tuple indexing syntax (e.g., `tuple.0`)
15+
= help: tuples are indexed with a dot and a literal index: `tuple.0`, `tuple.1`, etc.
1616

1717
error[E0608]: cannot index into a value of type `({integer},)`
1818
--> $DIR/issue-27842.rs:14:16
1919
|
2020
LL | let _ = tup[3];
2121
| ^^^
2222
|
23-
= help: to access tuple elements, use tuple indexing syntax (e.g., `tuple.0`)
23+
= help: tuples are indexed with a dot and a literal index: `tuple.0`, `tuple.1`, etc.
2424

2525
error: aborting due to 3 previous errors
2626

tests/ui/span/suggestion-non-ascii.stderr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0608]: cannot index into a value of type `({integer},)`
22
--> $DIR/suggestion-non-ascii.rs:3:24
33
|
44
LL | println!("☃{}", tup[0]);
5-
| ^^^ help: to access tuple elements, use: `.0`
5+
| ^^^ help: to access tuple element `0`, use: `.0`
6+
|
7+
= help: tuples are indexed with a dot and a literal index: `tuple.0`, `tuple.1`, etc.
68

79
error: aborting due to 1 previous error
810

tests/ui/typeck/coercion-check-for-indexing-expression-issue-40861.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0608]: cannot index into a value of type `()`
44
LL | ()[f(&[1.0])];
55
| ^^^^^^^^^^^
66
|
7-
= help: to access tuple elements, use tuple indexing syntax (e.g., `tuple.0`)
7+
= help: tuples are indexed with a dot and a literal index: `tuple.0`, `tuple.1`, etc.
88

99
error: aborting due to 1 previous error
1010

0 commit comments

Comments
 (0)