Skip to content

Commit a42f816

Browse files
Add E0612
1 parent 302f996 commit a42f816

File tree

5 files changed

+68
-24
lines changed

5 files changed

+68
-24
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3053,26 +3053,20 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
30533053
let struct_path = self.tcx().item_path_str(did);
30543054
struct_span_err!(self.tcx().sess, expr.span, E0611,
30553055
"field `{}` of tuple-struct `{}` is private",
3056-
idx.node, struct_path);
3056+
idx.node, struct_path).emit();
30573057
return field_ty;
30583058
}
30593059

3060-
self.type_error_message(
3061-
expr.span,
3062-
|actual| {
3063-
if tuple_like {
3064-
format!("attempted out-of-bounds tuple index `{}` on \
3065-
type `{}`",
3066-
idx.node,
3067-
actual)
3068-
} else {
3069-
format!("attempted tuple index `{}` on type `{}`, but the \
3070-
type was not a tuple or tuple struct",
3071-
idx.node,
3072-
actual)
3073-
}
3074-
},
3075-
expr_t);
3060+
if tuple_like {
3061+
type_error_struct!(self.tcx().sess, expr.span, expr_t, E0612,
3062+
"attempted out-of-bounds tuple index `{}` on type `{}`",
3063+
idx.node, expr_t).emit();
3064+
} else {
3065+
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",
3068+
idx.node, expr_t).emit();
3069+
}
30763070

30773071
self.tcx().types.err
30783072
}
@@ -3173,10 +3167,10 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
31733167
field_type_hint = tcx.types.err;
31743168
if let Some(_) = variant.find_field_named(field.name.node) {
31753169
let mut err = struct_span_err!(self.tcx.sess,
3176-
field.name.span,
3177-
E0062,
3178-
"field `{}` specified more than once",
3179-
field.name.node);
3170+
field.name.span,
3171+
E0062,
3172+
"field `{}` specified more than once",
3173+
field.name.node);
31803174

31813175
err.span_label(field.name.span, "used more than once");
31823176

src/librustc_typeck/diagnostics.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4208,6 +4208,40 @@ println!("{}", y.get()); // So we can get the value through the function.
42084208
```
42094209
"##,
42104210

4211+
E0612: r##"
4212+
Attempted out-of-bounds tuple index.
4213+
4214+
Erroneous code example:
4215+
4216+
```compile_fail,E0612
4217+
struct Foo(u32);
4218+
4219+
let y = Foo(0);
4220+
println!("{}", y.1); // error: attempted out-of-bounds tuple index `1`
4221+
// on type `Foo`
4222+
```
4223+
4224+
If a tuple/tuple-struct type has n fields, you can only try to access these n
4225+
fields from 0 to (n - 1). So in this case, you can only index `0`. Example:
4226+
4227+
```
4228+
struct Foo(u32);
4229+
4230+
let y = Foo(0);
4231+
println!("{}", y.0); // ok!
4232+
```
4233+
"##,
4234+
4235+
E0613: r##"
4236+
Attempted tuple index on a type which isn't a tuple nor a tuple-struct.
4237+
4238+
Erroneous code example:
4239+
4240+
```compile_fail,E0613
4241+
4242+
```
4243+
"##,
4244+
42114245
E0617: r##"
42124246
Attempted to pass an invalid type of variable into a variadic function.
42134247

src/test/compile-fail/E0612.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(u32);
12+
13+
fn main() {
14+
let y = Foo(0);
15+
y.1; //~ ERROR E0612
16+
}

src/test/compile-fail/struct-field-privacy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn test(a: A, b: inner::A, c: inner::B, d: xc::A, e: xc::B, z: inner::Z) {
4242
e.b; //~ ERROR: field `b` of struct `xc::B` is private
4343

4444
z.0;
45-
z.1; //~ ERROR: field `1` of struct `inner::Z` is private
45+
z.1; //~ ERROR: field `1` of tuple-struct `inner::Z` is private
4646
}
4747

4848
fn main() {}

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: attempted tuple index `0` on type `{integer}`, but the type was not a tuple or tuple struct
19+
error[E0613]: attempted 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: attempted tuple index `0` on type `{integer}`, but the type was not a tuple or tuple struct
46+
error[E0613]: attempted 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)