Skip to content

Add notes for missing PartialEq and PartialOrd, closes #28837 #28933

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 3 commits into from
Oct 17, 2015

Conversation

fhahn
Copy link
Contributor

@fhahn fhahn commented Oct 9, 2015

this PR adds notes for missing PartialEq and PartialOrd. I've added a test case but it seems like NOTE is ignored by the test runner.
#28837

@rust-highfive
Copy link
Contributor

r? @pcwalton

(rust_highfive has picked a reviewer for you, use r? to override)

@@ -0,0 +1,20 @@
struct A;
Copy link
Member

Choose a reason for hiding this comment

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

This file I believe will need a license to pass make tidy

@alexcrichton
Copy link
Member

Perhaps this could also handle all other binary operations? I think we've basically got a trait for all of them, so it could print a message out for each.

@apasel422
Copy link
Contributor

I guess it's not possible to use the #[rustc_on_unimplemented] attribute for this?

@fhahn
Copy link
Contributor Author

fhahn commented Oct 10, 2015

I've pushed a new commit that adds notes for all relevant std::ops::* traits. At the moment the note only states that a required trait is not implemented by the type A or one of its type arguments, which is not really precise and not correct as I just noticed.

It should be more like "it is not implemented by type A or any other type that's used in its eq method", but I am not sure how express this.

However, the ideal note would say exactly which type does not have an PartialEq implementation.

Consider

#[derive(PartialEq)]
struct Foo<T> {
    f: T,
}

struct Bar;

fn main() {
    let x = Foo { f: Bar };
    x == x;
}

Now the note would look like an implementation of "std::cmp::PartialEq" might be missing for "Foo" or one of its type arguments. The better note would be an implementation of "std::cmp::PartialEq" might be missing for "Bar". The problem here is with the comparison self.f == other.f in the derived eq function, but it gets raised when checking whether Foo implements PartialEq.

Note that this does not happen when PartialEq is implemented manually for Foo.

Do you have any pointers where to look in order to improve the note in the derive case?

@fhahn fhahn force-pushed the issue-28837-partialeq-note branch from 945c283 to 77efe39 Compare October 10, 2015 11:56
@alexcrichton
Copy link
Member

I'm not 100% sure how the deriving case could be improved, but dropping the "or one of its type arguments" seems fine to me, the qualification of "might" is probably good enough for "this message isn't always correct"

@fhahn
Copy link
Contributor Author

fhahn commented Oct 11, 2015

Ok, I dropped the "or one of its type arguments" from the message. I still think handling the deriving case better would be good, as the new note is still somewhat confusing in the case @AndiDog mentioned in the issue, especially for beginners I think.

But maybe improving it something to do in another pull request?

@@ -191,6 +191,27 @@ fn check_overloaded_binop<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
"binary operation `{}` cannot be applied to type `{}`",
hir_util::binop_to_string(op.node),
lhs_ty);
let (display_note, missing_trait) = match op.node {
hir::BiAdd => (true, "std::ops::Add"),
Copy link
Member

Choose a reason for hiding this comment

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

This may be more idiomatically done via Some("...") with a None at the end followed by a if let Some(missing_trait) = display_note at the printing location.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I've updated the commit.

@fhahn fhahn force-pushed the issue-28837-partialeq-note branch from 1e0c120 to da16902 Compare October 12, 2015 08:03
@alexcrichton
Copy link
Member

@bors: r+ da169028e3093ea6e74d50be3750efca9f3465fc

Thanks!

@bors
Copy link
Collaborator

bors commented Oct 12, 2015

⌛ Testing commit da16902 with merge a72423a...

@bors
Copy link
Collaborator

bors commented Oct 12, 2015

💔 Test failed - auto-mac-64-opt

@fhahn fhahn force-pushed the issue-28837-partialeq-note branch from da16902 to b21ae1a Compare October 13, 2015 07:59
@fhahn
Copy link
Contributor Author

fhahn commented Oct 13, 2015

Unfortunately there was a trailing whitespace (sorry for that). I've updated the commit.

@alexcrichton
Copy link
Member

@bors: r+ b21ae1a

@fhahn
Copy link
Contributor Author

fhahn commented Oct 17, 2015

Is it possible bors missed this PR?

@bluss
Copy link
Member

bluss commented Oct 17, 2015

@bors r=alexcrichton

@bors
Copy link
Collaborator

bors commented Oct 17, 2015

📌 Commit b21ae1a has been approved by alexcrichton

@bluss
Copy link
Member

bluss commented Oct 17, 2015

@bors retry

@bluss
Copy link
Member

bluss commented Oct 17, 2015

Ok, now it's back on the board

@fhahn
Copy link
Contributor Author

fhahn commented Oct 17, 2015

Wow that was fast. Thanks you very much for your swift response :)

bors added a commit that referenced this pull request Oct 17, 2015
this PR adds notes for missing `PartialEq` and `PartialOrd`. I've added a test case but it seems like `NOTE` is ignored by the test runner.

#28837
@bors
Copy link
Collaborator

bors commented Oct 17, 2015

⌛ Testing commit b21ae1a with merge c7a58b5...

@bors bors merged commit b21ae1a into rust-lang:master Oct 17, 2015
@fhahn fhahn deleted the issue-28837-partialeq-note branch October 17, 2015 15:11
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.

7 participants