Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions compiler/rustc_mir_build/src/builder/expr/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
this.diverge_from(loop_block);

// Logic for `match`.
let scrutinee_place_builder =
unpack!(body_block = this.as_place_builder(body_block, scrutinee));
let scrutinee_span = this.thir.exprs[scrutinee].span;
let scrutinee_place_builder = unpack!(
body_block = this.lower_scrutinee(body_block, scrutinee, scrutinee_span)
);

Comment on lines -298 to +302
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change adds a place mention of the scrutinee that was missing before.

let match_start_span = match_span.shrink_to_lo().to(scrutinee_span);

let mut patterns = Vec::with_capacity(arms.len());
Expand Down Expand Up @@ -345,7 +347,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
expr_span,
|this| {
this.lower_match_arms(
destination,
state_place,
scrutinee_place_builder,
scrutinee_span,
arms,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/builder/matches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}

/// Evaluate the scrutinee and add the PlaceMention for it.
fn lower_scrutinee(
pub(crate) fn lower_scrutinee(
&mut self,
mut block: BasicBlock,
scrutinee_id: ExprId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// MIR for `break_to_block_unit` after built

fn break_to_block_unit() -> u8 {
let mut _0: u8;
let mut _1: i32;
let mut _2: !;
scope 1 {
debug state => _1;
}

bb0: {
StorageLive(_1);
_1 = const 0_i32;
FakeRead(ForLet(None), _1);
StorageLive(_2);
goto -> bb1;
}

bb1: {
falseUnwind -> [real: bb2, unwind: bb10];
}

bb2: {
PlaceMention(_1);
_1 = const 2_i32;
goto -> bb5;
}

bb3: {
FakeRead(ForMatchedPlace(None), _1);
unreachable;
}

bb4: {
goto -> bb6;
}

bb5: {
goto -> bb6;
}

bb6: {
goto -> bb7;
}

bb7: {
goto -> bb1;
}

bb8: {
unreachable;
}

bb9: {
StorageDead(_2);
StorageDead(_1);
return;
}

bb10 (cleanup): {
resume;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// MIR for `infinite_a` after built

fn infinite_a(_1: u8) -> () {
debug state => _1;
let mut _0: ();
let mut _2: !;
let _3: u8;
scope 1 {
debug a => _3;
}

bb0: {
StorageLive(_2);
goto -> bb1;
}

bb1: {
falseUnwind -> [real: bb2, unwind: bb7];
}

bb2: {
PlaceMention(_1);
StorageLive(_3);
_3 = copy _1;
_1 = copy _3;
StorageDead(_3);
goto -> bb4;
}

bb3: {
FakeRead(ForMatchedPlace(None), _1);
unreachable;
}

bb4: {
goto -> bb1;
}

bb5: {
unreachable;
}

bb6: {
StorageDead(_2);
return;
}

bb7 (cleanup): {
resume;
}
}
68 changes: 68 additions & 0 deletions tests/mir-opt/building/loop_match_diverges.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// skip-filecheck
#![allow(incomplete_features)]
#![feature(loop_match)]
#![crate_type = "lib"]

// Test that a #[loop_match] without an explicit break from the loop generates valid MIR.

enum State {
A,
B,
C,
}

// EMIT_MIR loop_match_diverges.simple.built.after.mir
fn simple(mut state: State) -> State {
#[loop_match]
'a: loop {
state = 'blk: {
match state {
State::A => {
#[const_continue]
break 'blk State::B;
}
State::B => {
if true {
#[const_continue]
break 'blk State::C;
} else {
#[const_continue]
break 'blk State::A;
}
}
State::C => break 'a,
}
};
}

state
}

// EMIT_MIR loop_match_diverges.break_to_block_unit.built.after.mir
#[unsafe(no_mangle)]
fn break_to_block_unit() -> u8 {
let mut state = 0;
#[loop_match]
loop {
state = 'blk: {
match state {
_ => 'b: {
break 'b 2;
}
}
}
}
}

// EMIT_MIR loop_match_diverges.infinite_a.built.after.mir
#[unsafe(no_mangle)]
fn infinite_a(mut state: u8) {
#[loop_match]
loop {
state = 'blk: {
match state {
a => a,
}
}
}
}
Loading
Loading