diff options
| author | bors <bors@rust-lang.org> | 2017-06-16 00:40:05 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-06-16 00:40:05 +0000 |
| commit | a3114961a1c6b8a16cd06a854d585377ac576d47 (patch) | |
| tree | d5ce78294157cb85027586310739d828b8d4b70b | |
| parent | 258ae6dd9b1a8ac97986852fc9f00f7687004ccb (diff) | |
| parent | b6e9ed1e407b36270df9df08cdee67828efb5e6d (diff) | |
| download | rust-a3114961a1c6b8a16cd06a854d585377ac576d47.tar.gz rust-a3114961a1c6b8a16cd06a854d585377ac576d47.zip | |
Auto merge of #42568 - GuillaumeGomez:E0608, r=QuietMisdreavus
E0608 Part of #42229. cc @Susurrus
| -rw-r--r-- | src/librustc_typeck/check/mod.rs | 10 | ||||
| -rw-r--r-- | src/librustc_typeck/diagnostics.rs | 21 | ||||
| -rw-r--r-- | src/test/compile-fail/E0608.rs | 13 | ||||
| -rw-r--r-- | src/test/compile-fail/index-bot.rs | 2 | ||||
| -rw-r--r-- | src/test/compile-fail/index_message.rs | 2 | ||||
| -rw-r--r-- | src/test/compile-fail/issue-27842.rs | 4 | ||||
| -rw-r--r-- | src/test/compile-fail/issue-40861.rs | 2 | ||||
| -rw-r--r-- | src/test/compile-fail/slice-2.rs | 8 | ||||
| -rw-r--r-- | src/test/ui/span/suggestion-non-ascii.stderr | 2 |
9 files changed, 47 insertions, 17 deletions
diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index c12df083c30..b1d3292e04c 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3879,13 +3879,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; diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index f6103a259dc..df9356ea811 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -4096,6 +4096,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. diff --git a/src/test/compile-fail/E0608.rs b/src/test/compile-fail/E0608.rs new file mode 100644 index 00000000000..d47356a97ee --- /dev/null +++ b/src/test/compile-fail/E0608.rs @@ -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 +} diff --git a/src/test/compile-fail/index-bot.rs b/src/test/compile-fail/index-bot.rs index 05b04723300..fc88ff6f47b 100644 --- a/src/test/compile-fail/index-bot.rs +++ b/src/test/compile-fail/index-bot.rs @@ -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 `!` } diff --git a/src/test/compile-fail/index_message.rs b/src/test/compile-fail/index_message.rs index 26dd98757a8..b9daad936c3 100644 --- a/src/test/compile-fail/index_message.rs +++ b/src/test/compile-fail/index_message.rs @@ -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 `()` } diff --git a/src/test/compile-fail/issue-27842.rs b/src/test/compile-fail/issue-27842.rs index f7cd4e03c3b..8c71761df2f 100644 --- a/src/test/compile-fail/issue-27842.rs +++ b/src/test/compile-fail/issue-27842.rs @@ -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`) } diff --git a/src/test/compile-fail/issue-40861.rs b/src/test/compile-fail/issue-40861.rs index e525b3954f5..75d58c58538 100644 --- a/src/test/compile-fail/issue-40861.rs +++ b/src/test/compile-fail/issue-40861.rs @@ -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 `()` } diff --git a/src/test/compile-fail/slice-2.rs b/src/test/compile-fail/slice-2.rs index 99dc3e68c8f..44b9d94c566 100644 --- a/src/test/compile-fail/slice-2.rs +++ b/src/test/compile-fail/slice-2.rs @@ -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` } diff --git a/src/test/ui/span/suggestion-non-ascii.stderr b/src/test/ui/span/suggestion-non-ascii.stderr index b6353c0f6a2..68d43d3f5cd 100644 --- a/src/test/ui/span/suggestion-non-ascii.stderr +++ b/src/test/ui/span/suggestion-non-ascii.stderr @@ -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]); |
