Skip to content

Commit 7f1083e

Browse files
authored
Rollup merge of #40816 - estebank:issue-38321, r=nikomatsakis
Clarify suggetion for field used as method Instead of ```rust error: no method named `src_addr` found for type `&wire::ipv4::Repr` in the current scope --> src/wire/ipv4.rs:409:34 | 409 | packet.set_src_addr(self.src_addr()); | ^^^^^^^^ | note: did you mean to write `self.src_addr`? --> src/wire/ipv4.rs:409:34 | 409 | packet.set_src_addr(self.src_addr()); | ^^^^^^^^ ``` present ```rust error: no method named `src_addr` found for type `&wire::ipv4::Repr` in the current scope --> src/wire/ipv4.rs:409:34 | 409 | packet.set_src_addr(self.src_addr()); | ^^^^^^^^ field, not a method | = help: did you mean to write `self.src_addr` instead of `self.src_addr(...)`? ``` Fix #38321.
2 parents ea9c8b9 + 872d3bc commit 7f1083e

File tree

9 files changed

+186
-30
lines changed

9 files changed

+186
-30
lines changed

src/librustc_typeck/check/method/suggest.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,18 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
197197
let field_ty = field.ty(tcx, substs);
198198

199199
if self.is_fn_ty(&field_ty, span) {
200-
err.span_note(span,
201-
&format!("use `({0}.{1})(...)` if you \
202-
meant to call the function \
203-
stored in the `{1}` field",
204-
expr_string,
205-
item_name));
200+
err.help(&format!("use `({0}.{1})(...)` if you \
201+
meant to call the function \
202+
stored in the `{1}` field",
203+
expr_string,
204+
item_name));
206205
} else {
207-
err.span_note(span,
208-
&format!("did you mean to write `{0}.{1}`?",
209-
expr_string,
210-
item_name));
206+
err.help(&format!("did you mean to write `{0}.{1}` \
207+
instead of `{0}.{1}(...)`?",
208+
expr_string,
209+
item_name));
211210
}
211+
err.span_label(span, &"field, not a method");
212212
break;
213213
}
214214
}

