Skip to content

Commit 99aae9b

Browse files
committed
Improve error message and snippet for "did you mean x"
- Fixes #36164 - Part of #35233 - handles unknown fields - uses UI-style tests - update all related tests (cfail, ui, incremental)
1 parent 8b00355 commit 99aae9b

22 files changed

+89
-36
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2959,18 +2959,20 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
29592959
.emit();
29602960
self.tcx().types.err
29612961
} else {
2962-
let mut err = self.type_error_struct(expr.span, |actual| {
2963-
format!("attempted access of field `{}` on type `{}`, \
2964-
but no field with that name was found",
2962+
let mut err = self.type_error_struct(field.span, |actual| {
2963+
format!("no field `{}` on type `{}`",
29652964
field.node, actual)
29662965
}, expr_t);
29672966
match expr_t.sty {
29682967
ty::TyAdt(def, _) if !def.is_enum() => {
29692968
if let Some(suggested_field_name) =
29702969
Self::suggest_field_name(def.struct_variant(), field, vec![]) {
2971-
err.span_help(field.span,
2972-
&format!("did you mean `{}`?", suggested_field_name));
2973-
};
2970+
err.span_label(field.span,
2971+
&format!("did you mean `{}`?", suggested_field_name));
2972+
} else {
2973+
err.span_label(field.span,
2974+
&format!("unknown field"));
2975+
};
29742976
}
29752977
ty::TyRawPtr(..) => {
29762978
err.note(&format!("`{0}` is a native pointer; perhaps you need to deref with \

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

Lines changed: 2 additions & 2 deletions
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; //~ ERROR attempted access of field
15-
let _ = x.bar; //~ ERROR attempted access of field
14+
let _ = x.foo; //~ no field `foo` on type `{integer}`
15+
let _ = x.bar; //~ no field `bar` on type `{integer}`
1616
}

src/test/compile-fail/cast-rfc0401.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ fn main()
112112
//~| NOTE required for the cast to the object type `Foo`
113113

114114
// check no error cascade
115-
let _ = main.f as *const u32; //~ ERROR attempted access of field
115+
let _ = main.f as *const u32; //~ no field `f` on type `fn() {main}`
116116

117117
let cf: *const Foo = &0;
118118
let _ = cf as *const [u16];

src/test/compile-fail/derived-errors/issue-30580.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl<'a, 'tcx> Pass<'a, 'tcx>
1919
pub fn tcx(&self) -> &'a &'tcx () { self.1 }
2020
fn lol(&mut self, b: &Foo)
2121
{
22-
b.c; //~ ERROR no field with that name was found
22+
b.c; //~ ERROR no field `c` on type `&Foo`
2323
self.tcx();
2424
}
2525
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ struct A { x: i32, y: f64 }
1414

1515
#[cfg(not(works))]
1616
unsafe fn access(n:*mut A) -> (i32, f64) {
17-
let x : i32 = n.x; //~ ERROR attempted access of field `x`
17+
let x : i32 = n.x; //~ no field `x` on type `*mut A`
1818
//~| NOTE `n` is a native pointer; perhaps you need to deref with `(*n).x`
19-
let y : f64 = n.y; //~ ERROR attempted access of field `y`
19+
let y : f64 = n.y; //~ no field `y` on type `*mut A`
2020
//~| NOTE `n` is a native pointer; perhaps you need to deref with `(*n).y`
2121
(x, y)
2222
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@
1010

1111
fn main() {
1212
let foo = "str";
13-
println!("{}", foo.desc); //~ ERROR attempted access of field `desc` on type `&str`,
14-
// but no field with that name was found
13+
println!("{}", foo.desc); //~ no field `desc` on type `&str`
1514
}

src/test/compile-fail/issue-19244-2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ const STRUCT: MyStruct = MyStruct { field: 42 };
1313

1414
fn main() {
1515
let a: [isize; STRUCT.nonexistent_field];
16-
//~^ ERROR attempted access of field `nonexistent_field`
16+
//~^ no field `nonexistent_field` on type `MyStruct`
1717
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ enum Foo { Bar }
1212

1313
fn main() {
1414
Foo::Bar.a;
15-
//~^ ERROR: attempted access of field `a` on type `Foo`, but no field with that name was found
15+
//~^ no field `a` on type `Foo`
1616
}

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

Lines changed: 1 addition & 1 deletion
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[ //~ ERROR attempted access of field
12+
1.create_a_type_error[ //~ no field `create_a_type_error` on type `{integer}`
1313
()+() //~ ERROR binary operation `+` cannot be applied
1414
// ^ ensure that we typeck the inner expression ^
1515
];

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ pub enum Foo {
1717
}
1818

1919
fn test(a: Foo) {
20-
println!("{}", a.b); //~ ERROR attempted access of field
20+
println!("{}", a.b); //~ no field `b` on type `Foo`
2121
}
2222

2323
fn main() {
2424
let x = Attribute::Code {
2525
attr_name_idx: 42,
2626
};
27-
let z = (&x).attr_name_idx; //~ ERROR attempted access of field
28-
let y = x.attr_name_idx; //~ ERROR attempted access of field
27+
let z = (&x).attr_name_idx; //~ no field `attr_name_idx` on type `&Attribute`
28+
let y = x.attr_name_idx; //~ no field `attr_name_idx` on type `Attribute`
2929
}

0 commit comments

Comments
 (0)