From c215d08c4c85e5b887359e3d97f32888e80634aa Mon Sep 17 00:00:00 2001 From: boreeas Date: Fri, 30 Jun 2017 20:36:51 +0200 Subject: [PATCH] Fold E0613 into E0609 Resolves #42945 --- src/librustc_typeck/check/mod.rs | 18 +++--- src/librustc_typeck/diagnostics.rs | 55 +------------------ src/test/compile-fail/E0609.rs | 4 ++ src/test/compile-fail/E0613.rs | 16 ------ .../compile-fail/tuple-index-not-tuple.rs | 4 +- .../macro-backtrace-invalid-internals.stderr | 4 +- 6 files changed, 20 insertions(+), 81 deletions(-) delete mode 100644 src/test/compile-fail/E0613.rs diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 3241267bbc2e4..f8c7bfc27a225 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -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(&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, diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index 37f6f3753d7b4..61275bdde63d8 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -4443,60 +4443,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. @@ -4817,4 +4763,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) } diff --git a/src/test/compile-fail/E0609.rs b/src/test/compile-fail/E0609.rs index f76c97274bdea..ddfd9d5f21ab2 100644 --- a/src/test/compile-fail/E0609.rs +++ b/src/test/compile-fail/E0609.rs @@ -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 } diff --git a/src/test/compile-fail/E0613.rs b/src/test/compile-fail/E0613.rs deleted file mode 100644 index 189d1b1d3bad6..0000000000000 --- a/src/test/compile-fail/E0613.rs +++ /dev/null @@ -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 or the MIT license -// , 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 -} diff --git a/src/test/compile-fail/tuple-index-not-tuple.rs b/src/test/compile-fail/tuple-index-not-tuple.rs index 26decccdcd8af..471104d687201 100644 --- a/src/test/compile-fail/tuple-index-not-tuple.rs +++ b/src/test/compile-fail/tuple-index-not-tuple.rs @@ -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` } diff --git a/src/test/ui/macros/macro-backtrace-invalid-internals.stderr b/src/test/ui/macros/macro-backtrace-invalid-internals.stderr index 0f0d6d8ded313..c80c0fce35806 100644 --- a/src/test/ui/macros/macro-backtrace-invalid-internals.stderr +++ b/src/test/ui/macros/macro-backtrace-invalid-internals.stderr @@ -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