Skip to content

Commit 236d3cf

Browse files
committed
Split out various pattern type matches into their own function
1 parent d0ecde5 commit 236d3cf

File tree

7 files changed

+131
-90
lines changed

7 files changed

+131
-90
lines changed

compiler/rustc_hir_analysis/src/variance/constraints.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -251,13 +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-
ty::PatternKind::NotNull => {}
260-
}
254+
self.add_constraints_from_pat(current, variance, pat);
261255
self.add_constraints_from_ty(current, typ, variance);
262256
}
263257

@@ -335,6 +329,21 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
335329
}
336330
}
337331

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+
ty::PatternKind::NotNull => {}
344+
}
345+
}
346+
338347
/// Adds constraints appropriate for a nominal type (enum, struct,
339348
/// object, etc) appearing in a context with ambient variance `variance`
340349
fn add_constraints_from_args(

compiler/rustc_lint/src/types.rs

+37-20
Original file line numberDiff line numberDiff line change
@@ -879,26 +879,34 @@ 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-
ty::PatternKind::NotNull => true,
894-
}
895-
},
896-
)
882+
|| pat_ty_is_known_nonnull(tcx, typing_env, *pat)
897883
}
898884
_ => false,
899885
}
900886
}
901887

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+
ty::PatternKind::NotNull => true,
905+
}
906+
},
907+
)
908+
}
909+
902910
/// Given a non-null scalar (or transparent) type `ty`, return the nullable version of that type.
903911
/// If the type passed in was not scalar, returns None.
904912
fn get_nullable_type<'tcx>(
@@ -1040,15 +1048,24 @@ pub(crate) fn repr_nullable_ptr<'tcx>(
10401048
}
10411049
None
10421050
}
1043-
ty::Pat(base, pat) => match **pat {
1044-
ty::PatternKind::NotNull | ty::PatternKind::Range { .. } => {
1045-
get_nullable_type(tcx, typing_env, *base)
1046-
}
1047-
},
1051+
&ty::Pat(base, pat) => get_nullable_type_from_pat(tcx, typing_env, base, pat),
10481052
_ => None,
10491053
}
10501054
}
10511055

1056+
fn get_nullable_type_from_pat<'tcx>(
1057+
tcx: TyCtxt<'tcx>,
1058+
typing_env: ty::TypingEnv<'tcx>,
1059+
base: Ty<'tcx>,
1060+
pat: ty::Pattern<'tcx>,
1061+
) -> Option<Ty<'tcx>> {
1062+
match *pat {
1063+
ty::PatternKind::NotNull | ty::PatternKind::Range { .. } => {
1064+
get_nullable_type(tcx, typing_env, base)
1065+
}
1066+
}
1067+
}
1068+
10521069
impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
10531070
/// Check if the type is array and emit an unsafe type lint.
10541071
fn check_for_array_ty(&mut self, sp: Span, ty: Ty<'tcx>) -> bool {

compiler/rustc_middle/src/ty/flags.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -219,13 +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-
ty::PatternKind::NotNull => {}
228-
}
222+
self.add_pat(pat);
229223
}
230224

231225
&ty::Slice(tt) => self.add_ty(tt),
@@ -259,6 +253,16 @@ impl FlagComputation {
259253
}
260254
}
261255

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+
ty::PatternKind::NotNull => {}
263+
}
264+
}
265+
262266
fn add_predicate(&mut self, binder: ty::Binder<'_, ty::PredicateKind<'_>>) {
263267
self.bound_computation(binder, |computation, atom| computation.add_predicate_atom(atom));
264268
}

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
(ty::PatternKind::NotNull, ty::PatternKind::NotNull) => Ok(a),
6263
(ty::PatternKind::NotNull | ty::PatternKind::Range { .. }, _) => {

compiler/rustc_middle/src/ty/walk.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -136,13 +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-
ty::PatternKind::NotNull => {}
145-
}
139+
push_pat(stack, pat);
146140
stack.push(ty.into());
147141
}
148142
ty::Array(ty, len) => {
@@ -216,3 +210,13 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>)
216210
},
217211
}
218212
}
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+
ty::PatternKind::NotNull => {}
221+
}
222+
}

compiler/rustc_symbol_mangling/src/v0.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,18 @@ 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+
ty::PatternKind::NotNull => self.tcx.types.unit.print(self)?,
209+
})
210+
}
199211
}
200212

201213
impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
@@ -417,18 +429,7 @@ impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
417429
// HACK: constants are used in arrays, even if the types don't match.
418430
self.push("T");
419431
ty.print(self)?;
420-
match *pat {
421-
ty::PatternKind::Range { start, end } => {
422-
let consts = [start, end];
423-
for ct in consts {
424-
Ty::new_array_with_const_len(self.tcx, self.tcx.types.unit, ct)
425-
.print(self)?;
426-
}
427-
}
428-
ty::PatternKind::NotNull => {
429-
self.tcx.types.unit.print(self)?;
430-
}
431-
}
432+
self.print_pat(pat)?;
432433
self.push("E");
433434
}
434435

compiler/rustc_trait_selection/src/traits/wf.rs

+41-36
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,46 @@ 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+
ty::PatternKind::NotNull => {}
694+
}
695+
}
656696
}
657697

658698
impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for WfPredicates<'a, 'tcx> {
@@ -707,42 +747,7 @@ impl<'a, 'tcx> TypeVisitor<TyCtxt<'tcx>> for WfPredicates<'a, 'tcx> {
707747

708748
ty::Pat(subty, pat) => {
709749
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-
ty::PatternKind::NotNull => {}
745-
}
750+
self.check_pat(subty, pat);
746751
}
747752

748753
ty::Tuple(tys) => {

0 commit comments

Comments
 (0)