Skip to content

Commit 6341a8b

Browse files
committed
Clarify suggetion for field used as method
Instead of ``` 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 ``` 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()); | ^^^^^^^^ `src_addr` is a field, not a method | = help: did you mean to write `self.src_addr` instead of `self.src_addr(...)`? ```
1 parent c62e532 commit 6341a8b

File tree

5 files changed

+56
-30
lines changed

5 files changed

+56
-30
lines changed

src/librustc_typeck/check/method/suggest.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,23 @@ 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));
205+
err.span_label(span,
206+
&format!("`{}` is a field storing a \
207+
function, not a method",
208+
item_name));
206209
} else {
207-
err.span_note(span,
208-
&format!("did you mean to write `{0}.{1}`?",
209-
expr_string,
210-
item_name));
210+
err.help(&format!("did you mean to write `{0}.{1}` \
211+
instead of `{0}.{1}(...)`?",
212+
expr_string,
213+
item_name));
214+
err.span_label(span,
215+
&format!("`{}` is a field, not a method",
216+
item_name));
211217
}
212218
break;
213219
}

src/test/compile-fail/issue-18343.rs

+4-2
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 `closure` is a field storing a function, not a method
1921
}

src/test/compile-fail/issue-2392.rs

+26-13
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 `closure` is a field storing a function, 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 `not_closure` is a 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 `closure` is a field storing a function, 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 `boxed_closure` is a field storing a function, 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 `boxed_closure` is a field storing a function, 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 `closure` is a field storing a function, 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 `not_closure` is a 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 `closure` is a field storing a function, 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 `f1` is a field storing a function, 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 `f2` is a field storing a function, 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 `f3` is a field storing a function, not a method
90103
}
91104
}
92105
}

src/test/compile-fail/issue-32128.rs

+4-2
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 `example` is a field storing a function, not a method
2426
// (demo.example)(1);
2527
}

src/test/compile-fail/issue-33784.rs

+6-3
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
}

0 commit comments

Comments
 (0)