Skip to content

Commit 0e98682

Browse files
committed
When encountering method on Self that we can't suggest, mention it
1 parent c4a4926 commit 0e98682

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+28-30
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
214214
module: None,
215215
}
216216
} else {
217+
let mut span_label = None;
217218
let item_span = path.last().unwrap().ident.span;
218219
let (mod_prefix, mod_str, module, suggestion) = if path.len() == 1 {
219220
debug!(?self.diagnostic_metadata.current_impl_items);
@@ -224,39 +225,36 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
224225
&& let FnKind::Fn(_, _, sig, ..) = fn_kind
225226
&& let Some(items) = self.diagnostic_metadata.current_impl_items
226227
&& let Some(item) = items.iter().find(|i| {
227-
if i.ident.name == item_str.name
228+
i.ident.name == item_str.name
228229
// Don't suggest if the item is in Fn signature arguments (#112590).
229230
&& !sig.span.contains(item_span)
230-
{
231-
debug!(?item_str.name);
232-
return match &i.kind {
233-
AssocItemKind::Fn(fn_)
234-
if !sig.decl.has_self() && fn_.sig.decl.has_self() => {
235-
// Ensure that we only suggest `self.` if `self` is available,
236-
// you can't call `fn foo(&self)` from `fn bar()` (#115992).
237-
false
238-
}
239-
AssocItemKind::Fn(_) | AssocItemKind::Const(..) => true,
240-
_ => false
241-
}
242-
}
243-
false
244231
})
245232
{
246-
let self_sugg = match &item.kind {
247-
AssocItemKind::Fn(fn_) if fn_.sig.decl.has_self() => "self.",
248-
_ => "Self::",
249-
};
250-
251-
Some((
252-
item_span.shrink_to_lo(),
253-
match &item.kind {
254-
AssocItemKind::Fn(..) => "consider using the associated function",
255-
AssocItemKind::Const(..) => "consider using the associated constant",
256-
_ => unreachable!("item kind was filtered above"),
257-
},
258-
self_sugg.to_string()
259-
))
233+
let sp = item_span.shrink_to_lo();
234+
match &item.kind {
235+
AssocItemKind::Fn(fn_)
236+
if !sig.decl.has_self() && fn_.sig.decl.has_self() => {
237+
// Ensure that we only suggest `self.` if `self` is available,
238+
// you can't call `fn foo(&self)` from `fn bar()` (#115992).
239+
// We also want to mention that the method exists.
240+
span_label = Some((
241+
item.ident.span,
242+
"a method by that name is available on `Self` here",
243+
));
244+
None
245+
}
246+
AssocItemKind::Fn(fn_) => Some((
247+
sp,
248+
"consider using the associated function",
249+
if fn_.sig.decl.has_self() { "self." } else { "Self::" },
250+
)),
251+
AssocItemKind::Const(..) => Some((
252+
sp,
253+
"consider using the associated constant",
254+
"Self::",
255+
)),
256+
_ => None
257+
}
260258
} else {
261259
None
262260
};
@@ -321,7 +319,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
321319
msg: format!("cannot find {expected} `{item_str}` in {mod_prefix}{mod_str}"),
322320
fallback_label,
323321
span: item_span,
324-
span_label: None,
322+
span_label,
325323
could_be_expr: false,
326324
suggestion,
327325
module,

tests/ui/suggestions/assoc_fn_without_self.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ LL | Self::foo();
1212
error[E0425]: cannot find function `bar` in this scope
1313
--> $DIR/assoc_fn_without_self.rs:17:9
1414
|
15+
LL | fn bar(&self) {}
16+
| --- a method by that name is available on `Self` here
17+
...
1518
LL | bar();
1619
| ^^^ not found in this scope
1720

0 commit comments

Comments
 (0)