Skip to content

Commit 2c57fd0

Browse files
committed
indexing: reword help
1 parent d1ed52b commit 2c57fd0

File tree

6 files changed

+24
-32
lines changed

6 files changed

+24
-32
lines changed

compiler/rustc_error_codes/src/error_codes/E0608.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
An attempt to use index on a type which doesn't implement the `std::ops::Index`
2-
trait was performed.
1+
Indexing was attempted on a type which doesn't implement the `std::ops::Index` trait.
32

43
Erroneous code example:
54

@@ -16,3 +15,6 @@ let v: Vec<u8> = vec![0, 1, 2, 3];
1615
// The `Vec` type implements the `Index` trait so you can do:
1716
println!("{}", v[2]);
1817
```
18+
19+
Tuples and structs are indexed with dot (`._`), not with brackets (`[_]`).
20+
Tuple element names are their positions: tuple = (tuple.0, tuple.1, ...)

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3552,35 +3552,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
35523552
);
35533553
// Try to give some advice about indexing tuples.
35543554
if let ty::Tuple(types) = base_t.kind() {
3555-
let mut needs_note = true;
3556-
// If the index is an integer, we can show the actual
3557-
// fixed expression:
3555+
err.help(
3556+
"tuples are indexed (like structs) with dot (`._`) not with brackets (`[_]`), \
3557+
element names are their positions: tuple = (tuple.0, tuple.1, ...)",
3558+
);
3559+
// If index is an unsuffixed integer, show the fixed expression:
35583560
if let ExprKind::Lit(lit) = idx.kind
35593561
&& let ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) = lit.node
3560-
&& i.get()
3561-
< types
3562-
.len()
3563-
.try_into()
3564-
.expect("expected tuple index to be < usize length")
3562+
&& i.get() < types.len().try_into().expect("tuple length fits in u128")
35653563
{
35663564
err.span_suggestion(
35673565
brackets_span,
3568-
"to access tuple elements, use",
3566+
format!("to access tuple element `{i}`, use"),
35693567
format!(".{i}"),
35703568
Applicability::MachineApplicable,
35713569
);
3572-
needs_note = false;
3573-
} else if let ExprKind::Path(..) = idx.peel_borrows().kind {
3574-
err.span_label(
3575-
idx.span,
3576-
"cannot access tuple elements at a variable index",
3577-
);
3578-
}
3579-
if needs_note {
3580-
err.help(
3581-
"to access tuple elements, use tuple indexing \
3582-
syntax (e.g., `tuple.0`)",
3583-
);
35843570
}
35853571
}
35863572

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 (like structs) with dot (`._`) not with brackets (`[_]`), element names are their positions: tuple = (tuple.0, tuple.1, ...)
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 (like structs) with dot (`._`) not with brackets (`[_]`), element names are their positions: tuple = (tuple.0, tuple.1, ...)
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 (like structs) with dot (`._`) not with brackets (`[_]`), element names are their positions: tuple = (tuple.0, tuple.1, ...)
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 (like structs) with dot (`._`) not with brackets (`[_]`), element names are their positions: tuple = (tuple.0, tuple.1, ...)
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 (like structs) with dot (`._`) not with brackets (`[_]`), element names are their positions: tuple = (tuple.0, tuple.1, ...)
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 (like structs) with dot (`._`) not with brackets (`[_]`), element names are their positions: tuple = (tuple.0, tuple.1, ...)
88

99
error: aborting due to 1 previous error
1010

0 commit comments

Comments
 (0)