Skip to content

Commit 236b392

Browse files
committed
Return early when self resolve failure because of let self = ...
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
1 parent 000f038 commit 236b392

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/ui/error-codes/E0424.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ LL | fn qux(&self) {
4040
error[E0424]: expected unit struct, unit variant or constant, found module `self`
4141
--> $DIR/E0424.rs:20:9
4242
|
43-
LL | fn main () {
44-
| ---- this function can't have a `self` parameter
4543
LL | let self = "self";
4644
| ^^^^ `self` value is a keyword and may not be bound to variables or shadowed
4745

tests/ui/resolve/false-self-in-macro-issue-143134.stderr

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
error[E0424]: expected unit struct, unit variant or constant, found local variable `self`
22
--> $DIR/false-self-in-macro-issue-143134.rs:6:13
33
|
4-
LL | / fn f(self) {
5-
LL | | let self = ();
6-
| | ^^^^ `self` value is a keyword and may not be bound to variables or shadowed
7-
LL | | }
8-
| |_____- this function has a `self` parameter, but a macro invocation can only access identifiers it receives from parameters
4+
LL | let self = ();
5+
| ^^^^ `self` value is a keyword and may not be bound to variables or shadowed
96

107
error: aborting due to 1 previous error
118

0 commit comments

Comments
 (0)