Skip to content

Commit d9ed118

Browse files
lower bare boolean expression with if-construct
1 parent e5453b4 commit d9ed118

File tree

3 files changed

+31
-53
lines changed

3 files changed

+31
-53
lines changed

compiler/rustc_mir_build/src/build/expr/into.rs

+29-39
Original file line numberDiff line numberDiff line change
@@ -158,53 +158,43 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
158158
end_block.unit()
159159
}
160160
}
161-
ExprKind::LogicalOp { op, lhs, rhs } => {
162-
// And:
163-
//
164-
// [block: If(lhs)] -true-> [else_block: dest = (rhs)]
165-
// | (false)
166-
// [shortcircuit_block: dest = false]
167-
//
168-
// Or:
169-
//
170-
// [block: If(lhs)] -false-> [else_block: dest = (rhs)]
171-
// | (true)
172-
// [shortcircuit_block: dest = true]
173-
174-
let (shortcircuit_block, mut else_block, join_block) = (
175-
this.cfg.start_new_block(),
176-
this.cfg.start_new_block(),
177-
this.cfg.start_new_block(),
161+
ExprKind::LogicalOp { .. } => {
162+
let condition_scope = this.local_scope();
163+
let source_info = this.source_info(expr.span);
164+
let (then_block, else_block) =
165+
this.in_if_then_scope(condition_scope, expr.span, |this| {
166+
this.then_else_break(
167+
block,
168+
expr,
169+
Some(condition_scope),
170+
condition_scope,
171+
source_info,
172+
)
173+
});
174+
this.cfg.push_assign_constant(
175+
then_block,
176+
source_info,
177+
destination,
178+
Constant {
179+
span: expr.span,
180+
user_ty: None,
181+
literal: ConstantKind::from_bool(this.tcx, true),
182+
},
178183
);
179-
180-
let lhs = unpack!(block = this.as_local_operand(block, &this.thir[lhs]));
181-
let blocks = match op {
182-
LogicalOp::And => (else_block, shortcircuit_block),
183-
LogicalOp::Or => (shortcircuit_block, else_block),
184-
};
185-
let term = TerminatorKind::if_(lhs, blocks.0, blocks.1);
186-
this.cfg.terminate(block, source_info, term);
187-
188184
this.cfg.push_assign_constant(
189-
shortcircuit_block,
185+
else_block,
190186
source_info,
191187
destination,
192188
Constant {
193-
span: expr_span,
189+
span: expr.span,
194190
user_ty: None,
195-
literal: match op {
196-
LogicalOp::And => ConstantKind::from_bool(this.tcx, false),
197-
LogicalOp::Or => ConstantKind::from_bool(this.tcx, true),
198-
},
191+
literal: ConstantKind::from_bool(this.tcx, false),
199192
},
200193
);
201-
this.cfg.goto(shortcircuit_block, source_info, join_block);
202-
203-
let rhs = unpack!(else_block = this.as_local_operand(else_block, &this.thir[rhs]));
204-
this.cfg.push_assign(else_block, source_info, destination, Rvalue::Use(rhs));
205-
this.cfg.goto(else_block, source_info, join_block);
206-
207-
join_block.unit()
194+
let target = this.cfg.start_new_block();
195+
this.cfg.goto(then_block, source_info, target);
196+
this.cfg.goto(else_block, source_info, target);
197+
target.unit()
208198
}
209199
ExprKind::Loop { body } => {
210200
// [block]

tests/ui/rfcs/rfc-2497-if-let-chains/chains-without-let.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// check-pass
2+
13
fn and_chain() {
24
let z;
35
if true && { z = 3; true} && z == 3 {}
@@ -6,7 +8,6 @@ fn and_chain() {
68
fn and_chain_2() {
79
let z;
810
true && { z = 3; true} && z == 3;
9-
//~^ ERROR E0381
1011
}
1112

1213
fn or_chain() {

tests/ui/rfcs/rfc-2497-if-let-chains/chains-without-let.stderr

-13
This file was deleted.

0 commit comments

Comments
 (0)