Skip to content

Commit d1720e2

Browse files
committed
Add pattern types to the compiler
1 parent e187f88 commit d1720e2

File tree

55 files changed

+342
-28
lines changed

Some content is hidden

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

55 files changed

+342
-28
lines changed

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ fn push_debuginfo_type_name<'tcx>(
194194
}
195195
}
196196
}
197+
ty::Pat(inner_type, pat) => {
198+
if cpp_like_debuginfo {
199+
output.push_str("pat$<");
200+
push_debuginfo_type_name(tcx, inner_type, true, output, visited);
201+
write!(output, ",{:?}>", pat).unwrap();
202+
} else {
203+
write!(output, "{:?}", t).unwrap();
204+
}
205+
}
197206
ty::Slice(inner_type) => {
198207
if cpp_like_debuginfo {
199208
output.push_str("slice2$<");

compiler/rustc_const_eval/src/const_eval/valtrees.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ pub(crate) fn const_to_valtree_inner<'tcx>(
9696
Ok(ty::ValTree::Leaf(val.assert_int()))
9797
}
9898

99+
ty::Pat(..) => const_to_valtree_inner(ecx, &ecx.mplace_field(&place, 0).unwrap(), num_nodes),
100+
99101
// Raw pointers are not allowed in type level constants, as we cannot properly test them for
100102
// equality at compile-time (see `ptr_guaranteed_cmp`).
101103
// Technically we could allow function pointers (represented as `ty::Instance`), but this is not guaranteed to
@@ -265,7 +267,7 @@ pub fn valtree_to_const_value<'tcx>(
265267
let (param_env, ty) = param_env_ty.into_parts();
266268
let mut ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
267269

268-
match ty.kind() {
270+
match *ty.kind() {
269271
ty::FnDef(..) => {
270272
assert!(valtree.unwrap_branch().is_empty());
271273
ConstValue::ZeroSized
@@ -276,6 +278,7 @@ pub fn valtree_to_const_value<'tcx>(
276278
"ValTrees for Bool, Int, Uint, Float or Char should have the form ValTree::Leaf"
277279
),
278280
},
281+
ty::Pat(ty, _) => valtree_to_const_value(tcx, param_env.and(ty), valtree),
279282
ty::Ref(_, _, _) | ty::Tuple(_) | ty::Array(_, _) | ty::Adt(..) => {
280283
let mut place = match ty.kind() {
281284
ty::Ref(_, inner_ty, _) => {

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ pub(crate) fn eval_nullary_intrinsic<'tcx>(
8383
ty::Alias(..) | ty::Param(_) | ty::Placeholder(_) | ty::Infer(_) => {
8484
throw_inval!(TooGeneric)
8585
}
86+
ty::Pat(..) => {
87+
unimplemented!("pattern types need to calculate pattern from their pattern")
88+
}
8689
ty::Bound(_, _) => bug!("bound ty during ctfe"),
8790
ty::Bool
8891
| ty::Char

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
584584
// Nothing to check.
585585
Ok(true)
586586
}
587+
ty::Pat(..) => unimplemented!(),
587588
// The above should be all the primitive types. The rest is compound, we
588589
// check them by visiting their fields/variants.
589590
ty::Adt(..)

compiler/rustc_const_eval/src/util/type_name.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ impl<'tcx> Printer<'tcx> for AbsolutePathPrinter<'tcx> {
4040
| ty::Uint(_)
4141
| ty::Float(_)
4242
| ty::Str
43+
| ty::Pat(_, _)
4344
| ty::Array(_, _)
4445
| ty::Slice(_)
4546
| ty::RawPtr(_)

compiler/rustc_error_messages/locales/en-US/lint.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ lint_improper_ctypes_128bit = 128-bit integers don't currently have a known stab
237237
lint_improper_ctypes_char_reason = the `char` type has no C equivalent
238238
lint_improper_ctypes_char_help = consider using `u32` or `libc::wchar_t` instead
239239
240+
lint_improper_ctypes_pat_reason = pattern types have no C equivalent
241+
lint_improper_ctypes_pat_help = consider using the patterned type instead
242+
240243
lint_improper_ctypes_non_exhaustive = this enum is non-exhaustive
241244
lint_improper_ctypes_non_exhaustive_variant = this enum has non-exhaustive variants
242245

compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ impl<'tcx> InherentCollect<'tcx> {
208208
.note("define and implement a new trait or type instead")
209209
.emit();
210210
}
211+
ty::Pat(..) => {
212+
self.tcx.sess.span_err(ty.span, "cannot define inherent `impl` for pattern types");
213+
}
211214
ty::Bool
212215
| ty::Char
213216
| ty::Int(_)

compiler/rustc_hir_analysis/src/variance/constraints.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,16 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
231231
self.add_constraints_from_ty(current, typ, variance);
232232
}
233233

234+
ty::Pat(typ, pat) => {
235+
match *pat {
236+
ty::PatternKind::Range { start, end } => {
237+
self.add_constraints_from_const(current, start, variance);
238+
self.add_constraints_from_const(current, end, variance);
239+
}
240+
}
241+
self.add_constraints_from_ty(current, typ, variance);
242+
}
243+
234244
ty::Slice(typ) => {
235245
self.add_constraints_from_ty(current, typ, variance);
236246
}

compiler/rustc_hir_typeck/src/cast.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
132132
| ty::GeneratorWitness(..)
133133
| ty::RawPtr(_)
134134
| ty::Ref(..)
135+
| ty::Pat(..)
135136
| ty::FnDef(..)
136137
| ty::FnPtr(..)
137138
| ty::Closure(..)

compiler/rustc_infer/src/infer/canonical/canonicalizer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Canonicalizer<'cx, 'tcx> {
454454
| ty::Tuple(..)
455455
| ty::Alias(..)
456456
| ty::Foreign(..)
457+
| ty::Pat(..)
457458
| ty::Param(..) => {
458459
if t.flags().intersects(self.needs_canonical_flags) {
459460
t.super_fold_with(self)

0 commit comments

Comments
 (0)