Add more impls of PartialEq and PartialOrd for strings by joshtriplett · Pull Request #135536 · rust-lang/rust (original) (raw)

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

joshtriplett

@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

bors added a commit to rust-lang-ci/rust that referenced this pull request

Jan 15, 2025

@bors

Add more impls of PartialEq and PartialOrd for strings

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.

bors added a commit to rust-lang-ci/rust that referenced this pull request

Jan 15, 2025

@bors

Add more impls of PartialEq and PartialOrd for strings

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.

@rustbot rustbot added the T-bootstrap

Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)

label

Jan 18, 2025

@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

ibraheemdev

@rustbot rustbot added the T-bootstrap

Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)

label

Jan 31, 2025

@joshtriplett

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.

@joshtriplett

This avoids making a suggestion to write *x if x would also work.

@joshtriplett

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

@joshtriplett

This makes such comparisons more robust against the availability of other impls.

riking

flip1995

@djc djc mentioned this pull request

Feb 28, 2025