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

Add more impls of PartialEq and PartialOrd for strings #135536

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

joshtriplett
Copy link
Member

@joshtriplett joshtriplett commented Jan 15, 2025

Currently, some combinations of &String and &str don't support
comparison operators. For instance, this:

fn main() {
    let s1 = String::from("hello");
    let s2 = "world";
    _ = s1 < s2;
}

will fail with:

error[E0308]: mismatched types
 --> src/main.rs:4:14
  |
4 |     _ = s1 < s2;
  |         --   ^^- help: try using a conversion method: `.to_string()`
  |         |    |
  |         |    expected `String`, found `&str`
  |         expected because this is `String`

Other combinations only work because the compiler implicitly relies on
impls on different reference types, and that makes such combinations
fragile, breaking if any other impls of PartialOrd show up.

Add some additional impls to make such cases work, and to improve
robustness when adding other impls in the future. In particular, I'm hoping that adding these makes it possible to add comparisons with other stringy types without creating as many inference issues.

@joshtriplett joshtriplett added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. relnotes Marks issues that should be documented in the release notes of the next release. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. needs-crater This change needs a crater run to check for possible breakage in the ecosystem. labels Jan 15, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jan 15, 2025

r? @ibraheemdev

rustbot has assigned @ibraheemdev.
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 the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 15, 2025
@joshtriplett
Copy link
Member Author

@bors try

bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 15, 2025
Add more impls of PartialEq and PartialOrd for strings

Currently, some combinations of `&String` and `&str` don't support
comparison operators. For instance, this:

```rust
fn main() {
    let s1 = String::from("hello");
    let s2 = "world";
    _ = s1 < s2;
}
```

will fail with:
```
error[E0308]: mismatched types
 --> src/main.rs:4:14
  |
4 |     _ = s1 < s2;
  |         --   ^^- help: try using a conversion method: `.to_string()`
  |         |    |
  |         |    expected `String`, found `&str`
  |         expected because this is `String`
```

Other combinations only work because the compiler implicitly relies on
impls on different reference types, and that makes such combinations
fragile, breaking if any other impls of `PartialOrd` show up.

Add some additional impls to make such cases work, and to improve
robustness when adding other impls in the future. In particular, I'm hoping that adding these makes it possible to add comparisons with other stringy types without creating as many inference issues.
@bors
Copy link
Contributor

bors commented Jan 15, 2025

⌛ Trying commit 8b933dd with merge 7abdc84...

@rust-log-analyzer

This comment has been minimized.

@joshtriplett
Copy link
Member Author

Some of the changes in Clippy look good; for instance:

