Skip to content

Commit 2fb1261

Browse files
committed
Simplify the pattern unpeeling in lower_pattern_range_endpoint
1 parent 85f4cdc commit 2fb1261

File tree

1 file changed

+25
-26
lines changed
  • compiler/rustc_mir_build/src/thir/pattern

1 file changed

+25
-26
lines changed

Diff for: compiler/rustc_mir_build/src/thir/pattern/mod.rs

+25-26
Original file line numberDiff line numberDiff line change
@@ -161,35 +161,34 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
161161
) -> Result<Option<PatRangeBoundary<'tcx>>, ErrorGuaranteed> {
162162
let Some(expr) = expr else { return Ok(None) };
163163

164-
let (kind, ascr, inline_const) = match self.lower_lit(expr) {
165-
PatKind::ExpandedConstant { subpattern, def_id, is_inline: true } => {
166-
(subpattern.kind, None, def_id.as_local())
167-
}
168-
PatKind::ExpandedConstant { subpattern, is_inline: false, .. } => {
169-
(subpattern.kind, None, None)
170-
}
171-
PatKind::AscribeUserType { ascription, subpattern: box Pat { kind, .. } } => {
172-
(kind, Some(ascription), None)
173-
}
174-
kind => (kind, None, None),
175-
};
176-
let value = match kind {
177-
PatKind::Constant { value } => value,
178-
PatKind::ExpandedConstant { subpattern, .. }
179-
if let PatKind::Constant { value } = subpattern.kind =>
180-
{
181-
value
182-
}
183-
_ => {
184-
let msg = format!(
185-
"found bad range pattern endpoint `{expr:?}` outside of error recovery"
186-
);
187-
return Err(self.tcx.dcx().span_delayed_bug(expr.span, msg));
164+
// Lower the endpoint into a temporary `PatKind` that will then be
165+
// deconstructed to obtain the constant value and other data.
166+
let mut kind: PatKind<'tcx> = self.lower_lit(expr);
167+
168+
// Unpeel any ascription or inline-const wrapper nodes.
169+
loop {
170+
match kind {
171+
PatKind::AscribeUserType { ascription, subpattern } => {
172+
ascriptions.push(ascription);
173+
kind = subpattern.kind;
174+
}
175+
PatKind::ExpandedConstant { is_inline, def_id, subpattern } => {
176+
if is_inline {
177+
inline_consts.extend(def_id.as_local());
178+
}
179+
kind = subpattern.kind;
180+
}
181+
_ => break,
188182
}
183+
}
184+
185+
// The unpeeled kind should now be a constant, giving us the endpoint value.
186+
let PatKind::Constant { value } = kind else {
187+
let msg =
188+
format!("found bad range pattern endpoint `{expr:?}` outside of error recovery");
189+
return Err(self.tcx.dcx().span_delayed_bug(expr.span, msg));
189190
};
190191

191-
ascriptions.extend(ascr);
192-
inline_consts.extend(inline_const);
193192
Ok(Some(PatRangeBoundary::Finite(value)))
194193
}
195194

0 commit comments

Comments
 (0)