Skip to content

Commit c0c930d

Browse files
committed
Uncomplicate check_let_chain
1 parent 928722f commit c0c930d

File tree

1 file changed

+35
-45
lines changed

1 file changed

+35
-45
lines changed

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

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for MatchVisitor<'a, '_, 'tcx> {
150150
ExprKind::Let { box ref pat, expr } => {
151151
self.check_let(pat, expr, self.let_source, ex.span);
152152
}
153-
ExprKind::LogicalOp { op: LogicalOp::And, lhs, rhs } => {
154-
self.check_let_chain(self.let_source, ex.span, lhs, rhs);
153+
ExprKind::LogicalOp { op: LogicalOp::And, .. } => {
154+
self.check_let_chain(self.let_source, ex);
155155
}
156156
_ => {}
157157
};
@@ -326,71 +326,61 @@ impl<'p, 'tcx> MatchVisitor<'_, 'p, 'tcx> {
326326
}
327327

328328
#[instrument(level = "trace", skip(self))]
329-
fn check_let_chain(
330-
&mut self,
331-
let_source: LetSource,
332-
top_expr_span: Span,
333-
mut lhs: ExprId,
334-
rhs: ExprId,
335-
) {
329+
fn check_let_chain(&mut self, let_source: LetSource, expr: &Expr<'tcx>) {
336330
if let LetSource::None = let_source {
337331
return;
338332
}
339333

340-
// Lint level enclosing the next `lhs`.
341-
let mut cur_lint_level = self.lint_level;
334+
let top_expr_span = expr.span;
335+
336+
// Lint level enclosing `next_expr`.
337+
let mut next_expr_lint_level = self.lint_level;
342338

343339
// Obtain the refutabilities of all exprs in the chain,
344340
// and record chain members that aren't let exprs.
345341
let mut chain_refutabilities = Vec::new();
346342

347343
let mut error = Ok(());
348-
let mut add = |expr: ExprId, mut local_lint_level| {
349-
// `local_lint_level` is the lint level enclosing the pattern inside `expr`.
350-
let mut expr = &self.thir[expr];
351-
debug!(?expr, ?local_lint_level, "add");
344+
let mut next_expr = Some(expr);
345+
while let Some(mut expr) = next_expr {
346+
while let ExprKind::Scope { value, lint_level, .. } = expr.kind {
347+
if let LintLevel::Explicit(hir_id) = lint_level {
348+
next_expr_lint_level = hir_id
349+
}
350+
expr = &self.thir[value];
351+
}
352+
if let ExprKind::LogicalOp { op: LogicalOp::And, lhs, rhs } = expr.kind {
353+
expr = &self.thir[rhs];
354+
// Let chains recurse on the left, so we recurse into the lhs.
355+
next_expr = Some(&self.thir[lhs]);
356+
} else {
357+
next_expr = None;
358+
}
359+
360+
// Lint level enclosing `expr`.
361+
let mut expr_lint_level = next_expr_lint_level;
352362
// Fast-forward through scopes.
353363
while let ExprKind::Scope { value, lint_level, .. } = expr.kind {
354364
if let LintLevel::Explicit(hir_id) = lint_level {
355-
local_lint_level = hir_id
365+
expr_lint_level = hir_id
356366
}
357367
expr = &self.thir[value];
358368
}
359-
debug!(?expr, ?local_lint_level, "after scopes");
360-
match expr.kind {
369+
let value = match expr.kind {
361370
ExprKind::Let { box ref pat, expr: _ } => {
362371
if let Err(err) = pat.pat_error_reported() {
363372
error = Err(err);
364-
return None;
373+
None
374+
} else {
375+
let mut ncx = self.new_cx(expr_lint_level, true);
376+
let tpat = self.lower_pattern(&mut ncx, pat);
377+
let refutable = !is_let_irrefutable(&mut ncx, expr_lint_level, tpat);
378+
Some((expr.span, refutable))
365379
}
366-
let mut ncx = self.new_cx(local_lint_level, true);
367-
let tpat = self.lower_pattern(&mut ncx, pat);
368-
let refutable = !is_let_irrefutable(&mut ncx, local_lint_level, tpat);
369-
Some((expr.span, refutable))
370380
}
371381
_ => None,
372-
}
373-
};
374-
375-
// Let chains recurse on the left, so we start by adding the rightmost.
376-
chain_refutabilities.push(add(rhs, cur_lint_level));
377-
378-
loop {
379-
while let ExprKind::Scope { value, lint_level, .. } = self.thir[lhs].kind {
380-
if let LintLevel::Explicit(hir_id) = lint_level {
381-
cur_lint_level = hir_id
382-
}
383-
lhs = value;
384-
}
385-
if let ExprKind::LogicalOp { op: LogicalOp::And, lhs: new_lhs, rhs: expr } =
386-
self.thir[lhs].kind
387-
{
388-
chain_refutabilities.push(add(expr, cur_lint_level));
389-
lhs = new_lhs;
390-
} else {
391-
chain_refutabilities.push(add(lhs, cur_lint_level));
392-
break;
393-
}
382+
};
383+
chain_refutabilities.push(value);
394384
}
395385
debug!(?chain_refutabilities);
396386
chain_refutabilities.reverse();

0 commit comments

Comments
 (0)