Add E0609

This commit is contained in:
Guillaume Gomez 2017-06-10 21:19:40 +02:00
parent e1480499b4
commit f4dd365bbb
8 changed files with 64 additions and 9 deletions

View File

@ -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) =

View File

@ -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! {

View File

@ -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)*) => ({

View File

@ -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
}

View File

@ -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;

View File

@ -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;

View File

@ -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

View File

@ -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;