--- a/src/tools/clippy/tests/ui/iter_overeager_cloned.fixed
+++ b/src/tools/clippy/tests/ui/iter_overeager_cloned.fixed
@@ -13,13 +13,13 @@ fn main() {
 
     let _: Option<String> = vec.iter().chain(vec.iter()).next().cloned();
 
-    let _: usize = vec.iter().filter(|x| x == &"2").count();
+    let _: usize = vec.iter().filter(|x| x == "2").count();
--- a/src/tools/clippy/tests/ui/op_ref.fixed
+++ b/src/tools/clippy/tests/ui/op_ref.fixed
@@ -18,7 +18,7 @@ fn main() {
     let a = "a".to_string();
     let b = "a";
 
-    if b < &a {
+    if b < a {

It's good that this code doesn't need to write &"2".

However, some of them look bad, and seem to need a non-trivial fix in a Clippy lint:

--- a/src/tools/clippy/tests/ui/cmp_owned/with_suggestion.fixed
+++ b/src/tools/clippy/tests/ui/cmp_owned/with_suggestion.fixed
@@ -2,18 +2,18 @@
 #[allow(clippy::unnecessary_operation, clippy::no_effect, unused_must_use, clippy::eq_op)]
 fn main() {
     fn with_to_string(x: &str) {
-        x != "foo";
+        x != *"foo";
 
-        "foo" != x;
+        *"foo" != x;
--- a/src/tools/clippy/tests/ui/cmp_owned/with_suggestion.stderr
+++ b/src/tools/clippy/tests/ui/cmp_owned/with_suggestion.stderr
@@ -2,7 +2,7 @@ error: this creates an owned instance just for comparison
   --> tests/ui/cmp_owned/with_suggestion.rs:5:14
    |
 LL |         x != "foo".to_string();
-   |              ^^^^^^^^^^^^^^^^^ help: try: `"foo"`
+   |              ^^^^^^^^^^^^^^^^^ help: try: `*"foo"`
    |
    = note: `-D clippy::cmp-owned` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(clippy::cmp_owned)]`

This should not be suggesting *"foo", since "foo" works just fine.

Could someone from @rust-lang/clippy help me figure out how to get clippy to not make that suggestion for clippy::cmp-owned?

@rustbot
Copy link
Collaborator

rustbot commented Jan 15, 2025

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

@joshtriplett
Copy link
Member Author

Nevermind, I managed to figure it out.

@joshtriplett
Copy link
Member Author

@bors try

@bors
Copy link
Contributor

bors commented Jan 15, 2025

⌛ Trying commit d972108 with merge da49290...

bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 15, 2025
Add more impls of PartialEq and PartialOrd for strings

Currently, some combinations of `&String` and `&str` don't support
comparison operators. For instance, this:

```rust
fn main() {
    let s1 = String::from("hello");
    let s2 = "world";
    _ = s1 < s2;
}
```

will fail with:
```
error[E0308]: mismatched types
 --> src/main.rs:4:14
  |
4 |     _ = s1 < s2;
  |         --   ^^- help: try using a conversion method: `.to_string()`
  |         |    |
  |         |    expected `String`, found `&str`
  |         expected because this is `String`
```

Other combinations only work because the compiler implicitly relies on
impls on different reference types, and that makes such combinations
fragile, breaking if any other impls of `PartialOrd` show up.

Add some additional impls to make such cases work, and to improve
robustness when adding other impls in the future. In particular, I'm hoping that adding these makes it possible to add comparisons with other stringy types without creating as many inference issues.
@bors
Copy link
Contributor

bors commented Jan 15, 2025

☀️ Try build successful - checks-actions
Build commit: da49290 (da4929024a7261062885cb3209c9d7fdc919d233)

@joshtriplett
Copy link
Member Author

@craterbot check

@craterbot
Copy link
Collaborator

👌 Experiment pr-135536 created and queued.
🤖 Automatically detected try build da49290
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-crater Status: Waiting on a crater run to be completed. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 16, 2025
@craterbot
Copy link
Collaborator

🚧 Experiment pr-135536 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Collaborator

🎉 Experiment pr-135536 is completed!
📊 45 regressed and 3 fixed (569212 total)
📰 Open the full report.

⚠️ If you notice any spurious failure please add them to the denylist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Jan 18, 2025
@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added the T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) label Jan 18, 2025
@joshtriplett
Copy link
Member Author

Rebasing and reblessing.

@rustbot rustbot added the T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) label Jan 24, 2025
@joshtriplett joshtriplett removed the T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) label Jan 24, 2025
@bors
Copy link
Contributor

bors commented Jan 29, 2025

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

@joshtriplett
Copy link
Member Author

I'm going to wait to fix further conflicts on this PR until there are more checkboxes on the FCP.

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Jan 29, 2025
@rfcbot
Copy link

rfcbot commented Jan 29, 2025

🔔 This is now entering its final comment period, as per the review above. 🔔

@rustbot rustbot added the T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) label Jan 31, 2025
Currently, some combinations of `&String` and `&str` don't support
comparison operators. For instance, this:

```rust
fn main() {
    let s1 = String::from("hello");
    let s2 = "world";
    _ = s1 < s2;
}
```

will fail with:
```
error[E0308]: mismatched types
 --> src/main.rs:4:14
  |
4 |     _ = s1 < s2;
  |         --   ^^- help: try using a conversion method: `.to_string()`
  |         |    |
  |         |    expected `String`, found `&str`
  |         expected because this is `String`
```

Other combinations only work because the compiler implicitly relies on
impls on different reference types, and that makes such combinations
fragile, breaking if any other impls of `PartialOrd` show up.

Add some additional impls to make such cases work, and to improve
robustness when adding other impls in the future.
This avoids making a suggestion to write `*x` if `x` would also work.
In a couple of cases, adjust the test file, because otherwise the file
produces:

```
error: failed to apply suggestions for tests/ui/iter_overeager_cloned.rs with rustfix
cannot replace slice of data that was already replaced
Add //@no-rustfix to the test file to ignore rustfix suggestions
```
This makes such comparisons more robust against the availability of
other impls.
@joshtriplett
Copy link
Member Author

@ibraheemdev Does this now look good, other than waiting on FCP to complete?

@ibraheemdev
Copy link
Member

Yes the implementation looks good. r=me after the FCP.

@@ -2530,12 +2531,51 @@ macro_rules! impl_eq {

impl_eq! { String, str }
impl_eq! { String, &'a str }
impl_eq! { String, &String }
impl_eq! { &String, str }
Copy link

Choose a reason for hiding this comment

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

These two impls end up with incorrect stability attributes, which means Clippy msrv autodetection can't be added later.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's fixable, but is that worth complicating the macro for? There are many other examples of macro-generated instances with the same discrepancy.

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Feb 8, 2025
@rfcbot
Copy link

rfcbot commented Feb 8, 2025

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

This will be merged soon.

@rfcbot rfcbot added the to-announce Announce this issue on triage meeting label Feb 8, 2025
@ibraheemdev ibraheemdev 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 Feb 11, 2025
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Feb 13, 2025
@@ -28,6 +28,17 @@ LL | let _: usize = vec.iter().filter(|x| x == &"2").cloned().count();
= note: `-D clippy::redundant-clone` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::redundant_clone)]`

error: taken reference of right operand
Copy link
Member

Choose a reason for hiding this comment

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

This lint shouldn't trigger in the test file. Please fix the two new occurrences by removing the ref.

Copy link
Member Author

Choose a reason for hiding this comment

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

Alright, will fix.

@bors
Copy link
Contributor

bors commented Mar 2, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.