Skip to content

Commit 4a8b13e

Browse files
committed
Split out various pattern type matches into their own function
1 parent 5b109f9 commit 4a8b13e

File tree

7 files changed

+113
-77
lines changed

7 files changed

+113
-77
lines changed

compiler/rustc_hir_analysis/src/variance/constraints.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,7 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
251251
}
252252

253253
ty::Pat(typ, pat) => {
254-
match *pat {
255-
ty::PatternKind::Range { start, end } => {
256-
self.add_constraints_from_const(current, start, variance);
257-
self.add_constraints_from_const(current, end, variance);
258-
}
259-
}
254+
self.add_constraints_from_pat(current, variance, pat);
260255
self.add_constraints_from_ty(current, typ, variance);
261256
}
262257

@@ -334,6 +329,20 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
334329
}
335330
}
336331

332+
fn add_constraints_from_pat(
333+
&mut self,
334+
current: &CurrentItem,
335+
variance: VarianceTermPtr<'a>,
336+
pat: ty::Pattern<'tcx>,
337+
) {
338+
match *pat {
339+
ty::PatternKind::Range { start, end } => {
340+
self.add_constraints_from_const(current, start, variance);
341+
self.add_constraints_from_const(current, end, variance);
342+
}
343+
}
344+
}
345+
337346
/// Adds constraints appropriate for a nominal type (enum, struct,
338347
/// object, etc) appearing in a context with ambient variance `variance`
339348
fn add_constraints_from_args(

compiler/rustc_lint/src/types.rs

+22-14
Original file line numberDiff line numberDiff line change
@@ -876,25 +876,33 @@ fn ty_is_known_nonnull<'tcx>(
876876
}
877877
ty::Pat(base, pat) => {
878878
ty_is_known_nonnull(tcx, typing_env, *base, mode)
879-
|| Option::unwrap_or_default(
880-
try {
881-
match **pat {
882-
ty::PatternKind::Range { start, end } => {
883-
let start = start.try_to_value()?.try_to_bits(tcx, typing_env)?;
884-
let end = end.try_to_value()?.try_to_bits(tcx, typing_env)?;
885-
886-
// This also works for negative numbers, as we just need
887-
// to ensure we aren't wrapping over zero.
888-
start > 0 && end >= start
889-
}
890-
}
891-
},
892-
)
879+
|| pat_ty_is_known_nonnull(tcx, typing_env, *pat)
893880
}
894881
_ => false,
895882
}
896883
}
897884

885+
fn pat_ty_is_known_nonnull<'tcx>(
886+
tcx: TyCtxt<'tcx>,
887+
typing_env: ty::TypingEnv<'tcx>,
888+
pat: ty::Pattern<'tcx>,
889+
) -> bool {
890+
Option::unwrap_or_default(
891+
try {
892+
match *pat {
893+
ty::PatternKind::Range { start, end } => {
894+
let start = start.try_to_value()?.try_to_bits(tcx, typing_env)?;
895+
let end = end.try_to_value()?.try_to_bits(tcx, typing_env)?;
896+
897+
// This also works for negative numbers, as we just need
898+
// to ensure we aren't wrapping over zero.
899+
start > 0 && end >= start
900+
}
901+
}
902+
},
903+
)
904+
}
905+
898906
/// Given a non-null scalar (or transparent) type `ty`, return the nullable version of that type.
899907
/// If the type passed in was not scalar, returns None.
900908
fn get_nullable_type<'tcx>(

compiler/rustc_middle/src/ty/relate.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,15 @@ impl<'tcx> Relate<TyCtxt<'tcx>> for ty::Pattern<'tcx> {
4949
a: Self,
5050
b: Self,
5151
) -> RelateResult<'tcx, Self> {
52+
let tcx = relation.cx();
5253
match (&*a, &*b) {
5354
(
5455
&ty::PatternKind::Range { start: start_a, end: end_a },
5556
&ty::PatternKind::Range { start: start_b, end: end_b },
5657
) => {
5758
let start = relation.relate(start_a, start_b)?;
5859
let end = relation.relate(end_a, end_b)?;
59-
Ok(relation.cx().mk_pat(ty::PatternKind::Range { start, end }))
60+
Ok(tcx.mk_pat(ty::PatternKind::Range { start, end }))
6061
}
6162
}
6263
}

compiler/rustc_symbol_mangling/src/v0.rs

+19-14
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,17 @@ impl<'tcx> SymbolMangler<'tcx> {
247247

248248
Ok(())
249249
}
250+
251+
fn print_pat(&mut self, pat: ty::Pattern<'tcx>) -> Result<(), std::fmt::Error> {
252+
Ok(match *pat {
253+
ty::PatternKind::Range { start, end } => {
254+
let consts = [start, end];
255+
for ct in consts {
256+
Ty::new_array_with_const_len(self.tcx, self.tcx.types.unit, ct).print(self)?;
257+
}
258+
}
259+
})
260+
}
250261
}
251262

252263
impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
@@ -463,20 +474,14 @@ impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
463474
ty.print(self)?;
464475
}
465476

