]> git.lizzy.rs Git - rust.git/commitdiff
Add E0613
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Mon, 12 Jun 2017 19:45:48 +0000 (21:45 +0200)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Thu, 15 Jun 2017 08:14:29 +0000 (10:14 +0200)
src/librustc_typeck/check/mod.rs
src/librustc_typeck/diagnostics.rs
src/test/compile-fail/E0613.rs [new file with mode: 0644]
src/test/compile-fail/tuple-index-not-tuple.rs
src/test/ui/macros/macro-backtrace-invalid-internals.stderr

index fff6cb068c85e372e14e6467a756f5f06167c65d..5df4f3af1323cde7d9d397ab582d0747f33387e9 100644 (file)
@@ -3063,8 +3063,8 @@ fn check_tup_field(&self,
                                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();
         }
 
index 9eff22027c7d2c62857a607ae68b418e933fa2f3..308cc3b32cab4a2ac3804d3b3d1dacf1076443d1 100644 (file)
@@ -4238,7 +4238,51 @@ pub fn get(&self) -> &u32 { self.0 }
 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 (file)
index 0000000..189d1b1
--- /dev/null
@@ -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 <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.
+
+struct Foo;
+
+fn main() {
+   let y = Foo;
+   y.1; //~ ERROR E0613
+}
index bf2a63abbfd6c1c2a4aa522ee1595845fe87d84f..26decccdcd8af454e9ed7b82da851c75c8a44441 100644 (file)
@@ -14,7 +14,7 @@ struct Point { x: isize, y: isize }
 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
 }
index 136b04fcb0f588878b11d4a4e563c8eb915fcb84..5ed4ab4552a66e010beee95eca3198a17721d6e3 100644 (file)
@@ -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