Skip to content

Commit 6a972cd

Browse files
committed
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.
1 parent 783c6ec commit 6a972cd

File tree

8 files changed

+44
-24
lines changed

8 files changed

+44
-24
lines changed

src/librustc/ty/sty.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,15 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
12191219
}
12201220
}
12211221

1222+
pub fn is_enum(&self) -> bool {
1223+
match self.sty {
1224+
TyAdt(adt_def, _) => {
1225+
adt_def.is_enum()
1226+
}
1227+
_ => false,
1228+
}
1229+
}
1230+
12221231
pub fn is_closure(&self) -> bool {
12231232
match self.sty {
12241233
TyClosure(..) => true,
@@ -1233,6 +1242,13 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
12331242
}
12341243
}
12351244

1245+
pub fn is_fresh_ty(&self) -> bool {
1246+
match self.sty {
1247+
TyInfer(FreshTy(_)) => true,
1248+
_ => false,
1249+
}
1250+
}
1251+
12361252
pub fn is_fresh(&self) -> bool {
12371253
match self.sty {
12381254
TyInfer(FreshTy(_)) => true,

src/librustc_typeck/check/method/suggest.rs

+20-17
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
164164
};
165165

166166
match error {
167-
MethodError::NoMatch(NoMatchData { static_candidates: static_sources,
168-
unsatisfied_predicates,
169-
out_of_scope_traits,
170-
lev_candidate,
171-
mode,
172-
.. }) => {
167+
MethodError::NoMatch(NoMatchData {
168+
static_candidates: static_sources,
169+
unsatisfied_predicates,
170+
out_of_scope_traits,
171+
lev_candidate,
172+
mode,
173+
..
174+
}) => {
173175
let tcx = self.tcx;
174176

175177
let actual = self.resolve_type_vars_if_possible(&rcvr_ty);
@@ -179,18 +181,19 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
179181
current scope",
180182
if mode == Mode::MethodCall {
181183
"method"
184+
} else if actual.is_enum() {
185+
"variant"
182186
} else {
183-
match item_name.as_str().chars().next() {
184-
Some(name) => {
185-
if name.is_lowercase() {
186-
"function or associated item"
187-
} else {
188-
"associated item"
189-
}
190-
},
191-
None => {
192-
""
193-
},
187+
let fresh_ty = actual.is_fresh_ty();
188+
match (item_name.as_str().chars().next(), fresh_ty) {
189+
(Some(name), false) if name.is_lowercase() => {
190+
"function or associated item"
191+
}
192+
(Some(_), false) => "associated item",
193+
(Some(_), true) | (None, false) => {
194+
"variant or associated item"
195+
}
196+
(None, true) => "variant",
194197
}
195198
},
196199
item_name,

src/test/compile-fail/bogus-tag.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ fn main() {
1515
let red: color = color::rgb(255, 0, 0);
1616
match red {
1717
color::rgb(r, g, b) => { println!("rgb"); }
18-
color::hsl(h, s, l) => { println!("hsl"); } //~ ERROR no function
18+
color::hsl(h, s, l) => { println!("hsl"); } //~ ERROR no variant
1919
}
2020
}

src/test/compile-fail/empty-struct-braces-expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ fn main() {
2929

3030
let xe1 = XEmpty1; //~ ERROR expected value, found struct `XEmpty1`
3131
let xe1 = XEmpty1(); //~ ERROR expected function, found struct `XEmpty1`
32-
let xe3 = XE::Empty3; //~ ERROR no associated item named `Empty3` found for type
33-
let xe3 = XE::Empty3(); //~ ERROR no associated item named `Empty3` found for type
32+
let xe3 = XE::Empty3; //~ ERROR no variant named `Empty3` found for type
33+
let xe3 = XE::Empty3(); //~ ERROR no variant named `Empty3` found for type
3434
}

src/test/compile-fail/issue-22933-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ enum Delicious {
1212
Pie = 0x1,
1313
Apple = 0x2,
1414
ApplePie = Delicious::Apple as isize | Delicious::PIE as isize,
15-
//~^ ERROR no associated item named `PIE` found for type `Delicious`
15+
//~^ ERROR no variant named `PIE` found for type `Delicious`
1616
}
1717

1818
fn main() {}

src/test/compile-fail/issue-23173.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ enum Token { LeftParen, RightParen, Plus, Minus, /* etc */ }
1313
fn use_token(token: &Token) { unimplemented!() }
1414

1515
fn main() {
16-
use_token(&Token::Homura); //~ ERROR no associated item named
16+
use_token(&Token::Homura); //~ ERROR no variant named `Homura`
1717
}

src/test/compile-fail/issue-23217.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
pub enum SomeEnum {
1212
B = SomeEnum::A,
13-
//~^ ERROR no associated item named `A` found for type `SomeEnum`
13+
//~^ ERROR no variant named `A` found for type `SomeEnum`
1414
}
1515

1616
fn main() {}

src/test/compile-fail/issue-28971.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ enum Foo {
1616
fn main(){
1717
foo(|| {
1818
match Foo::Bar(1) {
19-
Foo::Baz(..) => (), //~ ERROR no associated
19+
Foo::Baz(..) => (),
20+
//~^ ERROR no variant named `Baz` found for type `Foo`
2021
_ => (),
2122
}
2223
});

0 commit comments

Comments
 (0)