Skip to content
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

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

Merged
merged 2 commits into from
Oct 28, 2014
Merged

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

merged 2 commits into from
Oct 28, 2014

Conversation

aochagavia
Copy link
Contributor

Closes #18343

@aochagavia
Copy link
Contributor Author

I don't know how to test this. Any ideas?

@aochagavia
Copy link
Contributor Author

Also, this is my first contribution to librustc itself, so please tell me if I am doing things right.


// Returns true if the type is a struct and contains a field with
// the same name as the not-found method
let is_field = || {
Copy link

Choose a reason for hiding this comment

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

I think this would actually be clearer as a match expression.

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
};

@ghost
Copy link

ghost commented Oct 26, 2014

@aochagavia Thanks for the PR! I added comments inline.

In terms of testing, you'll need a small self-contained Rust program that is supposed to produce this diagnostic and annotate it accordingly with the diagnostic messages you expect it to produce. See the files in src/test/compile-fail/ for examples. Also, the wiki has some information about this.

@ghost
Copy link

ghost commented Oct 26, 2014

I think in the future we may want this to also extend to fields that can be reached via an arbitrary number of autoderefs so that the note also shows up for structs contained in smart pointers and similar. But this is great for now!

Also, I wonder if there's any particular reason why a.f() doesn't work for closures in fields or if it just hasn't been implemented yet.

@aochagavia
Copy link
Contributor Author

This code will show the helping note even when the field of the struct is not a function/closure. Do you think that is ok? Maybe I could add some extra checks to see if the field is indeed a function/closure. How complex would it be? Is it worth it?

},
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!

@alexcrichton
Copy link
Member

Looks like there's some tidy issues (trailing whitespace)

@aochagavia
Copy link
Contributor Author

@alexcrichton thanks! I have fixed it

@alexcrichton
Copy link
Member

It may still be there in the test case (travis failures)

@aochagavia
Copy link
Contributor Author

Not anymore :)

alexcrichton added a commit to alexcrichton/rust that referenced this pull request Oct 27, 2014
@bors bors merged commit 2ce77b3 into rust-lang:master Oct 28, 2014
@aochagavia aochagavia deleted the closure-fields branch October 28, 2014 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Calling a closure stored in a struct, a bug or just a bad error message
3 participants