Skip to content

Commit

Permalink
Add E0613
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Jun 15, 2017
1 parent a42f816 commit e8cbb53
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -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();
}

Expand Down
44 changes: 44 additions & 0 deletions src/librustc_typeck/diagnostics.rs
Expand Up @@ -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);
```
"##,

Expand Down
16 changes: 16 additions & 0 deletions 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 <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
}
4 changes: 2 additions & 2 deletions src/test/compile-fail/tuple-index-not-tuple.rs
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions src/test/ui/macros/macro-backtrace-invalid-internals.stderr
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit e8cbb53

Please sign in to comment.