Skip to content

Commit

Permalink
Add E0609
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Jun 11, 2017
1 parent e148049 commit f4dd365
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 9 deletions.
7 changes: 3 additions & 4 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -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) =
Expand Down
27 changes: 27 additions & 0 deletions src/librustc_typeck/diagnostics.rs
Expand Up @@ -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! {
Expand Down
11 changes: 11 additions & 0 deletions src/libsyntax/diagnostics/macros.rs
Expand Up @@ -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)*) => ({
Expand Down
18 changes: 18 additions & 0 deletions 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
}
2 changes: 1 addition & 1 deletion 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;
Expand Down
2 changes: 1 addition & 1 deletion 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;
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/macros/macro-backtrace-invalid-internals.stderr
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/mismatched_types/cast-rfc0401.stderr
Expand Up @@ -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;
Expand Down

0 comments on commit f4dd365

Please sign in to comment.