Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

hkBst
Copy link
Member

@hkBst hkBst commented Jul 3, 2025

Removes manual impls of PartialEq::ne which are equivalent to the default.

@rustbot
Copy link
Collaborator

rustbot commented Jul 3, 2025

r? @tgross35

rustbot has assigned @tgross35.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 3, 2025
@taiki-e
Copy link
Member

taiki-e commented Jul 3, 2025

equivalent to the default

Note that implementations for reference types are not strictly the same as the default because they are forwarding of the underlying implementation. In such cases, if the compiler cannot optimize the inversion of the result from the underlying implementation (e.g., inline assembly or FFI is used), then ne may generate an extra 1-2 instructions (xor with constant 1 or set constant 1 to the register and xor with it).

@hkBst
Copy link
Member Author

hkBst commented Jul 3, 2025

@taiki-e that is a good point, thanks!

@rust-log-analyzer

This comment has been minimized.

Comment on lines 2027 to 2029
// if <A as PartialEq<B>>::ne uses inline assembly or FFI, then
// this forwarding impl may be more efficient than the default impl
#[allow(clippy::partialeq_ne_impl)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably allow->expect so we get a bit of noise if these change for some reason.

Maybe just make this a module-level attribute? Since it applies to all but one fn ne in this file.

Copy link
Member Author

Choose a reason for hiding this comment

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

But at module level would it not also conflict with the few missing ne, at least as expect?

I was thinking of removing duplication a bit with a macro here, since the four cases of (&mut, &mut), (&mut, &), (&, &mut), (&, &) are (nearly?) identical. That would also collapse most of these annotations...

Copy link
Member Author

Choose a reason for hiding this comment

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

I've now collapsed this with macros to just one PartialEq::ne impl with an expect.

@workingjubilee
Copy link
Member

workingjubilee commented Jul 6, 2025

I don't think we should add macros to appease the thing that is linting in an effort to make the code more readable.

Adding macros is rather the opposite of making the code more readable.

@hkBst
Copy link
Member Author

hkBst commented Jul 7, 2025

@workingjubilee macros like these are used all over the library for repeating impls just like here... Do you feel the same way about those, or can you explain why you think these are any different?

@workingjubilee
Copy link
Member

If you look you will find that I have pushed people to reduce the usage of macros in other PRs, yes.

@hkBst
Copy link
Member Author

hkBst commented Jul 8, 2025

Very well, I was just trying to clarify your point. Macros come with the disadvantage of more complicated code, but one advantage of using macros like this is that it is easier to see which code is identically duplicated and which parts contain differences. Opinions will vary on in which cases the upside outweighs the downside.

My latest change introduced 3 macros:

  • The first repeats something for all combinations of two references each of which can be mut or not, so 4 times total. To me this seems justified.
  • The second repeats a larger impl block 2 times, which also seems justified. It also raises the question of the 2 missing impls, to which I don't know the answer.
  • The last repeats a simple impl for ordinary and mutable refs, so 2 times. It is also very simple, so also seems justified to me, but I don't feel very strongly about this one.

@tgross35
Copy link
Contributor

tgross35 commented Jul 8, 2025

I don't have any strong preferences here

r? workingjubilee

@rustbot rustbot assigned workingjubilee and unassigned tgross35 Jul 8, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jul 8, 2025

workingjubilee is currently at their maximum review capacity.
They may take a while to respond.

@rust-cloud-vms rust-cloud-vms bot force-pushed the clippy-fix-5 branch 2 times, most recently from 59bece7 to 3c1c6e9 Compare July 9, 2025 13:01
@bors
Copy link
Collaborator

bors commented Jul 13, 2025

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

Comment on lines -1816 to -1865
#[inline]
fn ne(&self, other: &Self) -> bool { *self != *other }
Copy link
Member

Choose a reason for hiding this comment

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

Primitive ne isn't equivalent to !(*self == *other). This difference goes straight to MIR and codegen:

mir::BinOp::Ne
| mir::BinOp::Lt
| mir::BinOp::Gt
| mir::BinOp::Eq
| mir::BinOp::Le
| mir::BinOp::Ge => {
if is_float {
bx.fcmp(base::bin_op_to_fcmp_predicate(op), lhs, rhs)
} else {
bx.icmp(base::bin_op_to_icmp_predicate(op, is_signed), lhs, rhs)
}
}

Yes it can be trivially optimized away but why emit additional IR when you can emit less?

Copy link
Member Author

Choose a reason for hiding this comment

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

Interesting! I suppose that treatment may be necessary in case these are implemented by inline assembly or via FFI.

Yes it can be trivially optimized away but why emit additional IR when you can emit less?

The documentation in this very file says that:

