From 6341a8b0e5155abf4bf338d3df7f6d2971f83adb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 24 Mar 2017 23:40:24 -0700 Subject: [PATCH 1/2] 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(...)`? ``` --- src/librustc_typeck/check/method/suggest.rs | 26 ++++++++------ src/test/compile-fail/issue-18343.rs | 6 ++-- src/test/compile-fail/issue-2392.rs | 39 ++++++++++++++------- src/test/compile-fail/issue-32128.rs | 6 ++-- src/test/compile-fail/issue-33784.rs | 9 +++-- 5 files changed, 56 insertions(+), 30 deletions(-) diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index 6ce50d91124d4..b458c1af71d06 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -197,17 +197,23 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { let field_ty = field.ty(tcx, substs); if self.is_fn_ty(&field_ty, span) { - err.span_note(span, - &format!("use `({0}.{1})(...)` if you \ - meant to call the function \ - stored in the `{1}` field", - expr_string, - item_name)); + err.help(&format!("use `({0}.{1})(...)` if you \ + meant to call the function \ + stored in the `{1}` field", + expr_string, + item_name)); + err.span_label(span, + &format!("`{}` is a field storing a \ + function, not a method", + item_name)); } else { - err.span_note(span, - &format!("did you mean to write `{0}.{1}`?", - expr_string, - item_name)); + err.help(&format!("did you mean to write `{0}.{1}` \ + instead of `{0}.{1}(...)`?", + expr_string, + item_name)); + err.span_label(span, + &format!("`{}` is a field, not a method", + item_name)); } break; } diff --git a/src/test/compile-fail/issue-18343.rs b/src/test/compile-fail/issue-18343.rs index 4601db9dba0fc..a095bb4812610 100644 --- a/src/test/compile-fail/issue-18343.rs +++ b/src/test/compile-fail/issue-18343.rs @@ -14,6 +14,8 @@ struct Obj where F: FnMut() -> u32 { fn main() { let o = Obj { closure: || 42 }; - o.closure(); //~ ERROR no method named `closure` found - //~^ NOTE use `(o.closure)(...)` if you meant to call the function stored in the `closure` field + o.closure(); + //~^ ERROR no method named `closure` found + //~| HELP use `(o.closure)(...)` if you meant to call the function stored in the `closure` field + //~| NOTE `closure` is a field storing a function, not a method } diff --git a/src/test/compile-fail/issue-2392.rs b/src/test/compile-fail/issue-2392.rs index 805725dd749f5..12389f885364d 100644 --- a/src/test/compile-fail/issue-2392.rs +++ b/src/test/compile-fail/issue-2392.rs @@ -48,45 +48,58 @@ fn main() { let o_closure = Obj { closure: || 42, not_closure: 42 }; o_closure.closure(); //~ ERROR no method named `closure` found - //~^ NOTE use `(o_closure.closure)(...)` if you meant to call the function stored + //~^ HELP use `(o_closure.closure)(...)` if you meant to call the function stored + //~| NOTE `closure` is a field storing a function, not a method - o_closure.not_closure(); //~ ERROR no method named `not_closure` found - //~^ NOTE did you mean to write `o_closure.not_closure`? + o_closure.not_closure(); + //~^ ERROR no method named `not_closure` found + //~| NOTE `not_closure` is a field, not a method + //~| HELP did you mean to write `o_closure.not_closure` instead of `o_closure.not_closure(...)`? let o_func = Obj { closure: func, not_closure: 5 }; o_func.closure(); //~ ERROR no method named `closure` found - //~^ NOTE use `(o_func.closure)(...)` if you meant to call the function stored + //~^ HELP use `(o_func.closure)(...)` if you meant to call the function stored + //~| NOTE `closure` is a field storing a function, not a method let boxed_fn = BoxedObj { boxed_closure: Box::new(func) }; boxed_fn.boxed_closure();//~ ERROR no method named `boxed_closure` found - //~^ NOTE use `(boxed_fn.boxed_closure)(...)` if you meant to call the function stored + //~^ HELP use `(boxed_fn.boxed_closure)(...)` if you meant to call the function stored + //~| NOTE `boxed_closure` is a field storing a function, not a method let boxed_closure = BoxedObj { boxed_closure: Box::new(|| 42_u32) as Box u32> }; boxed_closure.boxed_closure();//~ ERROR no method named `boxed_closure` found - //~^ NOTE use `(boxed_closure.boxed_closure)(...)` if you meant to call the function stored + //~^ HELP use `(boxed_closure.boxed_closure)(...)` if you meant to call the function stored + //~| NOTE `boxed_closure` is a field storing a function, not a method // test expression writing in the notes let w = Wrapper { wrap: o_func }; w.wrap.closure();//~ ERROR no method named `closure` found - //~^ NOTE use `(w.wrap.closure)(...)` if you meant to call the function stored + //~^ HELP use `(w.wrap.closure)(...)` if you meant to call the function stored + //~| NOTE `closure` is a field storing a function, not a method - w.wrap.not_closure();//~ ERROR no method named `not_closure` found - //~^ NOTE did you mean to write `w.wrap.not_closure`? + w.wrap.not_closure(); + //~^ ERROR no method named `not_closure` found + //~| NOTE `not_closure` is a field, not a method + //~| HELP did you mean to write `w.wrap.not_closure` instead of `w.wrap.not_closure(...)`? check_expression().closure();//~ ERROR no method named `closure` found - //~^ NOTE use `(check_expression().closure)(...)` if you meant to call the function stored + //~^ HELP use `(check_expression().closure)(...)` if you meant to call the function stored + //~| NOTE `closure` is a field storing a function, not a method } impl FuncContainerOuter { fn run(&self) { unsafe { (*self.container).f1(1); //~ ERROR no method named `f1` found - //~^ NOTE use `((*self.container).f1)(...)` + //~^ HELP use `((*self.container).f1)(...)` + //~| NOTE `f1` is a field storing a function, not a method (*self.container).f2(1); //~ ERROR no method named `f2` found - //~^ NOTE use `((*self.container).f2)(...)` + //~^ HELP use `((*self.container).f2)(...)` + //~| NOTE `f2` is a field storing a function, not a method (*self.container).f3(1); //~ ERROR no method named `f3` found - //~^ NOTE use `((*self.container).f3)(...)` + //~^ HELP use `((*self.container).f3)(...)` + //~| NOTE `f3` is a field storing a function, not a method } } } diff --git a/src/test/compile-fail/issue-32128.rs b/src/test/compile-fail/issue-32128.rs index fe7e66a2116eb..74fbb33296e4b 100644 --- a/src/test/compile-fail/issue-32128.rs +++ b/src/test/compile-fail/issue-32128.rs @@ -19,7 +19,9 @@ fn main() { }) }; - demo.example(1); //~ ERROR no method named `example` - //~^ NOTE use `(demo.example)(...)` + demo.example(1); + //~^ ERROR no method named `example` + //~| HELP use `(demo.example)(...)` + //~| NOTE `example` is a field storing a function, not a method // (demo.example)(1); } diff --git a/src/test/compile-fail/issue-33784.rs b/src/test/compile-fail/issue-33784.rs index 4229be29473db..03c84fc57befe 100644 --- a/src/test/compile-fail/issue-33784.rs +++ b/src/test/compile-fail/issue-33784.rs @@ -35,12 +35,15 @@ fn main() { let o = Obj { fn_ptr: empty, closure: || 42 }; let p = &o; p.closure(); //~ ERROR no method named `closure` found - //~^ NOTE use `(p.closure)(...)` if you meant to call the function stored in the `closure` field + //~^ HELP use `(p.closure)(...)` if you meant to call the function stored in the `closure` field + //~| NOTE `closure` is a field storing a function, not a method let q = &p; q.fn_ptr(); //~ ERROR no method named `fn_ptr` found - //~^ NOTE use `(q.fn_ptr)(...)` if you meant to call the function stored in the `fn_ptr` field + //~^ HELP use `(q.fn_ptr)(...)` if you meant to call the function stored in the `fn_ptr` field + //~| NOTE `fn_ptr` is a field storing a function, not a method let r = D(C { c_fn_ptr: empty }); let s = &r; s.c_fn_ptr(); //~ ERROR no method named `c_fn_ptr` found - //~^ NOTE use `(s.c_fn_ptr)(...)` if you meant to call the function stored in the `c_fn_ptr` + //~^ HELP use `(s.c_fn_ptr)(...)` if you meant to call the function stored in the `c_fn_ptr` + //~| NOTE `c_fn_ptr` is a field storing a function, not a method } From 872d3bc0d713a85f2050f99d0cf33dd060318411 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 27 Mar 2017 14:54:15 -0700 Subject: [PATCH 2/2] Simplify labels and move tests to ui --- src/librustc_typeck/check/method/suggest.rs | 8 +- .../confuse-field-and-method}/issue-18343.rs | 2 +- .../issue-18343.stderr | 10 +++ .../confuse-field-and-method}/issue-2392.rs | 22 ++--- .../issue-2392.stderr | 90 +++++++++++++++++++ .../confuse-field-and-method}/issue-32128.rs | 2 +- .../issue-32128.stderr | 10 +++ .../confuse-field-and-method}/issue-33784.rs | 0 .../issue-33784.stderr | 26 ++++++ 9 files changed, 150 insertions(+), 20 deletions(-) rename src/test/{compile-fail => ui/suggestions/confuse-field-and-method}/issue-18343.rs (91%) create mode 100644 src/test/ui/suggestions/confuse-field-and-method/issue-18343.stderr rename src/test/{compile-fail => ui/suggestions/confuse-field-and-method}/issue-2392.rs (81%) create mode 100644 src/test/ui/suggestions/confuse-field-and-method/issue-2392.stderr rename src/test/{compile-fail => ui/suggestions/confuse-field-and-method}/issue-32128.rs (91%) create mode 100644 src/test/ui/suggestions/confuse-field-and-method/issue-32128.stderr rename src/test/{compile-fail => ui/suggestions/confuse-field-and-method}/issue-33784.rs (100%) create mode 100644 src/test/ui/suggestions/confuse-field-and-method/issue-33784.stderr diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index b458c1af71d06..67ee7ef586530 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -202,19 +202,13 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { stored in the `{1}` field", expr_string, item_name)); - err.span_label(span, - &format!("`{}` is a field storing a \ - function, not a method", - item_name)); } else { err.help(&format!("did you mean to write `{0}.{1}` \ instead of `{0}.{1}(...)`?", expr_string, item_name)); - err.span_label(span, - &format!("`{}` is a field, not a method", - item_name)); } + err.span_label(span, &"field, not a method"); break; } } diff --git a/src/test/compile-fail/issue-18343.rs b/src/test/ui/suggestions/confuse-field-and-method/issue-18343.rs similarity index 91% rename from src/test/compile-fail/issue-18343.rs rename to src/test/ui/suggestions/confuse-field-and-method/issue-18343.rs index a095bb4812610..fc3c58e5223a3 100644 --- a/src/test/compile-fail/issue-18343.rs +++ b/src/test/ui/suggestions/confuse-field-and-method/issue-18343.rs @@ -17,5 +17,5 @@ fn main() { o.closure(); //~^ ERROR no method named `closure` found //~| HELP use `(o.closure)(...)` if you meant to call the function stored in the `closure` field - //~| NOTE `closure` is a field storing a function, not a method + //~| NOTE field, not a method } diff --git a/src/test/ui/suggestions/confuse-field-and-method/issue-18343.stderr b/src/test/ui/suggestions/confuse-field-and-method/issue-18343.stderr new file mode 100644 index 0000000000000..9e5e4adb180d5 --- /dev/null +++ b/src/test/ui/suggestions/confuse-field-and-method/issue-18343.stderr @@ -0,0 +1,10 @@ +error: no method named `closure` found for type `Obj<[closure@$DIR/issue-18343.rs:16:28: 16:33]>` in the current scope + --> $DIR/issue-18343.rs:17:7 + | +17 | o.closure(); + | ^^^^^^^ field, not a method + | + = help: use `(o.closure)(...)` if you meant to call the function stored in the `closure` field + +error: aborting due to previous error + diff --git a/src/test/compile-fail/issue-2392.rs b/src/test/ui/suggestions/confuse-field-and-method/issue-2392.rs similarity index 81% rename from src/test/compile-fail/issue-2392.rs rename to src/test/ui/suggestions/confuse-field-and-method/issue-2392.rs index 12389f885364d..f84f35ce84bf9 100644 --- a/src/test/compile-fail/issue-2392.rs +++ b/src/test/ui/suggestions/confuse-field-and-method/issue-2392.rs @@ -49,43 +49,43 @@ fn main() { let o_closure = Obj { closure: || 42, not_closure: 42 }; o_closure.closure(); //~ ERROR no method named `closure` found //~^ HELP use `(o_closure.closure)(...)` if you meant to call the function stored - //~| NOTE `closure` is a field storing a function, not a method + //~| NOTE field, not a method o_closure.not_closure(); //~^ ERROR no method named `not_closure` found - //~| NOTE `not_closure` is a field, not a method + //~| NOTE field, not a method //~| HELP did you mean to write `o_closure.not_closure` instead of `o_closure.not_closure(...)`? let o_func = Obj { closure: func, not_closure: 5 }; o_func.closure(); //~ ERROR no method named `closure` found //~^ HELP use `(o_func.closure)(...)` if you meant to call the function stored - //~| NOTE `closure` is a field storing a function, not a method + //~| NOTE field, not a method let boxed_fn = BoxedObj { boxed_closure: Box::new(func) }; boxed_fn.boxed_closure();//~ ERROR no method named `boxed_closure` found //~^ HELP use `(boxed_fn.boxed_closure)(...)` if you meant to call the function stored - //~| NOTE `boxed_closure` is a field storing a function, not a method + //~| NOTE field, not a method let boxed_closure = BoxedObj { boxed_closure: Box::new(|| 42_u32) as Box u32> }; boxed_closure.boxed_closure();//~ ERROR no method named `boxed_closure` found //~^ HELP use `(boxed_closure.boxed_closure)(...)` if you meant to call the function stored - //~| NOTE `boxed_closure` is a field storing a function, not a method + //~| NOTE field, not a method // test expression writing in the notes let w = Wrapper { wrap: o_func }; w.wrap.closure();//~ ERROR no method named `closure` found //~^ HELP use `(w.wrap.closure)(...)` if you meant to call the function stored - //~| NOTE `closure` is a field storing a function, not a method + //~| NOTE field, not a method w.wrap.not_closure(); //~^ ERROR no method named `not_closure` found - //~| NOTE `not_closure` is a field, not a method + //~| NOTE field, not a method //~| HELP did you mean to write `w.wrap.not_closure` instead of `w.wrap.not_closure(...)`? check_expression().closure();//~ ERROR no method named `closure` found //~^ HELP use `(check_expression().closure)(...)` if you meant to call the function stored - //~| NOTE `closure` is a field storing a function, not a method + //~| NOTE field, not a method } impl FuncContainerOuter { @@ -93,13 +93,13 @@ impl FuncContainerOuter { unsafe { (*self.container).f1(1); //~ ERROR no method named `f1` found //~^ HELP use `((*self.container).f1)(...)` - //~| NOTE `f1` is a field storing a function, not a method + //~| NOTE field, not a method (*self.container).f2(1); //~ ERROR no method named `f2` found //~^ HELP use `((*self.container).f2)(...)` - //~| NOTE `f2` is a field storing a function, not a method + //~| NOTE field, not a method (*self.container).f3(1); //~ ERROR no method named `f3` found //~^ HELP use `((*self.container).f3)(...)` - //~| NOTE `f3` is a field storing a function, not a method + //~| NOTE field, not a method } } } diff --git a/src/test/ui/suggestions/confuse-field-and-method/issue-2392.stderr b/src/test/ui/suggestions/confuse-field-and-method/issue-2392.stderr new file mode 100644 index 0000000000000..56e1060bdb95a --- /dev/null +++ b/src/test/ui/suggestions/confuse-field-and-method/issue-2392.stderr @@ -0,0 +1,90 @@ +error: no method named `closure` found for type `Obj<[closure@$DIR/issue-2392.rs:49:36: 49:41]>` in the current scope + --> $DIR/issue-2392.rs:50:15 + | +50 | o_closure.closure(); //~ ERROR no method named `closure` found + | ^^^^^^^ field, not a method + | + = help: use `(o_closure.closure)(...)` if you meant to call the function stored in the `closure` field + +error: no method named `not_closure` found for type `Obj<[closure@$DIR/issue-2392.rs:49:36: 49:41]>` in the current scope + --> $DIR/issue-2392.rs:54:15 + | +54 | o_closure.not_closure(); + | ^^^^^^^^^^^ field, not a method + | + = help: did you mean to write `o_closure.not_closure` instead of `o_closure.not_closure(...)`? + +error: no method named `closure` found for type `Obj u32 {func}>` in the current scope + --> $DIR/issue-2392.rs:60:12 + | +60 | o_func.closure(); //~ ERROR no method named `closure` found + | ^^^^^^^ field, not a method + | + = help: use `(o_func.closure)(...)` if you meant to call the function stored in the `closure` field + +error: no method named `boxed_closure` found for type `BoxedObj` in the current scope + --> $DIR/issue-2392.rs:65:14 + | +65 | boxed_fn.boxed_closure();//~ ERROR no method named `boxed_closure` found + | ^^^^^^^^^^^^^ field, not a method + | + = help: use `(boxed_fn.boxed_closure)(...)` if you meant to call the function stored in the `boxed_closure` field + +error: no method named `boxed_closure` found for type `BoxedObj` in the current scope + --> $DIR/issue-2392.rs:70:19 + | +70 | boxed_closure.boxed_closure();//~ ERROR no method named `boxed_closure` found + | ^^^^^^^^^^^^^ field, not a method + | + = help: use `(boxed_closure.boxed_closure)(...)` if you meant to call the function stored in the `boxed_closure` field + +error: no method named `closure` found for type `Obj u32 {func}>` in the current scope + --> $DIR/issue-2392.rs:77:12 + | +77 | w.wrap.closure();//~ ERROR no method named `closure` found + | ^^^^^^^ field, not a method + | + = help: use `(w.wrap.closure)(...)` if you meant to call the function stored in the `closure` field + +error: no method named `not_closure` found for type `Obj u32 {func}>` in the current scope + --> $DIR/issue-2392.rs:81:12 + | +81 | w.wrap.not_closure(); + | ^^^^^^^^^^^ field, not a method + | + = help: did you mean to write `w.wrap.not_closure` instead of `w.wrap.not_closure(...)`? + +error: no method named `closure` found for type `Obj + 'static>>` in the current scope + --> $DIR/issue-2392.rs:86:24 + | +86 | check_expression().closure();//~ ERROR no method named `closure` found + | ^^^^^^^ field, not a method + | + = help: use `(check_expression().closure)(...)` if you meant to call the function stored in the `closure` field + +error: no method named `f1` found for type `FuncContainer` in the current scope + --> $DIR/issue-2392.rs:94:31 + | +94 | (*self.container).f1(1); //~ ERROR no method named `f1` found + | ^^ field, not a method + | + = help: use `((*self.container).f1)(...)` if you meant to call the function stored in the `f1` field + +error: no method named `f2` found for type `FuncContainer` in the current scope + --> $DIR/issue-2392.rs:97:31 + | +97 | (*self.container).f2(1); //~ ERROR no method named `f2` found + | ^^ field, not a method + | + = help: use `((*self.container).f2)(...)` if you meant to call the function stored in the `f2` field + +error: no method named `f3` found for type `FuncContainer` in the current scope + --> $DIR/issue-2392.rs:100:31 + | +100 | (*self.container).f3(1); //~ ERROR no method named `f3` found + | ^^ field, not a method + | + = help: use `((*self.container).f3)(...)` if you meant to call the function stored in the `f3` field + +error: aborting due to 11 previous errors + diff --git a/src/test/compile-fail/issue-32128.rs b/src/test/ui/suggestions/confuse-field-and-method/issue-32128.rs similarity index 91% rename from src/test/compile-fail/issue-32128.rs rename to src/test/ui/suggestions/confuse-field-and-method/issue-32128.rs index 74fbb33296e4b..2fd7dc246c206 100644 --- a/src/test/compile-fail/issue-32128.rs +++ b/src/test/ui/suggestions/confuse-field-and-method/issue-32128.rs @@ -22,6 +22,6 @@ fn main() { demo.example(1); //~^ ERROR no method named `example` //~| HELP use `(demo.example)(...)` - //~| NOTE `example` is a field storing a function, not a method + //~| NOTE field, not a method // (demo.example)(1); } diff --git a/src/test/ui/suggestions/confuse-field-and-method/issue-32128.stderr b/src/test/ui/suggestions/confuse-field-and-method/issue-32128.stderr new file mode 100644 index 0000000000000..0d2a895bad16d --- /dev/null +++ b/src/test/ui/suggestions/confuse-field-and-method/issue-32128.stderr @@ -0,0 +1,10 @@ +error: no method named `example` found for type `Example` in the current scope + --> $DIR/issue-32128.rs:22:10 + | +22 | demo.example(1); + | ^^^^^^^ field, not a method + | + = help: use `(demo.example)(...)` if you meant to call the function stored in the `example` field + +error: aborting due to previous error + diff --git a/src/test/compile-fail/issue-33784.rs b/src/test/ui/suggestions/confuse-field-and-method/issue-33784.rs similarity index 100% rename from src/test/compile-fail/issue-33784.rs rename to src/test/ui/suggestions/confuse-field-and-method/issue-33784.rs diff --git a/src/test/ui/suggestions/confuse-field-and-method/issue-33784.stderr b/src/test/ui/suggestions/confuse-field-and-method/issue-33784.stderr new file mode 100644 index 0000000000000..70d64e3ffa331 --- /dev/null +++ b/src/test/ui/suggestions/confuse-field-and-method/issue-33784.stderr @@ -0,0 +1,26 @@ +error: no method named `closure` found for type `&Obj<[closure@$DIR/issue-33784.rs:35:43: 35:48]>` in the current scope + --> $DIR/issue-33784.rs:37:7 + | +37 | p.closure(); //~ ERROR no method named `closure` found + | ^^^^^^^ field, not a method + | + = help: use `(p.closure)(...)` if you meant to call the function stored in the `closure` field + +error: no method named `fn_ptr` found for type `&&Obj<[closure@$DIR/issue-33784.rs:35:43: 35:48]>` in the current scope + --> $DIR/issue-33784.rs:41:7 + | +41 | q.fn_ptr(); //~ ERROR no method named `fn_ptr` found + | ^^^^^^ field, not a method + | + = help: use `(q.fn_ptr)(...)` if you meant to call the function stored in the `fn_ptr` field + +error: no method named `c_fn_ptr` found for type `&D` in the current scope + --> $DIR/issue-33784.rs:46:7 + | +46 | s.c_fn_ptr(); //~ ERROR no method named `c_fn_ptr` found + | ^^^^^^^^ field, not a method + | + = help: use `(s.c_fn_ptr)(...)` if you meant to call the function stored in the `c_fn_ptr` field + +error: aborting due to 3 previous errors +