Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ symbols! {
cmpxchg16b_target_feature,
cmse_nonsecure_entry,
coerce_pointee_validated,
coerce_shared,
coerce_unsized,
cold,
cold_path,
Expand Down
8 changes: 0 additions & 8 deletions library/core/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
3 changes: 3 additions & 0 deletions library/core/src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ mod function;
mod index;
mod index_range;
mod range;
mod reborrow;
mod try_trait;
mod unsize;

Expand Down Expand Up @@ -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, Reborrow};
#[unstable(feature = "try_trait_v2_residual", issue = "91285")]
pub use self::try_trait::Residual;
#[unstable(feature = "try_trait_v2_yeet", issue = "96374")]
Expand Down
16 changes: 16 additions & 0 deletions library/core/src/ops/reborrow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// 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.
#[lang = "coerce_shared"]
#[unstable(feature = "reborrow", issue = "145612")]
pub trait CoerceShared: Reborrow {
/// The type of this value when reborrowed as shared.
type Target: Copy;
}
3 changes: 3 additions & 0 deletions tests/ui/feature-gates/feature-gate-reborrow-coerce-shared.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use std::ops::CoerceShared; //~ ERROR use of unstable library feature `reborrow`

fn main() {}
13 changes: 13 additions & 0 deletions tests/ui/feature-gates/feature-gate-reborrow-coerce-shared.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
error[E0658]: use of unstable library feature `reborrow`
--> $DIR/feature-gate-reborrow-coerce-shared.rs:1:5
|
LL | use std::ops::CoerceShared;
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: see issue #145612 <https://github.com/rust-lang/rust/issues/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`.
2 changes: 1 addition & 1 deletion tests/ui/feature-gates/feature-gate-reborrow.rs
Original file line number Diff line number Diff line change
@@ -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() {}
4 changes: 2 additions & 2 deletions tests/ui/feature-gates/feature-gate-reborrow.stderr
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/rust-lang/rust/issues/145612> for more information
= help: add `#![feature(reborrow)]` to the crate attributes to enable
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/reborrow/custom_mut.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![feature(reborrow)]
use std::ops::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`
}
29 changes: 29 additions & 0 deletions tests/ui/reborrow/custom_mut.stderr
Original file line number Diff line number Diff line change
@@ -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`.
28 changes: 28 additions & 0 deletions tests/ui/reborrow/custom_mut_coerce_shared.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#![feature(reborrow)]
use std::ops::{CoerceShared, Reborrow};

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<'_, ()>`
}
19 changes: 19 additions & 0 deletions tests/ui/reborrow/custom_mut_coerce_shared.stderr
Original file line number Diff line number Diff line change
@@ -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`.
7 changes: 7 additions & 0 deletions tests/ui/reborrow/option_mut.rs
Original file line number Diff line number Diff line change
@@ -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`
}
21 changes: 21 additions & 0 deletions tests/ui/reborrow/option_mut.stderr
Original file line number Diff line number Diff line change
@@ -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`.
11 changes: 11 additions & 0 deletions tests/ui/reborrow/option_mut_coerce_shared.rs
Original file line number Diff line number Diff line change
@@ -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 ()>`
}
23 changes: 23 additions & 0 deletions tests/ui/reborrow/option_mut_coerce_shared.stderr
Original file line number Diff line number Diff line change
@@ -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`.
10 changes: 10 additions & 0 deletions tests/ui/reborrow/pin_mut.rs
Original file line number Diff line number Diff line change
@@ -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`
}
21 changes: 21 additions & 0 deletions tests/ui/reborrow/pin_mut.stderr
Original file line number Diff line number Diff line change
@@ -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`.
13 changes: 13 additions & 0 deletions tests/ui/reborrow/pin_mut_coerce_shared.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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<&()>`
}
19 changes: 19 additions & 0 deletions tests/ui/reborrow/pin_mut_coerce_shared.stderr
Original file line number Diff line number Diff line change
@@ -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`.
Loading