src/test/compile-fail/issue-18343.rs renamed to src/test/ui/suggestions/confuse-field-and-method/issue-18343.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ struct Obj<F> where F: FnMut() -> u32 {
1414

1515
fn main() {
1616
let o = Obj { closure: || 42 };
17-
o.closure(); //~ ERROR no method named `closure` found
18-
//~^ NOTE use `(o.closure)(...)` if you meant to call the function stored in the `closure` field
17+
o.closure();
18+
//~^ ERROR no method named `closure` found
19+
//~| HELP use `(o.closure)(...)` if you meant to call the function stored in the `closure` field
20+
//~| NOTE field, not a method
1921
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: no method named `closure` found for type `Obj<[closure@$DIR/issue-18343.rs:16:28: 16:33]>` in the current scope
2+
--> $DIR/issue-18343.rs:17:7
3+
|
4+
17 | o.closure();
5+
| ^^^^^^^ field, not a method
6+
|
7+
= help: use `(o.closure)(...)` if you meant to call the function stored in the `closure` field
8+
9+
error: aborting due to previous error
10+

src/test/compile-fail/issue-2392.rs renamed to src/test/ui/suggestions/confuse-field-and-method/issue-2392.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,45 +48,58 @@ fn main() {
4848

4949
let o_closure = Obj { closure: || 42, not_closure: 42 };
5050
o_closure.closure(); //~ ERROR no method named `closure` found
51-
//~^ NOTE use `(o_closure.closure)(...)` if you meant to call the function stored
51+
//~^ HELP use `(o_closure.closure)(...)` if you meant to call the function stored
52+
//~| NOTE field, not a method
5253

53-
o_closure.not_closure(); //~ ERROR no method named `not_closure` found
54-
//~^ NOTE did you mean to write `o_closure.not_closure`?
54+
o_closure.not_closure();
55+
//~^ ERROR no method named `not_closure` found
56+
//~| NOTE field, not a method
57+
//~| HELP did you mean to write `o_closure.not_closure` instead of `o_closure.not_closure(...)`?
5558

5659
let o_func = Obj { closure: func, not_closure: 5 };
5760
o_func.closure(); //~ ERROR no method named `closure` found
58-
//~^ NOTE use `(o_func.closure)(...)` if you meant to call the function stored
61+
//~^ HELP use `(o_func.closure)(...)` if you meant to call the function stored
62+
//~| NOTE field, not a method
5963

6064
let boxed_fn = BoxedObj { boxed_closure: Box::new(func) };
6165
boxed_fn.boxed_closure();//~ ERROR no method named `boxed_closure` found
62-
//~^ NOTE use `(boxed_fn.boxed_closure)(...)` if you meant to call the function stored
66+
//~^ HELP use `(boxed_fn.boxed_closure)(...)` if you meant to call the function stored
67+
//~| NOTE field, not a method
6368

6469
let boxed_closure = BoxedObj { boxed_closure: Box::new(|| 42_u32) as Box<FnBox() -> u32> };
6570
boxed_closure.boxed_closure();//~ ERROR no method named `boxed_closure` found
66-
//~^ NOTE use `(boxed_closure.boxed_closure)(...)` if you meant to call the function stored
71+
//~^ HELP use `(boxed_closure.boxed_closure)(...)` if you meant to call the function stored
72+
//~| NOTE field, not a method
6773

6874
// test expression writing in the notes
6975

7076
let w = Wrapper { wrap: o_func };
7177
w.wrap.closure();//~ ERROR no method named `closure` found
72-
//~^ NOTE use `(w.wrap.closure)(...)` if you meant to call the function stored
78+
//~^ HELP use `(w.wrap.closure)(...)` if you meant to call the function stored
79+
//~| NOTE field, not a method
7380

74-
w.wrap.not_closure();//~ ERROR no method named `not_closure` found
75-
//~^ NOTE did you mean to write `w.wrap.not_closure`?
81+
w.wrap.not_closure();
82+
//~^ ERROR no method named `not_closure` found
83+
//~| NOTE field, not a method
84+
//~| HELP did you mean to write `w.wrap.not_closure` instead of `w.wrap.not_closure(...)`?
7685

7786
check_expression().closure();//~ ERROR no method named `closure` found
78-
//~^ NOTE use `(check_expression().closure)(...)` if you meant to call the function stored
87+
//~^ HELP use `(check_expression().closure)(...)` if you meant to call the function stored
88+
//~| NOTE field, not a method
7989
}
8090

8191
impl FuncContainerOuter {
8292
fn run(&self) {
8393
unsafe {
8494
(*self.container).f1(1); //~ ERROR no method named `f1` found
85-
//~^ NOTE use `((*self.container).f1)(...)`
95+
//~^ HELP use `((*self.container).f1)(...)`
96+
//~| NOTE field, not a method
8697
(*self.container).f2(1); //~ ERROR no method named `f2` found
87-
//~^ NOTE use `((*self.container).f2)(...)`
98+
//~^ HELP use `((*self.container).f2)(...)`
99+
//~| NOTE field, not a method
88100
(*self.container).f3(1); //~ ERROR no method named `f3` found
89-
//~^ NOTE use `((*self.container).f3)(...)`
101+
//~^ HELP use `((*self.container).f3)(...)`
102+
//~| NOTE field, not a method
90103
}
91104
}
92105
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
error: no method named `closure` found for type `Obj<[closure@$DIR/issue-2392.rs:49:36: 49:41]>` in the current scope
2+
--> $DIR/issue-2392.rs:50:15
3+
|
4+
50 | o_closure.closure(); //~ ERROR no method named `closure` found
5+
| ^^^^^^^ field, not a method
6+
|
7+
= help: use `(o_closure.closure)(...)` if you meant to call the function stored in the `closure` field
8+
9+
error: no method named `not_closure` found for type `Obj<[closure@$DIR/issue-2392.rs:49:36: 49:41]>` in the current scope
10+
--> $DIR/issue-2392.rs:54:15
11+
|
12+
54 | o_closure.not_closure();
13+
| ^^^^^^^^^^^ field, not a method
14+
|
15+
= help: did you mean to write `o_closure.not_closure` instead of `o_closure.not_closure(...)`?
16+
17+
error: no method named `closure` found for type `Obj<fn() -> u32 {func}>` in the current scope
18+
--> $DIR/issue-2392.rs:60:12
19+
|
20+
60 | o_func.closure(); //~ ERROR no method named `closure` found
21+
| ^^^^^^^ field, not a method
22+
|
23+
= help: use `(o_func.closure)(...)` if you meant to call the function stored in the `closure` field
24+
25+
error: no method named `boxed_closure` found for type `BoxedObj` in the current scope
26+
--> $DIR/issue-2392.rs:65:14
27+
|
28+
65 | boxed_fn.boxed_closure();//~ ERROR no method named `boxed_closure` found
29+
| ^^^^^^^^^^^^^ field, not a method
30+
|
31+
= help: use `(boxed_fn.boxed_closure)(...)` if you meant to call the function stored in the `boxed_closure` field
32+
33+
error: no method named `boxed_closure` found for type `BoxedObj` in the current scope
34+
--> $DIR/issue-2392.rs:70:19
35+
|
36+
70 | boxed_closure.boxed_closure();//~ ERROR no method named `boxed_closure` found
37+
| ^^^^^^^^^^^^^ field, not a method
38+
|
39+
= help: use `(boxed_closure.boxed_closure)(...)` if you meant to call the function stored in the `boxed_closure` field
40+
41+
error: no method named `closure` found for type `Obj<fn() -> u32 {func}>` in the current scope
42+
--> $DIR/issue-2392.rs:77:12
43+
|
44+
77 | w.wrap.closure();//~ ERROR no method named `closure` found
45+
| ^^^^^^^ field, not a method
46+
|
47+
= help: use `(w.wrap.closure)(...)` if you meant to call the function stored in the `closure` field
48+
49+
error: no method named `not_closure` found for type `Obj<fn() -> u32 {func}>` in the current scope
50+
--> $DIR/issue-2392.rs:81:12
51+
|
52+
81 | w.wrap.not_closure();
53+
| ^^^^^^^^^^^ field, not a method
54+
|
55+
= help: did you mean to write `w.wrap.not_closure` instead of `w.wrap.not_closure(...)`?
56+
57+
error: no method named `closure` found for type `Obj<std::boxed::Box<std::boxed::FnBox<(), Output=u32> + 'static>>` in the current scope
58+
--> $DIR/issue-2392.rs:86:24
59+
|
60+
86 | check_expression().closure();//~ ERROR no method named `closure` found
61+
| ^^^^^^^ field, not a method
62+
|
63+
= help: use `(check_expression().closure)(...)` if you meant to call the function stored in the `closure` field
64+
65+
error: no method named `f1` found for type `FuncContainer` in the current scope
66+
--> $DIR/issue-2392.rs:94:31
67+
|
68+
94 | (*self.container).f1(1); //~ ERROR no method named `f1` found
69+
| ^^ field, not a method
70+
|
71+
= help: use `((*self.container).f1)(...)` if you meant to call the function stored in the `f1` field
72+
73+
error: no method named `f2` found for type `FuncContainer` in the current scope
74+
--> $DIR/issue-2392.rs:97:31
75+
|
76+
97 | (*self.container).f2(1); //~ ERROR no method named `f2` found
77+
| ^^ field, not a method
78+
|
79+
= help: use `((*self.container).f2)(...)` if you meant to call the function stored in the `f2` field
80+
81+
error: no method named `f3` found for type `FuncContainer` in the current scope
82+
--> $DIR/issue-2392.rs:100:31
83+
|
84+
100 | (*self.container).f3(1); //~ ERROR no method named `f3` found
85+
| ^^ field, not a method
86+
|
87+
= help: use `((*self.container).f3)(...)` if you meant to call the function stored in the `f3` field
88+
89+
error: aborting due to 11 previous errors
90+

src/test/compile-fail/issue-32128.rs renamed to src/test/ui/suggestions/confuse-field-and-method/issue-32128.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ fn main() {
1919
})
2020
};
2121

22-
demo.example(1); //~ ERROR no method named `example`
23-
//~^ NOTE use `(demo.example)(...)`
22+
demo.example(1);
23+
//~^ ERROR no method named `example`
24+
//~| HELP use `(demo.example)(...)`
25+
//~| NOTE field, not a method
2426
// (demo.example)(1);
2527
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: no method named `example` found for type `Example` in the current scope
2+
--> $DIR/issue-32128.rs:22:10
3+
|
4+
22 | demo.example(1);
5+
| ^^^^^^^ field, not a method
6+
|
7+
= help: use `(demo.example)(...)` if you meant to call the function stored in the `example` field
8+
9+
error: aborting due to previous error
10+

src/test/compile-fail/issue-33784.rs renamed to src/test/ui/suggestions/confuse-field-and-method/issue-33784.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ fn main() {
3535
let o = Obj { fn_ptr: empty, closure: || 42 };
3636
let p = &o;
3737
p.closure(); //~ ERROR no method named `closure` found
38-
//~^ NOTE use `(p.closure)(...)` if you meant to call the function stored in the `closure` field
38+
//~^ HELP use `(p.closure)(...)` if you meant to call the function stored in the `closure` field
39+
//~| NOTE `closure` is a field storing a function, not a method
3940
let q = &p;
4041
q.fn_ptr(); //~ ERROR no method named `fn_ptr` found
41-
//~^ NOTE use `(q.fn_ptr)(...)` if you meant to call the function stored in the `fn_ptr` field
42+
//~^ HELP use `(q.fn_ptr)(...)` if you meant to call the function stored in the `fn_ptr` field
43+
//~| NOTE `fn_ptr` is a field storing a function, not a method
4244
let r = D(C { c_fn_ptr: empty });
4345
let s = &r;
4446
s.c_fn_ptr(); //~ ERROR no method named `c_fn_ptr` found
45-
//~^ NOTE use `(s.c_fn_ptr)(...)` if you meant to call the function stored in the `c_fn_ptr`
47+
//~^ HELP use `(s.c_fn_ptr)(...)` if you meant to call the function stored in the `c_fn_ptr`
48+
//~| NOTE `c_fn_ptr` is a field storing a function, not a method
4649
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: no method named `closure` found for type `&Obj<[closure@$DIR/issue-33784.rs:35:43: 35:48]>` in the current scope
2+
--> $DIR/issue-33784.rs:37:7
3+
|
4+
37 | p.closure(); //~ ERROR no method named `closure` found
5+
| ^^^^^^^ field, not a method
6+
|
7+
= help: use `(p.closure)(...)` if you meant to call the function stored in the `closure` field
8+
9+
error: no method named `fn_ptr` found for type `&&Obj<[closure@$DIR/issue-33784.rs:35:43: 35:48]>` in the current scope
10+
--> $DIR/issue-33784.rs:41:7
11+
|
12+
41 | q.fn_ptr(); //~ ERROR no method named `fn_ptr` found
13+
| ^^^^^^ field, not a method
14+
|
15+
= help: use `(q.fn_ptr)(...)` if you meant to call the function stored in the `fn_ptr` field
16+
17+
error: no method named `c_fn_ptr` found for type `&D` in the current scope
18+
--> $DIR/issue-33784.rs:46:7
19+
|
20+
46 | s.c_fn_ptr(); //~ ERROR no method named `c_fn_ptr` found
21+
| ^^^^^^^^ field, not a method
22+
|
23+
= help: use `(s.c_fn_ptr)(...)` if you meant to call the function stored in the `c_fn_ptr` field
24+
25+
error: aborting due to 3 previous errors
26+

0 commit comments

Comments
 (0)