-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use T
's discriminant type in mem::Discriminant<T>
instead of u64
.
#70705
Changes from all commits
aab144f
9f7c5a8
b6975bf
25930e4
3188ca7
ff2940a
d6cb540
08b9b97
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
|
||
use crate::cell::UnsafeCell; | ||
use crate::cmp; | ||
use crate::fmt::Debug; | ||
use crate::hash::Hash; | ||
use crate::hash::Hasher; | ||
|
||
|
@@ -679,6 +680,37 @@ mod impls { | |
unsafe impl<T: Send + ?Sized> Send for &mut T {} | ||
} | ||
|
||
/// Compiler-internal trait used to indicate the type of enum discriminants. | ||
/// | ||
/// This trait is automatically implemented for every type and does not add any | ||
/// guarantees to [`mem::Discriminant`]. It is **undefined behavior** to transmute | ||
/// between `DiscriminantKind::Discriminant` and `mem::Discriminant`. | ||
/// | ||
/// [`mem::Discriminant`]: https://doc.rust-lang.org/stable/core/mem/struct.Discriminant.html | ||
#[unstable( | ||
feature = "discriminant_kind", | ||
issue = "none", | ||
reason = "this trait is unlikely to ever be stabilized, use `mem::discriminant` instead" | ||
)] | ||
#[cfg_attr(not(bootstrap), lang = "discriminant_kind")] | ||
pub trait DiscriminantKind { | ||
/// The type of the dicriminant, which must satisfy the trait | ||
/// bounds required by `mem::Discriminant`. | ||
type Discriminant: Clone + Copy + Debug + Eq + PartialEq + Hash + Send + Sync + Unpin; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be interesting to have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fact that we have to add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess. |
||
} | ||
|
||
// Manually implement `DiscriminantKind` for all types during bootstrap | ||
// to reduce the required amount of conditional compilation. | ||
#[unstable( | ||
feature = "discriminant_kind", | ||
issue = "none", | ||
reason = "this trait is unlikely to ever be stabilized, use `mem::discriminant` instead" | ||
)] | ||
#[cfg(bootstrap)] | ||
impl<T: ?Sized> DiscriminantKind for T { | ||
type Discriminant = u64; | ||
} | ||
|
||
/// Compiler-internal trait used to determine whether a type contains | ||
/// any `UnsafeCell` internally, but not through an indirection. | ||
/// This affects, for example, whether a `static` of that type is | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ use crate::cmp; | |
use crate::fmt; | ||
use crate::hash; | ||
use crate::intrinsics; | ||
use crate::marker::{Copy, PhantomData, Sized}; | ||
use crate::marker::{Copy, DiscriminantKind, Sized}; | ||
use crate::ptr; | ||
|
||
mod manually_drop; | ||
|
@@ -930,7 +930,7 @@ pub unsafe fn transmute_copy<T, U>(src: &T) -> U { | |
/// | ||
/// [`discriminant`]: fn.discriminant.html | ||
#[stable(feature = "discriminant_value", since = "1.21.0")] | ||
pub struct Discriminant<T>(u64, PhantomData<fn() -> T>); | ||
pub struct Discriminant<T>(<T as DiscriminantKind>::Discriminant); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uh, oh, cc @rust-lang/libs, this changes variance. That means this PR will require a crater run. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also do the variance change and crater run in a separate PR first. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. opened #71814 |
||
|
||
// N.B. These trait implementations cannot be derived because we don't want any bounds on T. | ||
|
||
|
@@ -995,5 +995,5 @@ impl<T> fmt::Debug for Discriminant<T> { | |
#[stable(feature = "discriminant_value", since = "1.21.0")] | ||
#[rustc_const_unstable(feature = "const_discriminant", issue = "69821")] | ||
pub const fn discriminant<T>(v: &T) -> Discriminant<T> { | ||
Discriminant(intrinsics::discriminant_value(v), PhantomData) | ||
Discriminant(intrinsics::discriminant_value(v)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I intentionally used
mem::discriminant
here, as I meant the method, not the type. Don't care either way though 🤷There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought you meant the type, as this trait is used to get the type of the discriminant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm. This message is not optimal 🤔
Would something like
this trait is unlikely to ever be stabilized, try using `mem::discriminant` directly
be better?