Skip to content

Commit

Permalink
Start using pattern types in libcore
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jan 27, 2025
1 parent 633a3fe commit 0409353
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2206,7 +2206,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
}
// For types with no regions we can just check that the
// both operands have the same type.
ty::Int(_) | ty::Uint(_) | ty::Bool | ty::Char | ty::Float(_)
ty::Int(_) | ty::Uint(_) | ty::Bool | ty::Char | ty::Float(_) | ty::Pat(..)
if ty_left == right.ty(body, tcx) => {}
// Other types are compared by trait methods, not by
// `Rvalue::BinaryOp`.
Expand Down
24 changes: 13 additions & 11 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1257,17 +1257,19 @@ impl<'tcx> Ty<'tcx> {
/// contents are abstract to rustc.)
#[inline]
pub fn is_scalar(self) -> bool {
matches!(
self.kind(),
Bool | Char
| Int(_)
| Float(_)
| Uint(_)
| FnDef(..)
| FnPtr(..)
| RawPtr(_, _)
| Infer(IntVar(_) | FloatVar(_))
)
match self.kind() {
Bool
| Char
| Int(_)
| Float(_)
| Uint(_)
| FnDef(..)
| FnPtr(..)
| RawPtr(_, _)
| Infer(IntVar(_) | FloatVar(_)) => true,
Pat(base, _) => base.is_scalar(),
_ => false,
}
}

/// Returns `true` if this type is a floating point type.
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
19 changes: 15 additions & 4 deletions library/core/src/num/niche_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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]
Expand All @@ -57,6 +66,8 @@ macro_rules! define_valid_range_type {
// by <https://github.com/rust-lang/compiler-team/issues/807>.
impl StructuralPartialEq for $name {}

impl Eq for $name {}

impl PartialEq for $name {
#[inline]
fn eq(&self, other: &Self) -> bool {
Expand Down

0 comments on commit 0409353

Please sign in to comment.