Skip to content

Commit f4dd365

Browse files
Add E0609
1 parent e148049 commit f4dd365

File tree

8 files changed

+64
-9
lines changed

8 files changed

+64
-9
lines changed

src/librustc_typeck/check/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -2921,10 +2921,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
29212921
.emit();
29222922
self.tcx().types.err
29232923
} else {
2924-
let mut err = self.type_error_struct(field.span, |actual| {
2925-
format!("no field `{}` on type `{}`",
2926-
field.node, actual)
2927-
}, expr_t);
2924+
let mut err = type_error_struct!(self.tcx().sess, field.span, expr_t, E0609,
2925+
"no field `{}` on type `{}`",
2926+
field.node, expr_t);
29282927
match expr_t.sty {
29292928
ty::TyAdt(def, _) if !def.is_enum() => {
29302929
if let Some(suggested_field_name) =

src/librustc_typeck/diagnostics.rs

+27
Original file line numberDiff line numberDiff line change
@@ -4095,6 +4095,33 @@ assert_eq!(!Question::No, true);
40954095
```
40964096
"##,
40974097

4098+
E0609: r##"
4099+
An attempt to access a non-existent field in a struct was performed.
4100+
4101+
Erroneous code example:
4102+
4103+
```compile_fail,E0609
4104+
struct StructWithFields {
4105+
x: u32,
4106+
}
4107+
4108+
let s = StructWithFields { x: 0 };
4109+
println!("{}", s.foo); // error: no field `foo` on type `StructWithFields`
4110+
```
4111+
4112+
To fix this error, check if you didn't misspell the field's name or that the
4113+
field actually exist. Example:
4114+
4115+
```
4116+
struct StructWithFields {
4117+
x: u32,
4118+
}
4119+
4120+
let s = StructWithFields { x: 0 };
4121+
println!("{}", s.x); // ok!
4122+
```
4123+
"##,
4124+
40984125
}
40994126

41004127
register_diagnostics! {

src/libsyntax/diagnostics/macros.rs

+11
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ macro_rules! struct_span_err {
7474
})
7575
}
7676

77+
#[macro_export]
78+
macro_rules! type_error_struct {
79+
($session:expr, $span:expr, $typ:expr, $code:ident, $($message:tt)*) => ({
80+
if $typ.references_error() {
81+
$session.diagnostic().struct_dummy()
82+
} else {
83+
struct_span_err!($session, $span, $code, $($message)*)
84+
}
85+
})
86+
}
87+
7788
#[macro_export]
7889
macro_rules! struct_span_warn {
7990
($session:expr, $span:expr, $code:ident, $($message:tt)*) => ({

src/test/compile-fail/E0609.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Foo {
12+
x: u32,
13+
}
14+
15+
fn main() {
16+
let x = Foo { x: 0 };
17+
let _ = x.foo; //~ ERROR E0609
18+
}

src/test/ui/did_you_mean/issue-36798.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: no field `baz` on type `Foo`
1+
error[E0609]: no field `baz` on type `Foo`
22
--> $DIR/issue-36798.rs:17:7
33
|
44
17 | f.baz;

src/test/ui/did_you_mean/issue-36798_unknown_field.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error: no field `zz` on type `Foo`
1+
error[E0609]: no field `zz` on type `Foo`
22
--> $DIR/issue-36798_unknown_field.rs:17:7
33
|
44
17 | f.zz;

src/test/ui/macros/macro-backtrace-invalid-internals.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ error[E0599]: no method named `fake` found for type `{integer}` in the current s
77
50 | fake_method_stmt!();
88
| -------------------- in this macro invocation
99

10-
error: no field `fake` on type `{integer}`
10+
error[E0609]: no field `fake` on type `{integer}`
1111
--> $DIR/macro-backtrace-invalid-internals.rs:21:13
1212
|
1313
21 | 1.fake
@@ -34,7 +34,7 @@ error[E0599]: no method named `fake` found for type `{integer}` in the current s
3434
54 | let _ = fake_method_expr!();
3535
| ------------------- in this macro invocation
3636

37-
error: no field `fake` on type `{integer}`
37+
error[E0609]: no field `fake` on type `{integer}`
3838
--> $DIR/macro-backtrace-invalid-internals.rs:39:13
3939
|
4040
39 | 1.fake

src/test/ui/mismatched_types/cast-rfc0401.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ error: casting `*const U` as `*const str` is invalid
1414
|
1515
= note: vtable kinds may not match
1616

17-
error: no field `f` on type `fn() {main}`
17+
error[E0609]: no field `f` on type `fn() {main}`
1818
--> $DIR/cast-rfc0401.rs:75:18
1919
|
2020
75 | let _ = main.f as *const u32;

0 commit comments

Comments
 (0)