Skip to content

Commit bb6c249

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 1ec980d commit bb6c249

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
@@ -1787,9 +1787,32 @@ impl<'tcx> IntRange<'tcx> {
17871787
param_env: ty::ParamEnv<'tcx>,
17881788
pat: &Pat<'tcx>,
17891789
) -> Option<IntRange<'tcx>> {
1790-
match pat_constructor(tcx, param_env, pat)? {
1791-
IntRange(range) => Some(range),
1792-
_ => None,
1790+
// This MUST be kept in sync with `pat_constructor`.
1791+
match *pat.kind {
1792+
PatKind::AscribeUserType { .. } => bug!(), // Handled by `expand_pattern`
1793+
PatKind::Or { .. } => bug!("Or-pattern should have been expanded earlier on."),
1794+
1795+
PatKind::Binding { .. }
1796+
| PatKind::Wild
1797+
| PatKind::Leaf { .. }
1798+
| PatKind::Deref { .. }
1799+
| PatKind::Variant { .. }
1800+
| PatKind::Array { .. }
1801+
| PatKind::Slice { .. } => None,
1802+
1803+
PatKind::Constant { value } => Self::from_const(tcx, param_env, value, pat.span),
1804+
1805+
PatKind::Range(PatRange { lo, hi, end }) => {
1806+
let ty = lo.ty;
1807+
Self::from_range(
1808+
tcx,
1809+
lo.eval_bits(tcx, param_env, lo.ty),
1810+
hi.eval_bits(tcx, param_env, hi.ty),
1811+
ty,
1812+
&end,
1813+
pat.span,
1814+
)
1815+
}
17931816
}
17941817
}
17951818

@@ -2196,6 +2219,7 @@ fn pat_constructor<'tcx>(
21962219
param_env: ty::ParamEnv<'tcx>,
21972220
pat: &Pat<'tcx>,
21982221
) -> Option<Constructor<'tcx>> {
2222+
// This MUST be kept in sync with `IntRange::from_pat`.
21992223
match *pat.kind {
22002224
PatKind::AscribeUserType { .. } => bug!(), // Handled by `expand_pattern`
22012225
PatKind::Binding { .. } | PatKind::Wild => None,

0 commit comments

Comments
 (0)