From c8663eec6a667ae6e7571297cb15192425311c33 Mon Sep 17 00:00:00 2001 From: Aapo Alasuutari Date: Fri, 29 Aug 2025 23:29:46 +0300 Subject: [PATCH 1/5] Introduce CoerceShared lang item and trait --- compiler/rustc_hir/src/lang_items.rs | 1 + compiler/rustc_span/src/symbol.rs | 1 + library/core/src/marker.rs | 9 +++++++++ 3 files changed, 11 insertions(+) diff --git a/compiler/rustc_hir/src/lang_items.rs b/compiler/rustc_hir/src/lang_items.rs index 67d2f15d41472..889b16a7a2bb6 100644 --- a/compiler/rustc_hir/src/lang_items.rs +++ b/compiler/rustc_hir/src/lang_items.rs @@ -441,6 +441,7 @@ language_item_table! { // Reborrowing related lang-items Reborrow, sym::reborrow, reborrow, Target::Trait, GenericRequirement::Exact(0); + CoerceShared, sym::coerce_shared, coerce_shared, Target::Trait, GenericRequirement::Exact(0); } /// The requirement imposed on the generics of a lang item diff --git a/compiler/rustc_span/src/symbol.rs b/compiler/rustc_span/src/symbol.rs index cdb0b5b58da6d..882abf7bcc977 100644 --- a/compiler/rustc_span/src/symbol.rs +++ b/compiler/rustc_span/src/symbol.rs @@ -679,6 +679,7 @@ symbols! { cmpxchg16b_target_feature, cmse_nonsecure_entry, coerce_pointee_validated, + coerce_shared, coerce_unsized, cold, cold_path, diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index d03d7a43469a7..8541a5b91cdde 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -1372,3 +1372,12 @@ pub trait CoercePointeeValidated { pub trait Reborrow { // Empty. } + +/// Allows reborrowable value to be reborrowed as shared, creating a copy of +/// that disables the source for writes for the lifetime of the copy. +#[lang = "coerce_shared"] +#[unstable(feature = "reborrow", issue = "145612")] +pub trait CoerceShared: Reborrow { + /// The type of this value when reborrowed as shared. + type Target: Copy; +} From fce8c13f77bcbebfea4461536be828d7136b8c1a Mon Sep 17 00:00:00 2001 From: Aapo Alasuutari Date: Fri, 29 Aug 2025 23:35:07 +0300 Subject: [PATCH 2/5] Add reborrow CoerceShared feature gate test --- .../feature-gate-reborrow-coerce-shared.rs | 3 +++ .../feature-gate-reborrow-coerce-shared.stderr | 13 +++++++++++++ 2 files changed, 16 insertions(+) create mode 100644 tests/ui/feature-gates/feature-gate-reborrow-coerce-shared.rs create mode 100644 tests/ui/feature-gates/feature-gate-reborrow-coerce-shared.stderr diff --git a/tests/ui/feature-gates/feature-gate-reborrow-coerce-shared.rs b/tests/ui/feature-gates/feature-gate-reborrow-coerce-shared.rs new file mode 100644 index 0000000000000..48a14959d8d64 --- /dev/null +++ b/tests/ui/feature-gates/feature-gate-reborrow-coerce-shared.rs @@ -0,0 +1,3 @@ +use std::marker::CoerceShared; //~ ERROR use of unstable library feature `reborrow` + +fn main() {} diff --git a/tests/ui/feature-gates/feature-gate-reborrow-coerce-shared.stderr b/tests/ui/feature-gates/feature-gate-reborrow-coerce-shared.stderr new file mode 100644 index 0000000000000..c4c5e06778af3 --- /dev/null +++ b/tests/ui/feature-gates/feature-gate-reborrow-coerce-shared.stderr @@ -0,0 +1,13 @@ +error[E0658]: use of unstable library feature `reborrow` + --> $DIR/feature-gate-reborrow-coerce-shared.rs:1:5 + | +LL | use std::marker::CoerceShared; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #145612 for more information + = help: add `#![feature(reborrow)]` to the crate attributes to enable + = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0658`. From 35bae9df1dfb164de7be95180a0982312128d559 Mon Sep 17 00:00:00 2001 From: Aapo Alasuutari Date: Sat, 30 Aug 2025 21:16:56 +0300 Subject: [PATCH 3/5] Introduce basic Reborrow tests --- tests/ui/reborrow/custom_mut.rs | 13 +++++++++ tests/ui/reborrow/custom_mut.stderr | 29 +++++++++++++++++++ tests/ui/reborrow/custom_mut_coerce_shared.rs | 28 ++++++++++++++++++ .../reborrow/custom_mut_coerce_shared.stderr | 19 ++++++++++++ tests/ui/reborrow/option_mut.rs | 7 +++++ tests/ui/reborrow/option_mut.stderr | 21 ++++++++++++++ tests/ui/reborrow/option_mut_coerce_shared.rs | 11 +++++++ .../reborrow/option_mut_coerce_shared.stderr | 23 +++++++++++++++ tests/ui/reborrow/pin_mut.rs | 10 +++++++ tests/ui/reborrow/pin_mut.stderr | 21 ++++++++++++++ tests/ui/reborrow/pin_mut_coerce_shared.rs | 14 +++++++++ .../ui/reborrow/pin_mut_coerce_shared.stderr | 19 ++++++++++++ 12 files changed, 215 insertions(+) create mode 100644 tests/ui/reborrow/custom_mut.rs create mode 100644 tests/ui/reborrow/custom_mut.stderr create mode 100644 tests/ui/reborrow/custom_mut_coerce_shared.rs create mode 100644 tests/ui/reborrow/custom_mut_coerce_shared.stderr create mode 100644 tests/ui/reborrow/option_mut.rs create mode 100644 tests/ui/reborrow/option_mut.stderr create mode 100644 tests/ui/reborrow/option_mut_coerce_shared.rs create mode 100644 tests/ui/reborrow/option_mut_coerce_shared.stderr create mode 100644 tests/ui/reborrow/pin_mut.rs create mode 100644 tests/ui/reborrow/pin_mut.stderr create mode 100644 tests/ui/reborrow/pin_mut_coerce_shared.rs create mode 100644 tests/ui/reborrow/pin_mut_coerce_shared.stderr diff --git a/tests/ui/reborrow/custom_mut.rs b/tests/ui/reborrow/custom_mut.rs new file mode 100644 index 0000000000000..b55a5e6faa3dc --- /dev/null +++ b/tests/ui/reborrow/custom_mut.rs @@ -0,0 +1,13 @@ +#![feature(reborrow)] +use std::marker::Reborrow; + +struct CustomMut<'a, T>(&'a mut T); +impl<'a, T> Reborrow for CustomMut<'a, T> {} + +fn method(a: CustomMut<'_, ()>) {} + +fn main() { + let a = CustomMut(&mut ()); + let _ = method(a); + let _ = method(a); //~ERROR use of moved value: `a` +} diff --git a/tests/ui/reborrow/custom_mut.stderr b/tests/ui/reborrow/custom_mut.stderr new file mode 100644 index 0000000000000..3b3f47b62d6fa --- /dev/null +++ b/tests/ui/reborrow/custom_mut.stderr @@ -0,0 +1,29 @@ +error[E0382]: use of moved value: `a` + --> $DIR/custom_mut.rs:12:20 + | +LL | let a = CustomMut(&mut ()); + | - move occurs because `a` has type `CustomMut<'_, ()>`, which does not implement the `Copy` trait +LL | let _ = method(a); + | - value moved here +LL | let _ = method(a); + | ^ value used here after move + | +note: consider changing this parameter type in function `method` to borrow instead if owning the value isn't necessary + --> $DIR/custom_mut.rs:7:14 + | +LL | fn method(a: CustomMut<'_, ()>) {} + | ------ ^^^^^^^^^^^^^^^^^ this parameter takes ownership of the value + | | + | in this function +note: if `CustomMut<'_, ()>` implemented `Clone`, you could clone the value + --> $DIR/custom_mut.rs:4:1 + | +LL | struct CustomMut<'a, T>(&'a mut T); + | ^^^^^^^^^^^^^^^^^^^^^^^ consider implementing `Clone` for this type +... +LL | let _ = method(a); + | - you could clone this value + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0382`. diff --git a/tests/ui/reborrow/custom_mut_coerce_shared.rs b/tests/ui/reborrow/custom_mut_coerce_shared.rs new file mode 100644 index 0000000000000..b292b5b073ce8 --- /dev/null +++ b/tests/ui/reborrow/custom_mut_coerce_shared.rs @@ -0,0 +1,28 @@ +#![feature(reborrow)] +use std::marker::{Reborrow, CoerceShared}; + +struct CustomMut<'a, T>(&'a mut T); +impl<'a, T> Reborrow for CustomMut<'a, T> {} +impl<'a, T> CoerceShared for CustomMut<'a, T> { + type Target = CustomRef<'a, T>; +} + +struct CustomRef<'a, T>(&'a T); + +impl<'a, T> Clone for CustomRef<'a, T> { + fn clone(&self) -> Self { + Self(self.0) + } +} +impl<'a, T> Copy for CustomRef<'a, T> {} + +fn method(a: CustomRef<'_, ()>) {} //~NOTE function defined here + +fn main() { + let a = CustomMut(&mut ()); + method(a); + //~^ ERROR mismatched types + //~| NOTE expected `CustomRef<'_, ()>`, found `CustomMut<'_, ()>` + //~| NOTE arguments to this function are incorrect + //~| NOTE expected struct `CustomRef<'_, ()>` +} diff --git a/tests/ui/reborrow/custom_mut_coerce_shared.stderr b/tests/ui/reborrow/custom_mut_coerce_shared.stderr new file mode 100644 index 0000000000000..508651badc0a4 --- /dev/null +++ b/tests/ui/reborrow/custom_mut_coerce_shared.stderr @@ -0,0 +1,19 @@ +error[E0308]: mismatched types + --> $DIR/custom_mut_coerce_shared.rs:23:12 + | +LL | method(a); + | ------ ^ expected `CustomRef<'_, ()>`, found `CustomMut<'_, ()>` + | | + | arguments to this function are incorrect + | + = note: expected struct `CustomRef<'_, ()>` + found struct `CustomMut<'_, ()>` +note: function defined here + --> $DIR/custom_mut_coerce_shared.rs:19:4 + | +LL | fn method(a: CustomRef<'_, ()>) {} + | ^^^^^^ -------------------- + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/reborrow/option_mut.rs b/tests/ui/reborrow/option_mut.rs new file mode 100644 index 0000000000000..60b7145a7b2e3 --- /dev/null +++ b/tests/ui/reborrow/option_mut.rs @@ -0,0 +1,7 @@ +fn method(a: Option<& mut ()>) {} + +fn main() { + let a = Some(&mut ()); + let _ = method(a); + let _ = method(a); //~ERROR use of moved value: `a` +} diff --git a/tests/ui/reborrow/option_mut.stderr b/tests/ui/reborrow/option_mut.stderr new file mode 100644 index 0000000000000..497319d2e9035 --- /dev/null +++ b/tests/ui/reborrow/option_mut.stderr @@ -0,0 +1,21 @@ +error[E0382]: use of moved value: `a` + --> $DIR/option_mut.rs:6:20 + | +LL | let a = Some(&mut ()); + | - move occurs because `a` has type `Option<&mut ()>`, which does not implement the `Copy` trait +LL | let _ = method(a); + | - value moved here +LL | let _ = method(a); + | ^ value used here after move + | +note: consider changing this parameter type in function `method` to borrow instead if owning the value isn't necessary + --> $DIR/option_mut.rs:1:14 + | +LL | fn method(a: Option<& mut ()>) {} + | ------ ^^^^^^^^^^^^^^^^ this parameter takes ownership of the value + | | + | in this function + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0382`. diff --git a/tests/ui/reborrow/option_mut_coerce_shared.rs b/tests/ui/reborrow/option_mut_coerce_shared.rs new file mode 100644 index 0000000000000..95d33ed94dd71 --- /dev/null +++ b/tests/ui/reborrow/option_mut_coerce_shared.rs @@ -0,0 +1,11 @@ +fn method(a: Option<&()>) {} //~NOTE function defined here + +fn main() { + let a = Some(&mut ()); + method(a); + //~^ ERROR mismatched types + //~| NOTE arguments to this function are incorrect + //~| NOTE types differ in mutability + //~| NOTE expected enum `Option<&()>` + //~| NOTE found enum `Option<&mut ()>` +} diff --git a/tests/ui/reborrow/option_mut_coerce_shared.stderr b/tests/ui/reborrow/option_mut_coerce_shared.stderr new file mode 100644 index 0000000000000..6ca1a2374610e --- /dev/null +++ b/tests/ui/reborrow/option_mut_coerce_shared.stderr @@ -0,0 +1,23 @@ +error[E0308]: mismatched types + --> $DIR/option_mut_coerce_shared.rs:5:12 + | +LL | method(a); + | ------ ^ types differ in mutability + | | + | arguments to this function are incorrect + | + = note: expected enum `Option<&()>` + found enum `Option<&mut ()>` +note: function defined here + --> $DIR/option_mut_coerce_shared.rs:1:4 + | +LL | fn method(a: Option<&()>) {} + | ^^^^^^ -------------- +help: try using `.as_deref()` to convert `Option<&mut ()>` to `Option<&()>` + | +LL | method(a.as_deref()); + | +++++++++++ + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0308`. diff --git a/tests/ui/reborrow/pin_mut.rs b/tests/ui/reborrow/pin_mut.rs new file mode 100644 index 0000000000000..966106969943a --- /dev/null +++ b/tests/ui/reborrow/pin_mut.rs @@ -0,0 +1,10 @@ +use std::pin::Pin; + +fn method(a: Pin<& mut ()>) {} + +fn main() { + let a = &mut (); + let a = Pin::new(a); + let _ = method(a); + let _ = method(a); //~ERROR use of moved value: `a` +} diff --git a/tests/ui/reborrow/pin_mut.stderr b/tests/ui/reborrow/pin_mut.stderr new file mode 100644 index 0000000000000..8a10e2d9af269 --- /dev/null +++ b/tests/ui/reborrow/pin_mut.stderr @@ -0,0 +1,21 @@ +error[E0382]: use of moved value: `a` + --> $DIR/pin_mut.rs:9:20 + | +LL | let a = Pin::new(a); + | - move occurs because `a` has type `Pin<&mut ()>`, which does not implement the `Copy` trait +LL | let _ = method(a); + | - value moved here +LL | let _ = method(a); + | ^ value used here after move + | +note: consider changing this parameter type in function `method` to borrow instead if owning the value isn't necessary + --> $DIR/pin_mut.rs:3:14 + | +LL | fn method(a: Pin<& mut ()>) {} + | ------ ^^^^^^^^^^^^^ this parameter takes ownership of the value + | | + | in this function + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0382`. diff --git a/tests/ui/reborrow/pin_mut_coerce_shared.rs b/tests/ui/reborrow/pin_mut_coerce_shared.rs new file mode 100644 index 0000000000000..fd529195fe01e --- /dev/null +++ b/tests/ui/reborrow/pin_mut_coerce_shared.rs @@ -0,0 +1,14 @@ +use std::pin::Pin; + +fn method(a: Pin<&()>) {} //~NOTE function defined here + +fn main() { + let a = &mut (); + let a = Pin::new(a); + method(a); + //~^ ERROR mismatched types + //~| NOTE arguments to this function are incorrect + //~| NOTE types differ in mutability + //~| NOTE expected struct `Pin<&()>` + //~| NOTE found struct `Pin<&mut ()>` +} diff --git a/tests/ui/reborrow/pin_mut_coerce_shared.stderr b/tests/ui/reborrow/pin_mut_coerce_shared.stderr new file mode 100644 index 0000000000000..74ecf4de4c785 --- /dev/null +++ b/tests/ui/reborrow/pin_mut_coerce_shared.stderr @@ -0,0 +1,19 @@ +error[E0308]: mismatched types + --> $DIR/pin_mut_coerce_shared.rs:8:12 + | +LL | method(a); + | ------ ^ types differ in mutability + | | + | arguments to this function are incorrect + | + = note: expected struct `Pin<&()>` + found struct `Pin<&mut ()>` +note: function defined here + --> $DIR/pin_mut_coerce_shared.rs:3:4 + | +LL | fn method(a: Pin<&()>) {} + | ^^^^^^ ----------- + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0308`. From c4a87eb62cd74e31e5d3889741a76a4bb2a48ed6 Mon Sep 17 00:00:00 2001 From: Aapo Alasuutari Date: Mon, 15 Sep 2025 20:32:06 +0300 Subject: [PATCH 4/5] fix: Move CoerceShared into ops --- library/core/src/marker.rs | 9 --------- library/core/src/ops/mod.rs | 3 +++ library/core/src/ops/reborrow.rs | 10 ++++++++++ .../feature-gate-reborrow-coerce-shared.rs | 2 +- .../feature-gate-reborrow-coerce-shared.stderr | 4 ++-- tests/ui/reborrow/custom_mut_coerce_shared.rs | 3 ++- tests/ui/reborrow/custom_mut_coerce_shared.stderr | 4 ++-- 7 files changed, 20 insertions(+), 15 deletions(-) create mode 100644 library/core/src/ops/reborrow.rs diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index 8541a5b91cdde..d03d7a43469a7 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -1372,12 +1372,3 @@ pub trait CoercePointeeValidated { pub trait Reborrow { // Empty. } - -/// Allows reborrowable value to be reborrowed as shared, creating a copy of -/// that disables the source for writes for the lifetime of the copy. -#[lang = "coerce_shared"] -#[unstable(feature = "reborrow", issue = "145612")] -pub trait CoerceShared: Reborrow { - /// The type of this value when reborrowed as shared. - type Target: Copy; -} diff --git a/library/core/src/ops/mod.rs b/library/core/src/ops/mod.rs index 87dd873fdb57d..9814f5d5795c6 100644 --- a/library/core/src/ops/mod.rs +++ b/library/core/src/ops/mod.rs @@ -149,6 +149,7 @@ mod function; mod index; mod index_range; mod range; +mod reborrow; mod try_trait; mod unsize; @@ -189,6 +190,8 @@ pub use self::range::{Bound, RangeBounds, RangeInclusive, RangeToInclusive}; pub use self::range::{OneSidedRange, OneSidedRangeBound}; #[stable(feature = "rust1", since = "1.0.0")] pub use self::range::{Range, RangeFrom, RangeFull, RangeTo}; +#[unstable(feature = "reborrow", issue = "145612")] +pub use self::reborrow::CoerceShared; #[unstable(feature = "try_trait_v2_residual", issue = "91285")] pub use self::try_trait::Residual; #[unstable(feature = "try_trait_v2_yeet", issue = "96374")] diff --git a/library/core/src/ops/reborrow.rs b/library/core/src/ops/reborrow.rs new file mode 100644 index 0000000000000..90288f766d502 --- /dev/null +++ b/library/core/src/ops/reborrow.rs @@ -0,0 +1,10 @@ +use crate::marker::Reborrow; + +/// Allows reborrowable value to be reborrowed as shared, creating a copy +/// that disables the source for writes for the lifetime of the copy. +#[lang = "coerce_shared"] +#[unstable(feature = "reborrow", issue = "145612")] +pub trait CoerceShared: Reborrow { + /// The type of this value when reborrowed as shared. + type Target: Copy; +} diff --git a/tests/ui/feature-gates/feature-gate-reborrow-coerce-shared.rs b/tests/ui/feature-gates/feature-gate-reborrow-coerce-shared.rs index 48a14959d8d64..c8ca453708912 100644 --- a/tests/ui/feature-gates/feature-gate-reborrow-coerce-shared.rs +++ b/tests/ui/feature-gates/feature-gate-reborrow-coerce-shared.rs @@ -1,3 +1,3 @@ -use std::marker::CoerceShared; //~ ERROR use of unstable library feature `reborrow` +use std::ops::CoerceShared; //~ ERROR use of unstable library feature `reborrow` fn main() {} diff --git a/tests/ui/feature-gates/feature-gate-reborrow-coerce-shared.stderr b/tests/ui/feature-gates/feature-gate-reborrow-coerce-shared.stderr index c4c5e06778af3..dbbbcdf2fd57d 100644 --- a/tests/ui/feature-gates/feature-gate-reborrow-coerce-shared.stderr +++ b/tests/ui/feature-gates/feature-gate-reborrow-coerce-shared.stderr @@ -1,8 +1,8 @@ error[E0658]: use of unstable library feature `reborrow` --> $DIR/feature-gate-reborrow-coerce-shared.rs:1:5 | -LL | use std::marker::CoerceShared; - | ^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | use std::ops::CoerceShared; + | ^^^^^^^^^^^^^^^^^^^^^^ | = note: see issue #145612 for more information = help: add `#![feature(reborrow)]` to the crate attributes to enable diff --git a/tests/ui/reborrow/custom_mut_coerce_shared.rs b/tests/ui/reborrow/custom_mut_coerce_shared.rs index b292b5b073ce8..28d802aabffe5 100644 --- a/tests/ui/reborrow/custom_mut_coerce_shared.rs +++ b/tests/ui/reborrow/custom_mut_coerce_shared.rs @@ -1,5 +1,6 @@ #![feature(reborrow)] -use std::marker::{Reborrow, CoerceShared}; +use std::marker::Reborrow; +use std::ops::CoerceShared; struct CustomMut<'a, T>(&'a mut T); impl<'a, T> Reborrow for CustomMut<'a, T> {} diff --git a/tests/ui/reborrow/custom_mut_coerce_shared.stderr b/tests/ui/reborrow/custom_mut_coerce_shared.stderr index 508651badc0a4..90d8a98160543 100644 --- a/tests/ui/reborrow/custom_mut_coerce_shared.stderr +++ b/tests/ui/reborrow/custom_mut_coerce_shared.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/custom_mut_coerce_shared.rs:23:12 + --> $DIR/custom_mut_coerce_shared.rs:24:12 | LL | method(a); | ------ ^ expected `CustomRef<'_, ()>`, found `CustomMut<'_, ()>` @@ -9,7 +9,7 @@ LL | method(a); = note: expected struct `CustomRef<'_, ()>` found struct `CustomMut<'_, ()>` note: function defined here - --> $DIR/custom_mut_coerce_shared.rs:19:4 + --> $DIR/custom_mut_coerce_shared.rs:20:4 | LL | fn method(a: CustomRef<'_, ()>) {} | ^^^^^^ -------------------- From e88fa086fb597041a207a9bb5df0654628d57b1a Mon Sep 17 00:00:00 2001 From: Aapo Alasuutari Date: Sat, 27 Sep 2025 01:11:01 +0300 Subject: [PATCH 5/5] move Reborrow to ops, fix fmt issues --- library/core/src/marker.rs | 8 -------- library/core/src/ops/mod.rs | 2 +- library/core/src/ops/reborrow.rs | 8 +++++++- tests/ui/feature-gates/feature-gate-reborrow.rs | 2 +- tests/ui/feature-gates/feature-gate-reborrow.stderr | 4 ++-- tests/ui/reborrow/custom_mut.rs | 2 +- tests/ui/reborrow/custom_mut_coerce_shared.rs | 3 +-- tests/ui/reborrow/custom_mut_coerce_shared.stderr | 4 ++-- tests/ui/reborrow/option_mut.rs | 2 +- tests/ui/reborrow/option_mut.stderr | 4 ++-- tests/ui/reborrow/pin_mut.rs | 2 +- tests/ui/reborrow/pin_mut.stderr | 4 ++-- tests/ui/reborrow/pin_mut_coerce_shared.rs | 1 - 13 files changed, 21 insertions(+), 25 deletions(-) diff --git a/library/core/src/marker.rs b/library/core/src/marker.rs index d03d7a43469a7..fc715207d5dad 100644 --- a/library/core/src/marker.rs +++ b/library/core/src/marker.rs @@ -1364,11 +1364,3 @@ pub macro CoercePointee($item:item) { pub trait CoercePointeeValidated { /* compiler built-in */ } - -/// Allows value to be reborrowed as exclusive, creating a copy of the value -/// that disables the source for reads and writes for the lifetime of the copy. -#[lang = "reborrow"] -#[unstable(feature = "reborrow", issue = "145612")] -pub trait Reborrow { - // Empty. -} diff --git a/library/core/src/ops/mod.rs b/library/core/src/ops/mod.rs index 9814f5d5795c6..ab1ad407ee282 100644 --- a/library/core/src/ops/mod.rs +++ b/library/core/src/ops/mod.rs @@ -191,7 +191,7 @@ pub use self::range::{OneSidedRange, OneSidedRangeBound}; #[stable(feature = "rust1", since = "1.0.0")] pub use self::range::{Range, RangeFrom, RangeFull, RangeTo}; #[unstable(feature = "reborrow", issue = "145612")] -pub use self::reborrow::CoerceShared; +pub use self::reborrow::{CoerceShared, Reborrow}; #[unstable(feature = "try_trait_v2_residual", issue = "91285")] pub use self::try_trait::Residual; #[unstable(feature = "try_trait_v2_yeet", issue = "96374")] diff --git a/library/core/src/ops/reborrow.rs b/library/core/src/ops/reborrow.rs index 90288f766d502..f83f4233a4de5 100644 --- a/library/core/src/ops/reborrow.rs +++ b/library/core/src/ops/reborrow.rs @@ -1,4 +1,10 @@ -use crate::marker::Reborrow; +/// Allows value to be reborrowed as exclusive, creating a copy of the value +/// that disables the source for reads and writes for the lifetime of the copy. +#[lang = "reborrow"] +#[unstable(feature = "reborrow", issue = "145612")] +pub trait Reborrow { + // Empty. +} /// Allows reborrowable value to be reborrowed as shared, creating a copy /// that disables the source for writes for the lifetime of the copy. diff --git a/tests/ui/feature-gates/feature-gate-reborrow.rs b/tests/ui/feature-gates/feature-gate-reborrow.rs index f016f6c6bfa59..96eecfb28a106 100644 --- a/tests/ui/feature-gates/feature-gate-reborrow.rs +++ b/tests/ui/feature-gates/feature-gate-reborrow.rs @@ -1,3 +1,3 @@ -use std::marker::Reborrow; //~ ERROR use of unstable library feature `reborrow` +use std::ops::Reborrow; //~ ERROR use of unstable library feature `reborrow` fn main() {} diff --git a/tests/ui/feature-gates/feature-gate-reborrow.stderr b/tests/ui/feature-gates/feature-gate-reborrow.stderr index 5e3033f3bf1fe..1224909f564bc 100644 --- a/tests/ui/feature-gates/feature-gate-reborrow.stderr +++ b/tests/ui/feature-gates/feature-gate-reborrow.stderr @@ -1,8 +1,8 @@ error[E0658]: use of unstable library feature `reborrow` --> $DIR/feature-gate-reborrow.rs:1:5 | -LL | use std::marker::Reborrow; - | ^^^^^^^^^^^^^^^^^^^^^ +LL | use std::ops::Reborrow; + | ^^^^^^^^^^^^^^^^^^ | = note: see issue #145612 for more information = help: add `#![feature(reborrow)]` to the crate attributes to enable diff --git a/tests/ui/reborrow/custom_mut.rs b/tests/ui/reborrow/custom_mut.rs index b55a5e6faa3dc..1e7c469323822 100644 --- a/tests/ui/reborrow/custom_mut.rs +++ b/tests/ui/reborrow/custom_mut.rs @@ -1,5 +1,5 @@ #![feature(reborrow)] -use std::marker::Reborrow; +use std::ops::Reborrow; struct CustomMut<'a, T>(&'a mut T); impl<'a, T> Reborrow for CustomMut<'a, T> {} diff --git a/tests/ui/reborrow/custom_mut_coerce_shared.rs b/tests/ui/reborrow/custom_mut_coerce_shared.rs index 28d802aabffe5..e2d25835c093a 100644 --- a/tests/ui/reborrow/custom_mut_coerce_shared.rs +++ b/tests/ui/reborrow/custom_mut_coerce_shared.rs @@ -1,6 +1,5 @@ #![feature(reborrow)] -use std::marker::Reborrow; -use std::ops::CoerceShared; +use std::ops::{CoerceShared, Reborrow}; struct CustomMut<'a, T>(&'a mut T); impl<'a, T> Reborrow for CustomMut<'a, T> {} diff --git a/tests/ui/reborrow/custom_mut_coerce_shared.stderr b/tests/ui/reborrow/custom_mut_coerce_shared.stderr index 90d8a98160543..508651badc0a4 100644 --- a/tests/ui/reborrow/custom_mut_coerce_shared.stderr +++ b/tests/ui/reborrow/custom_mut_coerce_shared.stderr @@ -1,5 +1,5 @@ error[E0308]: mismatched types - --> $DIR/custom_mut_coerce_shared.rs:24:12 + --> $DIR/custom_mut_coerce_shared.rs:23:12 | LL | method(a); | ------ ^ expected `CustomRef<'_, ()>`, found `CustomMut<'_, ()>` @@ -9,7 +9,7 @@ LL | method(a); = note: expected struct `CustomRef<'_, ()>` found struct `CustomMut<'_, ()>` note: function defined here - --> $DIR/custom_mut_coerce_shared.rs:20:4 + --> $DIR/custom_mut_coerce_shared.rs:19:4 | LL | fn method(a: CustomRef<'_, ()>) {} | ^^^^^^ -------------------- diff --git a/tests/ui/reborrow/option_mut.rs b/tests/ui/reborrow/option_mut.rs index 60b7145a7b2e3..04d8301772de9 100644 --- a/tests/ui/reborrow/option_mut.rs +++ b/tests/ui/reborrow/option_mut.rs @@ -1,4 +1,4 @@ -fn method(a: Option<& mut ()>) {} +fn method(a: Option<&mut ()>) {} fn main() { let a = Some(&mut ()); diff --git a/tests/ui/reborrow/option_mut.stderr b/tests/ui/reborrow/option_mut.stderr index 497319d2e9035..d665e266079ea 100644 --- a/tests/ui/reborrow/option_mut.stderr +++ b/tests/ui/reborrow/option_mut.stderr @@ -11,8 +11,8 @@ LL | let _ = method(a); note: consider changing this parameter type in function `method` to borrow instead if owning the value isn't necessary --> $DIR/option_mut.rs:1:14 | -LL | fn method(a: Option<& mut ()>) {} - | ------ ^^^^^^^^^^^^^^^^ this parameter takes ownership of the value +LL | fn method(a: Option<&mut ()>) {} + | ------ ^^^^^^^^^^^^^^^ this parameter takes ownership of the value | | | in this function diff --git a/tests/ui/reborrow/pin_mut.rs b/tests/ui/reborrow/pin_mut.rs index 966106969943a..959cb14f8c9ad 100644 --- a/tests/ui/reborrow/pin_mut.rs +++ b/tests/ui/reborrow/pin_mut.rs @@ -1,6 +1,6 @@ use std::pin::Pin; -fn method(a: Pin<& mut ()>) {} +fn method(a: Pin<&mut ()>) {} fn main() { let a = &mut (); diff --git a/tests/ui/reborrow/pin_mut.stderr b/tests/ui/reborrow/pin_mut.stderr index 8a10e2d9af269..64e3f603e1110 100644 --- a/tests/ui/reborrow/pin_mut.stderr +++ b/tests/ui/reborrow/pin_mut.stderr @@ -11,8 +11,8 @@ LL | let _ = method(a); note: consider changing this parameter type in function `method` to borrow instead if owning the value isn't necessary --> $DIR/pin_mut.rs:3:14 | -LL | fn method(a: Pin<& mut ()>) {} - | ------ ^^^^^^^^^^^^^ this parameter takes ownership of the value +LL | fn method(a: Pin<&mut ()>) {} + | ------ ^^^^^^^^^^^^ this parameter takes ownership of the value | | | in this function diff --git a/tests/ui/reborrow/pin_mut_coerce_shared.rs b/tests/ui/reborrow/pin_mut_coerce_shared.rs index fd529195fe01e..06af0b765d046 100644 --- a/tests/ui/reborrow/pin_mut_coerce_shared.rs +++ b/tests/ui/reborrow/pin_mut_coerce_shared.rs @@ -10,5 +10,4 @@ fn main() { //~| NOTE arguments to this function are incorrect //~| NOTE types differ in mutability //~| NOTE expected struct `Pin<&()>` - //~| NOTE found struct `Pin<&mut ()>` }