Skip to content

Commit 422d553

Browse files
committed
improve snippet for "did you mean x"
r? @nikomatsakis cc @jonathandturner Fixes #36164
1 parent 4f79716 commit 422d553

19 files changed

+77
-37
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2959,24 +2959,27 @@ 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, &format!("unknown field"));
2974+
}
29742975
}
29752976
ty::TyRawPtr(..) => {
29762977
err.note(&format!("`{0}` is a native pointer; perhaps you need to deref with \
29772978
`(*{0}).{1}`", pprust::expr_to_string(base), field.node));
29782979
}
2979-
_ => {}
2980+
_ => {
2981+
err.span_label(field.span, &format!("unknown field"));
2982+
}
29802983
}
29812984
err.emit();
29822985
self.tcx().types.err

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
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; //~ ERROR no field `foo` on type
15+
//~| NOTE unknown field
16+
let _ = x.bar; //~ ERROR no field `bar` on type
17+
//~| NOTE unknown field
1618
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ 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; //~ ERROR no field `f` on type `fn() {main}`
116+
//~| NOTE unknown field
116117

117118
let cf: *const Foo = &0;
118119
let _ = cf as *const [u16];

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ 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`
23+
//~| unknown field
2324
self.tcx();
2425
}
2526
}

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; //~ ERROR no field `x`
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; //~ ERROR no field `y`
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); //~ ERROR 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+
//~^ ERROR no field `nonexistent_field`
1717
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ 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+
//~^ ERROR: no field `a` on type `Foo`
16+
//~| NOTE unknown field
1617
}

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[ //~ ERROR no field `create_a_type_error`
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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ pub enum Foo {
1717
}
1818

1919
fn test(a: Foo) {
20-
println!("{}", a.b); //~ ERROR attempted access of field
20+
println!("{}", a.b); //~ ERROR no field `b` on type `Foo`
21+
//~| NOTE unknown field
22+
//~| NOTE in this expansion
2123
}
2224

2325
fn main() {
2426
let x = Attribute::Code {
2527
attr_name_idx: 42,
2628
};
27-
let z = (&x).attr_name_idx; //~ ERROR attempted access of field
28-
let y = x.attr_name_idx; //~ ERROR attempted access of field
29+
let z = (&x).attr_name_idx; //~ ERROR no field `attr_name_idx` on type `&Attribute`
30+
//~| NOTE unknown field
31+
let y = x.attr_name_idx; //~ ERROR no field `attr_name_idx` on type `Attribute`
32+
//~| NOTE unknown field
2933
}

0 commit comments

Comments
 (0)