Skip to content

Commit 9957a3c

Browse files
committed
Add or-patterns to pattern types
1 parent cb22e52 commit 9957a3c

File tree

33 files changed

+484
-9
lines changed

33 files changed

+484
-9
lines changed

compiler/rustc_ast/src/ast.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2364,6 +2364,8 @@ pub enum TyPatKind {
23642364
/// A range pattern (e.g., `1...2`, `1..2`, `1..`, `..2`, `1..=2`, `..=2`).
23652365
Range(Option<P<AnonConst>>, Option<P<AnonConst>>, Spanned<RangeEnd>),
23662366

2367+
Or(ThinVec<P<TyPat>>),
2368+
23672369
/// Placeholder for a pattern that wasn't syntactically well formed in some way.
23682370
Err(ErrorGuaranteed),
23692371
}

compiler/rustc_ast/src/mut_visit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ pub fn walk_ty_pat<T: MutVisitor>(vis: &mut T, ty: &mut P<TyPat>) {
609609
visit_opt(start, |c| vis.visit_anon_const(c));
610610
visit_opt(end, |c| vis.visit_anon_const(c));
611611
}
612+
TyPatKind::Or(variants) => visit_thin_vec(variants, |p| vis.visit_ty_pat(p)),
612613
TyPatKind::Err(_) => {}
613614
}
614615
visit_lazy_tts(vis, tokens);

compiler/rustc_ast/src/visit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ pub fn walk_ty_pat<'a, V: Visitor<'a>>(visitor: &mut V, tp: &'a TyPat) -> V::Res
565565
visit_opt!(visitor, visit_anon_const, start);
566566
visit_opt!(visitor, visit_anon_const, end);
567567
}
568+
TyPatKind::Or(variants) => walk_list!(visitor, visit_ty_pat, variants),
568569
TyPatKind::Err(_) => {}
569570
}
570571
V::Result::output()

compiler/rustc_ast_lowering/src/pat.rs

+5
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
463463
)
464464
}),
465465
),
466+
TyPatKind::Or(variants) => {
467+
hir::TyPatKind::Or(self.arena.alloc_from_iter(
468+
variants.iter().map(|pat| self.lower_ty_pat_mut(pat, base_type)),
469+
))
470+
}
466471
TyPatKind::Err(guar) => hir::TyPatKind::Err(*guar),
467472
};
468473

compiler/rustc_ast_pretty/src/pprust/state.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,17 @@ impl<'a> State<'a> {
11661166
self.print_expr_anon_const(end, &[]);
11671167
}
11681168
}
1169+
rustc_ast::TyPatKind::Or(variants) => {
1170+
let mut first = true;
1171+
for pat in variants {
1172+
if first {
1173+
first = false
1174+
} else {
1175+
self.word(" | ");
1176+
}
1177+
self.print_ty_pat(pat);
1178+
}
1179+
}
11691180
rustc_ast::TyPatKind::Err(_) => {
11701181
self.popen();
11711182
self.word("/*ERROR*/");

compiler/rustc_builtin_macros/src/pattern_type.rs

+15-1
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

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ pub(crate) fn eval_nullary_intrinsic<'tcx>(
7070
ty::Pat(_, pat) => match **pat {
7171
ty::PatternKind::Range { .. } => ConstValue::from_target_usize(0u64, &tcx),
7272
// Future pattern kinds may have more variants
73+
// FIXME(pattern_types): make this report the number of distinct variants used in the
74+
// or pattern in case the base type is an enum.
75+
ty::PatternKind::Or(_) => ConstValue::from_target_usize(0_u64, &tcx),
7376
},
7477
ty::Bound(_, _) => bug!("bound ty during ctfe"),
7578
ty::Bool

compiler/rustc_const_eval/src/interpret/validity.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,10 @@ 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+
// The layout may pessimistically cover actually illegal ranges.
1254+
ty::PatternKind::Or(_patterns) => {}
12511255
}
12521256
}
12531257
_ => {

compiler/rustc_hir/src/hir.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,9 @@ pub enum TyPatKind<'hir> {
16751675
/// A range pattern (e.g., `1..=2` or `1..2`).
16761676
Range(&'hir ConstArg<'hir>, &'hir ConstArg<'hir>),
16771677

1678+
/// A list of patterns where only one needs to be satisfied
1679+
Or(&'hir [TyPat<'hir>]),
1680+
16781681
/// A placeholder for a pattern that wasn't well formed in some way.
16791682
Err(ErrorGuaranteed),
16801683
}

