Skip to content

Commit e2b7e03

Browse files
hkBstfee1-dead
andcommitted
indexing: reword help
Co-authored-by: beef <[email protected]>
1 parent d1ed52b commit e2b7e03

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
@@ -3552,35 +3552,20 @@ 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 with a dot and a constant index, e.g.: `tuple.0`",
3557+
);
3558+
// If index is an unsuffixed integer, show the fixed expression:
35583559
if let ExprKind::Lit(lit) = idx.kind
35593560
&& 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")
3561+
&& i.get() < types.len().try_into().expect("tuple length fits in u128")
35653562
{
35663563
err.span_suggestion(
35673564
brackets_span,
3568-
"to access tuple elements, use",
3565+
format!("to access tuple element `{i}`, use"),
35693566
format!(".{i}"),
35703567
Applicability::MachineApplicable,
35713568
);
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-
);
35843569
}
35853570
}
35863571

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 constant index, e.g.: `tuple.0`
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 constant index, e.g.: `tuple.0`
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 constant index, e.g.: `tuple.0`
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 constant index, e.g.: `tuple.0`
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 constant index, e.g.: `tuple.0`
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 constant index, e.g.: `tuple.0`
88

99
error: aborting due to 1 previous error
1010

0 commit comments

Comments
 (0)