Skip to content

Commit 2f37894

Browse files
Add E0610
1 parent f4dd365 commit 2f37894

File tree

8 files changed

+98
-31
lines changed

8 files changed

+98
-31
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-22
Original file line numberDiff line numberDiff line change
@@ -2921,29 +2921,34 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
29212921
.emit();
29222922
self.tcx().types.err
29232923
} else {
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);
2927-
match expr_t.sty {
2928-
ty::TyAdt(def, _) if !def.is_enum() => {
2929-
if let Some(suggested_field_name) =
2930-
Self::suggest_field_name(def.struct_variant(), field, vec![]) {
2931-
err.span_label(field.span,
2932-
format!("did you mean `{}`?", suggested_field_name));
2933-
} else {
2934-
err.span_label(field.span,
2935-
"unknown field");
2936-
};
2937-
}
2938-
ty::TyRawPtr(..) => {
2939-
err.note(&format!("`{0}` is a native pointer; perhaps you need to deref with \
2940-
`(*{0}).{1}`",
2941-
self.tcx.hir.node_to_pretty_string(base.id),
2942-
field.node));
2924+
if !expr_t.is_primitive_ty() {
2925+
let mut err = type_error_struct!(self.tcx().sess, field.span, expr_t, E0609,
2926+
"no field `{}` on type `{}`",
2927+
field.node, expr_t);
2928+
match expr_t.sty {
2929+
ty::TyAdt(def, _) if !def.is_enum() => {
2930+
if let Some(suggested_field_name) =
2931+
Self::suggest_field_name(def.struct_variant(), field, vec![]) {
2932+
err.span_label(field.span,
2933+
format!("did you mean `{}`?", suggested_field_name));
2934+
} else {
2935+
err.span_label(field.span, "unknown field");
2936+
};
2937+
}
2938+
ty::TyRawPtr(..) => {
2939+
err.note(&format!("`{0}` is a native pointer; perhaps you need to deref \
2940+
with `(*{0}).{1}`",
2941+
self.tcx.hir.node_to_pretty_string(base.id),
2942+
field.node));
2943+
}
2944+
_ => {}
29432945
}
2944-
_ => {}
2945-
}
2946-
err.emit();
2946+
err
2947+
} else {
2948+
type_error_struct!(self.tcx().sess, field.span, expr_t, E0610,
2949+
"`{}` is a primitive type and therefore doesn't have fields",
2950+
expr_t)
2951+
}.emit();
29472952
self.tcx().types.err
29482953
}
29492954
}

src/librustc_typeck/diagnostics.rs

+33-3
Original file line numberDiff line numberDiff line change
@@ -4096,7 +4096,7 @@ assert_eq!(!Question::No, true);
40964096
"##,
40974097

40984098
E0609: r##"
4099-
An attempt to access a non-existent field in a struct was performed.
4099+
Attempted to access a non-existent field in a struct.
41004100
41014101
Erroneous code example:
41024102
@@ -4109,8 +4109,8 @@ let s = StructWithFields { x: 0 };
41094109
println!("{}", s.foo); // error: no field `foo` on type `StructWithFields`
41104110
```
41114111
4112-
To fix this error, check if you didn't misspell the field's name or that the
4113-
field actually exist. Example:
4112+
To fix this error, check that you didn't misspell the field's name or that the
4113+
field actually exists. Example:
41144114
41154115
```
41164116
struct StructWithFields {
@@ -4122,6 +4122,36 @@ println!("{}", s.x); // ok!
41224122
```
41234123
"##,
41244124

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+
41254155
}
41264156

41274157
register_diagnostics! {

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/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[E0609]: 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[E0609]: 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

0 commit comments

Comments
 (0)