Skip to content

Commit d21e489

Browse files
author
Jonathan Turner
authored
Rollup merge of #36267 - Cobrand:E0559, r=jonathandturner
Updated E0559 to new format Refactored a method that printed one suggested field name, into a method that returns an `Option` of a suggestion (Updated test cases accordingly) r? @jonathandturner Closes #36197
2 parents 0ffa53f + 1aa777b commit d21e489

File tree

6 files changed

+40
-27
lines changed

6 files changed

+40
-27
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ use syntax::parse::token::{self, InternedString, keywords};
118118
use syntax::ptr::P;
119119
use syntax::util::lev_distance::find_best_match_for_name;
120120
use syntax_pos::{self, Span};
121-
use errors::DiagnosticBuilder;
122121

123122
use rustc::hir::intravisit::{self, Visitor};
124123
use rustc::hir::{self, PatKind};
@@ -2959,7 +2958,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
29592958
}, expr_t);
29602959
match expr_t.sty {
29612960
ty::TyStruct(def, _) | ty::TyUnion(def, _) => {
2962-
Self::suggest_field_names(&mut err, def.struct_variant(), field, vec![]);
2961+
if let Some(suggested_field_name) =
2962+
Self::suggest_field_name(def.struct_variant(), field, vec![]) {
2963+
err.span_help(field.span,
2964+
&format!("did you mean `{}`?", suggested_field_name));
2965+
};
29632966
}
29642967
ty::TyRawPtr(..) => {
29652968
err.note(&format!("`{0}` is a native pointer; perhaps you need to deref with \
@@ -2972,11 +2975,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
29722975
}
29732976
}
29742977

2975-
// displays hints about the closest matches in field names
2976-
fn suggest_field_names(err: &mut DiagnosticBuilder,
2977-
variant: ty::VariantDef<'tcx>,
2978-
field: &Spanned<ast::Name>,
2979-
skip : Vec<InternedString>) {
2978+
// Return an hint about the closest match in field names
2979+
fn suggest_field_name(variant: ty::VariantDef<'tcx>,
2980+
field: &Spanned<ast::Name>,
2981+
skip : Vec<InternedString>)
2982+
-> Option<InternedString> {
29802983
let name = field.node.as_str();
29812984
let names = variant.fields.iter().filter_map(|field| {
29822985
// ignore already set fields and private fields from non-local crates
@@ -2989,10 +2992,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
29892992
});
29902993

29912994
// only find fits with at least one matching letter
2992-
if let Some(name) = find_best_match_for_name(names, &name, Some(name.len())) {
2993-
err.span_help(field.span,
2994-
&format!("did you mean `{}`?", name));
2995-
}
2995+
find_best_match_for_name(names, &name, Some(name.len()))
29962996
}
29972997

29982998
// Check tuple index expressions
@@ -3086,7 +3086,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
30863086
ty);
30873087
// prevent all specified fields from being suggested
30883088
let skip_fields = skip_fields.iter().map(|ref x| x.name.node.as_str());
3089-
Self::suggest_field_names(&mut err, variant, &field.name, skip_fields.collect());
3089+
if let Some(field_name) = Self::suggest_field_name(variant,
3090+
&field.name,
3091+
skip_fields.collect()) {
3092+
err.span_label(field.name.span,&format!("did you mean `{}`?",field_name));
3093+
};
30903094
err.emit();
30913095
}
30923096

src/test/compile-fail/E0559.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ enum Field {
1313
}
1414

1515
fn main() {
16-
let s = Field::Fool { joke: 0 }; //~ ERROR E0559
16+
let s = Field::Fool { joke: 0 };
17+
//~^ ERROR E0559
18+
//~| NOTE did you mean `x`?
1719
}

src/test/compile-fail/struct-fields-hints-no-dupe.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ struct A {
1717
fn main() {
1818
let a = A {
1919
foo : 5,
20-
bar : 42,//~ ERROR struct `A` has no field named `bar`
21-
//~^ HELP did you mean `barr`?
20+
bar : 42,
21+
//~^ ERROR struct `A` has no field named `bar`
22+
//~| NOTE did you mean `barr`?
2223
car : 9,
2324
};
2425
}

src/test/compile-fail/struct-fields-hints.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ struct A {
1717
fn main() {
1818
let a = A {
1919
foo : 5,
20-
bar : 42,//~ ERROR struct `A` has no field named `bar`
21-
//~^ HELP did you mean `car`?
20+
bar : 42,
21+
//~^ ERROR struct `A` has no field named `bar`
22+
//~| NOTE did you mean `car`?
2223
};
2324
}

src/test/compile-fail/suggest-private-fields.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,20 @@ struct A {
2222
fn main () {
2323
// external crate struct
2424
let k = B {
25-
aa: 20, //~ ERROR struct `xc::B` has no field named `aa`
26-
//~^ HELP did you mean `a`?
27-
bb: 20, //~ ERROR struct `xc::B` has no field named `bb`
28-
//~^ HELP did you mean `a`?
25+
aa: 20,
26+
//~^ ERROR struct `xc::B` has no field named `aa`
27+
//~| NOTE did you mean `a`?
28+
bb: 20,
29+
//~^ ERROR struct `xc::B` has no field named `bb`
30+
//~| NOTE did you mean `a`?
2931
};
3032
// local crate struct
3133
let l = A {
32-
aa: 20, //~ ERROR struct `A` has no field named `aa`
33-
//~^ HELP did you mean `a`?
34-
bb: 20, //~ ERROR struct `A` has no field named `bb`
35-
//~^ HELP did you mean `b`?
34+
aa: 20,
35+
//~^ ERROR struct `A` has no field named `aa`
36+
//~| NOTE did you mean `a`?
37+
bb: 20,
38+
//~^ ERROR struct `A` has no field named `bb`
39+
//~| NOTE did you mean `b`?
3640
};
3741
}

src/test/compile-fail/union/union-suggest-field.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ impl U {
1919
}
2020

2121
fn main() {
22-
let u = U { principle: 0 }; //~ ERROR union `U` has no field named `principle`
23-
//~^ HELP did you mean `principal`?
22+
let u = U { principle: 0 };
23+
//~^ ERROR union `U` has no field named `principle`
24+
//~| NOTE did you mean `principal`?
2425
let w = u.principial; //~ ERROR attempted access of field `principial` on type `U`
2526
//~^ HELP did you mean `principal`?
2627

0 commit comments

Comments
 (0)