Skip to content

Commit

Permalink
Rollup merge of rust-lang#36267 - Cobrand:E0559, r=jonathandturner
Browse files Browse the repository at this point in the history
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 rust-lang#36197
  • Loading branch information
Jonathan Turner authored Sep 6, 2016
2 parents 0ffa53f + 1aa777b commit d21e489
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 27 deletions.
28 changes: 16 additions & 12 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 \
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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();
}

Expand Down
4 changes: 3 additions & 1 deletion src/test/compile-fail/E0559.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`?
}
5 changes: 3 additions & 2 deletions src/test/compile-fail/struct-fields-hints-no-dupe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
5 changes: 3 additions & 2 deletions src/test/compile-fail/struct-fields-hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`?
};
}
20 changes: 12 additions & 8 deletions src/test/compile-fail/suggest-private-fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`?
};
}
5 changes: 3 additions & 2 deletions src/test/compile-fail/union/union-suggest-field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`?

Expand Down

0 comments on commit d21e489

Please sign in to comment.