Skip to content

Commit

Permalink
Make E0599's label more clear for field which is used like a method.
Browse files Browse the repository at this point in the history
fixes #127178
  • Loading branch information
surechen committed Jul 1, 2024
1 parent ad12a2a commit 28cd5c3
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
8 changes: 7 additions & 1 deletion compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2288,7 +2288,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}

let field_kind = if is_accessible { "field" } else { "private field" };
err.span_label(item_name.span, format!("{field_kind}, not a method"));
err.span_label(
item_name.span,
format!(
"`{item_name}` is a {field_kind} \
in the current scope, but is used like a method"
),
);
return true;
}
false
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/confuse-field-and-method/issue-18343.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | struct Obj<F> where F: FnMut() -> u32 {
| ------------- method `closure` not found for this struct
...
LL | o.closure();
| ^^^^^^^ field, not a method
| ^^^^^^^ `closure` is a field in the current scope, but is used like a method
|
help: to call the function stored in `closure`, surround the field access with parentheses
|
Expand Down
22 changes: 11 additions & 11 deletions tests/ui/confuse-field-and-method/issue-2392.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | struct Obj<F> where F: FnOnce() -> u32 {
| ------------- method `closure` not found for this struct
...
LL | o_closure.closure();
| ^^^^^^^ field, not a method
| ^^^^^^^ `closure` is a field in the current scope, but is used like a method
|
help: to call the function stored in `closure`, surround the field access with parentheses
|
Expand All @@ -21,7 +21,7 @@ LL | struct Obj<F> where F: FnOnce() -> u32 {
LL | o_closure.not_closure();
| ^^^^^^^^^^^-- help: remove the arguments
| |
| field, not a method
| `not_closure` is a field in the current scope, but is used like a method

error[E0599]: no method named `closure` found for struct `Obj` in the current scope
--> $DIR/issue-2392.rs:42:12
Expand All @@ -30,7 +30,7 @@ LL | struct Obj<F> where F: FnOnce() -> u32 {
| ------------- method `closure` not found for this struct
...
LL | o_func.closure();
| ^^^^^^^ field, not a method
| ^^^^^^^ `closure` is a field in the current scope, but is used like a method
|
help: to call the function stored in `closure`, surround the field access with parentheses
|
Expand All @@ -44,7 +44,7 @@ LL | struct BoxedObj {
| --------------- method `boxed_closure` not found for this struct
...
LL | boxed_fn.boxed_closure();
| ^^^^^^^^^^^^^ field, not a method
| ^^^^^^^^^^^^^ `boxed_closure` is a field in the current scope, but is used like a method
|
help: to call the function stored in `boxed_closure`, surround the field access with parentheses
|
Expand All @@ -58,7 +58,7 @@ LL | struct BoxedObj {
| --------------- method `boxed_closure` not found for this struct
...
LL | boxed_closure.boxed_closure();
| ^^^^^^^^^^^^^ field, not a method
| ^^^^^^^^^^^^^ `boxed_closure` is a field in the current scope, but is used like a method
|
help: to call the function stored in `boxed_closure`, surround the field access with parentheses
|
Expand All @@ -72,7 +72,7 @@ LL | struct Obj<F> where F: FnOnce() -> u32 {
| ------------- method `closure` not found for this struct
...
LL | w.wrap.closure();
| ^^^^^^^ field, not a method
| ^^^^^^^ `closure` is a field in the current scope, but is used like a method
|
help: to call the function stored in `closure`, surround the field access with parentheses
|
Expand All @@ -88,7 +88,7 @@ LL | struct Obj<F> where F: FnOnce() -> u32 {
LL | w.wrap.not_closure();
| ^^^^^^^^^^^-- help: remove the arguments
| |
| field, not a method
| `not_closure` is a field in the current scope, but is used like a method

error[E0599]: no method named `closure` found for struct `Obj` in the current scope
--> $DIR/issue-2392.rs:58:24
Expand All @@ -97,7 +97,7 @@ LL | struct Obj<F> where F: FnOnce() -> u32 {
| ------------- method `closure` not found for this struct
...
LL | check_expression().closure();
| ^^^^^^^ field, not a method
| ^^^^^^^ `closure` is a field in the current scope, but is used like a method
|
help: to call the function stored in `closure`, surround the field access with parentheses
|
Expand All @@ -111,7 +111,7 @@ LL | struct FuncContainer {
| -------------------- method `f1` not found for this struct
...
LL | (*self.container).f1(1);
| ^^ field, not a method
| ^^ `f1` is a field in the current scope, but is used like a method
|
help: to call the function stored in `f1`, surround the field access with parentheses
|
Expand All @@ -125,7 +125,7 @@ LL | struct FuncContainer {
| -------------------- method `f2` not found for this struct
...
LL | (*self.container).f2(1);
| ^^ field, not a method
| ^^ `f2` is a field in the current scope, but is used like a method
|
help: to call the function stored in `f2`, surround the field access with parentheses
|
Expand All @@ -139,7 +139,7 @@ LL | struct FuncContainer {
| -------------------- method `f3` not found for this struct
...
LL | (*self.container).f3(1);
| ^^ field, not a method
| ^^ `f3` is a field in the current scope, but is used like a method
|
help: to call the function stored in `f3`, surround the field access with parentheses
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/confuse-field-and-method/issue-32128.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | struct Example {
| -------------- method `example` not found for this struct
...
LL | demo.example(1);
| ^^^^^^^ field, not a method
| ^^^^^^^ `example` is a field in the current scope, but is used like a method
|
help: to call the function stored in `example`, surround the field access with parentheses
|
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/confuse-field-and-method/issue-33784.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0599]: no method named `closure` found for reference `&Obj<{closure@$DIR/
--> $DIR/issue-33784.rs:27:7
|
LL | p.closure();
| ^^^^^^^ field, not a method
| ^^^^^^^ `closure` is a field in the current scope, but is used like a method
|
help: to call the function stored in `closure`, surround the field access with parentheses
|
Expand All @@ -17,7 +17,7 @@ error[E0599]: no method named `fn_ptr` found for reference `&&Obj<{closure@$DIR/
--> $DIR/issue-33784.rs:29:7
|
LL | q.fn_ptr();
| ^^^^^^ field, not a method
| ^^^^^^ `fn_ptr` is a field in the current scope, but is used like a method
|
help: to call the function stored in `fn_ptr`, surround the field access with parentheses
|
Expand All @@ -28,7 +28,7 @@ error[E0599]: no method named `c_fn_ptr` found for reference `&D` in the current
--> $DIR/issue-33784.rs:32:7
|
LL | s.c_fn_ptr();
| ^^^^^^^^ field, not a method
| ^^^^^^^^ `c_fn_ptr` is a field in the current scope, but is used like a method
|
help: to call the function stored in `c_fn_ptr`, surround the field access with parentheses
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/confuse-field-and-method/private-field.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ LL | pub struct Dog {
| -------------- method `dog_age` not found for this struct
...
LL | let dog_age = dog.dog_age();
| ^^^^^^^ private field, not a method
| ^^^^^^^ `dog_age` is a private field in the current scope, but is used like a method

error: aborting due to 1 previous error

Expand Down

0 comments on commit 28cd5c3

Please sign in to comment.