Skip to content

Commit c4a4926

Browse files
committed
More accurate suggestion for self. and Self::
Fix #115992.
1 parent 0fd7ce9 commit c4a4926

File tree

3 files changed

+57
-8
lines changed

3 files changed

+57
-8
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,21 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
224224
&& let FnKind::Fn(_, _, sig, ..) = fn_kind
225225
&& let Some(items) = self.diagnostic_metadata.current_impl_items
226226
&& let Some(item) = items.iter().find(|i| {
227-
if let AssocItemKind::Fn(..) | AssocItemKind::Const(..) = &i.kind
228-
&& i.ident.name == item_str.name
229-
// don't suggest if the item is in Fn signature arguments
230-
// issue #112590
227+
if i.ident.name == item_str.name
228+
// Don't suggest if the item is in Fn signature arguments (#112590).
231229
&& !sig.span.contains(item_span)
232230
{
233231
debug!(?item_str.name);
234-
return true
232+
return match &i.kind {
233+
AssocItemKind::Fn(fn_)
234+
if !sig.decl.has_self() && fn_.sig.decl.has_self() => {
235+
// Ensure that we only suggest `self.` if `self` is available,
236+
// you can't call `fn foo(&self)` from `fn bar()` (#115992).
237+
false
238+
}
239+
AssocItemKind::Fn(_) | AssocItemKind::Const(..) => true,
240+
_ => false
241+
}
235242
}
236243
false
237244
})

tests/ui/suggestions/assoc_fn_without_self.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,12 @@ impl S {
1717
bar(); //~ ERROR cannot find function `bar` in this scope
1818
baz(2, 3); //~ ERROR cannot find function `baz` in this scope
1919
}
20+
fn d(&self) {
21+
fn c() {
22+
foo(); //~ ERROR cannot find function `foo` in this scope
23+
}
24+
foo(); //~ ERROR cannot find function `foo` in this scope
25+
bar(); //~ ERROR cannot find function `bar` in this scope
26+
baz(2, 3); //~ ERROR cannot find function `baz` in this scope
27+
}
2028
}

tests/ui/suggestions/assoc_fn_without_self.stderr

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,40 @@ LL | Self::foo();
1212
error[E0425]: cannot find function `bar` in this scope
1313
--> $DIR/assoc_fn_without_self.rs:17:9
1414
|
15+
LL | bar();
16+
| ^^^ not found in this scope
17+
18+
error[E0425]: cannot find function `baz` in this scope
19+
--> $DIR/assoc_fn_without_self.rs:18:9
20+
|
21+
LL | baz(2, 3);
22+
| ^^^ not found in this scope
23+
|
24+
help: consider using the associated function
25+
|
26+
LL | Self::baz(2, 3);
27+
| ++++++
28+
29+
error[E0425]: cannot find function `foo` in this scope
30+
--> $DIR/assoc_fn_without_self.rs:14:13
31+
|
32+
LL | foo();
33+
| ^^^ not found in this scope
34+
35+
error[E0425]: cannot find function `foo` in this scope
36+
--> $DIR/assoc_fn_without_self.rs:24:9
37+
|
38+
LL | foo();
39+
| ^^^ not found in this scope
40+
|
41+
help: consider using the associated function
42+
|
43+
LL | Self::foo();
44+
| ++++++
45+
46+
error[E0425]: cannot find function `bar` in this scope
47+
--> $DIR/assoc_fn_without_self.rs:25:9
48+
|
1549
LL | bar();
1650
| ^^^ not found in this scope
1751
|
@@ -21,7 +55,7 @@ LL | self.bar();
2155
| +++++
2256

2357
error[E0425]: cannot find function `baz` in this scope
24-
--> $DIR/assoc_fn_without_self.rs:18:9
58+
--> $DIR/assoc_fn_without_self.rs:26:9
2559
|
2660
LL | baz(2, 3);
2761
| ^^^ not found in this scope
@@ -32,11 +66,11 @@ LL | Self::baz(2, 3);
3266
| ++++++
3367

3468
error[E0425]: cannot find function `foo` in this scope
35-
--> $DIR/assoc_fn_without_self.rs:14:13
69+
--> $DIR/assoc_fn_without_self.rs:22:13
3670
|
3771
LL | foo();
3872
| ^^^ not found in this scope
3973

40-
error: aborting due to 4 previous errors
74+
error: aborting due to 8 previous errors
4175

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

0 commit comments

Comments
 (0)