Skip to content

Conversation

@aapoalas
Copy link
Contributor

@aapoalas aapoalas commented Aug 30, 2025

Part of #145612: This introduces the CoerceShared trait which is the Reborrow equivalent of a &mut T -> &T coercion. The trait has a Target GAT which makes this (currently) unique in the core/src/marker.rs; I'm not sure if this can be considered problematic. Maybe this is not the way such things should be done at the marker trait level? Or maybe it is fine.

Improtantly, this PR introduces a battery of basic Reborrow and CoerceShared tests. These test the very basics of the feature; custom marker types intended to have exclusive semantics (Custom<'a>(PhantomData<&'a mut ()>)), custom exclusive reference wrappers, and standard library exclusive reference wrappers (Pin<&mut T> and Option<&mut T>). None of these of course work since the implementation for Reborrow and CoerceShared is entirely missing, but this is the first step towards making these work.

Future PRs will introduce more tests, such as "recursive" reborrowing (ie. reborrowing structs that contain multiple reborrowable fields) and checks around the lifetime semantics of reborrowing ie. that a reborrow produces a new type with the same lifetime as the original.

@rustbot
Copy link
Collaborator

rustbot commented Aug 30, 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

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

tmandry commented Aug 31, 2025

The definition I use of a marker trait is a trait with no items, so this would not be a marker trait. (This concept comes up in regards to coherence; it is sound to allow overlapping impls of marker traits, because they don't have items whose definitions can conflict.)

@aapoalas
Copy link
Contributor Author

The definition I use of a marker trait is a trait with no items, so this would not be a marker trait. (This concept comes up in regards to coherence; it is sound to allow overlapping impls of marker traits, because they don't have items whose definitions can conflict.)

Alright; I was kind of just following CoercePointee since the names are similar :D Would this be more appropriate in ops perhaps? Or somewhere else entirely?

@nnethercote
Copy link
Contributor

This looks fine to me from the compiler side, where very little code is added. Any controversial bits seems to be libs/lang related, where I don't have much insight.

I will forward this review on to Tyler who knows more about it. I'm not sure if a library reviewer should also be involved.

r? @tmandry

@rustbot rustbot assigned tmandry and unassigned nnethercote Sep 1, 2025
@aapoalas aapoalas force-pushed the reborrow-lang-experiment branch from fb6acfa to c4a87eb Compare September 15, 2025 17:50
@rustbot
Copy link
Collaborator

rustbot commented Sep 15, 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.

@aapoalas
Copy link
Contributor Author

The definition I use of a marker trait is a trait with no items, so this would not be a marker trait. (This concept comes up in regards to coherence; it is sound to allow overlapping impls of marker traits, because they don't have items whose definitions can conflict.)

Moved the trait to Reborrow as we talked on Thursday.

Comment on lines 10 to 13
//~| NOTE arguments to this function are incorrect
//~| NOTE types differ in mutability
//~| NOTE expected struct `Pin<&()>`
//~| NOTE found struct `Pin<&mut ()>`
Copy link
Member

Choose a reason for hiding this comment

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

This seems like a lot of notes which makes the test more brittle. Maybe only "types differ in mutability" is needed?

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 couldn't remove these; the test indeed does need them to pass.

@tmandry
Copy link
Member

tmandry commented Sep 24, 2025

@bors delegate+

r=me after comments.

@bors
Copy link
Collaborator

bors commented Sep 24, 2025

✌️ @aapoalas, you can now approve this pull request!

If @tmandry told you to "r=me" after making some further change, please make that change, then do @bors r=@tmandry

@aapoalas
Copy link
Contributor Author

@bors r=@tmandry

@bors
Copy link
Collaborator

bors commented Sep 26, 2025

📌 Commit e88fa08 has been approved by tmandry

It is now in the queue for this repository.

@bors bors 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 Sep 26, 2025
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Sep 27, 2025
… r=tmandry

Introduce CoerceShared lang item and trait, and basic Reborrow tests

Part of rust-lang#145612: This introduces the `CoerceShared` trait which is the `Reborrow` equivalent of a `&mut T` -> `&T` coercion. The trait has a `Target` GAT which makes this (currently) unique in the `core/src/marker.rs`; I'm not sure if this can be considered problematic. Maybe this is not the way such things should be done at the marker trait level? Or maybe it is fine.

Improtantly, this PR introduces a battery of basic `Reborrow` and `CoerceShared` tests. These test the very basics of the feature; custom marker types intended to have exclusive semantics (`Custom<'a>(PhantomData<&'a mut ()>)`), custom exclusive reference wrappers, and standard library exclusive reference wrappers (`Pin<&mut T>` and `Option<&mut T>`). None of these of course work since the implementation for `Reborrow` and `CoerceShared` is entirely missing, but this is the first step towards making these work.

Future PRs will introduce more tests, such as "recursive" reborrowing (ie. reborrowing structs that contain multiple reborrowable fields) and checks around the lifetime semantics of reborrowing ie. that a reborrow produces a new type with the same lifetime as the original.
bors added a commit that referenced this pull request Sep 27, 2025
Rollup of 9 pull requests

Successful merges:

 - #140482 (std::net: update tcp deferaccept delay type to Duration.)
 - #146037 (Introduce CoerceShared lang item and trait, and basic Reborrow tests)
 - #146732 (tests: relax expectations after llvm change 902ddda120a5)
 - #147018 (re-order normalizations in run-make linker-warning test)
 - #147032 (Fix doctest compilation time display)
 - #147046 (Rename `rust.use-lld` to `rust.bootstrap-override-lld`)
 - #147050 (PassWrapper: update for new PGOOptions args in LLVM 22)
 - #147075 (Make `def_path_hash_to_def_id` not panic when passed an invalid hash)
 - #147076 (update issue number for more_float_constants)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit that referenced this pull request Sep 27, 2025
Rollup of 8 pull requests

Successful merges:

 - #146037 (Introduce CoerceShared lang item and trait, and basic Reborrow tests)
 - #146732 (tests: relax expectations after llvm change 902ddda120a5)
 - #147018 (re-order normalizations in run-make linker-warning test)
 - #147032 (Fix doctest compilation time display)
 - #147046 (Rename `rust.use-lld` to `rust.bootstrap-override-lld`)
 - #147050 (PassWrapper: update for new PGOOptions args in LLVM 22)
 - #147075 (Make `def_path_hash_to_def_id` not panic when passed an invalid hash)
 - #147076 (update issue number for more_float_constants)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 01c17f8 into rust-lang:master Sep 28, 2025
10 checks passed
@rustbot rustbot added this to the 1.92.0 milestone Sep 28, 2025
rust-timer added a commit that referenced this pull request Sep 28, 2025
Rollup merge of #146037 - aapoalas:reborrow-lang-experiment, r=tmandry

Introduce CoerceShared lang item and trait, and basic Reborrow tests

Part of #145612: This introduces the `CoerceShared` trait which is the `Reborrow` equivalent of a `&mut T` -> `&T` coercion. The trait has a `Target` GAT which makes this (currently) unique in the `core/src/marker.rs`; I'm not sure if this can be considered problematic. Maybe this is not the way such things should be done at the marker trait level? Or maybe it is fine.

Improtantly, this PR introduces a battery of basic `Reborrow` and `CoerceShared` tests. These test the very basics of the feature; custom marker types intended to have exclusive semantics (`Custom<'a>(PhantomData<&'a mut ()>)`), custom exclusive reference wrappers, and standard library exclusive reference wrappers (`Pin<&mut T>` and `Option<&mut T>`). None of these of course work since the implementation for `Reborrow` and `CoerceShared` is entirely missing, but this is the first step towards making these work.

Future PRs will introduce more tests, such as "recursive" reborrowing (ie. reborrowing structs that contain multiple reborrowable fields) and checks around the lifetime semantics of reborrowing ie. that a reborrow produces a new type with the same lifetime as the original.
github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Oct 9, 2025
… r=tmandry

Introduce CoerceShared lang item and trait, and basic Reborrow tests

Part of rust-lang#145612: This introduces the `CoerceShared` trait which is the `Reborrow` equivalent of a `&mut T` -> `&T` coercion. The trait has a `Target` GAT which makes this (currently) unique in the `core/src/marker.rs`; I'm not sure if this can be considered problematic. Maybe this is not the way such things should be done at the marker trait level? Or maybe it is fine.

Improtantly, this PR introduces a battery of basic `Reborrow` and `CoerceShared` tests. These test the very basics of the feature; custom marker types intended to have exclusive semantics (`Custom<'a>(PhantomData<&'a mut ()>)`), custom exclusive reference wrappers, and standard library exclusive reference wrappers (`Pin<&mut T>` and `Option<&mut T>`). None of these of course work since the implementation for `Reborrow` and `CoerceShared` is entirely missing, but this is the first step towards making these work.

Future PRs will introduce more tests, such as "recursive" reborrowing (ie. reborrowing structs that contain multiple reborrowable fields) and checks around the lifetime semantics of reborrowing ie. that a reborrow produces a new type with the same lifetime as the original.
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-compiler Relevant to the compiler team, which will review and decide on the PR/issue. 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.

5 participants