Skip to content

Commit e8cbb53

Browse files
Add E0613
1 parent a42f816 commit e8cbb53

File tree

5 files changed

+66
-6
lines changed

5 files changed

+66
-6
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3063,8 +3063,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
30633063
idx.node, expr_t).emit();
30643064
} else {
30653065
type_error_struct!(self.tcx().sess, expr.span, expr_t, E0613,
3066-
"attempted tuple index `{}` on type `{}`, but the type was not a \
3067-
tuple or tuple struct",
3066+
"attempted to access tuple index `{}` on type `{}`, but the type \
3067+
was not a tuple or tuple struct",
30683068
idx.node, expr_t).emit();
30693069
}
30703070

src/librustc_typeck/diagnostics.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4238,7 +4238,51 @@ Attempted tuple index on a type which isn't a tuple nor a tuple-struct.
42384238
Erroneous code example:
42394239
42404240
```compile_fail,E0613
4241+
struct Foo;
4242+
4243+
let y = Foo;
4244+
println!("{}", y.1); // error: attempted to access tuple index `1` on type
4245+
// `Foo`, but the type was not a tuple or tuple
4246+
// struct
4247+
```
4248+
4249+
Only tuple and tuple-struct types can be indexed this way. Example:
4250+
4251+
```
4252+
// Let's create a tuple first:
4253+
let x: (u32, u32, u32, u32) = (0, 1, 1, 2);
4254+
// You can index its fields this way:
4255+
println!("({}, {}, {}, {})", x.0, x.1, x.2, x.3);
4256+
4257+
// Now let's declare a tuple-struct:
4258+
struct TupleStruct(u32, u32, u32, u32);
4259+
// Let's instantiate it:
4260+
let x = TupleStruct(0, 1, 1, 2);
4261+
// And just like the tuple:
4262+
println!("({}, {}, {}, {})", x.0, x.1, x.2, x.3);
4263+
```
4264+
4265+
If you want to index into an array, use `[]` instead:
4266+
4267+
```
4268+
let x = &[0, 1, 1, 2];
4269+
println!("[{}, {}, {}, {}]", x[0], x[1], x[2], x[3]);
4270+
```
4271+
4272+
If you want to access a field of a struct, check the field's name wasn't
4273+
misspelled:
4274+
4275+
```
4276+
struct SomeStruct {
4277+
x: u32,
4278+
y: i32,
4279+
}
42414280
4281+
let s = SomeStruct {
4282+
x: 0,
4283+
y: -1,
4284+
};
4285+
println!("x: {} y: {}", s.x, s.y);
42424286
```
42434287
"##,
42444288

src/test/compile-fail/E0613.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Foo;
12+
13+
fn main() {
14+
let y = Foo;
15+
y.1; //~ ERROR E0613
16+
}

src/test/compile-fail/tuple-index-not-tuple.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct Empty;
1414
fn main() {
1515
let origin = Point { x: 0, y: 0 };
1616
origin.0;
17-
//~^ ERROR attempted tuple index `0` on type `Point`, but the type was not
17+
//~^ ERROR attempted to access tuple index `0` on type `Point`, but the type was not
1818
Empty.0;
19-
//~^ ERROR attempted tuple index `0` on type `Empty`, but the type was not
19+
//~^ ERROR attempted to access tuple index `0` on type `Empty`, but the type was not
2020
}

src/test/ui/macros/macro-backtrace-invalid-internals.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
1616
51 | fake_field_stmt!();
1717
| ------------------- in this macro invocation
1818

19-
error[E0613]: attempted tuple index `0` on type `{integer}`, but the type was not a tuple or tuple struct
19+
error[E0613]: attempted to access tuple index `0` on type `{integer}`, but the type was not a tuple or tuple struct
2020
--> $DIR/macro-backtrace-invalid-internals.rs:27:11
2121
|
2222
27 | (1).0
@@ -43,7 +43,7 @@ error[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
4343
55 | let _ = fake_field_expr!();
4444
| ------------------ in this macro invocation
4545

46-
error[E0613]: attempted tuple index `0` on type `{integer}`, but the type was not a tuple or tuple struct
46+
error[E0613]: attempted to access tuple index `0` on type `{integer}`, but the type was not a tuple or tuple struct
4747
--> $DIR/macro-backtrace-invalid-internals.rs:45:11
4848
|
4949
45 | (1).0

0 commit comments

Comments
 (0)