about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2017-06-10 21:19:40 +0200
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2017-06-11 19:48:53 +0200
commitf4dd365bbb362a0aab0beaa31db73bf55d6a0481 (patch)
tree0bbcccd00e35b8ce6146579fc6f95326cd0082fb
parente1480499b484d142dfa704ae20bd33eae518c1d0 (diff)
downloadrust-f4dd365bbb362a0aab0beaa31db73bf55d6a0481.tar.gz
rust-f4dd365bbb362a0aab0beaa31db73bf55d6a0481.zip
Add E0609
-rw-r--r--src/librustc_typeck/check/mod.rs7
-rw-r--r--src/librustc_typeck/diagnostics.rs27
-rw-r--r--src/libsyntax/diagnostics/macros.rs11
-rw-r--r--src/test/compile-fail/E0609.rs18
-rw-r--r--src/test/ui/did_you_mean/issue-36798.stderr2
-rw-r--r--src/test/ui/did_you_mean/issue-36798_unknown_field.stderr2
-rw-r--r--src/test/ui/macros/macro-backtrace-invalid-internals.stderr4
-rw-r--r--src/test/ui/mismatched_types/cast-rfc0401.stderr2
8 files changed, 64 insertions, 9 deletions
diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs
index 04469dcaf2b..9f0ee92930d 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 910d5d74024..4f1eb929b8e 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 13016d72127..e8ecf58072a 100644
--- a/src/libsyntax/diagnostics/macros.rs
+++ b/src/libsyntax/diagnostics/macros.rs
@@ -75,6 +75,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)*) => ({
         __diagnostic_used!($code);
diff --git a/src/test/compile-fail/E0609.rs b/src/test/compile-fail/E0609.rs
new file mode 100644
index 00000000000..f76c97274bd
--- /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 <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 {
+    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 ea628f10e0f..a8d978d5514 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 a9090e3911b..8228f9f3fac 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 2c83a84f004..9694783b08b 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 8d31dd7500a..58cd130dcc2 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;