/// The default implementation of `ne` provides this consistency and is almost
/// always sufficient. It should not be overridden without very good reason.

Is the performance impact of emitting slightly more IR really a very good reason? I would expect it is on the edge of immeasurable.

Copy link
Member

Choose a reason for hiding this comment

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

Is the performance impact of emitting slightly more IR really a very good reason? I would expect it is on the edge of immeasurable.

That is a very interesting question since we do in fact have some measurements! Let's at least find out if our existing ones would say anything.

Copy link
Member

Choose a reason for hiding this comment

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

My reservation wasn't mainly about the performance impact. I'm thinking of alternative codegen backends that read Rust MIR and generate code with instruction sets that have ne as well as eq for primitives.

Rather than telling people to rely on MIR opts or LLVM opts (when the backend may not be LLVM) I'd prefer us to do something better on the outset.

The other thing is that it just feels weird? I just don't think clippy is very applicable to this specific case.

Copy link
Member

Choose a reason for hiding this comment

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

And like, what's the benefit here? Of course we don't have to be biased towards the status quo but what is the difference between the change and the status quo?

These are just two lines in a macro. Removing them isn't too much different from keeping them.

Copy link
Member Author

Choose a reason for hiding this comment

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

The difference would be that the code would then do what the comment about implementing ne seems to recommend. Removing ne is just one of three ways of achieving that, and merely the one that seemed most likely to work at the time. But I'm also happy to document the "very good reason" why we want to impl ne here, or change the comment or both.

@bors
Copy link
Collaborator

bors commented Aug 7, 2025

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

@rustbot

This comment has been minimized.

@workingjubilee
Copy link
Member

The question of measurable performance impact if any was raised so we might as well test this much, even if it's not the be-all end-all of performance testing since our runtime benches remain lacking:

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Sep 10, 2025
clippy fix: remove manual PartialEq::ne
@rust-bors

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 10, 2025
@rust-bors
Copy link

rust-bors bot commented Sep 10, 2025

☀️ Try build successful (CI)
Build commit: ed44707 (ed4470790af94f2233b1f4e18fa14d11b4c0aafb, parent: 7ad23f43a225546c095123de52cc07d8719f8e2b)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (ed44707): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results (primary 2.8%, secondary 0.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.8% [2.8%, 2.8%] 1
Regressions ❌
(secondary)
2.0% [1.8%, 2.2%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.6% [-2.6%, -2.6%] 1
All ❌✅ (primary) 2.8% [2.8%, 2.8%] 1

Cycles

Results (secondary -2.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.1% [2.1%, 2.1%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.9% [-3.4%, -2.1%] 6
All ❌✅ (primary) - - 0

Binary size

Results (primary -0.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.0% [0.0%, 0.0%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.0% [-0.0%, -0.0%] 3
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.0% [-0.0%, 0.0%] 4

Bootstrap: 469.26s -> 468.084s (-0.25%)
Artifact size: 387.51 MiB -> 387.50 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 10, 2025
@bors
Copy link
Collaborator

bors commented Sep 12, 2025

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

PartialEq::ne(*self, *other)
}
}
partial_ord_impl!((&A => A, &B => B) /*(&A => A, &mut B => B) (&mut A => A, &B => B)*/ (&mut A => A, &mut B => B));
Copy link
Member

Choose a reason for hiding this comment

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

My primary objection with macros is I find deducing their invented syntax, like this, to often be confounding, unless it is a simple listing of things. I broadly prefer it when the "argument" continues to look like Rust code, and am actually willing to tolerate a more complex macro if it continues looking like Rust code. It also is nice if a span actually consistently points to the trait definition instead of a macro invocation.

Copy link
Member

Choose a reason for hiding this comment

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

The following oversimplified example is valid to do:

trait Something {
    fn stuff();
    fn thingy();
}

macro_rules! something_body {
    () => {
        fn stuff() {
            ()
        }
        fn thingy() {
            ()
        }
    }
}

impl Something for u8 {
    something_body!();
}

I would somewhat prefer it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for these comments. I agree that the current invented syntax is not optimal. I'll see if I can do better.

Copy link
Member Author

Choose a reason for hiding this comment

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

I changed the syntax to be more rust-like. Let me know if it helps.

@rustbot ready

Copy link
Member Author

Choose a reason for hiding this comment

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

I just happened across https://github.com/rust-lang/rust/blob/master/library/std/src/path.rs#L3594-L3685 which does things very similarly to my current proposal.

@workingjubilee
Copy link
Member

@rustbot author

@rustbot rustbot 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 Sep 12, 2025
@rustbot
Copy link
Collaborator

rustbot commented Sep 17, 2025

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants