Skip to content

Commit 29ef412

Browse files
committed
Auto merge of #42585 - GuillaumeGomez:E0609, r=Susurrus
Add E0609 Part of #42229. cc @Susurrus
2 parents e2eaef8 + 2f37894 commit 29ef412

File tree

13 files changed

+154
-32
lines changed

13 files changed

+154
-32
lines changed

src/librustc/ty/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,24 @@ impl<'tcx> Hash for TyS<'tcx> {
465465
}
466466
}
467467

468+
impl<'tcx> TyS<'tcx> {
469+
pub fn is_primitive_ty(&self) -> bool {
470+
match self.sty {
471+
TypeVariants::TyBool |
472+
TypeVariants::TyChar |
473+
TypeVariants::TyInt(_) |
474+
TypeVariants::TyUint(_) |
475+
TypeVariants::TyFloat(_) |
476+
TypeVariants::TyInfer(InferTy::IntVar(_)) |
477+
TypeVariants::TyInfer(InferTy::FloatVar(_)) |
478+
TypeVariants::TyInfer(InferTy::FreshIntTy(_)) |
479+
TypeVariants::TyInfer(InferTy::FreshFloatTy(_)) => true,
480+
TypeVariants::TyRef(_, x) => x.ty.is_primitive_ty(),
481+
_ => false,
482+
}
483+
}
484+
}
485+
468486
impl<'a, 'gcx, 'tcx> HashStable<StableHashingContext<'a, 'gcx, 'tcx>> for ty::TyS<'tcx> {
469487
fn hash_stable<W: StableHasherResult>(&self,
470488
hcx: &mut StableHashingContext<'a, 'gcx, 'tcx>,

src/librustc_typeck/check/mod.rs

+27-23
Original file line numberDiff line numberDiff line change
@@ -2952,30 +2952,34 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
29522952
.emit();
29532953
self.tcx().types.err
29542954
} else {
2955-
let mut err = self.type_error_struct(field.span, |actual| {
2956-
format!("no field `{}` on type `{}`",
2957-
field.node, actual)
2958-
}, expr_t);
2959-
match expr_t.sty {
2960-
ty::TyAdt(def, _) if !def.is_enum() => {
2961-
if let Some(suggested_field_name) =
2962-
Self::suggest_field_name(def.struct_variant(), field, vec![]) {
2963-
err.span_label(field.span,
2964-
format!("did you mean `{}`?", suggested_field_name));
2965-
} else {
2966-
err.span_label(field.span,
2967-
"unknown field");
2968-
};
2969-
}
2970-
ty::TyRawPtr(..) => {
2971-
err.note(&format!("`{0}` is a native pointer; perhaps you need to deref with \
2972-
`(*{0}).{1}`",
2973-
self.tcx.hir.node_to_pretty_string(base.id),
2974-
field.node));
2955+
if !expr_t.is_primitive_ty() {
2956+
let mut err = type_error_struct!(self.tcx().sess, field.span, expr_t, E0609,
2957+
"no field `{}` on type `{}`",
2958+
field.node, expr_t);
2959+
match expr_t.sty {
2960+
ty::TyAdt(def, _) if !def.is_enum() => {
2961+
if let Some(suggested_field_name) =
2962+
Self::suggest_field_name(def.struct_variant(), field, vec![]) {
2963+
err.span_label(field.span,
2964+
format!("did you mean `{}`?", suggested_field_name));
2965+
} else {
2966+
err.span_label(field.span, "unknown field");
2967+
};
2968+
}
2969+
ty::TyRawPtr(..) => {
2970+
err.note(&format!("`{0}` is a native pointer; perhaps you need to deref \
2971+
with `(*{0}).{1}`",
2972+
self.tcx.hir.node_to_pretty_string(base.id),
2973+
field.node));
2974+
}
2975+
_ => {}
29752976
}
2976-
_ => {}
2977-
}
2978-
err.emit();
2977+
err
2978+
} else {
2979+
type_error_struct!(self.tcx().sess, field.span, expr_t, E0610,
2980+
"`{}` is a primitive type and therefore doesn't have fields",
2981+
expr_t)
2982+
}.emit();
29792983
self.tcx().types.err
29802984
}
29812985
}

src/librustc_typeck/diagnostics.rs

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

4098+
E0609: r##"
4099+
Attempted to access a non-existent field in a struct.
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 that you didn't misspell the field's name or that the
4113+
field actually exists. 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+
4125+
E0610: r##"
4126+
Attempted to access a field on a primitive type.
4127+
4128+
Erroneous code example:
4129+
4130+
```compile_fail,E0610
4131+
let x: u32 = 0;
4132+
println!("{}", x.foo); // error: `{integer}` is a primitive type, therefore
4133+
// doesn't have fields
4134+
```
4135+
4136+
Primitive types are the most basic types available in Rust and don't have
4137+
fields. To access data via named fields, struct types are used. Example:
4138+
4139+
```
4140+
// We declare struct called `Foo` containing two fields:
4141+
struct Foo {
4142+
x: u32,
4143+
y: i64,
4144+
}
4145+
4146+
// We create an instance of this struct:
4147+
let variable = Foo { x: 0, y: -12 };
4148+
// And we can now access its fields:
4149+
println!("x: {}, y: {}", variable.x, variable.y);
4150+
```
4151+
4152+
For more information see The Rust Book: https://doc.rust-lang.org/book/
4153+
"##,
4154+
40984155
}
40994156

41004157
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/compile-fail/E0610.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
fn main() {
12+
let x = 0;
13+
let _ = x.foo; //~ ERROR E0610
14+
}

src/test/compile-fail/attempted-access-non-fatal.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111
// Check that bogus field access is non-fatal
1212
fn main() {
1313
let x = 0;
14-
let _ = x.foo; //~ no field `foo` on type `{integer}`
15-
let _ = x.bar; //~ no field `bar` on type `{integer}`
14+
let _ = x.foo; //~ `{integer}` is a primitive type and therefore doesn't have fields [E0610]
15+
let _ = x.bar; //~ `{integer}` is a primitive type and therefore doesn't have fields [E0610]
1616
}

src/test/compile-fail/issue-24363.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
fn main() {
12-
1.create_a_type_error[ //~ no field `create_a_type_error` on type `{integer}`
12+
1.create_a_type_error[ //~ `{integer}` is a primitive type and therefore doesn't have fields
1313
()+() //~ ERROR binary operation `+` cannot be applied
1414
// ^ ensure that we typeck the inner expression ^
1515
];

src/test/compile-fail/parse-error-correct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ fn main() {
1717
let y = 42;
1818
let x = y.; //~ ERROR unexpected token
1919
let x = y.(); //~ ERROR unexpected token
20-
let x = y.foo; //~ ERROR no field
20+
let x = y.foo; //~ ERROR `{integer}` is a primitive type and therefore doesn't have fields [E061
2121
}

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[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
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[E0610]: `{integer}` is a primitive type and therefore doesn't have fields
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)