Skip to content

Commit 2d1065b

Browse files
committed
Auto merge of #54694 - csmoe:self_this, r=estebank
Suggest to use self for fake-self from other languages Closes #54019 r? @estebank
2 parents 7cbcdae + 4470b1c commit 2d1065b

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

src/librustc_resolve/lib.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -2968,13 +2968,13 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
29682968
// Make the base error.
29692969
let expected = source.descr_expected();
29702970
let path_str = names_to_string(path);
2971+
let item_str = path[path.len() - 1];
29712972
let code = source.error_code(def.is_some());
29722973
let (base_msg, fallback_label, base_span) = if let Some(def) = def {
29732974
(format!("expected {}, found {} `{}`", expected, def.kind_name(), path_str),
29742975
format!("not a {}", expected),
29752976
span)
29762977
} else {
2977-
let item_str = path[path.len() - 1];
29782978
let item_span = path[path.len() - 1].span;
29792979
let (mod_prefix, mod_str) = if path.len() == 1 {
29802980
(String::new(), "this scope".to_string())
@@ -2997,6 +2997,20 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
29972997
let code = DiagnosticId::Error(code.into());
29982998
let mut err = this.session.struct_span_err_with_code(base_span, &base_msg, code);
29992999

3000+
// Emit help message for fake-self from other languages like `this`(javascript)
3001+
let fake_self: Vec<Ident> = ["this", "my"].iter().map(
3002+
|s| Ident::from_str(*s)
3003+
).collect();
3004+
if fake_self.contains(&item_str)
3005+
&& this.self_value_is_available(path[0].span, span) {
3006+
err.span_suggestion_with_applicability(
3007+
span,
3008+
"did you mean",
3009+
"self".to_string(),
3010+
Applicability::MaybeIncorrect,
3011+
);
3012+
}
3013+
30003014
// Emit special messages for unresolved `Self` and `self`.
30013015
if is_self_type(path, ns) {
30023016
__diagnostic_used!(E0411);

src/test/ui/self/suggest-self.rs

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Foo {
12+
x: i32,
13+
}
14+
15+
impl Foo {
16+
fn this1(&self) -> i32 {
17+
let this = self;
18+
let a = 1;
19+
this.x
20+
}
21+
22+
fn this2(&self) -> i32 {
23+
let a = Foo {
24+
x: 2
25+
};
26+
let this = a;
27+
this.x
28+
}
29+
30+
fn foo(&self) -> i32 {
31+
this.x
32+
//~^ ERROR cannot find value `this` in this scope
33+
}
34+
35+
fn bar(&self) -> i32 {
36+
this.foo()
37+
//~^ ERROR cannot find value `this` in this scope
38+
}
39+
40+
fn baz(&self) -> i32 {
41+
my.bar()
42+
//~^ ERROR cannot find value `my` in this scope
43+
}
44+
}
45+
46+
fn main() {
47+
let this = vec![1, 2, 3];
48+
let my = vec![1, 2, 3];
49+
let len = this.len();
50+
let len = my.len();
51+
}
52+

src/test/ui/self/suggest-self.stderr

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error[E0425]: cannot find value `this` in this scope
2+
--> $DIR/suggest-self.rs:31:9
3+
|
4+
LL | this.x
5+
| ^^^^
6+
| |
7+
| not found in this scope
8+
| help: did you mean: `self`
9+
10+
error[E0425]: cannot find value `this` in this scope
11+
--> $DIR/suggest-self.rs:36:9
12+
|
13+
LL | this.foo()
14+
| ^^^^
15+
| |
16+
| not found in this scope
17+
| help: did you mean: `self`
18+
19+
error[E0425]: cannot find value `my` in this scope
20+
--> $DIR/suggest-self.rs:41:9
21+
|
22+
LL | my.bar()
23+
| ^^
24+
| |
25+
| not found in this scope
26+
| help: did you mean: `self`
27+
28+
error: aborting due to 3 previous errors
29+
30+
For more information about this error, try `rustc --explain E0425`.

0 commit comments

Comments
 (0)