From 1aa777b51f03593eb557d4e550830f8406ee37c3 Mon Sep 17 00:00:00 2001 From: Cobrand Date: Sun, 4 Sep 2016 21:14:41 +0200 Subject: [PATCH] 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 --- src/librustc_typeck/check/mod.rs | 28 +++++++++++-------- src/test/compile-fail/E0559.rs | 4 ++- .../struct-fields-hints-no-dupe.rs | 5 ++-- src/test/compile-fail/struct-fields-hints.rs | 5 ++-- .../compile-fail/suggest-private-fields.rs | 20 +++++++------ .../compile-fail/union/union-suggest-field.rs | 5 ++-- 6 files changed, 40 insertions(+), 27 deletions(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index f4fea5542b3de..8518689fcb40b 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -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}; @@ -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 \ @@ -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, - skip : Vec) { + // Return an hint about the closest match in field names + fn suggest_field_name(variant: ty::VariantDef<'tcx>, + field: &Spanned, + skip : Vec) + -> Option { 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 @@ -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 @@ -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(); } diff --git a/src/test/compile-fail/E0559.rs b/src/test/compile-fail/E0559.rs index 80eeb203a850e..aeeeae4222813 100644 --- a/src/test/compile-fail/E0559.rs +++ b/src/test/compile-fail/E0559.rs @@ -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`? } diff --git a/src/test/compile-fail/struct-fields-hints-no-dupe.rs b/src/test/compile-fail/struct-fields-hints-no-dupe.rs index 5f1f8ca856f9c..f25f01af33fd1 100644 --- a/src/test/compile-fail/struct-fields-hints-no-dupe.rs +++ b/src/test/compile-fail/struct-fields-hints-no-dupe.rs @@ -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, }; } diff --git a/src/test/compile-fail/struct-fields-hints.rs b/src/test/compile-fail/struct-fields-hints.rs index 4ba1fd2f7bb33..62ec6e6b0d249 100644 --- a/src/test/compile-fail/struct-fields-hints.rs +++ b/src/test/compile-fail/struct-fields-hints.rs @@ -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`? }; } diff --git a/src/test/compile-fail/suggest-private-fields.rs b/src/test/compile-fail/suggest-private-fields.rs index 41bd00a518c5c..906bfc78498e4 100644 --- a/src/test/compile-fail/suggest-private-fields.rs +++ b/src/test/compile-fail/suggest-private-fields.rs @@ -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`? }; } diff --git a/src/test/compile-fail/union/union-suggest-field.rs b/src/test/compile-fail/union/union-suggest-field.rs index b05e9b6e27334..92811b6b5be11 100644 --- a/src/test/compile-fail/union/union-suggest-field.rs +++ b/src/test/compile-fail/union/union-suggest-field.rs @@ -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`?