Skip to content

Make Rcs and Arcs use pointer comparison for unsized types#155579

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
Gaming32:fix-154998
Apr 27, 2026
Merged

Make Rcs and Arcs use pointer comparison for unsized types#155579
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
Gaming32:fix-154998

Conversation

@Gaming32
Copy link
Copy Markdown
Contributor

@Gaming32 Gaming32 commented Apr 21, 2026

Rc and Arcs have an Eq implementation that first attempt to compare the pointers as an optimization. This, however, was not extended to DSTs, which is what this PR fixes.

Fixes #154998.

@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 Apr 21, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 21, 2026

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 6 candidates
  • Random selection from Mark-Simulacrum, jhpratt

@Mark-Simulacrum
Copy link
Copy Markdown
Member

Can you provide some context on how this addresses the concerns from @theemathas and @Jules-Bertholet in the issue? Those seem at least plausible to me (i.e., this is a change in behavior).

@Gaming32
Copy link
Copy Markdown
Contributor Author

This implements the idea from #154998 (comment). I included the example from @Jules-Bertholet in a test as well, and confirmed that it is still sound under the changes.

@Mark-Simulacrum
Copy link
Copy Markdown
Member

@bors r+

OK, I think this makes sense. Thanks for clarifying!

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 27, 2026

📌 Commit 6f28cd5 has been approved by Mark-Simulacrum

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 27, 2026
rust-bors Bot pushed a commit that referenced this pull request Apr 27, 2026
Rollup of 12 pull requests

Successful merges:

 - #149624 (Fix requires_lto targets needing lto set in cargo)
 - #155317 (`std::io::Take`: Clarify & optimize `BorrowedBuf::set_init` usage.)
 - #155579 (Make Rcs and Arcs use pointer comparison for unsized types)
 - #155588 (Implement more traits for FRTs)
 - #155708 (Fix heap overflow in slice::join caused by misbehaving Borrow)
 - #155778 (Avoid Vec allocation in TyCtxt::mk_place_elem)
 - #151014 (std: sys: process: uefi: Add program searching)
 - #155682 (Add boxing suggestions for `impl Trait` return type mismatches)
 - #155770 (Avoid misleading closure return type note)
 - #155818 (Convert attribute `FinalizeFn` to fn pointer)
 - #155829 (rustc_attr_parsing: use a `try {}` in `or_malformed`)
 - #155835 (couple of `crate_name` cleanups)
Comment thread library/alloc/src/rc.rs
#[inline]
fn eq(&self, other: &Rc<T, A>) -> bool {
Rc::ptr_eq(self, other) || **self == **other
ptr::eq(self.ptr.as_ptr(), other.ptr.as_ptr()) || **self == **other
Copy link
Copy Markdown
Contributor

@inkreasing inkreasing Apr 27, 2026

Choose a reason for hiding this comment

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

Is this correct? The docs for ptr::eq state that vtable comparisons are unreliable.

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If the vtables are equal, and the pointer is the same, then it will be the same value, as dereferencing either value will get identical results. If the vtables aren't equal, then we get the slow path, but this is the slow path that always happened anyway before this PR.

@rust-bors rust-bors Bot merged commit 72cd5fb into rust-lang:main Apr 27, 2026
11 checks passed
@rustbot rustbot added this to the 1.97.0 milestone Apr 27, 2026
rust-timer added a commit that referenced this pull request Apr 27, 2026
Rollup merge of #155579 - Gaming32:fix-154998, r=Mark-Simulacrum

Make Rcs and Arcs use pointer comparison for unsized types

`Rc` and `Arc`s have an `Eq` implementation that first attempt to compare the pointers as an optimization. This, however, was not extended to DSTs, which is what this PR fixes.

Fixes #154998.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

MarkerEq doesn't specialize on unsized types

4 participants