466-
ty::Pat(ty, pat) => match *pat {
467-
ty::PatternKind::Range { start, end } => {
468-
let consts = [start, end];
469-
// HACK: Represent as tuple until we have something better.
470-
// HACK: constants are used in arrays, even if the types don't match.
471-
self.push("T");
472-
ty.print(self)?;
473-
for ct in consts {
474-
Ty::new_array_with_const_len(self.tcx, self.tcx.types.unit, ct)
475-
.print(self)?;
476-
}
477-
self.push("E");
478-
}
479-
},
477+
ty::Pat(ty, pat) => {
478+
// HACK: Represent as tuple until we have something better.
479+
// HACK: constants are used in arrays, even if the types don't match.
480+
self.push("T");
481+
ty.print(self)?;
482+
self.print_pat(pat)?;
483+
self.push("E");
484+
}
480485

481486
ty::Array(ty, len) => {
482487
self.push("A");

compiler/rustc_trait_selection/src/traits/wf.rs

+40-35
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,45 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
653653
}
654654
}
655655
}
656+
657+
fn check_pat(&mut self, subty: Ty<'tcx>, pat: ty::Pattern<'tcx>) {
658+
let tcx = self.tcx();
659+
match *pat {
660+
ty::PatternKind::Range { start, end } => {
661+
let mut check = |c| {
662+
let cause = self.cause(ObligationCauseCode::Misc);
663+
self.out.push(traits::Obligation::with_depth(
664+
tcx,
665+
cause.clone(),
666+
self.recursion_depth,
667+
self.param_env,
668+
ty::Binder::dummy(ty::PredicateKind::Clause(
669+
ty::ClauseKind::ConstArgHasType(c, subty),
670+
)),
671+
));
672+
if !tcx.features().generic_pattern_types() {
673+
if c.has_param() {
674+
if self.span.is_dummy() {
675+
self.tcx()
676+
.dcx()
677+
.delayed_bug("feature error should be reported elsewhere, too");
678+
} else {
679+
feature_err(
680+
&self.tcx().sess,
681+
sym::generic_pattern_types,
682+
self.span,
683+
"wraparound pattern type ranges cause monomorphization time errors",
684+
)
685+
.emit();
686+
}
687+
}
688+
}
689+
};
690+
check(start);
691+
check(end);
692+
}
693+
}
694+
}
656695
}
657696

658697
impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for WfPredicates<'a, 'tcx> {
@@ -707,41 +746,7 @@ impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for WfPredicates<'a, 'tcx> {
707746

708747
ty::Pat(subty, pat) => {
709748
self.require_sized(subty, ObligationCauseCode::Misc);
710-
match *pat {
711-
ty::PatternKind::Range { start, end } => {
712-
let mut check = |c| {
713-
let cause = self.cause(ObligationCauseCode::Misc);
714-
self.out.push(traits::Obligation::with_depth(
715-
tcx,
716-
cause.clone(),
717-
self.recursion_depth,
718-
self.param_env,
719-
ty::Binder::dummy(ty::PredicateKind::Clause(
720-
ty::ClauseKind::ConstArgHasType(c, subty),
721-
)),
722-
));
723-
if !tcx.features().generic_pattern_types() {
724-
if c.has_param() {
725-
if self.span.is_dummy() {
726-
self.tcx().dcx().delayed_bug(
727-
"feature error should be reported elsewhere, too",
728-
);
729-
} else {
730-
feature_err(
731-
&self.tcx().sess,
732-
sym::generic_pattern_types,
733-
self.span,
734-
"wraparound pattern type ranges cause monomorphization time errors",
735-
)
736-
.emit();
737-
}
738-
}
739-
}
740-
};
741-
check(start);
742-
check(end);
743-
}
744-
}
749+
self.check_pat(subty, pat);
745750
}
746751

747752
ty::Tuple(tys) => {

compiler/rustc_type_ir/src/flags.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl<I: Interner> FlagComputation<I> {
306306

307307
ty::Pat(ty, pat) => {
308308
self.add_ty(ty);
309-
self.add_flags(pat.flags());
309+
self.add_ty_pat(pat);
310310
}
311311

312312
ty::Slice(tt) => self.add_ty(tt),
@@ -340,6 +340,10 @@ impl<I: Interner> FlagComputation<I> {
340340
}
341341
}
342342

343+
fn add_ty_pat(&mut self, pat: <I as Interner>::Pat) {
344+
self.add_flags(pat.flags());
345+
}
346+
343347
fn add_predicate(&mut self, binder: ty::Binder<I, ty::PredicateKind<I>>) {
344348
self.bound_computation(binder, |computation, atom| computation.add_predicate_atom(atom));
345349
}

compiler/rustc_type_ir/src/walk.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,7 @@ fn push_inner<I: Interner>(stack: &mut TypeWalkerStack<I>, parent: I::GenericArg
8989
| ty::Foreign(..) => {}
9090

9191
ty::Pat(ty, pat) => {
92-
match pat.kind() {
93-
ty::PatternKind::Range { start, end } => {
94-
stack.push(end.into());
95-
stack.push(start.into());
96-
}
97-
}
92+
push_ty_pat::<I>(stack, pat);
9893
stack.push(ty.into());
9994
}
10095
ty::Array(ty, len) => {
@@ -171,3 +166,12 @@ fn push_inner<I: Interner>(stack: &mut TypeWalkerStack<I>, parent: I::GenericArg
171166
},
172167
}
173168
}
169+
170+
fn push_ty_pat<I: Interner>(stack: &mut TypeWalkerStack<I>, pat: I::Pat) {
171+
match pat.kind() {
172+
ty::PatternKind::Range { start, end } => {
173+
stack.push(end.into());
174+
stack.push(start.into());
175+
}
176+
}
177+
}

0 commit comments

Comments
 (0)