From 6a972cd8558d5952b1093d7f7e525b5fd8b78d06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 15 Nov 2017 18:55:45 -0800 Subject: [PATCH] Use the proper term when using non-existing variant When using a non-existing variant, function or associated item, refer to the proper term, instead of defaulting to "associated item" in diagnostics. --- src/librustc/ty/sty.rs | 16 ++++++++ src/librustc_typeck/check/method/suggest.rs | 37 ++++++++++--------- src/test/compile-fail/bogus-tag.rs | 2 +- .../compile-fail/empty-struct-braces-expr.rs | 4 +- src/test/compile-fail/issue-22933-2.rs | 2 +- src/test/compile-fail/issue-23173.rs | 2 +- src/test/compile-fail/issue-23217.rs | 2 +- src/test/compile-fail/issue-28971.rs | 3 +- 8 files changed, 44 insertions(+), 24 deletions(-) diff --git a/src/librustc/ty/sty.rs b/src/librustc/ty/sty.rs index a60cad0de9f..8129b27eb6a 100644 --- a/src/librustc/ty/sty.rs +++ b/src/librustc/ty/sty.rs @@ -1219,6 +1219,15 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> { } } + pub fn is_enum(&self) -> bool { + match self.sty { + TyAdt(adt_def, _) => { + adt_def.is_enum() + } + _ => false, + } + } + pub fn is_closure(&self) -> bool { match self.sty { TyClosure(..) => true, @@ -1233,6 +1242,13 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> { } } + pub fn is_fresh_ty(&self) -> bool { + match self.sty { + TyInfer(FreshTy(_)) => true, + _ => false, + } + } + pub fn is_fresh(&self) -> bool { match self.sty { TyInfer(FreshTy(_)) => true, diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index 8613ec86b4a..ba7a2b4fb33 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -164,12 +164,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { }; match error { - MethodError::NoMatch(NoMatchData { static_candidates: static_sources, - unsatisfied_predicates, - out_of_scope_traits, - lev_candidate, - mode, - .. }) => { + MethodError::NoMatch(NoMatchData { + static_candidates: static_sources, + unsatisfied_predicates, + out_of_scope_traits, + lev_candidate, + mode, + .. + }) => { let tcx = self.tcx; let actual = self.resolve_type_vars_if_possible(&rcvr_ty); @@ -179,18 +181,19 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { current scope", if mode == Mode::MethodCall { "method" + } else if actual.is_enum() { + "variant" } else { - match item_name.as_str().chars().next() { - Some(name) => { - if name.is_lowercase() { - "function or associated item" - } else { - "associated item" - } - }, - None => { - "" - }, + let fresh_ty = actual.is_fresh_ty(); + match (item_name.as_str().chars().next(), fresh_ty) { + (Some(name), false) if name.is_lowercase() => { + "function or associated item" + } + (Some(_), false) => "associated item", + (Some(_), true) | (None, false) => { + "variant or associated item" + } + (None, true) => "variant", } }, item_name, diff --git a/src/test/compile-fail/bogus-tag.rs b/src/test/compile-fail/bogus-tag.rs index c388d47da6e..65daa9a82ea 100644 --- a/src/test/compile-fail/bogus-tag.rs +++ b/src/test/compile-fail/bogus-tag.rs @@ -15,6 +15,6 @@ fn main() { let red: color = color::rgb(255, 0, 0); match red { color::rgb(r, g, b) => { println!("rgb"); } - color::hsl(h, s, l) => { println!("hsl"); } //~ ERROR no function + color::hsl(h, s, l) => { println!("hsl"); } //~ ERROR no variant } } diff --git a/src/test/compile-fail/empty-struct-braces-expr.rs b/src/test/compile-fail/empty-struct-braces-expr.rs index d4e85e9744d..3096e8f8313 100644 --- a/src/test/compile-fail/empty-struct-braces-expr.rs +++ b/src/test/compile-fail/empty-struct-braces-expr.rs @@ -29,6 +29,6 @@ fn main() { let xe1 = XEmpty1; //~ ERROR expected value, found struct `XEmpty1` let xe1 = XEmpty1(); //~ ERROR expected function, found struct `XEmpty1` - let xe3 = XE::Empty3; //~ ERROR no associated item named `Empty3` found for type - let xe3 = XE::Empty3(); //~ ERROR no associated item named `Empty3` found for type + let xe3 = XE::Empty3; //~ ERROR no variant named `Empty3` found for type + let xe3 = XE::Empty3(); //~ ERROR no variant named `Empty3` found for type } diff --git a/src/test/compile-fail/issue-22933-2.rs b/src/test/compile-fail/issue-22933-2.rs index 97456c2da87..e887c7760d6 100644 --- a/src/test/compile-fail/issue-22933-2.rs +++ b/src/test/compile-fail/issue-22933-2.rs @@ -12,7 +12,7 @@ enum Delicious { Pie = 0x1, Apple = 0x2, ApplePie = Delicious::Apple as isize | Delicious::PIE as isize, - //~^ ERROR no associated item named `PIE` found for type `Delicious` + //~^ ERROR no variant named `PIE` found for type `Delicious` } fn main() {} diff --git a/src/test/compile-fail/issue-23173.rs b/src/test/compile-fail/issue-23173.rs index 946e4b9e96e..0ad583e5283 100644 --- a/src/test/compile-fail/issue-23173.rs +++ b/src/test/compile-fail/issue-23173.rs @@ -13,5 +13,5 @@ enum Token { LeftParen, RightParen, Plus, Minus, /* etc */ } fn use_token(token: &Token) { unimplemented!() } fn main() { - use_token(&Token::Homura); //~ ERROR no associated item named + use_token(&Token::Homura); //~ ERROR no variant named `Homura` } diff --git a/src/test/compile-fail/issue-23217.rs b/src/test/compile-fail/issue-23217.rs index 95f6526f115..dc51adf42aa 100644 --- a/src/test/compile-fail/issue-23217.rs +++ b/src/test/compile-fail/issue-23217.rs @@ -10,7 +10,7 @@ pub enum SomeEnum { B = SomeEnum::A, - //~^ ERROR no associated item named `A` found for type `SomeEnum` + //~^ ERROR no variant named `A` found for type `SomeEnum` } fn main() {} diff --git a/src/test/compile-fail/issue-28971.rs b/src/test/compile-fail/issue-28971.rs index 1d14b71a40e..e553fee5a62 100644 --- a/src/test/compile-fail/issue-28971.rs +++ b/src/test/compile-fail/issue-28971.rs @@ -16,7 +16,8 @@ enum Foo { fn main(){ foo(|| { match Foo::Bar(1) { - Foo::Baz(..) => (), //~ ERROR no associated + Foo::Baz(..) => (), + //~^ ERROR no variant named `Baz` found for type `Foo` _ => (), } });