diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index 01ed3cc69a2cc..ead92655899af 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -181,6 +181,8 @@ #![feature(no_core)] #![feature(no_sanitize)] #![feature(optimize_attribute)] +#![feature(pattern_type_macro)] +#![feature(pattern_types)] #![feature(prelude_import)] #![feature(repr_simd)] #![feature(rustc_allow_const_fn_unstable)] diff --git a/library/core/src/num/niche_types.rs b/library/core/src/num/niche_types.rs index 096713c318f8d..71b56cdaf912b 100644 --- a/library/core/src/num/niche_types.rs +++ b/library/core/src/num/niche_types.rs @@ -8,19 +8,28 @@ use crate::cmp::Ordering; use crate::fmt; use crate::hash::{Hash, Hasher}; use crate::marker::StructuralPartialEq; +#[cfg(not(bootstrap))] +use crate::pattern_type; macro_rules! define_valid_range_type { ($( $(#[$m:meta])* $vis:vis struct $name:ident($int:ident as $uint:ident in $low:literal..=$high:literal); )+) => {$( - #[derive(Clone, Copy, Eq)] + #[derive(Clone, Copy)] #[repr(transparent)] - #[rustc_layout_scalar_valid_range_start($low)] - #[rustc_layout_scalar_valid_range_end($high)] + #[cfg_attr(bootstrap, rustc_layout_scalar_valid_range_start($low))] + #[cfg_attr(bootstrap, rustc_layout_scalar_valid_range_end($high))] $(#[$m])* + #[cfg(bootstrap)] $vis struct $name($int); + #[derive(Clone, Copy)] + #[repr(transparent)] + $(#[$m])* + #[cfg(not(bootstrap))] + $vis struct $name(pattern_type!($int is $low..=$high)); + const _: () = { // With the `valid_range` attributes, it's always specified as unsigned assert!(<$uint>::MIN == 0); @@ -41,7 +50,7 @@ macro_rules! define_valid_range_type { #[inline] pub const unsafe fn new_unchecked(val: $int) -> Self { // SAFETY: Caller promised that `val` is non-zero. - unsafe { $name(val) } + unsafe { $name(crate::mem::transmute(val)) } } #[inline] @@ -57,6 +66,8 @@ macro_rules! define_valid_range_type { // by . impl StructuralPartialEq for $name {} + impl Eq for $name {} + impl PartialEq for $name { #[inline] fn eq(&self, other: &Self) -> bool {