compiler/rustc_hir/src/intravisit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ pub fn walk_ty_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v TyPat<'v>)
693693
try_visit!(visitor.visit_const_arg_unambig(lower_bound));
694694
try_visit!(visitor.visit_const_arg_unambig(upper_bound));
695695
}
696+
TyPatKind::Or(patterns) => walk_list!(visitor, visit_pattern_type_pattern, patterns),
696697
TyPatKind::Err(_) => (),
697698
}
698699
V::Result::output()

compiler/rustc_hir_analysis/src/collect/type_of.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,12 @@ fn const_arg_anon_type_of<'tcx>(icx: &ItemCtxt<'tcx>, arg_hir_id: HirId, span: S
9393
}
9494

9595
Node::TyPat(pat) => {
96-
let hir::TyKind::Pat(ty, p) = tcx.parent_hir_node(pat.hir_id).expect_ty().kind else {
97-
bug!()
96+
let node = match tcx.parent_hir_node(pat.hir_id) {
97+
// Or patterns can be nested one level deep
98+
Node::TyPat(p) => tcx.parent_hir_node(p.hir_id),
99+
other => other,
98100
};
99-
assert_eq!(p.hir_id, pat.hir_id);
101+
let hir::TyKind::Pat(ty, _) = node.expect_ty().kind else { bug!() };
100102
icx.lower_ty(ty)
101103
}
102104

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2718,6 +2718,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
27182718
ty_span: Span,
27192719
pat: &hir::TyPat<'tcx>,
27202720
) -> Result<ty::PatternKind<'tcx>, ErrorGuaranteed> {
2721+
let tcx = self.tcx();
27212722
match pat.kind {
27222723
hir::TyPatKind::Range(start, end) => {
27232724
match ty.kind() {
@@ -2733,6 +2734,13 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
27332734
.span_delayed_bug(ty_span, "invalid base type for range pattern")),
27342735
}
27352736
}
2737+
hir::TyPatKind::Or(patterns) => {
2738+
self.tcx()
2739+
.mk_patterns_from_iter(patterns.iter().map(|pat| {
2740+
self.lower_pat_ty_pat(ty, ty_span, pat).map(|pat| tcx.mk_pat(pat))
2741+
}))
2742+
.map(ty::PatternKind::Or)
2743+
}
27362744
hir::TyPatKind::Err(e) => Err(e),
27372745
}
27382746
}

compiler/rustc_hir_analysis/src/variance/constraints.rs

+5
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,11 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
340340
self.add_constraints_from_const(current, start, variance);
341341
self.add_constraints_from_const(current, end, variance);
342342
}
343+
ty::PatternKind::Or(patterns) => {
344+
for pat in patterns {
345+
self.add_constraints_from_pat(current, variance, pat)
346+
}
347+
}
343348
}
344349
}
345350

compiler/rustc_hir_pretty/src/lib.rs

+13
Original file line numberDiff line numberDiff line change
@@ -1878,6 +1878,19 @@ impl<'a> State<'a> {
18781878
self.word("..=");
18791879
self.print_const_arg(end);
18801880
}
1881+
TyPatKind::Or(patterns) => {
1882+
self.popen();
1883+
let mut first = true;
1884+
for pat in patterns {
1885+
if first {
1886+
first = false;
1887+
} else {
1888+
self.word(" | ");
1889+
}
1890+
self.print_ty_pat(pat);
1891+
}
1892+
self.pclose();
1893+
}
18811894
TyPatKind::Err(_) => {
18821895
self.popen();
18831896
self.word("/*ERROR*/");

compiler/rustc_lint/src/types.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,9 @@ fn pat_ty_is_known_nonnull<'tcx>(
901901
// to ensure we aren't wrapping over zero.
902902
start > 0 && end >= start
903903
}
904+
ty::PatternKind::Or(patterns) => {
905+
patterns.iter().all(|pat| pat_ty_is_known_nonnull(tcx, typing_env, pat))
906+
}
904907
}
905908
},
906909
)
@@ -1047,13 +1050,29 @@ pub(crate) fn repr_nullable_ptr<'tcx>(
10471050
}
10481051
None
10491052
}
1050-
ty::Pat(base, pat) => match **pat {
1051-
ty::PatternKind::Range { .. } => get_nullable_type(tcx, typing_env, *base),
1052-
},
1053+
ty::Pat(base, pat) => get_nullable_type_from_pat(tcx, typing_env, *base, *pat),
10531054
_ => None,
10541055
}
10551056
}
10561057

