diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index fff6cb068c85..5df4f3af1323 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3063,8 +3063,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { idx.node, expr_t).emit(); } else { type_error_struct!(self.tcx().sess, expr.span, expr_t, E0613, - "attempted tuple index `{}` on type `{}`, but the type was not a \ - tuple or tuple struct", + "attempted to access tuple index `{}` on type `{}`, but the type \ + was not a tuple or tuple struct", idx.node, expr_t).emit(); } diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index 9eff22027c7d..308cc3b32cab 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -4238,7 +4238,51 @@ Attempted tuple index on a type which isn't a tuple nor a tuple-struct. Erroneous code example: ```compile_fail,E0613 +struct Foo; +let y = Foo; +println!("{}", y.1); // error: attempted to access tuple index `1` on type + // `Foo`, but the type was not a tuple or tuple + // struct +``` + +Only tuple and tuple-struct types can be indexed this way. Example: + +``` +// Let's create a tuple first: +let x: (u32, u32, u32, u32) = (0, 1, 1, 2); +// You can index its fields this way: +println!("({}, {}, {}, {})", x.0, x.1, x.2, x.3); + +// Now let's declare a tuple-struct: +struct TupleStruct(u32, u32, u32, u32); +// Let's instantiate it: +let x = TupleStruct(0, 1, 1, 2); +// And just like the tuple: +println!("({}, {}, {}, {})", x.0, x.1, x.2, x.3); +``` + +If you want to index into an array, use `[]` instead: + +``` +let x = &[0, 1, 1, 2]; +println!("[{}, {}, {}, {}]", x[0], x[1], x[2], x[3]); +``` + +If you want to access a field of a struct, check the field's name wasn't +misspelled: + +``` +struct SomeStruct { + x: u32, + y: i32, +} + +let s = SomeStruct { + x: 0, + y: -1, +}; +println!("x: {} y: {}", s.x, s.y); ``` "##, diff --git a/src/test/compile-fail/E0613.rs b/src/test/compile-fail/E0613.rs new file mode 100644 index 000000000000..189d1b1d3bad --- /dev/null +++ b/src/test/compile-fail/E0613.rs @@ -0,0 +1,16 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +struct Foo; + +fn main() { + let y = Foo; + y.1; //~ ERROR E0613 +} diff --git a/src/test/compile-fail/tuple-index-not-tuple.rs b/src/test/compile-fail/tuple-index-not-tuple.rs index bf2a63abbfd6..26decccdcd8a 100644 --- a/src/test/compile-fail/tuple-index-not-tuple.rs +++ b/src/test/compile-fail/tuple-index-not-tuple.rs @@ -14,7 +14,7 @@ struct Empty; fn main() { let origin = Point { x: 0, y: 0 }; origin.0; - //~^ ERROR attempted tuple index `0` on type `Point`, but the type was not + //~^ ERROR attempted to access tuple index `0` on type `Point`, but the type was not Empty.0; - //~^ ERROR attempted tuple index `0` on type `Empty`, but the type was not + //~^ ERROR attempted to access tuple index `0` on type `Empty`, but the type was not } diff --git a/src/test/ui/macros/macro-backtrace-invalid-internals.stderr b/src/test/ui/macros/macro-backtrace-invalid-internals.stderr index 136b04fcb0f5..5ed4ab4552a6 100644 --- a/src/test/ui/macros/macro-backtrace-invalid-internals.stderr +++ b/src/test/ui/macros/macro-backtrace-invalid-internals.stderr @@ -16,7 +16,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields 51 | fake_field_stmt!(); | ------------------- in this macro invocation -error[E0613]: attempted tuple index `0` on type `{integer}`, but the type was not a tuple or tuple struct +error[E0613]: attempted to access tuple index `0` on type `{integer}`, but the type was not a tuple or tuple struct --> $DIR/macro-backtrace-invalid-internals.rs:27:11 | 27 | (1).0 @@ -43,7 +43,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields 55 | let _ = fake_field_expr!(); | ------------------ in this macro invocation -error[E0613]: attempted tuple index `0` on type `{integer}`, but the type was not a tuple or tuple struct +error[E0613]: attempted to access tuple index `0` on type `{integer}`, but the type was not a tuple or tuple struct --> $DIR/macro-backtrace-invalid-internals.rs:45:11 | 45 | (1).0