Skip to content

Commit 9dab887

Browse files
committed
Show a note when closure field is called as method
Closes #18343
1 parent a93e9c2 commit 9dab887

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/librustc/middle/typeck/check/method.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,37 @@ pub fn report_error(fcx: &FnCtxt,
223223
{
224224
match error {
225225
NoMatch(static_sources) => {
226+
let cx = fcx.tcx();
227+
let method_ustring = method_name.user_string(cx);
228+
229+
// True if the type is a struct and contains a field with
230+
// the same name as the not-found method
231+
let is_field = match ty::get(rcvr_ty).sty {
232+
ty_struct(did, _) =>
233+
ty::lookup_struct_fields(cx, did)
234+
.iter()
235+
.any(|f| f.name.user_string(cx) == method_ustring),
236+
_ => false
237+
};
238+
226239
fcx.type_error_message(
227240
span,
228241
|actual| {
229242
format!("type `{}` does not implement any \
230243
method in scope named `{}`",
231244
actual,
232-
method_name.user_string(fcx.tcx()))
245+
method_ustring)
233246
},
234247
rcvr_ty,
235248
None);
236249

250+
// If the method has the name of a field, give a help note
251+
if is_field {
252+
cx.sess.span_note(span,
253+
format!("use `(s.{0})(...)` if you meant to call the \
254+
function stored in the `{0}` field", method_ustring).as_slice());
255+
}
256+
237257
if static_sources.len() > 0 {
238258
fcx.tcx().sess.fileline_note(
239259
span,

0 commit comments

Comments
 (0)