Skip to content

Commit a311496

Browse files
committed
Auto merge of #42568 - GuillaumeGomez:E0608, r=QuietMisdreavus
E0608 Part of #42229. cc @Susurrus
2 parents 258ae6d + b6e9ed1 commit a311496

File tree

9 files changed

+47
-17
lines changed

9 files changed

+47
-17
lines changed

src/librustc_typeck/check/mod.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -3879,13 +3879,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
38793879
element_ty
38803880
}
38813881
None => {
3882-
let mut err = self.type_error_struct(
3883-
expr.span,
3884-
|actual| {
3885-
format!("cannot index a value of type `{}`",
3886-
actual)
3887-
},
3888-
base_t);
3882+
let mut err = type_error_struct!(tcx.sess, expr.span, base_t, E0608,
3883+
"cannot index into a value of type `{}`",
3884+
base_t);
38893885
// Try to give some advice about indexing tuples.
38903886
if let ty::TyTuple(..) = base_t.sty {
38913887
let mut needs_note = true;

src/librustc_typeck/diagnostics.rs

+21
Original file line numberDiff line numberDiff line change
@@ -4096,6 +4096,27 @@ assert_eq!(!Question::No, true);
40964096
```
40974097
"##,
40984098

4099+
E0608: r##"
4100+
An attempt to index into a type which doesn't implement the `std::ops::Index`
4101+
trait was performed.
4102+
4103+
Erroneous code example:
4104+
4105+
```compile_fail,E0608
4106+
0u8[2]; // error: cannot index into a value of type `u8`
4107+
```
4108+
4109+
To be able to index into a type it needs to implement the `std::ops::Index`
4110+
trait. Example:
4111+
4112+
```
4113+
let v: Vec<u8> = vec![0, 1, 2, 3];
4114+
4115+
// The `Vec` type implements the `Index` trait so you can do:
4116+
println!("{}", v[2]);
4117+
```
4118+
"##,
4119+
40994120
E0609: r##"
41004121
Attempted to access a non-existent field in a struct.
41014122

src/test/compile-fail/E0608.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
0u8[2]; //~ ERROR E0608
13+
}

src/test/compile-fail/index-bot.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
// except according to those terms.
1010

1111
fn main() {
12-
(return)[0]; //~ ERROR cannot index a value of type `!`
12+
(return)[0]; //~ ERROR cannot index into a value of type `!`
1313
}

src/test/compile-fail/index_message.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010

1111
fn main() {
1212
let z = ();
13-
let _ = z[0]; //~ ERROR cannot index a value of type `()`
13+
let _ = z[0]; //~ ERROR cannot index into a value of type `()`
1414
}

src/test/compile-fail/issue-27842.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ fn main() {
1212
let tup = (0, 1, 2);
1313
// the case where we show a suggestion
1414
let _ = tup[0];
15-
//~^ ERROR cannot index a value of type
15+
//~^ ERROR cannot index into a value of type
1616
//~| HELP to access tuple elements, use
1717
//~| SUGGESTION let _ = tup.0
1818

1919
// the case where we show just a general hint
2020
let i = 0_usize;
2121
let _ = tup[i];
22-
//~^ ERROR cannot index a value of type
22+
//~^ ERROR cannot index into a value of type
2323
//~| HELP to access tuple elements, use tuple indexing syntax (e.g. `tuple.0`)
2424
}

src/test/compile-fail/issue-40861.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ fn f(_: &[f32]) {}
1212

1313
fn main() {
1414
()[f(&[1.0])];
15-
//~^ ERROR cannot index a value of type `()`
15+
//~^ ERROR cannot index into a value of type `()`
1616
}

src/test/compile-fail/slice-2.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ struct Foo;
1414

1515
fn main() {
1616
let x = Foo;
17-
&x[..]; //~ ERROR cannot index a value of type `Foo`
18-
&x[Foo..]; //~ ERROR cannot index a value of type `Foo`
19-
&x[..Foo]; //~ ERROR cannot index a value of type `Foo`
20-
&x[Foo..Foo]; //~ ERROR cannot index a value of type `Foo`
17+
&x[..]; //~ ERROR cannot index into a value of type `Foo`
18+
&x[Foo..]; //~ ERROR cannot index into a value of type `Foo`
19+
&x[..Foo]; //~ ERROR cannot index into a value of type `Foo`
20+
&x[Foo..Foo]; //~ ERROR cannot index into a value of type `Foo`
2121
}

src/test/ui/span/suggestion-non-ascii.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: cannot index a value of type `({integer},)`
1+
error[E0608]: cannot index into a value of type `({integer},)`
22
--> $DIR/suggestion-non-ascii.rs:14:21
33
|
44
14 | println!("☃{}", tup[0]);

0 commit comments

Comments
 (0)