Add E0613
This commit is contained in:
parent
a42f8160ed
commit
e8cbb53309
|
@ -3063,8 +3063,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||||
idx.node, expr_t).emit();
|
idx.node, expr_t).emit();
|
||||||
} else {
|
} else {
|
||||||
type_error_struct!(self.tcx().sess, expr.span, expr_t, E0613,
|
type_error_struct!(self.tcx().sess, expr.span, expr_t, E0613,
|
||||||
"attempted tuple index `{}` on type `{}`, but the type was not a \
|
"attempted to access tuple index `{}` on type `{}`, but the type \
|
||||||
tuple or tuple struct",
|
was not a tuple or tuple struct",
|
||||||
idx.node, expr_t).emit();
|
idx.node, expr_t).emit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4238,7 +4238,51 @@ Attempted tuple index on a type which isn't a tuple nor a tuple-struct.
|
||||||
Erroneous code example:
|
Erroneous code example:
|
||||||
|
|
||||||
```compile_fail,E0613
|
```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);
|
||||||
```
|
```
|
||||||
"##,
|
"##,
|
||||||
|
|
||||||
|
|
16
src/test/compile-fail/E0613.rs
Normal file
16
src/test/compile-fail/E0613.rs
Normal file
|
@ -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
|
||||||
|
}
|
|
@ -14,7 +14,7 @@ struct Empty;
|
||||||
fn main() {
|
fn main() {
|
||||||
let origin = Point { x: 0, y: 0 };
|
let origin = Point { x: 0, y: 0 };
|
||||||
origin.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;
|
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
|
||||||
51 | fake_field_stmt!();
|
51 | fake_field_stmt!();
|
||||||
| ------------------- in this macro invocation
|
| ------------------- 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
|
--> $DIR/macro-backtrace-invalid-internals.rs:27:11
|
||||||
|
|
|
|
||||||
27 | (1).0
|
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!();
|
55 | let _ = fake_field_expr!();
|
||||||
| ------------------ in this macro invocation
|
| ------------------ 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
|
--> $DIR/macro-backtrace-invalid-internals.rs:45:11
|
||||||
|
|
|
|
||||||
45 | (1).0
|
45 | (1).0
|
||||||
|
|
Loading…
Reference in a new issue