Skip to content

Commit 0409353

Browse files
committed
Start using pattern types in libcore
1 parent 633a3fe commit 0409353

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2206,7 +2206,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
22062206
}
22072207
// For types with no regions we can just check that the
22082208
// both operands have the same type.
2209-
ty::Int(_) | ty::Uint(_) | ty::Bool | ty::Char | ty::Float(_)
2209+
ty::Int(_) | ty::Uint(_) | ty::Bool | ty::Char | ty::Float(_) | ty::Pat(..)
22102210
if ty_left == right.ty(body, tcx) => {}
22112211
// Other types are compared by trait methods, not by
22122212
// `Rvalue::BinaryOp`.

compiler/rustc_middle/src/ty/sty.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -1257,17 +1257,19 @@ impl<'tcx> Ty<'tcx> {
12571257
/// contents are abstract to rustc.)
12581258
#[inline]
12591259
pub fn is_scalar(self) -> bool {
1260-
matches!(
1261-
self.kind(),
1262-
Bool | Char
1263-
| Int(_)
1264-
| Float(_)
1265-
| Uint(_)
1266-
| FnDef(..)
1267-
| FnPtr(..)
1268-
| RawPtr(_, _)
1269-
| Infer(IntVar(_) | FloatVar(_))
1270-
)
1260+
match self.kind() {
1261+
Bool
1262+
| Char
1263+
| Int(_)
1264+
| Float(_)
1265+
| Uint(_)
1266+
| FnDef(..)
1267+
| FnPtr(..)
1268+
| RawPtr(_, _)
1269+
| Infer(IntVar(_) | FloatVar(_)) => true,
1270+
Pat(base, _) => base.is_scalar(),
1271+
_ => false,
1272+
}
12711273
}
12721274

12731275
/// Returns `true` if this type is a floating point type.

library/core/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@
181181
#![feature(no_core)]
182182
#![feature(no_sanitize)]
183183
#![feature(optimize_attribute)]
184+
#![feature(pattern_type_macro)]
185+
#![feature(pattern_types)]
184186
#![feature(prelude_import)]
185187
#![feature(repr_simd)]
186188
#![feature(rustc_allow_const_fn_unstable)]

library/core/src/num/niche_types.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,28 @@ use crate::cmp::Ordering;
88
use crate::fmt;
99
use crate::hash::{Hash, Hasher};
1010
use crate::marker::StructuralPartialEq;
11+
#[cfg(not(bootstrap))]
12+
use crate::pattern_type;
1113

1214
macro_rules! define_valid_range_type {
1315
($(
1416
$(#[$m:meta])*
1517
$vis:vis struct $name:ident($int:ident as $uint:ident in $low:literal..=$high:literal);
1618
)+) => {$(
17-
#[derive(Clone, Copy, Eq)]
19+
#[derive(Clone, Copy)]
1820
#[repr(transparent)]
19-
#[rustc_layout_scalar_valid_range_start($low)]
20-
#[rustc_layout_scalar_valid_range_end($high)]
21+
#[cfg_attr(bootstrap, rustc_layout_scalar_valid_range_start($low))]
22+
#[cfg_attr(bootstrap, rustc_layout_scalar_valid_range_end($high))]
2123
$(#[$m])*
24+
#[cfg(bootstrap)]
2225
$vis struct $name($int);
2326

27+
#[derive(Clone, Copy)]
28+
#[repr(transparent)]
29+
$(#[$m])*
30+
#[cfg(not(bootstrap))]
31+
$vis struct $name(pattern_type!($int is $low..=$high));
32+
2433
const _: () = {
2534
// With the `valid_range` attributes, it's always specified as unsigned
2635
assert!(<$uint>::MIN == 0);
@@ -41,7 +50,7 @@ macro_rules! define_valid_range_type {
4150
#[inline]
4251
pub const unsafe fn new_unchecked(val: $int) -> Self {
4352
// SAFETY: Caller promised that `val` is non-zero.
44-
unsafe { $name(val) }
53+
unsafe { $name(crate::mem::transmute(val)) }
4554
}
4655

4756
#[inline]
@@ -57,6 +66,8 @@ macro_rules! define_valid_range_type {
5766
// by <https://github.com/rust-lang/compiler-team/issues/807>.
5867
impl StructuralPartialEq for $name {}
5968

69+
impl Eq for $name {}
70+
6071
impl PartialEq for $name {
6172
#[inline]
6273
fn eq(&self, other: &Self) -> bool {

0 commit comments

Comments
 (0)