Skip to content

Commit f8518d5

Browse files
authored
Rollup merge of rust-lang#91798 - bugadani:issue-91783, r=michaelwoerister
Avoid suggest adding `self` in visibility spec Fixes rust-lang#91783
2 parents 404c847 + f3a08fd commit f8518d5

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

Diff for: compiler/rustc_resolve/src/late/diagnostics.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,16 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
298298
.get(0)
299299
.map(|p| (p.span.shrink_to_lo(), "&self, "))
300300
.unwrap_or_else(|| {
301+
// Try to look for the "(" after the function name, if possible.
302+
// This avoids placing the suggestion into the visibility specifier.
303+
let span = fn_kind
304+
.ident()
305+
.map_or(*span, |ident| span.with_lo(ident.span.hi()));
301306
(
302307
self.r
303308
.session
304309
.source_map()
305-
.span_through_char(*span, '(')
310+
.span_through_char(span, '(')
306311
.shrink_to_hi(),
307312
"&self",
308313
)

Diff for: src/test/ui/suggestions/suggest-add-self.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
struct X(i32);
2+
3+
impl X {
4+
pub(crate) fn f() {
5+
self.0
6+
//~^ ERROR expected value, found module `self`
7+
}
8+
9+
pub fn g() {
10+
self.0
11+
//~^ ERROR expected value, found module `self`
12+
}
13+
}
14+
15+
fn main() {}

Diff for: src/test/ui/suggestions/suggest-add-self.stderr

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error[E0424]: expected value, found module `self`
2+
--> $DIR/suggest-add-self.rs:5:9
3+
|
4+
LL | pub(crate) fn f() {
5+
| - this function doesn't have a `self` parameter
6+
LL | self.0
7+
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
8+
|
9+
help: add a `self` receiver parameter to make the associated `fn` a method
10+
|
11+
LL | pub(crate) fn f(&self) {
12+
| +++++
13+
14+
error[E0424]: expected value, found module `self`
15+
--> $DIR/suggest-add-self.rs:10:9
16+
|
17+
LL | pub fn g() {
18+
| - this function doesn't have a `self` parameter
19+
LL | self.0
20+
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
21+
|
22+
help: add a `self` receiver parameter to make the associated `fn` a method
23+
|
24+
LL | pub fn g(&self) {
25+
| +++++
26+
27+
error: aborting due to 2 previous errors
28+
29+
For more information about this error, try `rustc --explain E0424`.

0 commit comments

Comments
 (0)