Skip to content

Show a note when closure field is called as method #18346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/librustc/middle/typeck/check/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,37 @@ pub fn report_error(fcx: &FnCtxt,
{
match error {
NoMatch(static_sources) => {
let cx = fcx.tcx();
let method_ustring = method_name.user_string(cx);

// True if the type is a struct and contains a field with
// the same name as the not-found method
let is_field = match ty::get(rcvr_ty).sty {
ty_struct(did, _) =>
ty::lookup_struct_fields(cx, did)
.iter()
.any(|f| f.name.user_string(cx) == method_ustring),
_ => false
};

fcx.type_error_message(
span,
|actual| {
format!("type `{}` does not implement any \
method in scope named `{}`",
actual,
method_name.user_string(fcx.tcx()))
method_ustring)
},
rcvr_ty,
None);

// If the method has the name of a field, give a help note
if is_field {
cx.sess.span_note(span,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have used span_note because the meaning seems to be the same and I can use NOTE to check if the message is correct (see the test)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also check for HELP in tests and I think we're moving towards HELP for "this is how you fix the error" kind of messages but don't worry about it, whoever drives by this part of code can fix it next time!

format!("use `(s.{0})(...)` if you meant to call the \
function stored in the `{0}` field", method_ustring).as_slice());
}

if static_sources.len() > 0 {
fcx.tcx().sess.fileline_note(
span,
Expand Down
19 changes: 19 additions & 0 deletions src/test/compile-fail/issue-18343.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

struct Obj<'a> {
closure: ||: 'a -> u32
}

fn main() {
let o = Obj { closure: || 42 };
o.closure(); //~ ERROR type `Obj<'_>` does not implement any method in scope named `closure`
//~^ NOTE use `(s.closure)(...)` if you meant to call the function stored in the `closure` field
}