Skip to content

Commit 1aa777b

Browse files
committed
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
1 parent b7d1989 commit 1aa777b

File tree

6 files changed

+40
-27
lines changed

6 files changed

+40
-27
lines changed

src/librustc_typeck/check/mod.rs

+16-12
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};
@@ -2996,7 +2995,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
29962995
}, expr_t);
29972996
match expr_t.sty {
29982997
ty::TyStruct(def, _) | ty::TyUnion(def, _) => {
2999-
Self::suggest_field_names(&mut err, def.struct_variant(), field, vec![]);
2998+
if let Some(suggested_field_name) =
2999+
Self::suggest_field_name(def.struct_variant(), field, vec![]) {
3000+
err.span_help(field.span,
3001+
&format!("did you mean `{}`?", suggested_field_name));
3002+
};
30003003
}
30013004
ty::TyRawPtr(..) => {
30023005
err.note(&format!("`{0}` is a native pointer; perhaps you need to deref with \
@@ -3009,11 +3012,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
30093012
}
30103013
}
30113014

3012-
// displays hints about the closest matches in field names
3013-
fn suggest_field_names(err: &mut DiagnosticBuilder,
3014-
variant: ty::VariantDef<'tcx>,
3015-
field: &Spanned<ast::Name>,
3016-
skip : Vec<InternedString>) {
3015+
// Return an hint about the closest match in field names
3016+
fn suggest_field_name(variant: ty::VariantDef<'tcx>,
3017+
field: &Spanned<ast::Name>,
3018+
skip : Vec<InternedString>)
3019+
-> Option<InternedString> {
30173020
let name = field.node.as_str();
30183021
let names = variant.fields.iter().filter_map(|field| {
30193022
// ignore already set fields and private fields from non-local crates
@@ -3026,10 +3029,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
30263029
});
30273030

30283031
// only find fits with at least one matching letter
3029-
if let Some(name) = find_best_match_for_name(names, &name, Some(name.len())) {
3030-
err.span_help(field.span,
3031-
&format!("did you mean `{}`?", name));
3032-
}
3032+
find_best_match_for_name(names, &name, Some(name.len()))
30333033
}
30343034

30353035
// Check tuple index expressions
@@ -3125,7 +3125,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
31253125
ty);
31263126
// prevent all specified fields from being suggested
31273127
let skip_fields = skip_fields.iter().map(|ref x| x.name.node.as_str());
3128-
Self::suggest_field_names(&mut err, variant, &field.name, skip_fields.collect());
3128+
if let Some(field_name) = Self::suggest_field_name(variant,
3129+
&field.name,
3130+
skip_fields.collect()) {
3131+
err.span_label(field.name.span,&format!("did you mean `{}`?",field_name));
3132+
};
31293133
err.emit();
31303134
}
31313135

src/test/compile-fail/E0559.rs

+3-1
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

+3-2
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

+3-2
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

+12-8
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

+3-2
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)