Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
  • Loading branch information
Cobrand committed Sep 4, 2016
commit 1aa777b51f03593eb557d4e550830f8406ee37c3
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 @@ -2996,7 +2995,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 @@ -3009,11 +3012,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 @@ -3026,10 +3029,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 @@ -3125,7 +3125,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