Skip to content

Commit 8b12d5f

Browse files
committed
suggest qualifying bare associated constants
1 parent 8e68090 commit 8b12d5f

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -227,20 +227,27 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
227227
&& let Some(FnCtxt::Assoc(_)) = fn_kind.ctxt()
228228
&& let Some(items) = self.diagnostic_metadata.current_impl_items
229229
&& let Some(item) = items.iter().find(|i| {
230-
if let AssocItemKind::Fn(_) = &i.kind && i.ident.name == item_str.name
230+
if let AssocItemKind::Fn(..) | AssocItemKind::Const(..) = &i.kind
231+
&& i.ident.name == item_str.name
231232
{
232233
debug!(?item_str.name);
233234
return true
234235
}
235236
false
236237
})
237-
&& let AssocItemKind::Fn(fn_) = &item.kind
238238
{
239-
debug!(?fn_);
240-
let self_sugg = if fn_.sig.decl.has_self() { "self." } else { "Self::" };
239+
let self_sugg = match &item.kind {
240+
AssocItemKind::Fn(fn_) if fn_.sig.decl.has_self() => "self.",
241+
_ => "Self::",
242+
};
243+
241244
Some((
242245
item_span.shrink_to_lo(),
243-
"consider using the associated function",
246+
match &item.kind {
247+
AssocItemKind::Fn(..) => "consider using the associated function",
248+
AssocItemKind::Const(..) => "consider using the associated constant",
249+
_ => unreachable!("item kind was filtered above"),
250+
},
244251
self_sugg.to_string()
245252
))
246253
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct Foo;
2+
3+
impl Foo {
4+
const A_CONST: usize = 1;
5+
6+
fn foo() -> usize {
7+
A_CONST //~ ERROR cannot find value `A_CONST` in this scope
8+
}
9+
}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0425]: cannot find value `A_CONST` in this scope
2+
--> $DIR/assoc-const-without-self.rs:7:9
3+
|
4+
LL | A_CONST
5+
| ^^^^^^^ not found in this scope
6+
|
7+
help: consider using the associated constant
8+
|
9+
LL | Self::A_CONST
10+
| ++++++
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)