@@ -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,32 +225,41 @@ 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 let AssocItemKind :: Fn ( ..) | AssocItemKind :: Const ( ..) = & i. kind
228- && i. ident . name == item_str. name
229- // don't suggest if the item is in Fn signature arguments
230- // issue #112590
228+ i. ident . name == item_str. name
229+ // Don't suggest if the item is in Fn signature arguments (#112590).
231230 && !sig. span . contains ( item_span)
232- {
233- debug ! ( ?item_str. name) ;
234- return true
235- }
236- false
237231 } )
238232 {
239- let self_sugg = match & item. kind {
240- AssocItemKind :: Fn ( fn_) if fn_. sig . decl . has_self ( ) => "self." ,
241- _ => "Self::" ,
242- } ;
243-
244- Some ( (
245- item_span. shrink_to_lo ( ) ,
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- } ,
251- self_sugg. to_string ( )
252- ) )
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_) if fn_. sig . decl . has_self ( ) => Some ( (
247+ sp,
248+ "consider using the method on `Self`" ,
249+ "self." . to_string ( ) ,
250+ ) ) ,
251+ AssocItemKind :: Fn ( _) => Some ( (
252+ sp,
253+ "consider using the associated function on `Self`" ,
254+ "Self::" . to_string ( ) ,
255+ ) ) ,
256+ AssocItemKind :: Const ( ..) => Some ( (
257+ sp,
258+ "consider using the associated constant on `Self`" ,
259+ "Self::" . to_string ( ) ,
260+ ) ) ,
261+ _ => None
262+ }
253263 } else {
254264 None
255265 } ;
@@ -314,7 +324,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
314324 msg : format ! ( "cannot find {expected} `{item_str}` in {mod_prefix}{mod_str}" ) ,
315325 fallback_label,
316326 span : item_span,
317- span_label : None ,
327+ span_label,
318328 could_be_expr : false ,
319329 suggestion,
320330 module,
0 commit comments