Skip to content

fix: Reject upvar scrutinees for loop_match #144451

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

Merged
merged 1 commit into from
Jul 29, 2025
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
21 changes: 14 additions & 7 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,21 +955,25 @@ impl<'tcx> ThirBuildCx<'tcx> {
dcx.emit_fatal(LoopMatchBadRhs { span: block_body_expr.span })
};

fn local(expr: &rustc_hir::Expr<'_>) -> Option<hir::HirId> {
fn local(
cx: &mut ThirBuildCx<'_>,
expr: &rustc_hir::Expr<'_>,
) -> Option<hir::HirId> {
if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = expr.kind
&& let Res::Local(hir_id) = path.res
&& !cx.is_upvar(hir_id)
{
return Some(hir_id);
}

None
}

let Some(scrutinee_hir_id) = local(scrutinee) else {
let Some(scrutinee_hir_id) = local(self, scrutinee) else {
dcx.emit_fatal(LoopMatchInvalidMatch { span: scrutinee.span })
};

if local(state) != Some(scrutinee_hir_id) {
if local(self, state) != Some(scrutinee_hir_id) {
dcx.emit_fatal(LoopMatchInvalidUpdate {
scrutinee: scrutinee.span,
lhs: state.span,
Expand Down Expand Up @@ -1260,10 +1264,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
fn convert_var(&mut self, var_hir_id: hir::HirId) -> ExprKind<'tcx> {
// We want upvars here not captures.
// Captures will be handled in MIR.
let is_upvar = self
.tcx
.upvars_mentioned(self.body_owner)
.is_some_and(|upvars| upvars.contains_key(&var_hir_id));
let is_upvar = self.is_upvar(var_hir_id);

debug!(
"convert_var({:?}): is_upvar={}, body_owner={:?}",
Expand Down Expand Up @@ -1443,6 +1444,12 @@ impl<'tcx> ThirBuildCx<'tcx> {
}
}

fn is_upvar(&mut self, var_hir_id: hir::HirId) -> bool {
self.tcx
.upvars_mentioned(self.body_owner)
.is_some_and(|upvars| upvars.contains_key(&var_hir_id))
}

/// Converts a list of named fields (i.e., for struct-like struct/enum ADTs) into FieldExpr.
fn field_refs(&mut self, fields: &'tcx [hir::ExprField<'tcx>]) -> Box<[FieldExpr]> {
fields
Expand Down
81 changes: 81 additions & 0 deletions tests/ui/loop-match/upvar-scrutinee.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#![allow(incomplete_features)]
#![feature(loop_match)]

#[derive(Clone, Copy)]
enum State {
A,
B,
}

fn main() {
let mut state = State::A;

#[loop_match]
loop {
state = 'blk: {
match state {
State::A => {
#[const_continue]
break 'blk State::B;
}
State::B => {
return;
}
}
}
}

|| {
#[loop_match]
loop {
state = 'blk: {
match state {
//~^ ERROR invalid match on `#[loop_match]` state
State::A => {
#[const_continue]
break 'blk State::B;
}
State::B => {
return;
}
}
}
}
};

|| {
let mut state = state;
#[loop_match]
loop {
state = 'blk: {
match state {
State::A => {
#[const_continue]
break 'blk State::B;
}
State::B => {
return;
}
}
}
}
};

move || {
#[loop_match]
loop {
state = 'blk: {
match state {
//~^ ERROR invalid match on `#[loop_match]` state
State::A => {
#[const_continue]
break 'blk State::B;
}
State::B => {
return;
}
}
}
}
};
}
18 changes: 18 additions & 0 deletions tests/ui/loop-match/upvar-scrutinee.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: invalid match on `#[loop_match]` state
--> $DIR/upvar-scrutinee.rs:32:23
|
LL | match state {
| ^^^^^
|
= note: a local variable must be the scrutinee within a `#[loop_match]`

error: invalid match on `#[loop_match]` state
--> $DIR/upvar-scrutinee.rs:68:23
|
LL | match state {
| ^^^^^
|
= note: a local variable must be the scrutinee within a `#[loop_match]`

error: aborting due to 2 previous errors

Loading