Skip to content

Commit 05eec30

Browse files
authored
Rollup merge of rust-lang#146037 - aapoalas:reborrow-lang-experiment, 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.
2 parents 898ea47 + 1b63e7d commit 05eec30

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

core/src/marker.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,11 +1341,3 @@ pub macro CoercePointee($item:item) {
13411341
pub trait CoercePointeeValidated {
13421342
/* compiler built-in */
13431343
}
1344-
1345-
/// Allows value to be reborrowed as exclusive, creating a copy of the value
1346-
/// that disables the source for reads and writes for the lifetime of the copy.
1347-
#[lang = "reborrow"]
1348-
#[unstable(feature = "reborrow", issue = "145612")]
1349-
pub trait Reborrow {
1350-
// Empty.
1351-
}

core/src/ops/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ mod function;
149149
mod index;
150150
mod index_range;
151151
mod range;
152+
mod reborrow;
152153
mod try_trait;
153154
mod unsize;
154155

@@ -189,6 +190,8 @@ pub use self::range::{Bound, RangeBounds, RangeInclusive, RangeToInclusive};
189190
pub use self::range::{OneSidedRange, OneSidedRangeBound};
190191
#[stable(feature = "rust1", since = "1.0.0")]
191192
pub use self::range::{Range, RangeFrom, RangeFull, RangeTo};
193+
#[unstable(feature = "reborrow", issue = "145612")]
194+
pub use self::reborrow::{CoerceShared, Reborrow};
192195
#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
193196
pub use self::try_trait::Residual;
194197
#[unstable(feature = "try_trait_v2_yeet", issue = "96374")]

core/src/ops/reborrow.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// Allows value to be reborrowed as exclusive, creating a copy of the value
2+
/// that disables the source for reads and writes for the lifetime of the copy.
3+
#[lang = "reborrow"]
4+
#[unstable(feature = "reborrow", issue = "145612")]
5+
pub trait Reborrow {
6+
// Empty.
7+
}
8+
9+
/// Allows reborrowable value to be reborrowed as shared, creating a copy
10+
/// that disables the source for writes for the lifetime of the copy.
11+
#[lang = "coerce_shared"]
12+
#[unstable(feature = "reborrow", issue = "145612")]
13+
pub trait CoerceShared: Reborrow {
14+
/// The type of this value when reborrowed as shared.
15+
type Target: Copy;
16+
}

0 commit comments

Comments
 (0)