From 9adb3dfdcb2cab7a10d7ac7a48170672b3961fe8 Mon Sep 17 00:00:00 2001 From: Paul Faria Date: Sat, 13 Jun 2015 14:47:10 -0400 Subject: [PATCH] Uncomplete fix for #2392 --- src/librustc_typeck/check/method/suggest.rs | 22 ++++++++++++++++----- src/test/compile-fail/issue-18343.rs | 11 +++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index b098fb56d4db6..be34c7057210c 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -59,12 +59,24 @@ pub fn report_error<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, None); // If the item has the name of a field, give a help note - if let (&ty::TyStruct(did, _), Some(_)) = (&rcvr_ty.sty, rcvr_expr) { + if let (&ty::TyStruct(did, substs), Some(_)) = (&rcvr_ty.sty, rcvr_expr) { let fields = ty::lookup_struct_fields(cx, did); - if fields.iter().any(|f| f.name == item_name) { - cx.sess.span_note(span, - &format!("use `(s.{0})(...)` if you meant to call the \ - function stored in the `{0}` field", item_name)); + + if let Some(field) = fields.iter().find(|f| f.name == item_name) { + + match ty::lookup_field_type(cx, did, field.id, substs).sty { + ty::TyClosure(_, _) | ty::TyBareFn(_,_) => { + cx.sess.span_note(span, + &format!("use `({0}.{1})(...)` if you meant to call the \ + function stored in the `{1}` field", + ty::item_path_str(cx, did), item_name)); + }, + _ => { + cx.sess.span_note(span, + &format!("did you mean to write `{0}.{1}`?", + ty::item_path_str(cx, did), item_name)); + }, + }; } } diff --git a/src/test/compile-fail/issue-18343.rs b/src/test/compile-fail/issue-18343.rs index 43e9ca5fa6e7a..3572e5084c15a 100644 --- a/src/test/compile-fail/issue-18343.rs +++ b/src/test/compile-fail/issue-18343.rs @@ -10,10 +10,21 @@ struct Obj where F: FnMut() -> u32 { closure: F, + nfn: usize, +} + +fn func() -> u32 { + 0 } fn main() { let o = Obj { closure: || 42 }; o.closure(); //~ ERROR no method named `closure` found //~^ NOTE use `(s.closure)(...)` if you meant to call the function stored in the `closure` field + let x = o.nfn(); //~ ERROR no method named `closure` found + //~^ NOTE did you mean `o.nfn`? + + let b = Obj { closure: func }; + b.closure(); //~ ERROR no method named `closure` found + //~^ NOTE use `(s.closure)(...)` if you meant to call the function stored in the `closure` field }