Skip to content

Commit b023856

Browse files
committed
Add or-patterns to pattern types
1 parent cb6d371 commit b023856

File tree

34 files changed

+504
-14
lines changed

34 files changed

+504
-14
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,6 +2469,8 @@ pub enum TyPatKind {
24692469
/// A range pattern (e.g., `1...2`, `1..2`, `1..`, `..2`, `1..=2`, `..=2`).
24702470
Range(Option<P<AnonConst>>, Option<P<AnonConst>>, Spanned<RangeEnd>),
24712471

2472+
Or(ThinVec<P<TyPat>>),
2473+
24722474
/// Placeholder for a pattern that wasn't syntactically well formed in some way.
24732475
Err(ErrorGuaranteed),
24742476
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ pub fn walk_ty_pat<T: MutVisitor>(vis: &mut T, ty: &mut P<TyPat>) {
612612
visit_opt(start, |c| vis.visit_anon_const(c));
613613
visit_opt(end, |c| vis.visit_anon_const(c));
614614
}
615+
TyPatKind::Or(variants) => visit_thin_vec(variants, |p| vis.visit_ty_pat(p)),
615616
TyPatKind::Err(_) => {}
616617
}
617618
visit_lazy_tts(vis, tokens);

compiler/rustc_ast/src/visit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ pub fn walk_ty_pat<'a, V: Visitor<'a>>(visitor: &mut V, tp: &'a TyPat) -> V::Res
608608
visit_opt!(visitor, visit_anon_const, start);
609609
visit_opt!(visitor, visit_anon_const, end);
610610
}
611+
TyPatKind::Or(variants) => walk_list!(visitor, visit_ty_pat, variants),
611612
TyPatKind::Err(_) => {}
612613
}
613614
V::Result::output()

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
464464
)
465465
}),
466466
),
467+
TyPatKind::Or(variants) => {
468+
hir::TyPatKind::Or(self.arena.alloc_from_iter(
469+
variants.iter().map(|pat| self.lower_ty_pat_mut(pat, base_type)),
470+
))
471+
}
467472
TyPatKind::Err(guar) => hir::TyPatKind::Err(*guar),
468473
};
469474

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,6 +1162,17 @@ impl<'a> State<'a> {
11621162
self.print_expr_anon_const(end, &[]);
11631163
}
11641164
}
1165+
rustc_ast::TyPatKind::Or(variants) => {
1166+
let mut first = true;
1167+
for pat in variants {
1168+
if first {
1169+
first = false
1170+
} else {
1171+
self.word(" | ");
1172+
}
1173+
self.print_ty_pat(pat);
1174+
}
1175+
}
11651176
rustc_ast::TyPatKind::Err(_) => {
11661177
self.popen();
11671178
self.word("/*ERROR*/");

compiler/rustc_builtin_macros/src/pattern_type.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_ast::{AnonConst, DUMMY_NODE_ID, Ty, TyPat, TyPatKind, ast, token};
44
use rustc_errors::PResult;
55
use rustc_expand::base::{self, DummyResult, ExpandResult, ExtCtxt, MacroExpanderResult};
66
use rustc_parse::exp;
7+
use rustc_parse::parser::{CommaRecoveryMode, RecoverColon, RecoverComma};
78
use rustc_span::Span;
89

910
pub(crate) fn expand<'cx>(
@@ -27,7 +28,17 @@ fn parse_pat_ty<'a>(cx: &mut ExtCtxt<'a>, stream: TokenStream) -> PResult<'a, (P
2728
let ty = parser.parse_ty()?;
2829
parser.expect_keyword(exp!(Is))?;
2930

30-
let pat = pat_to_ty_pat(cx, parser.parse_pat_no_top_alt(None, None)?.into_inner());
31+
let pat = pat_to_ty_pat(
32+
cx,
33+
parser
34+
.parse_pat_no_top_guard(
35+
None,
36+
RecoverComma::No,
37+
RecoverColon::No,
38+
CommaRecoveryMode::EitherTupleOrPipe,
39+
)?
40+
.into_inner(),
41+
);
3142

