Skip to content

Commit cb6d371

Browse files
committed
Split out various pattern type matches into their own function
1 parent 6008592 commit cb6d371

File tree

7 files changed

+115
-79
lines changed

7 files changed

+115
-79
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
@@ -878,25 +878,33 @@ fn ty_is_known_nonnull<'tcx>(
878878
}
879879
ty::Pat(base, pat) => {
880880
ty_is_known_nonnull(tcx, typing_env, *base, mode)
881-
|| Option::unwrap_or_default(
882-
try {
883-
match **pat {
884-
ty::PatternKind::Range { start, end } => {
885-
let start = start.try_to_value()?.try_to_bits(tcx, typing_env)?;
886-
let end = end.try_to_value()?.try_to_bits(tcx, typing_env)?;
887-
888-
// This also works for negative numbers, as we just need
889-
// to ensure we aren't wrapping over zero.
890-
start > 0 && end >= start
891-
}
892-
}
893-
},
894-
)
881+
|| pat_ty_is_known_nonnull(tcx, typing_env, *pat)
895882
}
896883
_ => false,
897884
}
898885
}
899886

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

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

663702
impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for WfPredicates<'a, 'tcx> {
@@ -710,43 +749,9 @@ impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for WfPredicates<'a, 'tcx> {
710749
));
711750
}
712751

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

752757
ty::Tuple(tys) => {

compiler/rustc_type_ir/src/flags.rs

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

305305
ty::Pat(ty, pat) => {
306306
self.add_ty(ty);
307-
self.add_flags(pat.flags());
307+
self.add_ty_pat(pat);
308308
}
309309

310310
ty::Slice(tt) => self.add_ty(tt),
@@ -338,6 +338,10 @@ impl<I: Interner> FlagComputation<I> {
338338
}
339339
}
340340

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

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)