Skip to content

Commit cb22e52

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

File tree

7 files changed

+118
-82
lines changed

7 files changed

+118
-82
lines changed

Diff for: 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(

Diff for: compiler/rustc_lint/src/types.rs

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

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

Diff for: compiler/rustc_middle/src/ty/flags.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,7 @@ impl FlagComputation {
219219

220220
&ty::Pat(ty, pat) => {
221221
self.add_ty(ty);
222-
match *pat {
223-
ty::PatternKind::Range { start, end } => {
224-
self.add_const(start);
225-
self.add_const(end);
226-
}
227-
}
222+
self.add_pat(pat);
228223
}
229224

230225
&ty::Slice(tt) => self.add_ty(tt),
@@ -258,6 +253,15 @@ impl FlagComputation {
258253
}
259254
}
260255

256+
fn add_pat(&mut self, pat: ty::Pattern<'_>) {
257+
match *pat {
258+
ty::PatternKind::Range { start, end } => {
259+
self.add_const(start);
260+
self.add_const(end);
261+
}
262+
}
263+
}
264+
261265
fn add_predicate(&mut self, binder: ty::Binder<'_, ty::PredicateKind<'_>>) {
262266
self.bound_computation(binder, |computation, atom| computation.add_predicate_atom(atom));
263267
}

Diff for: 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
}

Diff for: compiler/rustc_middle/src/ty/walk.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,7 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
136136
| ty::Foreign(..) => {}
137137

138138
ty::Pat(ty, pat) => {
139-
match *pat {
140-
ty::PatternKind::Range { start, end } => {
141-
stack.push(end.into());
142-
stack.push(start.into());
143-
}
144-
}
139+
push_pat(stack, pat);
145140
stack.push(ty.into());
146141
}
147142
ty::Array(ty, len) => {
@@ -215,3 +210,12 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
215210
},
216211
}
217212
}
213+
214+
fn push_pat<'tcx>(stack: &mut SmallVec<[GenericArg<'tcx>; 8]>, pat: ty::Pattern<'tcx>) {
215+
match *pat {
216+
ty::PatternKind::Range { start, end } => {
217+
stack.push(end.into());
218+
stack.push(start.into());
219+
}
220+
}
221+
}

Diff for: compiler/rustc_symbol_mangling/src/v0.rs

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

197197
Ok(())
198198
}
199+
200+
fn print_pat(&mut self, pat: ty::Pattern<'tcx>) -> Result<(), std::fmt::Error> {
201+
Ok(match *pat {
202+
ty::PatternKind::Range { start, end } => {
203+
let consts = [start, end];
204+
for ct in consts {
205+
Ty::new_array_with_const_len(self.tcx, self.tcx.types.unit, ct).print(self)?;
206+
}
207+
}
208+
})
209+
}
199210
}
200211

201212
impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
@@ -412,20 +423,14 @@ impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
412423
ty.print(self)?;
413424
}
414425

415-
ty::Pat(ty, pat) => match *pat {
416-
ty::PatternKind::Range { start, end } => {
417-
let consts = [start, end];
418-
// HACK: Represent as tuple until we have something better.
419-
// HACK: constants are used in arrays, even if the types don't match.
420-
self.push("T");
421-
ty.print(self)?;
422-
for ct in consts {
423-
Ty::new_array_with_const_len(self.tcx, self.tcx.types.unit, ct)
424-
.print(self)?;
425-
}
426-
self.push("E");
427-
}
428-
},
426+
ty::Pat(ty, pat) => {
427+
// HACK: Represent as tuple until we have something better.
428+
// HACK: constants are used in arrays, even if the types don't match.
429+
self.push("T");
430+
ty.print(self)?;
431+
self.print_pat(pat)?;
432+
self.push("E");
433+
}
429434

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

Diff for: 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) => {

0 commit comments

Comments
 (0)