Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 643258f

Browse files
committed
Tweak suggestion for this -> self
1 parent 3df25ae commit 643258f

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

src/librustc_resolve/late/diagnostics.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{PathResult, PathSource, Segment};
77

88
use rustc_ast::ast::{self, Expr, ExprKind, Item, ItemKind, NodeId, Path, Ty, TyKind};
99
use rustc_ast::util::lev_distance::find_best_match_for_name;
10+
use rustc_ast::visit::FnKind;
1011
use rustc_data_structures::fx::FxHashSet;
1112
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
1213
use rustc_hir as hir;
@@ -175,15 +176,38 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
175176
let mut err = self.r.session.struct_span_err_with_code(base_span, &base_msg, code);
176177

177178
// Emit help message for fake-self from other languages (e.g., `this` in Javascript).
178-
if ["this", "my"].contains(&&*item_str.as_str())
179-
&& self.self_value_is_available(path[0].ident.span, span)
180-
{
179+
if ["this", "my"].contains(&&*item_str.as_str()) && self.self_type_is_available(span) {
181180
err.span_suggestion_short(
182181
span,
183182
"you might have meant to use `self` here instead",
184183
"self".to_string(),
185184
Applicability::MaybeIncorrect,
186185
);
186+
if !self.self_value_is_available(path[0].ident.span, span) {
187+
if let Some((FnKind::Fn(_, _, sig, ..), fn_span)) =
188+
&self.diagnostic_metadata.current_function
189+
{
190+
if let Some(param) = sig.decl.inputs.get(0) {
191+
err.span_suggestion_verbose(
192+
param.span.shrink_to_lo(),
193+
"you are also missing a `self` receiver argument",
194+
"&self, ".to_string(),
195+
Applicability::MaybeIncorrect,
196+
);
197+
} else {
198+
err.span_suggestion_verbose(
199+
self.r
200+
.session
201+
.source_map()
202+
.span_through_char(*fn_span, '(')
203+
.shrink_to_hi(),
204+
"you are also missing a `self` receiver argument",
205+
"&self".to_string(),
206+
Applicability::MaybeIncorrect,
207+
);
208+
}
209+
}
210+
}
187211
}
188212

189213
// Emit special messages for unresolved `Self` and `self`.

src/test/ui/issues/issue-5099.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1-
trait B < A > { fn a() -> A { this.a } } //~ ERROR cannot find value `this` in this scope
1+
trait B <A> {
2+
fn a() -> A {
3+
this.a //~ ERROR cannot find value `this` in this scope
4+
}
5+
fn b(x: i32) {
6+
this.b(x); //~ ERROR cannot find value `this` in this scope
7+
}
8+
}
29

310
fn main() {}

src/test/ui/issues/issue-5099.stderr

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
error[E0425]: cannot find value `this` in this scope
2-
--> $DIR/issue-5099.rs:1:31
2+
--> $DIR/issue-5099.rs:3:9
33
|
4-
LL | trait B < A > { fn a() -> A { this.a } }
5-
| ^^^^ not found in this scope
4+
LL | this.a
5+
| ^^^^ not found in this scope
6+
|
7+
help: you might have meant to use `self` here instead
8+
|
9+
LL | self.a
10+
| ^^^^
11+
help: you are also missing a `self` receiver argument
12+
|
13+
LL | fn a(&self) -> A {
14+
| ^^^^^
15+
16+
error[E0425]: cannot find value `this` in this scope
17+
--> $DIR/issue-5099.rs:6:9
18+
|
19+
LL | this.b(x);
20+
| ^^^^ not found in this scope
21+
|
22+
help: you might have meant to use `self` here instead
23+
|
24+
LL | self.b(x);
25+
| ^^^^
26+
help: you are also missing a `self` receiver argument
27+
|
28+
LL | fn b(&self, x: i32) {
29+
| ^^^^^^
630

7-
error: aborting due to previous error
31+
error: aborting due to 2 previous errors
832

933
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)