mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 06:44:35 +00:00
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
This commit is contained in:
commit
1e5162ce5b
@ -111,6 +111,7 @@ use util::nodemap::{DefIdMap, FxHashMap, NodeMap};
|
||||
use std::cell::{Cell, RefCell, Ref, RefMut};
|
||||
use std::collections::hash_map::Entry;
|
||||
use std::cmp;
|
||||
use std::fmt::Display;
|
||||
use std::mem::replace;
|
||||
use std::ops::{self, Deref};
|
||||
use syntax::abi::Abi;
|
||||
@ -2945,9 +2946,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||
self.tcx().types.err
|
||||
} else {
|
||||
if !expr_t.is_primitive_ty() {
|
||||
let mut err = type_error_struct!(self.tcx().sess, field.span, expr_t, E0609,
|
||||
"no field `{}` on type `{}`",
|
||||
field.node, expr_t);
|
||||
let mut err = self.no_such_field_err(field.span, &field.node, expr_t);
|
||||
|
||||
match expr_t.sty {
|
||||
ty::TyAdt(def, _) if !def.is_enum() => {
|
||||
if let Some(suggested_field_name) =
|
||||
@ -3064,15 +3064,19 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||
"attempted out-of-bounds tuple index `{}` on type `{}`",
|
||||
idx.node, expr_t).emit();
|
||||
} else {
|
||||
type_error_struct!(self.tcx().sess, expr.span, expr_t, E0613,
|
||||
"attempted to access tuple index `{}` on type `{}`, but the type \
|
||||
was not a tuple or tuple struct",
|
||||
idx.node, expr_t).emit();
|
||||
self.no_such_field_err(expr.span, idx.node, expr_t).emit();
|
||||
}
|
||||
|
||||
self.tcx().types.err
|
||||
}
|
||||
|
||||
fn no_such_field_err<T: Display>(&self, span: Span, field: T, expr_t: &ty::TyS)
|
||||
-> DiagnosticBuilder {
|
||||
type_error_struct!(self.tcx().sess, span, expr_t, E0609,
|
||||
"no field `{}` on type `{}`",
|
||||
field, expr_t)
|
||||
}
|
||||
|
||||
fn report_unknown_field(&self,
|
||||
ty: Ty<'tcx>,
|
||||
variant: &'tcx ty::VariantDef,
|
||||
|
@ -4425,60 +4425,6 @@ println!("{}", y.0); // ok!
|
||||
```
|
||||
"##,
|
||||
|
||||
E0613: r##"
|
||||
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);
|
||||
```
|
||||
"##,
|
||||
|
||||
E0614: r##"
|
||||
Attempted to dereference a variable which cannot be dereferenced.
|
||||
|
||||
@ -4799,4 +4745,5 @@ register_diagnostics! {
|
||||
E0568, // auto-traits can not have predicates,
|
||||
E0588, // packed struct cannot transitively contain a `[repr(align)]` struct
|
||||
E0592, // duplicate definitions with name `{}`
|
||||
// E0613, // Removed (merged with E0609)
|
||||
}
|
||||
|
@ -11,8 +11,12 @@
|
||||
struct Foo {
|
||||
x: u32,
|
||||
}
|
||||
struct Bar;
|
||||
|
||||
fn main() {
|
||||
let x = Foo { x: 0 };
|
||||
let _ = x.foo; //~ ERROR E0609
|
||||
|
||||
let y = Bar;
|
||||
y.1; //~ ERROR E0609
|
||||
}
|
||||
|
@ -1,16 +0,0 @@
|
||||
// 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() {
|
||||
let origin = Point { x: 0, y: 0 };
|
||||
origin.0;
|
||||
//~^ ERROR attempted to access tuple index `0` on type `Point`, but the type was not
|
||||
//~^ ERROR no field `0` on type `Point`
|
||||
Empty.0;
|
||||
//~^ ERROR attempted to access tuple index `0` on type `Empty`, but the type was not
|
||||
//~^ ERROR no field `0` on type `Empty`
|
||||
}
|
||||
|
@ -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 to access tuple index `0` on type `{integer}`, but the type was not a tuple or tuple struct
|
||||
error[E0609]: no field `0` on type `{integer}`
|
||||
--> $DIR/macro-backtrace-invalid-internals.rs:27:11
|
||||
|
|
||||
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!();
|
||||
| ------------------ in this macro invocation
|
||||
|
||||
error[E0613]: attempted to access tuple index `0` on type `{integer}`, but the type was not a tuple or tuple struct
|
||||
error[E0609]: no field `0` on type `{integer}`
|
||||
--> $DIR/macro-backtrace-invalid-internals.rs:45:11
|
||||
|
|
||||
45 | (1).0
|
||||
|
Loading…
Reference in New Issue
Block a user