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
This commit is contained in:
parent
b7d19899de
commit
1aa777b51f
6 changed files with 40 additions and 27 deletions
|
@ -118,7 +118,6 @@ use syntax::parse::token::{self, InternedString, keywords};
|
||||||
use syntax::ptr::P;
|
use syntax::ptr::P;
|
||||||
use syntax::util::lev_distance::find_best_match_for_name;
|
use syntax::util::lev_distance::find_best_match_for_name;
|
||||||
use syntax_pos::{self, Span};
|
use syntax_pos::{self, Span};
|
||||||
use errors::DiagnosticBuilder;
|
|
||||||
|
|
||||||
use rustc::hir::intravisit::{self, Visitor};
|
use rustc::hir::intravisit::{self, Visitor};
|
||||||
use rustc::hir::{self, PatKind};
|
use rustc::hir::{self, PatKind};
|
||||||
|
@ -2996,7 +2995,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||||
}, expr_t);
|
}, expr_t);
|
||||||
match expr_t.sty {
|
match expr_t.sty {
|
||||||
ty::TyStruct(def, _) | ty::TyUnion(def, _) => {
|
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(..) => {
|
ty::TyRawPtr(..) => {
|
||||||
err.note(&format!("`{0}` is a native pointer; perhaps you need to deref with \
|
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> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// displays hints about the closest matches in field names
|
// Return an hint about the closest match in field names
|
||||||
fn suggest_field_names(err: &mut DiagnosticBuilder,
|
fn suggest_field_name(variant: ty::VariantDef<'tcx>,
|
||||||
variant: ty::VariantDef<'tcx>,
|
|
||||||
field: &Spanned<ast::Name>,
|
field: &Spanned<ast::Name>,
|
||||||
skip : Vec<InternedString>) {
|
skip : Vec<InternedString>)
|
||||||
|
-> Option<InternedString> {
|
||||||
let name = field.node.as_str();
|
let name = field.node.as_str();
|
||||||
let names = variant.fields.iter().filter_map(|field| {
|
let names = variant.fields.iter().filter_map(|field| {
|
||||||
// ignore already set fields and private fields from non-local crates
|
// ignore already set fields and private fields from non-local crates
|
||||||
|
@ -3026,10 +3029,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||||
});
|
});
|
||||||
|
|
||||||
// only find fits with at least one matching letter
|
// only find fits with at least one matching letter
|
||||||
if let Some(name) = find_best_match_for_name(names, &name, Some(name.len())) {
|
find_best_match_for_name(names, &name, Some(name.len()))
|
||||||
err.span_help(field.span,
|
|
||||||
&format!("did you mean `{}`?", name));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check tuple index expressions
|
// Check tuple index expressions
|
||||||
|
@ -3125,7 +3125,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
|
||||||
ty);
|
ty);
|
||||||
// prevent all specified fields from being suggested
|
// prevent all specified fields from being suggested
|
||||||
let skip_fields = skip_fields.iter().map(|ref x| x.name.node.as_str());
|
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();
|
err.emit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,5 +13,7 @@ enum Field {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let s = Field::Fool { joke: 0 }; //~ ERROR E0559
|
let s = Field::Fool { joke: 0 };
|
||||||
|
//~^ ERROR E0559
|
||||||
|
//~| NOTE did you mean `x`?
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,8 +17,9 @@ struct A {
|
||||||
fn main() {
|
fn main() {
|
||||||
let a = A {
|
let a = A {
|
||||||
foo : 5,
|
foo : 5,
|
||||||
bar : 42,//~ ERROR struct `A` has no field named `bar`
|
bar : 42,
|
||||||
//~^ HELP did you mean `barr`?
|
//~^ ERROR struct `A` has no field named `bar`
|
||||||
|
//~| NOTE did you mean `barr`?
|
||||||
car : 9,
|
car : 9,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,8 @@ struct A {
|
||||||
fn main() {
|
fn main() {
|
||||||
let a = A {
|
let a = A {
|
||||||
foo : 5,
|
foo : 5,
|
||||||
bar : 42,//~ ERROR struct `A` has no field named `bar`
|
bar : 42,
|
||||||
//~^ HELP did you mean `car`?
|
//~^ ERROR struct `A` has no field named `bar`
|
||||||
|
//~| NOTE did you mean `car`?
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,16 +22,20 @@ struct A {
|
||||||
fn main () {
|
fn main () {
|
||||||
// external crate struct
|
// external crate struct
|
||||||
let k = B {
|
let k = B {
|
||||||
aa: 20, //~ ERROR struct `xc::B` has no field named `aa`
|
aa: 20,
|
||||||
//~^ HELP did you mean `a`?
|
//~^ ERROR struct `xc::B` has no field named `aa`
|
||||||
bb: 20, //~ ERROR struct `xc::B` has no field named `bb`
|
//~| NOTE did you mean `a`?
|
||||||
//~^ HELP did you mean `a`?
|
bb: 20,
|
||||||
|
//~^ ERROR struct `xc::B` has no field named `bb`
|
||||||
|
//~| NOTE did you mean `a`?
|
||||||
};
|
};
|
||||||
// local crate struct
|
// local crate struct
|
||||||
let l = A {
|
let l = A {
|
||||||
aa: 20, //~ ERROR struct `A` has no field named `aa`
|
aa: 20,
|
||||||
//~^ HELP did you mean `a`?
|
//~^ ERROR struct `A` has no field named `aa`
|
||||||
bb: 20, //~ ERROR struct `A` has no field named `bb`
|
//~| NOTE did you mean `a`?
|
||||||
//~^ HELP did you mean `b`?
|
bb: 20,
|
||||||
|
//~^ ERROR struct `A` has no field named `bb`
|
||||||
|
//~| NOTE did you mean `b`?
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,8 +19,9 @@ impl U {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let u = U { principle: 0 }; //~ ERROR union `U` has no field named `principle`
|
let u = U { principle: 0 };
|
||||||
//~^ HELP did you mean `principal`?
|
//~^ 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`
|
let w = u.principial; //~ ERROR attempted access of field `principial` on type `U`
|
||||||
//~^ HELP did you mean `principal`?
|
//~^ HELP did you mean `principal`?
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue