Skip to content

Commit 1e5162c

Browse files
committed
Auto merge of #42996 - Boreeas:merge-e0609-e0612, r=GuillaumeGomez
Fold E0612, E0613 into E0609 As discussed in #42945, with PR 1506 tuple indices are no longer considered a separate case from normal field. This PR folds E06012 ("tuple index out of bounds") and E0613 ("type is not a tuple") into E0609 ("type does not have field with that name") Resolves #42945
2 parents ac1b675 + c215d08 commit 1e5162c

File tree

6 files changed

+20
-81
lines changed

6 files changed

+20
-81
lines changed

src/librustc_typeck/check/mod.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ use util::nodemap::{DefIdMap, FxHashMap, NodeMap};
111111
use std::cell::{Cell, RefCell, Ref, RefMut};
112112
use std::collections::hash_map::Entry;
113113
use std::cmp;
114+
use std::fmt::Display;
114115
use std::mem::replace;
115116
use std::ops::{self, Deref};
116117
use syntax::abi::Abi;
@@ -2945,9 +2946,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
29452946
self.tcx().types.err
29462947
} else {
29472948
if !expr_t.is_primitive_ty() {
2948-
let mut err = type_error_struct!(self.tcx().sess, field.span, expr_t, E0609,
2949-
"no field `{}` on type `{}`",
2950-
field.node, expr_t);
2949+
let mut err = self.no_such_field_err(field.span, &field.node, expr_t);
2950+
29512951
match expr_t.sty {
29522952
ty::TyAdt(def, _) if !def.is_enum() => {
29532953
if let Some(suggested_field_name) =
@@ -3064,15 +3064,19 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
30643064
"attempted out-of-bounds tuple index `{}` on type `{}`",
30653065
idx.node, expr_t).emit();
30663066
} else {
3067-
type_error_struct!(self.tcx().sess, expr.span, expr_t, E0613,
3068-
"attempted to access tuple index `{}` on type `{}`, but the type \
3069-
was not a tuple or tuple struct",
3070-
idx.node, expr_t).emit();
3067+
self.no_such_field_err(expr.span, idx.node, expr_t).emit();
30713068
}
30723069

30733070
self.tcx().types.err
30743071
}
30753072

3073+
fn no_such_field_err<T: Display>(&self, span: Span, field: T, expr_t: &ty::TyS)
3074+
-> DiagnosticBuilder {
3075+
type_error_struct!(self.tcx().sess, span, expr_t, E0609,
3076+
"no field `{}` on type `{}`",
3077+
field, expr_t)
3078+
}
3079+
30763080
fn report_unknown_field(&self,
30773081
ty: Ty<'tcx>,
30783082
variant: &'tcx ty::VariantDef,

src/librustc_typeck/diagnostics.rs

+1-54
Original file line numberDiff line numberDiff line change
@@ -4425,60 +4425,6 @@ println!("{}", y.0); // ok!
44254425
```
44264426
"##,
44274427

4428-
E0613: r##"
4429-
Attempted tuple index on a type which isn't a tuple nor a tuple-struct.
4430-
4431-
Erroneous code example:
4432-
4433-
```compile_fail,E0613
4434-
struct Foo;
4435-
4436-
let y = Foo;
4437-
println!("{}", y.1); // error: attempted to access tuple index `1` on type
4438-
// `Foo`, but the type was not a tuple or tuple
4439-
// struct
4440-
```
4441-
4442-
Only tuple and tuple-struct types can be indexed this way. Example:
4443-
4444-
```
4445-
// Let's create a tuple first:
4446-
let x: (u32, u32, u32, u32) = (0, 1, 1, 2);
4447-
// You can index its fields this way:
4448-
println!("({}, {}, {}, {})", x.0, x.1, x.2, x.3);
4449-
4450-
// Now let's declare a tuple-struct:
4451-
struct TupleStruct(u32, u32, u32, u32);
4452-
// Let's instantiate it:
4453-
let x = TupleStruct(0, 1, 1, 2);
4454-
// And just like the tuple:
4455-
println!("({}, {}, {}, {})", x.0, x.1, x.2, x.3);
4456-
```
4457-
4458-
If you want to index into an array, use `[]` instead:
4459-
4460-
```
4461-
let x = &[0, 1, 1, 2];
4462-
println!("[{}, {}, {}, {}]", x[0], x[1], x[2], x[3]);
4463-
```
4464-
4465-
If you want to access a field of a struct, check the field's name wasn't
4466-
misspelled:
4467-
4468-
```
4469-
struct SomeStruct {
4470-
x: u32,
4471-
y: i32,
4472-
}
4473-
4474-
let s = SomeStruct {
4475-
x: 0,
4476-
y: -1,
4477-
};
4478-
println!("x: {} y: {}", s.x, s.y);
4479-
```
4480-
"##,
4481-
44824428
E0614: r##"
44834429
Attempted to dereference a variable which cannot be dereferenced.
44844430
@@ -4799,4 +4745,5 @@ register_diagnostics! {
47994745
E0568, // auto-traits can not have predicates,
48004746
E0588, // packed struct cannot transitively contain a `[repr(align)]` struct
48014747
E0592, // duplicate definitions with name `{}`
4748+
// E0613, // Removed (merged with E0609)
48024749
}

src/test/compile-fail/E0609.rs

+4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
struct Foo {
1212
x: u32,
1313
}
14+
struct Bar;
1415

1516
fn main() {
1617
let x = Foo { x: 0 };
1718
let _ = x.foo; //~ ERROR E0609
19+
20+
let y = Bar;
21+
y.1; //~ ERROR E0609
1822
}

src/test/compile-fail/E0613.rs

-16
This file was deleted.

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

+2-2
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 to access tuple index `0` on type `Point`, but the type was not
17+
//~^ ERROR no field `0` on type `Point`
1818
Empty.0;
19-
//~^ ERROR attempted to access tuple index `0` on type `Empty`, but the type was not
19+
//~^ ERROR no field `0` on type `Empty`
2020
}

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

+2-2
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 to access tuple index `0` on type `{integer}`, but the type was not a tuple or tuple struct
19+
error[E0609]: no field `0` on type `{integer}`
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 to access tuple index `0` on type `{integer}`, but the type was not a tuple or tuple struct
46+
error[E0609]: no field `0` on type `{integer}`
4747
--> $DIR/macro-backtrace-invalid-internals.rs:45:11
4848
|
4949
45 | (1).0

0 commit comments

Comments
 (0)