Skip to content

Commit 28cd5c3

Browse files
committed
Make E0599's label more clear for field which is used like a method.
fixes #127178
1 parent ad12a2a commit 28cd5c3

File tree

6 files changed

+24
-18
lines changed

6 files changed

+24
-18
lines changed

compiler/rustc_hir_typeck/src/method/suggest.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2288,7 +2288,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22882288
}
22892289

22902290
let field_kind = if is_accessible { "field" } else { "private field" };
2291-
err.span_label(item_name.span, format!("{field_kind}, not a method"));
2291+
err.span_label(
2292+
item_name.span,
2293+
format!(
2294+
"`{item_name}` is a {field_kind} \
2295+
in the current scope, but is used like a method"
2296+
),
2297+
);
22922298
return true;
22932299
}
22942300
false

tests/ui/confuse-field-and-method/issue-18343.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | struct Obj<F> where F: FnMut() -> u32 {
55
| ------------- method `closure` not found for this struct
66
...
77
LL | o.closure();
8-
| ^^^^^^^ field, not a method
8+
| ^^^^^^^ `closure` is a field in the current scope, but is used like a method
99
|
1010
help: to call the function stored in `closure`, surround the field access with parentheses
1111
|

tests/ui/confuse-field-and-method/issue-2392.stderr

+11-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | struct Obj<F> where F: FnOnce() -> u32 {
55
| ------------- method `closure` not found for this struct
66
...
77
LL | o_closure.closure();
8-
| ^^^^^^^ field, not a method
8+
| ^^^^^^^ `closure` is a field in the current scope, but is used like a method
99
|
1010
help: to call the function stored in `closure`, surround the field access with parentheses
1111
|
@@ -21,7 +21,7 @@ LL | struct Obj<F> where F: FnOnce() -> u32 {
2121
LL | o_closure.not_closure();
2222
| ^^^^^^^^^^^-- help: remove the arguments
2323
| |
24-
| field, not a method
24+
| `not_closure` is a field in the current scope, but is used like a method
2525

2626
error[E0599]: no method named `closure` found for struct `Obj` in the current scope
2727
--> $DIR/issue-2392.rs:42:12
@@ -30,7 +30,7 @@ LL | struct Obj<F> where F: FnOnce() -> u32 {
3030
| ------------- method `closure` not found for this struct
3131
...
3232
LL | o_func.closure();
33-
| ^^^^^^^ field, not a method
33+
| ^^^^^^^ `closure` is a field in the current scope, but is used like a method
3434
|
3535
help: to call the function stored in `closure`, surround the field access with parentheses
3636
|
@@ -44,7 +44,7 @@ LL | struct BoxedObj {
4444
| --------------- method `boxed_closure` not found for this struct
4545
...
4646
LL | boxed_fn.boxed_closure();
47-
| ^^^^^^^^^^^^^ field, not a method
47+
| ^^^^^^^^^^^^^ `boxed_closure` is a field in the current scope, but is used like a method
4848
|
4949
help: to call the function stored in `boxed_closure`, surround the field access with parentheses
5050
|
@@ -58,7 +58,7 @@ LL | struct BoxedObj {
5858
| --------------- method `boxed_closure` not found for this struct
5959
...
6060
LL | boxed_closure.boxed_closure();
61-
| ^^^^^^^^^^^^^ field, not a method
61+
| ^^^^^^^^^^^^^ `boxed_closure` is a field in the current scope, but is used like a method
6262
|
6363
help: to call the function stored in `boxed_closure`, surround the field access with parentheses
6464
|
@@ -72,7 +72,7 @@ LL | struct Obj<F> where F: FnOnce() -> u32 {
7272
| ------------- method `closure` not found for this struct
7373
...
7474
LL | w.wrap.closure();
75-
| ^^^^^^^ field, not a method
75+
| ^^^^^^^ `closure` is a field in the current scope, but is used like a method
7676
|
7777
help: to call the function stored in `closure`, surround the field access with parentheses
7878
|
@@ -88,7 +88,7 @@ LL | struct Obj<F> where F: FnOnce() -> u32 {
8888
LL | w.wrap.not_closure();
8989
| ^^^^^^^^^^^-- help: remove the arguments
9090
| |
91-
| field, not a method
91+
| `not_closure` is a field in the current scope, but is used like a method
9292

9393
error[E0599]: no method named `closure` found for struct `Obj` in the current scope
9494
--> $DIR/issue-2392.rs:58:24
@@ -97,7 +97,7 @@ LL | struct Obj<F> where F: FnOnce() -> u32 {
9797
| ------------- method `closure` not found for this struct
9898
...
9999
LL | check_expression().closure();
100-
| ^^^^^^^ field, not a method
100+
| ^^^^^^^ `closure` is a field in the current scope, but is used like a method
101101
|
102102
help: to call the function stored in `closure`, surround the field access with parentheses
103103
|
@@ -111,7 +111,7 @@ LL | struct FuncContainer {
111111
| -------------------- method `f1` not found for this struct
112112
...
113113
LL | (*self.container).f1(1);
114-
| ^^ field, not a method
114+
| ^^ `f1` is a field in the current scope, but is used like a method
115115
|
116116
help: to call the function stored in `f1`, surround the field access with parentheses
117117
|
@@ -125,7 +125,7 @@ LL | struct FuncContainer {
125125
| -------------------- method `f2` not found for this struct
126126
...
127127
LL | (*self.container).f2(1);
128-
| ^^ field, not a method
128+
| ^^ `f2` is a field in the current scope, but is used like a method
129129
|
130130
help: to call the function stored in `f2`, surround the field access with parentheses
131131
|
@@ -139,7 +139,7 @@ LL | struct FuncContainer {
139139
| -------------------- method `f3` not found for this struct
140140
...
141141
LL | (*self.container).f3(1);
142-
| ^^ field, not a method
142+
| ^^ `f3` is a field in the current scope, but is used like a method
143143
|
144144
help: to call the function stored in `f3`, surround the field access with parentheses
145145
|

tests/ui/confuse-field-and-method/issue-32128.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | struct Example {
55
| -------------- method `example` not found for this struct
66
...
77
LL | demo.example(1);
8-
| ^^^^^^^ field, not a method
8+
| ^^^^^^^ `example` is a field in the current scope, but is used like a method
99
|
1010
help: to call the function stored in `example`, surround the field access with parentheses
1111
|

tests/ui/confuse-field-and-method/issue-33784.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0599]: no method named `closure` found for reference `&Obj<{closure@$DIR/
22
--> $DIR/issue-33784.rs:27:7
33
|
44
LL | p.closure();
5-
| ^^^^^^^ field, not a method
5+
| ^^^^^^^ `closure` is a field in the current scope, but is used like a method
66
|
77
help: to call the function stored in `closure`, surround the field access with parentheses
88
|
@@ -17,7 +17,7 @@ error[E0599]: no method named `fn_ptr` found for reference `&&Obj<{closure@$DIR/
1717
--> $DIR/issue-33784.rs:29:7
1818
|
1919
LL | q.fn_ptr();
20-
| ^^^^^^ field, not a method
20+
| ^^^^^^ `fn_ptr` is a field in the current scope, but is used like a method
2121
|
2222
help: to call the function stored in `fn_ptr`, surround the field access with parentheses
2323
|
@@ -28,7 +28,7 @@ error[E0599]: no method named `c_fn_ptr` found for reference `&D` in the current
2828
--> $DIR/issue-33784.rs:32:7
2929
|
3030
LL | s.c_fn_ptr();
31-
| ^^^^^^^^ field, not a method
31+
| ^^^^^^^^ `c_fn_ptr` is a field in the current scope, but is used like a method
3232
|
3333
help: to call the function stored in `c_fn_ptr`, surround the field access with parentheses
3434
|

tests/ui/confuse-field-and-method/private-field.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | pub struct Dog {
55
| -------------- method `dog_age` not found for this struct
66
...
77
LL | let dog_age = dog.dog_age();
8-
| ^^^^^^^ private field, not a method
8+
| ^^^^^^^ `dog_age` is a private field in the current scope, but is used like a method
99

1010
error: aborting due to 1 previous error
1111

0 commit comments

Comments
 (0)