Skip to content

E0608 #42568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 16, 2017
Merged

E0608 #42568

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3889,13 +3889,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
element_ty
}
None => {
let mut err = self.type_error_struct(
expr.span,
|actual| {
format!("cannot index a value of type `{}`",
actual)
},
base_t);
let mut err = type_error_struct!(tcx.sess, expr.span, base_t, E0608,
"cannot index into a value of type `{}`",
base_t);
// Try to give some advice about indexing tuples.
if let ty::TyTuple(..) = base_t.sty {
let mut needs_note = true;
Expand Down
21 changes: 21 additions & 0 deletions src/librustc_typeck/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4095,6 +4095,27 @@ assert_eq!(!Question::No, true);
```
"##,

E0608: r##"
An attempt to index into a type which doesn't implement the `std::ops::Index`
trait was performed.

Erroneous code example:

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

To be able to index into a type it needs to implement the `std::ops::Index`
trait. Example:

```
let v: Vec<u8> = vec![0, 1, 2, 3];

// The `Vec` type implements the `Index` trait so you can do:
println!("{}", v[2]);
```
"##,

E0609: r##"
Attempted to access a non-existent field in a struct.

Expand Down
13 changes: 13 additions & 0 deletions src/test/compile-fail/E0608.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
0u8[2]; //~ ERROR E0608
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/index-bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
// except according to those terms.

fn main() {
(return)[0]; //~ ERROR cannot index a value of type `!`
(return)[0]; //~ ERROR cannot index into a value of type `!`
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/index_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@

fn main() {
let z = ();
let _ = z[0]; //~ ERROR cannot index a value of type `()`
let _ = z[0]; //~ ERROR cannot index into a value of type `()`
}
4 changes: 2 additions & 2 deletions src/test/compile-fail/issue-27842.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ fn main() {
let tup = (0, 1, 2);
// the case where we show a suggestion
let _ = tup[0];
//~^ ERROR cannot index a value of type
//~^ ERROR cannot index into a value of type
//~| HELP to access tuple elements, use
//~| SUGGESTION let _ = tup.0

// the case where we show just a general hint
let i = 0_usize;
let _ = tup[i];
//~^ ERROR cannot index a value of type
//~^ ERROR cannot index into a value of type
//~| HELP to access tuple elements, use tuple indexing syntax (e.g. `tuple.0`)
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-40861.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ fn f(_: &[f32]) {}

fn main() {
()[f(&[1.0])];
//~^ ERROR cannot index a value of type `()`
//~^ ERROR cannot index into a value of type `()`
}
8 changes: 4 additions & 4 deletions src/test/compile-fail/slice-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ struct Foo;

fn main() {
let x = Foo;
&x[..]; //~ ERROR cannot index a value of type `Foo`
&x[Foo..]; //~ ERROR cannot index a value of type `Foo`
&x[..Foo]; //~ ERROR cannot index a value of type `Foo`
&x[Foo..Foo]; //~ ERROR cannot index a value of type `Foo`
&x[..]; //~ ERROR cannot index into a value of type `Foo`
&x[Foo..]; //~ ERROR cannot index into a value of type `Foo`
&x[..Foo]; //~ ERROR cannot index into a value of type `Foo`
&x[Foo..Foo]; //~ ERROR cannot index into a value of type `Foo`
}
2 changes: 1 addition & 1 deletion src/test/ui/span/suggestion-non-ascii.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: cannot index a value of type `({integer},)`
error[E0608]: cannot index into a value of type `({integer},)`
--> $DIR/suggestion-non-ascii.rs:14:21
|
14 | println!("☃{}", tup[0]);
Expand Down