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

Fixed #85845: Added a note in E0369 if the missing trait is PartialEq #85929

Closed
wants to merge 9 commits into from

Conversation

ccgauche
Copy link

@ccgauche ccgauche commented Jun 2, 2021

This PR fixes #85845.
A new note has been added if the missing trait is PartialEq:

add `#[derive(PartialEq)]` or manually implement `PartialEq` for `MyStruct`

I'm not sure the ends_with is the best way but I want this change to affect core, std or custom libs. So using a full strict path isn't possible.

This is my first contribution so tell me if some guidelines aren't respected.

…rive.

This is my first contribution so tell me if some guidelines aren't respected.
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @davidtwco (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 2, 2021
@fee1-dead
Copy link
Member

You should add a UI test for this, see this

compiler/rustc_typeck/src/check/op.rs Outdated Show resolved Hide resolved
@@ -959,6 +959,13 @@ fn suggest_impl_missing(err: &mut DiagnosticBuilder<'_>, ty: Ty<'_>, missing_tra
"an implementation of `{}` might be missing for `{}`",
missing_trait, ty
));
// This checks if the missing trait is PartialEq to suggest adding a derive
if missing_trait.ends_with("PartialEq") {
err.note(&format!(
Copy link
Member

Choose a reason for hiding this comment

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

Use span_suggestion, as this is a suggestion. You should move this to somewhere else, so you can look up the span of the original definition for the type.

Copy link
Author

Choose a reason for hiding this comment

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

I'm not sure that a span suggestion is better. A note at the end is clearer. Why do you want a span_suggestion?

Copy link
Member

Choose a reason for hiding this comment

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

Because you want to suggest adding a derive to the type being compared here. So you use the span of the original definition and add a #[derive(PartialEq)] on top of it, or add PartialEq to the existing derive.

Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

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

Apologies for how long its taken me to get to this review, this is a good improvement, and I've made a suggestion on how the condition you've added could be more robust.

compiler/rustc_typeck/src/check/op.rs Outdated Show resolved Hide resolved
@davidtwco davidtwco added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 16, 2021
@crlf0710
Copy link
Member

crlf0710 commented Jul 4, 2021

@ccgauche Ping from triage, would you mind addressing the review comments above? Thanks!

@ccgauche
Copy link
Author

ccgauche commented Jul 5, 2021 via email

@crlf0710
Copy link
Member

@ccgauche Ping from triage, any updates here?

@ccgauche
Copy link
Author

Yep sorry it is a little more complicated on my side than expected. I'll see what I can do in the next few days.

 - Made the check stricter.
**Note: I've checked and there is no other trait that can be possibly passed than std ones. (BinOps) so there is no need for a `can_derive` or something similar. Furthermore, this mean we can't pass another thing than a str.**
@rust-log-analyzer

This comment has been minimized.

@ccgauche
Copy link
Author

ccgauche commented Aug 1, 2021

I also fixed the issue for PartialOrd

@ccgauche ccgauche requested a review from davidtwco August 1, 2021 22:25
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@fee1-dead
Copy link
Member

@ccgauche Do you still want to work on this, or is it fine if I take this on myself?

@ccgauche
Copy link
Author

ccgauche commented Aug 5, 2021

You can work on it if you want! I'm currently in vacations.

Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

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

I'd still like to see the changes suggested in #85929 (comment) be made, I'd rather we didn't add string comparions if it can be avoided.

@fee1-dead
Copy link
Member

Yeah, I'm sorry I don't have time to implement this. Maybe someone else can try?

@ccgauche
Copy link
Author

@davidtwco Can you check my changes and confirm that's what you want?

This fix replaces all strings with an enum that has defined states so we are sure we don't encounter problems with string checks.
This also makes the code easier to read and more generic for the rest of the op.rs file

@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

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

Looks good now, left a few small nits but r=me after that.

@@ -1124,6 +1167,13 @@ impl UnOp {
Self::Neg => "-",
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
}
}

nit: newline

@@ -951,14 +938,56 @@ fn is_builtin_binop<'tcx>(lhs: Ty<'tcx>, rhs: Ty<'tcx>, op: hir::BinOp) -> bool
}
}

enum STDImplementationMissing {
Unop(hir::UnOp),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Unop(hir::UnOp),
UnOp(hir::UnOp),

nit: consistency

@@ -951,14 +938,56 @@ fn is_builtin_binop<'tcx>(lhs: Ty<'tcx>, rhs: Ty<'tcx>, op: hir::BinOp) -> bool
}
}

enum STDImplementationMissing {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
enum STDImplementationMissing {
enum StdImplementationMissing {

nit: capitalisation

@bors
Copy link
Contributor

bors commented Aug 27, 2021

☔ The latest upstream changes (presumably #87280) made this pull request unmergeable. Please resolve the merge conflicts.

@JohnCSimon
Copy link
Member

Ping from triage:

@ccgauche can you please resolve the merge conflict?

@JohnCSimon
Copy link
Member

Ping again from triage:

@ccgauche can you please resolve the merge conflict?

@apiraino apiraino added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Oct 14, 2021
@jackh726
Copy link
Member

I'm going to go ahead and close this. Looks like this suggestion was implemented in #86943. Sorry @ccgauche, but your work is appreciated nonetheless!

@jackh726 jackh726 closed this Oct 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Suggest #[derive(PartialEq)] on E0369
10 participants