Skip to content

Commit 9adb3df

Browse files
committed
Uncomplete fix for rust-lang#2392
1 parent e4efb47 commit 9adb3df

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/librustc_typeck/check/method/suggest.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,24 @@ pub fn report_error<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
5959
None);
6060

6161
// If the item has the name of a field, give a help note
62-
if let (&ty::TyStruct(did, _), Some(_)) = (&rcvr_ty.sty, rcvr_expr) {
62+
if let (&ty::TyStruct(did, substs), Some(_)) = (&rcvr_ty.sty, rcvr_expr) {
6363
let fields = ty::lookup_struct_fields(cx, did);
64-
if fields.iter().any(|f| f.name == item_name) {
65-
cx.sess.span_note(span,
66-
&format!("use `(s.{0})(...)` if you meant to call the \
67-
function stored in the `{0}` field", item_name));
64+
65+
if let Some(field) = fields.iter().find(|f| f.name == item_name) {
66+
67+
match ty::lookup_field_type(cx, did, field.id, substs).sty {
68+
ty::TyClosure(_, _) | ty::TyBareFn(_,_) => {
69+
cx.sess.span_note(span,
70+
&format!("use `({0}.{1})(...)` if you meant to call the \
71+
function stored in the `{1}` field",
72+
ty::item_path_str(cx, did), item_name));
73+
},
74+
_ => {
75+
cx.sess.span_note(span,
76+
&format!("did you mean to write `{0}.{1}`?",
77+
ty::item_path_str(cx, did), item_name));
78+
},
79+
};
6880
}
6981
}
7082

src/test/compile-fail/issue-18343.rs

+11
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,21 @@
1010

1111
struct Obj<F> where F: FnMut() -> u32 {
1212
closure: F,
13+
nfn: usize,
14+
}
15+
16+
fn func() -> u32 {
17+
0
1318
}
1419

1520
fn main() {
1621
let o = Obj { closure: || 42 };
1722
o.closure(); //~ ERROR no method named `closure` found
1823
//~^ NOTE use `(s.closure)(...)` if you meant to call the function stored in the `closure` field
24+
let x = o.nfn(); //~ ERROR no method named `closure` found
25+
//~^ NOTE did you mean `o.nfn`?
26+
27+
let b = Obj { closure: func };
28+
b.closure(); //~ ERROR no method named `closure` found
29+
//~^ NOTE use `(s.closure)(...)` if you meant to call the function stored in the `closure` field
1930
}

0 commit comments

Comments
 (0)