Skip to content

Implement coercions between &pin (mut|const) T and &(mut) T when T: Unpin#149130

Open
frank-king wants to merge 2 commits intorust-lang:mainfrom
frank-king:feature/pin-coerce
Open

Implement coercions between &pin (mut|const) T and &(mut) T when T: Unpin#149130
frank-king wants to merge 2 commits intorust-lang:mainfrom
frank-king:feature/pin-coerce

Conversation

@frank-king
Copy link
Contributor

@frank-king frank-king commented Nov 20, 2025

This allows the following (mutual) coercions when T: Unpin:

  • &T <-> Pin<&T>
  • &mut T <-> Pin<&mut T>
  • &mut T -> Pin<&T>
  • Pin<&mut T> -> &T

Part of Pin Ergonomics.

@rustbot rustbot added 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. labels Nov 20, 2025
@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Nov 20, 2025
@frank-king frank-king marked this pull request as ready for review November 20, 2025 11:57
@rustbot
Copy link
Collaborator

rustbot commented Nov 20, 2025

Some changes occurred in need_type_info.rs

cc @lcnr

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred in exhaustiveness checking

cc @Nadrieril

@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 Nov 20, 2025
@rustbot
Copy link
Collaborator

rustbot commented Nov 20, 2025

r? @nnethercote

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

@nnethercote
Copy link
Contributor

I am not a good person to review this. How about...

r? @lcnr

@rustbot rustbot assigned lcnr and unassigned nnethercote Nov 21, 2025
@lcnr
Copy link
Contributor

lcnr commented Nov 24, 2025

r? types

@rustbot rustbot added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Nov 24, 2025
@rustbot rustbot assigned spastorino and unassigned lcnr Nov 24, 2025
@spastorino
Copy link
Member

r? types

@rustbot rustbot assigned jackh726 and unassigned spastorino Nov 24, 2025
@bors
Copy link
Collaborator

bors commented Dec 5, 2025

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

@rustbot

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Dec 18, 2025

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

Copy link
Member

@jackh726 jackh726 left a comment

Choose a reason for hiding this comment

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

Incredibly sorry about the review latency here.

Overall, the changes look not bad. That being said, there are two very mechanical changes that would be useful to split into a separate PR (or really, a separate commit is fine, but I prefer a separate PR since they stand alone well).

View changes since this review

@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 Jan 23, 2026
@rustbot
Copy link
Collaborator

rustbot commented Jan 23, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot

This comment has been minimized.

@frank-king
Copy link
Contributor Author

frank-king commented Jan 30, 2026

I've split the two refactors to separated PRs:

and a post-refactor PR #151932 (remove Adjust::ReborrowPin)

@rustbot ready

@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 Jan 30, 2026
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 31, 2026
…r=jackh726

refactor: remove `Ty::pinned_ref` in favor of `Ty::maybe_pinned_ref`

Also returns the `Region` of the reference type in `Ty::maybe_pinned_Ref`.

Part of rust-lang#149130.

r? jackh726
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Jan 31, 2026
…r=jackh726

refactor: add an `enum DerefAdjustKind` in favor of `Option<OverloadedDeref>`

Part of rust-lang#149130.

r? jackh726
rust-timer added a commit that referenced this pull request Jan 31, 2026
Rollup merge of #151849 - frank-king:refactor/pin-coerce-1, r=jackh726

refactor: remove `Ty::pinned_ref` in favor of `Ty::maybe_pinned_ref`

Also returns the `Region` of the reference type in `Ty::maybe_pinned_Ref`.

Part of #149130.

r? jackh726
@rust-bors

This comment has been minimized.

rust-timer added a commit that referenced this pull request Jan 31, 2026
Rollup merge of #151850 - frank-king:refactor/pin-coerce-2, r=jackh726

refactor: add an `enum DerefAdjustKind` in favor of `Option<OverloadedDeref>`

Part of #149130.

r? jackh726
@rustbot
Copy link
Collaborator

rustbot commented Feb 1, 2026

This PR was rebased onto a different main 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.

Comment on lines 272 to 277
if self.tcx.features().pin_ergonomics()
&& a.pinned_ty().is_some_and(|ty| ty.is_ref())
&& let Ok(coerce) = self.commit_if_ok(|_| self.coerce_maybe_pinned_ref(a, b))
{
return Ok(coerce);
}
Copy link
Member

Choose a reason for hiding this comment

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

I am sort of thinking that this code (and the lines added under the ty::Adt arm, should go in coerce_to_ref and coerce_to_pin_ref respectively - keeping this function relatively clean.

Copy link
Member

Choose a reason for hiding this comment

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

This also would help to eliminate some span_bugs from coerce_maybe_pinned_ref

Comment on lines 867 to 872
let Some((a_ty, a_pinnedness, a_mutbl, a_region)) = a.maybe_pinned_ref() else {
span_bug!(span, "expect pinned reference or reference, found {:?}", a);
};
let Some((_b_ty, b_pinnedness, b_mutbl, _b_region)) = b.maybe_pinned_ref() else {
span_bug!(span, "expect pinned reference or reference, found {:?}", b);
};
Copy link
Member

Choose a reason for hiding this comment

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

Something feels a bit off here to me...I like have one function that covers "all the pinned ref" coercions - but simultaneously all the span_bugs here scream this function being ripe for getting misused or logic changing elsewhere but not updated here.

I sort of wonder if everything before let mut coerce = self.unify_and needs to be done in the calling functions, and necessarily bits get passed in.

Copy link
Contributor 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 if I fully get your ideas. I removed the coerce_maybe_pinned_ref and split it into coerce_to_pin_ref and coerce_pin_ref_to_ref.

Theoretically, coerce_pin_ref_to_ref can be merged into coerce_to_ref, but I have no idea how to add an autoref before the autoderef (I mean, the Autoderef iterator handles &mut Pin<&mut T> to &mut T and &Pin<&T> to &T well, but for Pin<&mut T> and Pin<&T>, there need to be an autoref at the beginning). I'm also not sure if doing that would mess up the existing code.

Copy link
Member

Choose a reason for hiding this comment

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

Skimming over, I think your changes look better, but I'm noticing there are test changes, so there are semantic changes i need to think about. Will do a proper review this week.

@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 Feb 1, 2026
Use `coerce_to_pin_ref` and `coerce_pin_ref_to_ref` instead.
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-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants