Skip to content

Commit

Permalink
suggest qualifying bare associated constants
Browse files Browse the repository at this point in the history
  • Loading branch information
euclio committed Jan 25, 2023
1 parent 8e68090 commit 8b12d5f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
17 changes: 12 additions & 5 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,27 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
&& let Some(FnCtxt::Assoc(_)) = fn_kind.ctxt()
&& let Some(items) = self.diagnostic_metadata.current_impl_items
&& let Some(item) = items.iter().find(|i| {
if let AssocItemKind::Fn(_) = &i.kind && i.ident.name == item_str.name
if let AssocItemKind::Fn(..) | AssocItemKind::Const(..) = &i.kind
&& i.ident.name == item_str.name
{
debug!(?item_str.name);
return true
}
false
})
&& let AssocItemKind::Fn(fn_) = &item.kind
{
debug!(?fn_);
let self_sugg = if fn_.sig.decl.has_self() { "self." } else { "Self::" };
let self_sugg = match &item.kind {
AssocItemKind::Fn(fn_) if fn_.sig.decl.has_self() => "self.",
_ => "Self::",
};

Some((
item_span.shrink_to_lo(),
"consider using the associated function",
match &item.kind {
AssocItemKind::Fn(..) => "consider using the associated function",
AssocItemKind::Const(..) => "consider using the associated constant",
_ => unreachable!("item kind was filtered above"),
},
self_sugg.to_string()
))
} else {
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/suggestions/assoc-const-without-self.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
struct Foo;

impl Foo {
const A_CONST: usize = 1;

fn foo() -> usize {
A_CONST //~ ERROR cannot find value `A_CONST` in this scope
}
}

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui/suggestions/assoc-const-without-self.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0425]: cannot find value `A_CONST` in this scope
--> $DIR/assoc-const-without-self.rs:7:9
|
LL | A_CONST
| ^^^^^^^ not found in this scope
|
help: consider using the associated constant
|
LL | Self::A_CONST
| ++++++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0425`.

0 comments on commit 8b12d5f

Please sign in to comment.