Skip to content

Commit 06ff63e

Browse files
committed
Actually create ranged int types in the type system.
1 parent bdeea24 commit 06ff63e

File tree

97 files changed

+1208
-77
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+1208
-77
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2152,7 +2152,7 @@ pub enum TyKind {
21522152
MacCall(P<MacCall>),
21532153
/// Placeholder for a `va_list`.
21542154
CVarArgs,
2155-
/// Pattern types like `u32 as 1..=`, which is the same as `NonZeroU32`,
2155+
/// Pattern types like `pattern_type!(u32 is 1..=)`, which is the same as `NonZeroU32`,
21562156
/// just as part of the type system.
21572157
Pat(P<Ty>, P<Pat>),
21582158
/// Sometimes we need a dummy value when no error has occurred.

compiler/rustc_ast_lowering/src/index.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,4 +381,8 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
381381
ArrayLen::Body(..) => intravisit::walk_array_len(self, len),
382382
}
383383
}
384+
385+
fn visit_pattern_type_pattern(&mut self, p: &'hir hir::Pat<'hir>) {
386+
self.visit_pat(p)
387+
}
384388
}

compiler/rustc_borrowck/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,6 +1606,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
16061606
| ty::Foreign(_)
16071607
| ty::Str
16081608
| ty::Array(_, _)
1609+
| ty::Pat(_, _)
16091610
| ty::Slice(_)
16101611
| ty::FnDef(_, _)
16111612
| ty::FnPtr(_)
@@ -1648,6 +1649,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
16481649
| ty::Foreign(_)
16491650
| ty::Str
16501651
| ty::Array(_, _)
1652+
| ty::Pat(_, _)
16511653
| ty::Slice(_)
16521654
| ty::RawPtr(_, _)
16531655
| ty::Ref(_, _, _)

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,16 @@ fn push_debuginfo_type_name<'tcx>(
202202
}
203203
}
204204
}
205+
ty::Pat(inner_type, pat) => {
206+
if cpp_like_debuginfo {
207+
output.push_str("pat$<");
208+
push_debuginfo_type_name(tcx, inner_type, true, output, visited);
209+
// FIXME(wg-debugging): implement CPP like printing for patterns.
210+
write!(output, ",{:?}>", pat).unwrap();
211+
} else {
212+
write!(output, "{:?}", t).unwrap();
213+
}
214+
}
205215
ty::Slice(inner_type) => {
206216
if cpp_like_debuginfo {
207217
output.push_str("slice2$<");

compiler/rustc_const_eval/src/const_eval/valtrees.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_data_structures::stack::ensure_sufficient_stack;
12
use rustc_middle::mir;
23
use rustc_middle::mir::interpret::{EvalToValTreeResult, GlobalId};
34
use rustc_middle::ty::layout::{LayoutCx, LayoutOf, TyAndLayout};
@@ -98,6 +99,16 @@ fn const_to_valtree_inner<'tcx>(
9899
Ok(ty::ValTree::Leaf(val.assert_int()))
99100
}
100101

102+
ty::Pat(base, ..) => {
103+
let mut place = place.clone();
104+
// The valtree of the base type is the same as the valtree of the pattern type.
105+
// Since the returned valtree does not contain the type or layout, we can just
106+
// switch to the base type.
107+
place.layout = ecx.layout_of(*base).unwrap();
108+
ensure_sufficient_stack(|| const_to_valtree_inner(ecx, &place, num_nodes))
109+
},
110+
111+
101112
ty::RawPtr(_, _) => {
102113
// Not all raw pointers are allowed, as we cannot properly test them for
103114
// equality at compile-time (see `ptr_guaranteed_cmp`).
@@ -273,7 +284,7 @@ pub fn valtree_to_const_value<'tcx>(
273284

274285
let (param_env, ty) = param_env_ty.into_parts();
275286

276-
match ty.kind() {
287+
match *ty.kind() {
277288
ty::FnDef(..) => {
278289
assert!(valtree.unwrap_branch().is_empty());
279290
mir::ConstValue::ZeroSized
@@ -286,10 +297,11 @@ pub fn valtree_to_const_value<'tcx>(
286297
),
287298
}
288299
}
300+
ty::Pat(ty, _) => valtree_to_const_value(tcx, param_env.and(ty), valtree),
289301
ty::Ref(_, inner_ty, _) => {
290302
let mut ecx =
291303
mk_eval_cx_to_read_const_val(tcx, DUMMY_SP, param_env, CanAccessMutGlobal::No);
292-
let imm = valtree_to_ref(&mut ecx, valtree, *inner_ty);
304+
let imm = valtree_to_ref(&mut ecx, valtree, inner_ty);
293305
let imm = ImmTy::from_immediate(imm, tcx.layout_of(param_env_ty).unwrap());
294306
op_to_const(&ecx, &imm.into(), /* for diagnostics */ false)
295307
}

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
10601060

10611061
ty::Tuple(tys) => tys.last().iter().all(|ty| is_very_trivially_sized(**ty)),
10621062

1063+
ty::Pat(ty, ..) => is_very_trivially_sized(*ty),
1064+
10631065
// We don't want to do any queries, so there is not much we can do with ADTs.
10641066
ty::Adt(..) => false,
10651067

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ pub(crate) fn eval_nullary_intrinsic<'tcx>(
6969
ty::Alias(..) | ty::Param(_) | ty::Placeholder(_) | ty::Infer(_) => {
7070
throw_inval!(TooGeneric)
7171
}
72+
ty::Pat(_, pat) => match **pat {
73+
ty::PatternKind::Range { .. } => ConstValue::from_target_usize(0u64, &tcx),
74+
// Future pattern kinds may have more variants
75+
},
7276
ty::Bound(_, _) => bug!("bound ty during ctfe"),
7377
ty::Bool
7478
| ty::Char

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
640640
| ty::Str
641641
| ty::Dynamic(..)
642642
| ty::Closure(..)
643+
| ty::Pat(..)
643644
| ty::CoroutineClosure(..)
644645
| ty::Coroutine(..) => Ok(false),
645646
// Some types only occur during typechecking, they have no layout.

compiler/rustc_const_eval/src/util/type_name.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
3131
| ty::Uint(_)
3232
| ty::Float(_)
3333
| ty::Str
34+
| ty::Pat(_, _)
3435
| ty::Array(_, _)
3536
| ty::Slice(_)
3637
| ty::RawPtr(_, _)

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2624,7 +2624,7 @@ pub enum TyKind<'hir> {
26242624
Infer,
26252625
/// Placeholder for a type that has failed to be defined.
26262626
Err(rustc_span::ErrorGuaranteed),
2627-
/// Pattern types (`u32 as 1..`)
2627+
/// Pattern types (`pattern_type!(u32 is 1..)`)
26282628
Pat(&'hir Ty<'hir>, &'hir Pat<'hir>),
26292629
}
26302630

0 commit comments

Comments
 (0)