diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 04469dcaf2bd5..9f0ee92930d39 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -2921,10 +2921,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { .emit(); self.tcx().types.err } else { - let mut err = self.type_error_struct(field.span, |actual| { - format!("no field `{}` on type `{}`", - field.node, actual) - }, expr_t); + let mut err = type_error_struct!(self.tcx().sess, field.span, expr_t, E0609, + "no field `{}` on type `{}`", + field.node, expr_t); match expr_t.sty { ty::TyAdt(def, _) if !def.is_enum() => { if let Some(suggested_field_name) = diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index 910d5d7402478..4f1eb929b8e83 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -4095,6 +4095,33 @@ assert_eq!(!Question::No, true); ``` "##, +E0609: r##" +An attempt to access a non-existent field in a struct was performed. + +Erroneous code example: + +```compile_fail,E0609 +struct StructWithFields { + x: u32, +} + +let s = StructWithFields { x: 0 }; +println!("{}", s.foo); // error: no field `foo` on type `StructWithFields` +``` + +To fix this error, check if you didn't misspell the field's name or that the +field actually exist. Example: + +``` +struct StructWithFields { + x: u32, +} + +let s = StructWithFields { x: 0 }; +println!("{}", s.x); // ok! +``` +"##, + } register_diagnostics! { diff --git a/src/libsyntax/diagnostics/macros.rs b/src/libsyntax/diagnostics/macros.rs index 13016d72127ea..e8ecf58072a69 100644 --- a/src/libsyntax/diagnostics/macros.rs +++ b/src/libsyntax/diagnostics/macros.rs @@ -74,6 +74,17 @@ macro_rules! struct_span_err { }) } +#[macro_export] +macro_rules! type_error_struct { + ($session:expr, $span:expr, $typ:expr, $code:ident, $($message:tt)*) => ({ + if $typ.references_error() { + $session.diagnostic().struct_dummy() + } else { + struct_span_err!($session, $span, $code, $($message)*) + } + }) +} + #[macro_export] macro_rules! struct_span_warn { ($session:expr, $span:expr, $code:ident, $($message:tt)*) => ({ diff --git a/src/test/compile-fail/E0609.rs b/src/test/compile-fail/E0609.rs new file mode 100644 index 0000000000000..f76c97274bdea --- /dev/null +++ b/src/test/compile-fail/E0609.rs @@ -0,0 +1,18 @@ +// 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 { + x: u32, +} + +fn main() { + let x = Foo { x: 0 }; + let _ = x.foo; //~ ERROR E0609 +} diff --git a/src/test/ui/did_you_mean/issue-36798.stderr b/src/test/ui/did_you_mean/issue-36798.stderr index ea628f10e0fd6..a8d978d55140b 100644 --- a/src/test/ui/did_you_mean/issue-36798.stderr +++ b/src/test/ui/did_you_mean/issue-36798.stderr @@ -1,4 +1,4 @@ -error: no field `baz` on type `Foo` +error[E0609]: no field `baz` on type `Foo` --> $DIR/issue-36798.rs:17:7 | 17 | f.baz; diff --git a/src/test/ui/did_you_mean/issue-36798_unknown_field.stderr b/src/test/ui/did_you_mean/issue-36798_unknown_field.stderr index a9090e3911b07..8228f9f3face9 100644 --- a/src/test/ui/did_you_mean/issue-36798_unknown_field.stderr +++ b/src/test/ui/did_you_mean/issue-36798_unknown_field.stderr @@ -1,4 +1,4 @@ -error: no field `zz` on type `Foo` +error[E0609]: no field `zz` on type `Foo` --> $DIR/issue-36798_unknown_field.rs:17:7 | 17 | f.zz; diff --git a/src/test/ui/macros/macro-backtrace-invalid-internals.stderr b/src/test/ui/macros/macro-backtrace-invalid-internals.stderr index 2c83a84f0040f..9694783b08b94 100644 --- a/src/test/ui/macros/macro-backtrace-invalid-internals.stderr +++ b/src/test/ui/macros/macro-backtrace-invalid-internals.stderr @@ -7,7 +7,7 @@ error[E0599]: no method named `fake` found for type `{integer}` in the current s 50 | fake_method_stmt!(); | -------------------- in this macro invocation -error: no field `fake` on type `{integer}` +error[E0609]: no field `fake` on type `{integer}` --> $DIR/macro-backtrace-invalid-internals.rs:21:13 | 21 | 1.fake @@ -34,7 +34,7 @@ error[E0599]: no method named `fake` found for type `{integer}` in the current s 54 | let _ = fake_method_expr!(); | ------------------- in this macro invocation -error: no field `fake` on type `{integer}` +error[E0609]: no field `fake` on type `{integer}` --> $DIR/macro-backtrace-invalid-internals.rs:39:13 | 39 | 1.fake diff --git a/src/test/ui/mismatched_types/cast-rfc0401.stderr b/src/test/ui/mismatched_types/cast-rfc0401.stderr index 8d31dd7500a34..58cd130dcc253 100644 --- a/src/test/ui/mismatched_types/cast-rfc0401.stderr +++ b/src/test/ui/mismatched_types/cast-rfc0401.stderr @@ -14,7 +14,7 @@ error: casting `*const U` as `*const str` is invalid | = note: vtable kinds may not match -error: no field `f` on type `fn() {main}` +error[E0609]: no field `f` on type `fn() {main}` --> $DIR/cast-rfc0401.rs:75:18 | 75 | let _ = main.f as *const u32;