Skip to content

Commit

Permalink
Uncomplete fix for rust-lang#2392
Browse files Browse the repository at this point in the history
  • Loading branch information
Nashenas88 committed Jun 20, 2015
1 parent e4efb47 commit 9adb3df
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/librustc_typeck/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
},
};
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/test/compile-fail/issue-18343.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,21 @@

struct Obj<F> 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
}

0 comments on commit 9adb3df

Please sign in to comment.