3243
if parser.token != token::Eof {
3344
parser.unexpected()?;
@@ -47,6 +58,9 @@ fn pat_to_ty_pat(cx: &mut ExtCtxt<'_>, pat: ast::Pat) -> P<TyPat> {
4758
end.map(|value| P(AnonConst { id: DUMMY_NODE_ID, value })),
4859
include_end,
4960
),
61+
ast::PatKind::Or(variants) => TyPatKind::Or(
62+
variants.into_iter().map(|pat| pat_to_ty_pat(cx, pat.into_inner())).collect(),
63+
),
5064
ast::PatKind::Err(guar) => TyPatKind::Err(guar),
5165
_ => TyPatKind::Err(cx.dcx().span_err(pat.span, "pattern not supported in pattern types")),
5266
};

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,21 @@ pub(crate) fn eval_nullary_intrinsic<'tcx>(
6161
ensure_monomorphic_enough(tcx, tp_ty)?;
6262
ConstValue::from_u128(tcx.type_id_hash(tp_ty).as_u128())
6363
}
64-
sym::variant_count => match tp_ty.kind() {
64+
sym::variant_count => match match tp_ty.kind() {
65+
// Pattern types have the same number of variants as their base type.
66+
// Even if we restrict e.g. which variants are valid, the variants are essentially just uninhabited.
67+
// And `Result<(), !>` still has two variants according to `variant_count`.
68+
ty::Pat(base, _) => *base,
69+
_ => tp_ty,
70+
}
71+
.kind()
72+
{
6573
// Correctly handles non-monomorphic calls, so there is no need for ensure_monomorphic_enough.
6674
ty::Adt(adt, _) => ConstValue::from_target_usize(adt.variants().len() as u64, &tcx),
6775
ty::Alias(..) | ty::Param(_) | ty::Placeholder(_) | ty::Infer(_) => {
6876
throw_inval!(TooGeneric)
6977
}
70-
ty::Pat(_, pat) => match **pat {
71-
ty::PatternKind::Range { .. } => ConstValue::from_target_usize(0u64, &tcx),
72-
// Future pattern kinds may have more variants
73-
},
78+
ty::Pat(..) => unreachable!(),
7479
ty::Bound(_, _) => bug!("bound ty during ctfe"),
7580
ty::Bool
7681
| ty::Char

compiler/rustc_const_eval/src/interpret/validity.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,14 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValueVisitor<'tcx, M> for ValidityVisitor<'rt,
12481248
// Range patterns are precisely reflected into `valid_range` and thus
12491249
// handled fully by `visit_scalar` (called below).
12501250
ty::PatternKind::Range { .. } => {},
1251+
1252+
// FIXME(pattern_types): check that the value is covered by one of the variants.
1253+
// For now, we rely on layout computation setting the scalar's `valid_range` to
1254+
// match the pattern. However, this cannot always work; the layout may
1255+
// pessimistically cover actually illegal ranges and Miri would miss that UB.
1256+
// The consolation here is that codegen also will miss that UB, so at least
1257+
// we won't see optimizations actually breaking such programs.
1258+
ty::PatternKind::Or(_patterns) => {}
12511259
}
12521260
}
12531261
_ => {

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,9 @@ pub enum TyPatKind<'hir> {
18131813
/// A range pattern (e.g., `1..=2` or `1..2`).
18141814
Range(&'hir ConstArg<'hir>, &'hir ConstArg<'hir>),
18151815

1816+
/// A list of patterns where only one needs to be satisfied
1817+
Or(&'hir [TyPat<'hir>]),
1818+
18161819
/// A placeholder for a pattern that wasn't well formed in some way.
18171820
Err(ErrorGuaranteed),
18181821
}

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ pub fn walk_ty_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v TyPat<'v>)
710710
try_visit!(visitor.visit_const_arg_unambig(lower_bound));
711711
try_visit!(visitor.visit_const_arg_unambig(upper_bound));
712712
}
713+
TyPatKind::Or(patterns) => walk_list!(visitor, visit_pattern_type_pattern, patterns),
713714
TyPatKind::Err(_) => (),
714715
}
715716
V::Result::output()

0 commit comments

Comments
 (0)