1058+
fn get_nullable_type_from_pat<'tcx>(
1059+
tcx: TyCtxt<'tcx>,
1060+
typing_env: ty::TypingEnv<'tcx>,
1061+
base: Ty<'tcx>,
1062+
pat: ty::Pattern<'tcx>,
1063+
) -> Option<Ty<'tcx>> {
1064+
match *pat {
1065+
ty::PatternKind::Range { .. } => get_nullable_type(tcx, typing_env, base),
1066+
ty::PatternKind::Or(patterns) => {
1067+
let first = get_nullable_type_from_pat(tcx, typing_env, base, patterns[0])?;
1068+
for &pat in &patterns[1..] {
1069+
assert_eq!(first, get_nullable_type_from_pat(tcx, typing_env, base, pat)?);
1070+
}
1071+
Some(first)
1072+
}
1073+
}
1074+
}
1075+
10571076
impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
10581077
/// Check if the type is array and emit an unsafe type lint.
10591078
fn check_for_array_ty(&mut self, sp: Span, ty: Ty<'tcx>) -> bool {

compiler/rustc_middle/src/ty/codec.rs

+10
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,15 @@ impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D>
419419
}
420420
}
421421

422+
impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D> for ty::List<ty::Pattern<'tcx>> {
423+
fn decode(decoder: &mut D) -> &'tcx Self {
424+
let len = decoder.read_usize();
425+
decoder.interner().mk_patterns_from_iter(
426+
(0..len).map::<ty::Pattern<'tcx>, _>(|_| Decodable::decode(decoder)),
427+
)
428+
}
429+
}
430+
422431
impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D> for ty::List<ty::Const<'tcx>> {
423432
fn decode(decoder: &mut D) -> &'tcx Self {
424433
let len = decoder.read_usize();
@@ -482,6 +491,7 @@ impl_decodable_via_ref! {
482491
&'tcx mir::Body<'tcx>,
483492
&'tcx mir::BorrowCheckResult<'tcx>,
484493
&'tcx ty::List<ty::BoundVariableKind>,
494+
&'tcx ty::List<ty::Pattern<'tcx>>,
485495
&'tcx ty::ListWithCachedTypeInfo<ty::Clause<'tcx>>,
486496
&'tcx ty::List<FieldIdx>,
487497
&'tcx ty::List<(VariantIdx, FieldIdx)>,

compiler/rustc_middle/src/ty/context.rs

+11
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,7 @@ pub struct CtxtInterners<'tcx> {
812812
captures: InternedSet<'tcx, List<&'tcx ty::CapturedPlace<'tcx>>>,
813813
offset_of: InternedSet<'tcx, List<(VariantIdx, FieldIdx)>>,
814814
valtree: InternedSet<'tcx, ty::ValTreeKind<'tcx>>,
815+
patterns: InternedSet<'tcx, List<ty::Pattern<'tcx>>>,
815816
}
816817

817818
impl<'tcx> CtxtInterners<'tcx> {
@@ -848,6 +849,7 @@ impl<'tcx> CtxtInterners<'tcx> {
848849
captures: InternedSet::with_capacity(N),
849850
offset_of: InternedSet::with_capacity(N),
850851
valtree: InternedSet::with_capacity(N),
852+
patterns: InternedSet::with_capacity(N),
851853
}
852854
}
853855

@@ -2594,6 +2596,7 @@ slice_interners!(
25942596
local_def_ids: intern_local_def_ids(LocalDefId),
25952597
captures: intern_captures(&'tcx ty::CapturedPlace<'tcx>),
25962598
offset_of: pub mk_offset_of((VariantIdx, FieldIdx)),
2599+
patterns: pub mk_patterns(Pattern<'tcx>),
25972600
);
25982601

25992602
impl<'tcx> TyCtxt<'tcx> {
@@ -2867,6 +2870,14 @@ impl<'tcx> TyCtxt<'tcx> {
28672870
self.intern_local_def_ids(clauses)
28682871
}
28692872

2873+
pub fn mk_patterns_from_iter<I, T>(self, iter: I) -> T::Output
2874+
where
2875+
I: Iterator<Item = T>,
2876+
T: CollectAndApply<ty::Pattern<'tcx>, &'tcx List<ty::Pattern<'tcx>>>,
2877+
{
2878+
T::collect_and_apply(iter, |xs| self.mk_patterns(xs))
2879+
}
2880+
28702881
pub fn mk_local_def_ids_from_iter<I, T>(self, iter: I) -> T::Output
28712882
where
28722883
I: Iterator<Item = T>,

compiler/rustc_middle/src/ty/flags.rs

+5
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ impl FlagComputation {
259259
self.add_const(start);
260260
self.add_const(end);
261261
}
262+
ty::PatternKind::Or(patterns) => {
263+
for pat in patterns {
264+
self.add_pat(pat);
265+
}
266+
}
262267
}
263268
}
264269

