Skip to content

Commit

Permalink
fix: Check whether next_node is else-less if in get_return_block
Browse files Browse the repository at this point in the history
Fix rust-lang#124819, where a if-less block causes a wrong output. It is
caused by get_return_block in get_fn_decl. In get_return_block,
when a else-less if expression is the tail expression, the check
for next_node will keep iterating. So it is necessary to make a
early return in the check.
  • Loading branch information
cardigan1008 committed May 9, 2024
1 parent c54301f commit 62318b3
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ impl<'hir> Map<'hir> {
Node::Block(Block { expr: None, .. }) => return None,
// The current node is not the tail expression of its parent.
Node::Block(Block { expr: Some(e), .. }) if hir_id != e.hir_id => return None,
Node::Block(Block { expr: Some(e), ..}) if matches!(e.kind, ExprKind::If(_, _, None)) => return None,
_ => {}
}
}
Expand All @@ -563,9 +564,6 @@ impl<'hir> Map<'hir> {
// We verify that indirectly by checking that the previous node is the
// current node's body
if node.body_id().map(|b| b.hir_id) == prev_hir_id => {
if let Node::Expr(Expr { kind: ExprKind::Block(_, _), ..}) = self.tcx.hir_node(prev_hir_id.unwrap()) {
return None;
}
return Some(hir_id)
}
// Ignore `return`s on the first iteration
Expand Down

0 comments on commit 62318b3

Please sign in to comment.