@@ -1183,15 +1183,23 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
11831183 _ => "`self` value is a keyword only available in methods with a `self` parameter" ,
11841184 } ,
11851185 ) ;
1186+
1187+ // using `let self` is wrong even if we're not in an associated method or if we're in a macro expansion.
1188+ // So, we should return early if we're in a pattern, see issue #143134.
1189+ if matches ! ( source, PathSource :: Pat ) {
1190+ return true ;
1191+ }
1192+
11861193 let is_assoc_fn = self . self_type_is_available ( ) ;
11871194 let self_from_macro = "a `self` parameter, but a macro invocation can only \
11881195 access identifiers it receives from parameters";
1189- if let Some ( ( fn_kind, span ) ) = & self . diag_metadata . current_function {
1196+ if let Some ( ( fn_kind, fn_span ) ) = & self . diag_metadata . current_function {
11901197 // The current function has a `self` parameter, but we were unable to resolve
11911198 // a reference to `self`. This can only happen if the `self` identifier we
1192- // are resolving came from a different hygiene context.
1199+ // are resolving came from a different hygiene context or a variable binding.
1200+ // But variable binding error is returned early above.
11931201 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}" ) ) ;
1202+ err. span_label ( * fn_span , format ! ( "this function has {self_from_macro}" ) ) ;
11951203 } else {
11961204 let doesnt = if is_assoc_fn {
11971205 let ( span, sugg) = fn_kind
@@ -1204,7 +1212,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
12041212 // This avoids placing the suggestion into the visibility specifier.
12051213 let span = fn_kind
12061214 . ident ( )
1207- . map_or ( * span , |ident| span . with_lo ( ident. span . hi ( ) ) ) ;
1215+ . map_or ( * fn_span , |ident| fn_span . with_lo ( ident. span . hi ( ) ) ) ;
12081216 (
12091217 self . r
12101218 . tcx
0 commit comments