Skip to content

Commit 7d8ed78

Browse files
Speed up IntRange::from_pat
Previously, this method called the more general `pat_constructor` function, which can return other pattern variants besides `IntRange`. Then it throws away any non-`IntRange` variants. Specialize it so work is only done when it could result in an `IntRange`.
1 parent e0bc267 commit 7d8ed78

File tree

1 file changed

+27
-3
lines changed
  • compiler/rustc_mir_build/src/thir/pattern

1 file changed

+27
-3
lines changed

compiler/rustc_mir_build/src/thir/pattern/_match.rs

+27-3
Original file line numberDiff line numberDiff line change
@@ -1644,9 +1644,32 @@ impl<'tcx> IntRange<'tcx> {
16441644
param_env: ty::ParamEnv<'tcx>,
16451645
pat: &Pat<'tcx>,
16461646
) -> Option<IntRange<'tcx>> {
1647-
match pat_constructor(tcx, param_env, pat)? {
1648-
IntRange(range) => Some(range),
1649-
_ => None,
1647+
// This MUST be kept in sync with `pat_constructor`.
1648+
match *pat.kind {
1649+
PatKind::AscribeUserType { .. } => bug!(), // Handled by `expand_pattern`
1650+
PatKind::Or { .. } => bug!("Or-pattern should have been expanded earlier on."),
1651+
1652+
PatKind::Binding { .. }
1653+
| PatKind::Wild
1654+
| PatKind::Leaf { .. }
1655+
| PatKind::Deref { .. }
1656+
| PatKind::Variant { .. }
1657+
| PatKind::Array { .. }
1658+
| PatKind::Slice { .. } => None,
1659+
1660+
PatKind::Constant { value } => Self::from_const(tcx, param_env, value, pat.span),
1661+
1662+
PatKind::Range(PatRange { lo, hi, end }) => {
1663+
let ty = lo.ty;
1664+
Self::from_range(
1665+
tcx,
1666+
lo.eval_bits(tcx, param_env, lo.ty),
1667+
hi.eval_bits(tcx, param_env, hi.ty),
1668+
ty,
1669+
&end,
1670+
pat.span,
1671+
)
1672+
}
16501673
}
16511674
}
16521675

@@ -2053,6 +2076,7 @@ fn pat_constructor<'tcx>(
20532076
param_env: ty::ParamEnv<'tcx>,
20542077
pat: &Pat<'tcx>,
20552078
) -> Option<Constructor<'tcx>> {
2079+
// This MUST be kept in sync with `IntRange::from_pat`.
20562080
match *pat.kind {
20572081
PatKind::AscribeUserType { .. } => bug!(), // Handled by `expand_pattern`
20582082
PatKind::Binding { .. } | PatKind::Wild => None,

0 commit comments

Comments
 (0)