1
Fork 0

Simplify the pattern unpeeling in lower_pattern_range_endpoint

This commit is contained in:
Zalathar 2025-02-03 14:29:13 +11:00
parent 85f4cdc626
commit 2fb1261c21

View file

@ -161,35 +161,34 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
) -> Result<Option<PatRangeBoundary<'tcx>>, ErrorGuaranteed> { ) -> Result<Option<PatRangeBoundary<'tcx>>, ErrorGuaranteed> {
let Some(expr) = expr else { return Ok(None) }; let Some(expr) = expr else { return Ok(None) };
let (kind, ascr, inline_const) = match self.lower_lit(expr) { // Lower the endpoint into a temporary `PatKind` that will then be
PatKind::ExpandedConstant { subpattern, def_id, is_inline: true } => { // deconstructed to obtain the constant value and other data.
(subpattern.kind, None, def_id.as_local()) let mut kind: PatKind<'tcx> = self.lower_lit(expr);
}
PatKind::ExpandedConstant { subpattern, is_inline: false, .. } => { // Unpeel any ascription or inline-const wrapper nodes.
(subpattern.kind, None, None) loop {
} match kind {
PatKind::AscribeUserType { ascription, subpattern: box Pat { kind, .. } } => { PatKind::AscribeUserType { ascription, subpattern } => {
(kind, Some(ascription), None) ascriptions.push(ascription);
} kind = subpattern.kind;
kind => (kind, None, None), }
}; PatKind::ExpandedConstant { is_inline, def_id, subpattern } => {
let value = match kind { if is_inline {
PatKind::Constant { value } => value, inline_consts.extend(def_id.as_local());
PatKind::ExpandedConstant { subpattern, .. } }
if let PatKind::Constant { value } = subpattern.kind => kind = subpattern.kind;
{ }
value _ => break,
}
_ => {
let msg = format!(
"found bad range pattern endpoint `{expr:?}` outside of error recovery"
);
return Err(self.tcx.dcx().span_delayed_bug(expr.span, msg));
} }
}
// The unpeeled kind should now be a constant, giving us the endpoint value.
let PatKind::Constant { value } = kind else {
let msg =
format!("found bad range pattern endpoint `{expr:?}` outside of error recovery");
return Err(self.tcx.dcx().span_delayed_bug(expr.span, msg));
}; };
ascriptions.extend(ascr);
inline_consts.extend(inline_const);
Ok(Some(PatRangeBoundary::Finite(value))) Ok(Some(PatRangeBoundary::Finite(value)))
} }