Skip to content

Commit f8d3658

Browse files
Add E0608
1 parent 5aa3403 commit f8d3658

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
@@ -3889,13 +3889,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
38893889
element_ty
38903890
}
38913891
None => {
3892-
let mut err = self.type_error_struct(
3893-
expr.span,
3894-
|actual| {
3895-
format!("cannot index a value of type `{}`",
3896-
actual)
3897-
},
3898-
base_t);
3892+
let mut err = struct_span_err!(tcx.sess, expr.span, E0608,
3893+
"cannot index into a value of type `{}`",
3894+
base_t);
38993895
// Try to give some advice about indexing tuples.
39003896
if let ty::TyTuple(..) = base_t.sty {
39013897
let mut needs_note = true;

src/librustc_typeck/diagnostics.rs

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

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

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)