compiler/rustc_middle/src/ty/pattern.rs

+14
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,19 @@ impl<'tcx> fmt::Debug for PatternKind<'tcx> {
5151

5252
write!(f, "..={end}")
5353
}
54+
PatternKind::Or(patterns) => {
55+
write!(f, "(")?;
56+
let mut first = true;
57+
for pat in patterns {
58+
if first {
59+
first = false
60+
} else {
61+
write!(f, " | ")?;
62+
}
63+
write!(f, "{pat:?}")?;
64+
}
65+
write!(f, ")")
66+
}
5467
}
5568
}
5669
}
@@ -59,4 +72,5 @@ impl<'tcx> fmt::Debug for PatternKind<'tcx> {
5972
#[derive(HashStable, TyEncodable, TyDecodable, TypeVisitable, TypeFoldable)]
6073
pub enum PatternKind<'tcx> {
6174
Range { start: ty::Const<'tcx>, end: ty::Const<'tcx> },
75+
Or(&'tcx ty::List<Pattern<'tcx>>),
6276
}

compiler/rustc_middle/src/ty/relate.rs

+9
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ impl<'tcx> Relate<TyCtxt<'tcx>> for ty::Pattern<'tcx> {
5959
let end = relation.relate(end_a, end_b)?;
6060
Ok(tcx.mk_pat(ty::PatternKind::Range { start, end }))
6161
}
62+
(&ty::PatternKind::Or(a), &ty::PatternKind::Or(b)) => {
63+
if a.len() != b.len() {
64+
return Err(TypeError::Mismatch);
65+
}
66+
let v = iter::zip(a, b).map(|(a, b)| relation.relate(a, b));
67+
let patterns = tcx.mk_patterns_from_iter(v)?;
68+
Ok(tcx.mk_pat(ty::PatternKind::Or(patterns)))
69+
}
70+
(ty::PatternKind::Range { .. } | ty::PatternKind::Or(_), _) => Err(TypeError::Mismatch),
6271
}
6372
}
6473
}

compiler/rustc_middle/src/ty/structural_impls.rs

+9
Original file line numberDiff line numberDiff line change
@@ -720,3 +720,12 @@ impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx ty::List<PlaceElem<'tcx>> {
720720
ty::util::fold_list(self, folder, |tcx, v| tcx.mk_place_elems(v))
721721
}
722722
}
723+
724+
impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx ty::List<ty::Pattern<'tcx>> {
725+
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
726+
self,
727+
folder: &mut F,
728+
) -> Result<Self, F::Error> {
729+
ty::util::fold_list(self, folder, |tcx, v| tcx.mk_patterns(v))
730+
}
731+
}

compiler/rustc_middle/src/ty/walk.rs

+5
Original file line numberDiff line numberDiff line change
@@ -217,5 +217,10 @@ fn push_pat<'tcx>(stack: &mut SmallVec<[GenericArg<'tcx>; 8]>, pat: ty::Pattern<
217217
stack.push(end.into());
218218
stack.push(start.into());
219219
}
220+
ty::PatternKind::Or(patterns) => {
221+
for pat in patterns {
222+
push_pat(stack, pat)
223+
}
224+
}
220225
}
221226
}

compiler/rustc_resolve/src/late.rs

+5
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,11 @@ impl<'ra: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'r
957957
self.resolve_anon_const(end, AnonConstKind::ConstArg(IsRepeatExpr::No));
958958
}
959959
}
960+
TyPatKind::Or(patterns) => {
961+
for pat in patterns {
962+
self.visit_ty_pat(pat)
963+
}
964+
}
960965
TyPatKind::Err(_) => {}
961966
}
962967
}

0 commit comments

Comments
 (0)