Skip to content

fix #[loop_match] on diverging loop #144783

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
44 changes: 44 additions & 0 deletions tests/ui/loop-match/diverges.rs
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mind making some of those tests MIR building tests? In tests/mir-opt/building.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//@ build-pass
//@ compile-flags: -Zvalidate-mir
#![allow(incomplete_features)]
#![feature(loop_match)]
#![crate_type = "lib"]

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

fn break_to_block_unit() -> u8 {
let mut state = 0;
#[loop_match]
loop {
state = 'blk: {
match state {
_ => 'b: {
break 'b 2;
}
}
}
}
}

fn break_to_block_value() -> u8 {
let mut state = 0u8;
#[loop_match]
'a: loop {
state = 'blk: {
match state {
_ => break 'blk state,
}
}
}
}

fn infinite_a(mut state: u8) {
#[loop_match]
loop {
state = 'blk: {
match state {
a => a,
}
}
}
}
Loading