@@ -1186,12 +1186,18 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
11861186 let is_assoc_fn = self . self_type_is_available ( ) ;
11871187 let self_from_macro = "a `self` parameter, but a macro invocation can only \
11881188 access identifiers it receives from parameters";
1189- if let Some ( ( fn_kind, span ) ) = & self . diag_metadata . current_function {
1189+ if let Some ( ( fn_kind, fn_span ) ) = & self . diag_metadata . current_function {
11901190 // The current function has a `self` parameter, but we were unable to resolve
11911191 // a reference to `self`. This can only happen if the `self` identifier we
1192- // are resolving came from a different hygiene context.
1192+ // are resolving came from a different hygiene context or a variable binding .
11931193 if fn_kind. decl ( ) . inputs . get ( 0 ) . is_some_and ( |p| p. is_self ( ) ) {
1194- err. span_label ( * span, format ! ( "this function has {self_from_macro}" ) ) ;
1194+ if span. from_expansion ( ) {
1195+ err. span_label ( * fn_span, format ! ( "this function has {self_from_macro}" ) ) ;
1196+ } else {
1197+ // If the `self` is neither from a macro nor from a function self parameter,
1198+ // It is a local variable, we should suggest to rename `self` to `self_`
1199+ err. span_help ( span, format ! ( "rename `self` to `self_`" ) ) ;
1200+ }
11951201 } else {
11961202 let doesnt = if is_assoc_fn {
11971203 let ( span, sugg) = fn_kind
@@ -1204,7 +1210,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
12041210 // This avoids placing the suggestion into the visibility specifier.
12051211 let span = fn_kind
12061212 . ident ( )
1207- . map_or ( * span , |ident| span . with_lo ( ident. span . hi ( ) ) ) ;
1213+ . map_or ( * fn_span , |ident| fn_span . with_lo ( ident. span . hi ( ) ) ) ;
12081214 (
12091215 self . r
12101216 . tcx
0 commit comments