@@ -1183,15 +1183,23 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
1183
1183
_ => "`self` value is a keyword only available in methods with a `self` parameter" ,
1184
1184
} ,
1185
1185
) ;
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
+
1186
1193
let is_assoc_fn = self . self_type_is_available ( ) ;
1187
1194
let self_from_macro = "a `self` parameter, but a macro invocation can only \
1188
1195
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 {
1190
1197
// The current function has a `self` parameter, but we were unable to resolve
1191
1198
// 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.
1193
1201
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}" ) ) ;
1195
1203
} else {
1196
1204
let doesnt = if is_assoc_fn {
1197
1205
let ( span, sugg) = fn_kind
@@ -1204,7 +1212,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
1204
1212
// This avoids placing the suggestion into the visibility specifier.
1205
1213
let span = fn_kind
1206
1214
. 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 ( ) ) ) ;
1208
1216
(
1209
1217
self . r
1210
1218
. tcx
0 commit comments