Skip to content

Commit 3f8a70b

Browse files
committed
Fixed note message to display expression in recommendations
1 parent 9adb3df commit 3f8a70b

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

src/librustc_typeck/check/method/suggest.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,35 @@ 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, substs), Some(_)) = (&rcvr_ty.sty, rcvr_expr) {
62+
if let (&ty::TyStruct(did, substs), Some(expr)) = (&rcvr_ty.sty, rcvr_expr) {
6363
let fields = ty::lookup_struct_fields(cx, did);
6464

6565
if let Some(field) = fields.iter().find(|f| f.name == item_name) {
66+
let expr_string = match cx.sess.codemap().span_to_snippet(expr.span) {
67+
Ok(expr_string) => expr_string,
68+
_ => "s".into() // default to generic placeholder for expression
69+
};
6670

71+
// TODO Fix when closure note is displayed
72+
// below commented code from eddyb on irc
73+
// let substs = subst::Substs::new_trait(vec![fcx.inh.infcx.next_ty_var()], Vec::new(), field_ty);
74+
// let trait_ref = ty::TraitRef::new(trait_def_id, fcx.tcx().mk_substs(substs));
75+
// let poly_trait_ref = trait_ref.to_poly_trait_ref();
76+
// let obligation = traits::Obligation::misc(span, fcx.body_id, poly_trait_ref.as_predicate());
77+
// let mut selcx = traits::SelectionContext::new(fcx.infcx(), fcx);
78+
// if selcx.evaluate_obligation(&obligation) { /* suggest */ }
79+
6780
match ty::lookup_field_type(cx, did, field.id, substs).sty {
6881
ty::TyClosure(_, _) | ty::TyBareFn(_,_) => {
6982
cx.sess.span_note(span,
7083
&format!("use `({0}.{1})(...)` if you meant to call the \
7184
function stored in the `{1}` field",
72-
ty::item_path_str(cx, did), item_name));
85+
expr_string, item_name));
7386
},
7487
_ => {
7588
cx.sess.span_note(span,
7689
&format!("did you mean to write `{0}.{1}`?",
77-
ty::item_path_str(cx, did), item_name));
90+
expr_string, item_name));
7891
},
7992
};
8093
}

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,30 @@ struct Obj<F> where F: FnMut() -> u32 {
1313
nfn: usize,
1414
}
1515

16+
struct S<F> where F: FnMut() -> u32 {
17+
v: Obj<F>,
18+
}
19+
1620
fn func() -> u32 {
1721
0
1822
}
1923

2024
fn main() {
21-
let o = Obj { closure: || 42 };
25+
let o = Obj { closure: || 42, nfn: 42 };
2226
o.closure(); //~ ERROR no method named `closure` found
23-
//~^ 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`?
27+
//~^ NOTE use `(o.closure)(...)` if you meant to call the function stored in the `closure` field
2628

27-
let b = Obj { closure: func };
29+
// TODO move these to a new test for #2392
30+
let x = o.nfn(); //~ ERROR no method named `nfn` found
31+
//~^ NOTE did you mean to write `o.nfn`?
32+
33+
let b = Obj { closure: func, nfn: 5 };
2834
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
35+
//~^ NOTE use `(b.closure)(...)` if you meant to call the function stored in the `closure` field
36+
37+
let s = S { v: b };
38+
s.v.closure();//~ ERROR no method named `closure` found
39+
//~^ NOTE use `(s.v.closure)(...)` if you meant to call the function stored in the `closure` field
40+
s.v.nfn();//~ ERROR no method named `nfn` found
41+
//~^ NOTE did you mean to write `s.v.nfn`?
3042
}

0 commit comments

Comments
 (0)