mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
Constify Deref and DerefMut
This commit is contained in:
parent
15b663e684
commit
04d1bdc377
@ -133,6 +133,7 @@
|
|||||||
#[doc(alias = "&*")]
|
#[doc(alias = "&*")]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
#[rustc_diagnostic_item = "Deref"]
|
#[rustc_diagnostic_item = "Deref"]
|
||||||
|
#[cfg_attr(not(bootstrap), const_trait)]
|
||||||
pub trait Deref {
|
pub trait Deref {
|
||||||
/// The resulting type after dereferencing.
|
/// The resulting type after dereferencing.
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
@ -147,6 +148,7 @@ pub trait Deref {
|
|||||||
fn deref(&self) -> &Self::Target;
|
fn deref(&self) -> &Self::Target;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(bootstrap)]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl<T: ?Sized> Deref for &T {
|
impl<T: ?Sized> Deref for &T {
|
||||||
type Target = T;
|
type Target = T;
|
||||||
@ -157,9 +159,21 @@ impl<T: ?Sized> Deref for &T {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(bootstrap))]
|
||||||
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
impl<T: ?Sized> const Deref for &T {
|
||||||
|
type Target = T;
|
||||||
|
|
||||||
|
#[rustc_diagnostic_item = "noop_method_deref"]
|
||||||
|
fn deref(&self) -> &T {
|
||||||
|
*self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl<T: ?Sized> !DerefMut for &T {}
|
impl<T: ?Sized> !DerefMut for &T {}
|
||||||
|
|
||||||
|
#[cfg(bootstrap)]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl<T: ?Sized> Deref for &mut T {
|
impl<T: ?Sized> Deref for &mut T {
|
||||||
type Target = T;
|
type Target = T;
|
||||||
@ -169,6 +183,16 @@ impl<T: ?Sized> Deref for &mut T {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(bootstrap))]
|
||||||
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
impl<T: ?Sized> const Deref for &mut T {
|
||||||
|
type Target = T;
|
||||||
|
|
||||||
|
fn deref(&self) -> &T {
|
||||||
|
*self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Used for mutable dereferencing operations, like in `*v = 1;`.
|
/// Used for mutable dereferencing operations, like in `*v = 1;`.
|
||||||
///
|
///
|
||||||
/// In addition to being used for explicit dereferencing operations with the
|
/// In addition to being used for explicit dereferencing operations with the
|
||||||
@ -258,9 +282,23 @@ impl<T: ?Sized> Deref for &mut T {
|
|||||||
/// *x = 'b';
|
/// *x = 'b';
|
||||||
/// assert_eq!('b', x.value);
|
/// assert_eq!('b', x.value);
|
||||||
/// ```
|
/// ```
|
||||||
|
#[cfg(not(bootstrap))]
|
||||||
#[lang = "deref_mut"]
|
#[lang = "deref_mut"]
|
||||||
#[doc(alias = "*")]
|
#[doc(alias = "*")]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
#[const_trait]
|
||||||
|
pub trait DerefMut: ~const Deref {
|
||||||
|
/// Mutably dereferences the value.
|
||||||
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
#[rustc_diagnostic_item = "deref_mut_method"]
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Bootstrap
|
||||||
|
#[lang = "deref_mut"]
|
||||||
|
#[doc(alias = "*")]
|
||||||
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
#[cfg(bootstrap)]
|
||||||
pub trait DerefMut: Deref {
|
pub trait DerefMut: Deref {
|
||||||
/// Mutably dereferences the value.
|
/// Mutably dereferences the value.
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
@ -268,6 +306,7 @@ pub trait DerefMut: Deref {
|
|||||||
fn deref_mut(&mut self) -> &mut Self::Target;
|
fn deref_mut(&mut self) -> &mut Self::Target;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(bootstrap)]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl<T: ?Sized> DerefMut for &mut T {
|
impl<T: ?Sized> DerefMut for &mut T {
|
||||||
fn deref_mut(&mut self) -> &mut T {
|
fn deref_mut(&mut self) -> &mut T {
|
||||||
@ -275,6 +314,14 @@ impl<T: ?Sized> DerefMut for &mut T {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(bootstrap))]
|
||||||
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
impl<T: ?Sized> const DerefMut for &mut T {
|
||||||
|
fn deref_mut(&mut self) -> &mut T {
|
||||||
|
*self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Perma-unstable marker trait. Indicates that the type has a well-behaved [`Deref`]
|
/// Perma-unstable marker trait. Indicates that the type has a well-behaved [`Deref`]
|
||||||
/// (and, if applicable, [`DerefMut`]) implementation. This is relied on for soundness
|
/// (and, if applicable, [`DerefMut`]) implementation. This is relied on for soundness
|
||||||
/// of deref patterns.
|
/// of deref patterns.
|
||||||
|
@ -2,7 +2,7 @@ struct A;
|
|||||||
struct B;
|
struct B;
|
||||||
|
|
||||||
static S: &'static B = &A;
|
static S: &'static B = &A;
|
||||||
//~^ ERROR cannot perform deref coercion
|
//~^ ERROR cannot call conditionally-const method
|
||||||
|
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
|
||||||
|
@ -1,23 +1,13 @@
|
|||||||
error[E0015]: cannot perform deref coercion on `A` in statics
|
error[E0658]: cannot call conditionally-const method `<A as Deref>::deref` in statics
|
||||||
--> $DIR/issue-25901.rs:4:24
|
--> $DIR/issue-25901.rs:4:24
|
||||||
|
|
|
|
||||||
LL | static S: &'static B = &A;
|
LL | static S: &'static B = &A;
|
||||||
| ^^
|
| ^^
|
||||||
|
|
|
|
||||||
= note: attempting to deref into `B`
|
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
|
||||||
note: deref defined here
|
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
|
||||||
--> $DIR/issue-25901.rs:10:5
|
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
||||||
|
|
|
||||||
LL | type Target = B;
|
|
||||||
| ^^^^^^^^^^^
|
|
||||||
note: impl defined here, but it is not `const`
|
|
||||||
--> $DIR/issue-25901.rs:9:1
|
|
||||||
|
|
|
||||||
LL | impl Deref for A {
|
|
||||||
| ^^^^^^^^^^^^^^^^
|
|
||||||
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
|
|
||||||
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
|
|
||||||
|
|
||||||
error: aborting due to 1 previous error
|
error: aborting due to 1 previous error
|
||||||
|
|
||||||
For more information about this error, try `rustc --explain E0015`.
|
For more information about this error, try `rustc --explain E0658`.
|
||||||
|
@ -11,7 +11,7 @@ impl Foo {
|
|||||||
//~^ ERROR invalid generic `self` parameter type
|
//~^ ERROR invalid generic `self` parameter type
|
||||||
//~| ERROR destructor of `R` cannot be evaluated at compile-time
|
//~| ERROR destructor of `R` cannot be evaluated at compile-time
|
||||||
self.0
|
self.0
|
||||||
//~^ ERROR cannot call non-const fn `<R as Deref>::deref` in constant function
|
//~^ ERROR cannot call conditionally-const method `<R as Deref>::deref` in constant function
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
error[E0015]: cannot call non-const fn `<R as Deref>::deref` in constant functions
|
error[E0658]: cannot call conditionally-const method `<R as Deref>::deref` in constant functions
|
||||||
--> $DIR/arbitrary-self-from-method-substs-ice.rs:13:9
|
--> $DIR/arbitrary-self-from-method-substs-ice.rs:13:9
|
||||||
|
|
|
|
||||||
LL | self.0
|
LL | self.0
|
||||||
| ^^^^^^
|
| ^^^^^^
|
||||||
|
|
|
|
||||||
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
|
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
|
||||||
|
= help: add `#![feature(const_trait_impl)]` 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[E0493]: destructor of `R` cannot be evaluated at compile-time
|
error[E0493]: destructor of `R` cannot be evaluated at compile-time
|
||||||
--> $DIR/arbitrary-self-from-method-substs-ice.rs:10:43
|
--> $DIR/arbitrary-self-from-method-substs-ice.rs:10:43
|
||||||
@ -26,5 +28,5 @@ LL | const fn get<R: Deref<Target = Self>>(self: R) -> u32 {
|
|||||||
|
|
||||||
error: aborting due to 3 previous errors
|
error: aborting due to 3 previous errors
|
||||||
|
|
||||||
Some errors have detailed explanations: E0015, E0493, E0801.
|
Some errors have detailed explanations: E0493, E0658, E0801.
|
||||||
For more information about an error, try `rustc --explain E0015`.
|
For more information about an error, try `rustc --explain E0493`.
|
||||||
|
Loading…
Reference in New Issue
Block a user