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
This commit is contained in:
Jonathan Turner 2016-09-06 09:38:03 -07:00 committed by GitHub
commit c0e5a777d4
6 changed files with 40 additions and 27 deletions

View File

@ -118,7 +118,6 @@ use syntax::parse::token::{self, InternedString, keywords};
use syntax::ptr::P;
use syntax::util::lev_distance::find_best_match_for_name;
use syntax_pos::{self, Span};
use errors::DiagnosticBuilder;
use rustc::hir::intravisit::{self, Visitor};
use rustc::hir::{self, PatKind};
@ -2959,7 +2958,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
}, expr_t);
match expr_t.sty {
ty::TyStruct(def, _) | ty::TyUnion(def, _) => {
Self::suggest_field_names(&mut err, def.struct_variant(), field, vec![]);
if let Some(suggested_field_name) =
Self::suggest_field_name(def.struct_variant(), field, vec![]) {
err.span_help(field.span,
&format!("did you mean `{}`?", suggested_field_name));
};
}
ty::TyRawPtr(..) => {
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> {
}
}
// displays hints about the closest matches in field names
fn suggest_field_names(err: &mut DiagnosticBuilder,
variant: ty::VariantDef<'tcx>,
field: &Spanned<ast::Name>,
skip : Vec<InternedString>) {
// Return an hint about the closest match in field names
fn suggest_field_name(variant: ty::VariantDef<'tcx>,
field: &Spanned<ast::Name>,
skip : Vec<InternedString>)
-> Option<InternedString> {
let name = field.node.as_str();
let names = variant.fields.iter().filter_map(|field| {
// ignore already set fields and private fields from non-local crates
@ -2989,10 +2992,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
});
// only find fits with at least one matching letter
if let Some(name) = find_best_match_for_name(names, &name, Some(name.len())) {
err.span_help(field.span,
&format!("did you mean `{}`?", name));
}
find_best_match_for_name(names, &name, Some(name.len()))
}
// Check tuple index expressions
@ -3086,7 +3086,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
ty);
// prevent all specified fields from being suggested
let skip_fields = skip_fields.iter().map(|ref x| x.name.node.as_str());
Self::suggest_field_names(&mut err, variant, &field.name, skip_fields.collect());
if let Some(field_name) = Self::suggest_field_name(variant,
&field.name,
skip_fields.collect()) {
err.span_label(field.name.span,&format!("did you mean `{}`?",field_name));
};
err.emit();
}

View File

@ -13,5 +13,7 @@ enum Field {
}
fn main() {
let s = Field::Fool { joke: 0 }; //~ ERROR E0559
let s = Field::Fool { joke: 0 };
//~^ ERROR E0559
//~| NOTE did you mean `x`?
}

View File

@ -17,8 +17,9 @@ struct A {
fn main() {
let a = A {
foo : 5,
bar : 42,//~ ERROR struct `A` has no field named `bar`
//~^ HELP did you mean `barr`?
bar : 42,
//~^ ERROR struct `A` has no field named `bar`
//~| NOTE did you mean `barr`?
car : 9,
};
}

View File

@ -17,7 +17,8 @@ struct A {
fn main() {
let a = A {
foo : 5,
bar : 42,//~ ERROR struct `A` has no field named `bar`
//~^ HELP did you mean `car`?
bar : 42,
//~^ ERROR struct `A` has no field named `bar`
//~| NOTE did you mean `car`?
};
}

View File

@ -22,16 +22,20 @@ struct A {
fn main () {
// external crate struct
let k = B {
aa: 20, //~ ERROR struct `xc::B` has no field named `aa`
//~^ HELP did you mean `a`?
bb: 20, //~ ERROR struct `xc::B` has no field named `bb`
//~^ HELP did you mean `a`?
aa: 20,
//~^ ERROR struct `xc::B` has no field named `aa`
//~| NOTE did you mean `a`?
bb: 20,
//~^ ERROR struct `xc::B` has no field named `bb`
//~| NOTE did you mean `a`?
};
// local crate struct
let l = A {
aa: 20, //~ ERROR struct `A` has no field named `aa`
//~^ HELP did you mean `a`?
bb: 20, //~ ERROR struct `A` has no field named `bb`
//~^ HELP did you mean `b`?
aa: 20,
//~^ ERROR struct `A` has no field named `aa`
//~| NOTE did you mean `a`?
bb: 20,
//~^ ERROR struct `A` has no field named `bb`
//~| NOTE did you mean `b`?
};
}

View File

@ -19,8 +19,9 @@ impl U {
}
fn main() {
let u = U { principle: 0 }; //~ ERROR union `U` has no field named `principle`
//~^ HELP did you mean `principal`?
let u = U { principle: 0 };
//~^ ERROR union `U` has no field named `principle`
//~| NOTE did you mean `principal`?
let w = u.principial; //~ ERROR attempted access of field `principial` on type `U`
//~^ HELP did you mean `principal`?