Skip to content

Commit 2bd147a

Browse files
authored
Unrolled build for #146037
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.
2 parents 4082d6a + e88fa08 commit 2bd147a

21 files changed

+254
-11
lines changed

compiler/rustc_hir/src/lang_items.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ language_item_table! {
440440

441441
// Reborrowing related lang-items
442442
Reborrow, sym::reborrow, reborrow, Target::Trait, GenericRequirement::Exact(0);
443+
CoerceShared, sym::coerce_shared, coerce_shared, Target::Trait, GenericRequirement::Exact(0);
443444
}
444445

445446
/// The requirement imposed on the generics of a lang item

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ symbols! {
679679
cmpxchg16b_target_feature,
680680
cmse_nonsecure_entry,
681681
coerce_pointee_validated,
682+
coerce_shared,
682683
coerce_unsized,
683684
cold,
684685
cold_path,

library/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-
}

library/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")]

library/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+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use std::ops::CoerceShared; //~ ERROR use of unstable library feature `reborrow`
2+
3+
fn main() {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0658]: use of unstable library feature `reborrow`
2+
--> $DIR/feature-gate-reborrow-coerce-shared.rs:1:5
3+
|
4+
LL | use std::ops::CoerceShared;
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #145612 <https://github.com/rust-lang/rust/issues/145612> for more information
8+
= help: add `#![feature(reborrow)]` to the crate attributes to enable
9+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10+
11+
error: aborting due to 1 previous error
12+
13+
For more information about this error, try `rustc --explain E0658`.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
use std::marker::Reborrow; //~ ERROR use of unstable library feature `reborrow`
1+
use std::ops::Reborrow; //~ ERROR use of unstable library feature `reborrow`
22

33
fn main() {}

tests/ui/feature-gates/feature-gate-reborrow.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0658]: use of unstable library feature `reborrow`
22
--> $DIR/feature-gate-reborrow.rs:1:5
33
|
4-
LL | use std::marker::Reborrow;
5-
| ^^^^^^^^^^^^^^^^^^^^^
4+
LL | use std::ops::Reborrow;
5+
| ^^^^^^^^^^^^^^^^^^
66
|
77
= note: see issue #145612 <https://github.com/rust-lang/rust/issues/145612> for more information
88
= help: add `#![feature(reborrow)]` to the crate attributes to enable

tests/ui/reborrow/custom_mut.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(reborrow)]
2+
use std::ops::Reborrow;
3+
4+
struct CustomMut<'a, T>(&'a mut T);
5+
impl<'a, T> Reborrow for CustomMut<'a, T> {}
6+
7+
fn method(a: CustomMut<'_, ()>) {}
8+
9+
fn main() {
10+
let a = CustomMut(&mut ());
11+
let _ = method(a);
12+
let _ = method(a); //~ERROR use of moved value: `a`
13+
}

0 commit comments

Comments
 (0)