1
Fork 0

Fixed note message to display expression in recommendations

This commit is contained in:
Paul Faria 2015-06-14 02:49:00 -04:00
parent 9adb3dfdcb
commit 3f8a70b613
2 changed files with 34 additions and 9 deletions

View file

@ -59,22 +59,35 @@ 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, substs), Some(_)) = (&rcvr_ty.sty, rcvr_expr) {
if let (&ty::TyStruct(did, substs), Some(expr)) = (&rcvr_ty.sty, rcvr_expr) {
let fields = ty::lookup_struct_fields(cx, did);
if let Some(field) = fields.iter().find(|f| f.name == item_name) {
let expr_string = match cx.sess.codemap().span_to_snippet(expr.span) {
Ok(expr_string) => expr_string,
_ => "s".into() // default to generic placeholder for expression
};
// TODO Fix when closure note is displayed
// below commented code from eddyb on irc
// let substs = subst::Substs::new_trait(vec![fcx.inh.infcx.next_ty_var()], Vec::new(), field_ty);
// let trait_ref = ty::TraitRef::new(trait_def_id, fcx.tcx().mk_substs(substs));
// let poly_trait_ref = trait_ref.to_poly_trait_ref();
// let obligation = traits::Obligation::misc(span, fcx.body_id, poly_trait_ref.as_predicate());
// let mut selcx = traits::SelectionContext::new(fcx.infcx(), fcx);
// if selcx.evaluate_obligation(&obligation) { /* suggest */ }
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));
expr_string, item_name));
},
_ => {
cx.sess.span_note(span,
&format!("did you mean to write `{0}.{1}`?",
ty::item_path_str(cx, did), item_name));
expr_string, item_name));
},
};
}

View file

@ -13,18 +13,30 @@ struct Obj<F> where F: FnMut() -> u32 {
nfn: usize,
}
struct S<F> where F: FnMut() -> u32 {
v: Obj<F>,
}
fn func() -> u32 {
0
}
fn main() {
let o = Obj { closure: || 42 };
let o = Obj { closure: || 42, nfn: 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`?
//~^ NOTE use `(o.closure)(...)` if you meant to call the function stored in the `closure` field
let b = Obj { closure: func };
// TODO move these to a new test for #2392
let x = o.nfn(); //~ ERROR no method named `nfn` found
//~^ NOTE did you mean to write `o.nfn`?
let b = Obj { closure: func, nfn: 5 };
b.closure(); //~ ERROR no method named `closure` found
//~^ NOTE use `(s.closure)(...)` if you meant to call the function stored in the `closure` field
//~^ NOTE use `(b.closure)(...)` if you meant to call the function stored in the `closure` field
let s = S { v: b };
s.v.closure();//~ ERROR no method named `closure` found
//~^ NOTE use `(s.v.closure)(...)` if you meant to call the function stored in the `closure` field
s.v.nfn();//~ ERROR no method named `nfn` found
//~^ NOTE did you mean to write `s.v.nfn`?
}