Allow pure statements that contain early returns

This commit is contained in:
Sam Mohr 2024-11-21 12:22:01 -08:00
parent 6a3db1e59a
commit d449d83a84
No known key found for this signature in database
GPG key ID: EA41D161A3C1BC99
3 changed files with 283 additions and 9 deletions

View file

@ -3394,12 +3394,18 @@ fn constrain_let_def(
)
});
// Ignored def must be effectful, otherwise it's dead code
let effectful_constraint = Constraint::ExpectEffectful(
fx_var,
ExpectEffectfulReason::Ignored,
def.loc_pattern.region,
);
let effectful_constraint = if def.loc_expr.value.contains_any_early_returns() {
// If the statement has early returns, it doesn't need to be effectful to
// potentially affect the output of the containing function
Constraint::True
} else {
// If there are no early returns, it must be effectful or else it's dead code
Constraint::ExpectEffectful(
fx_var,
ExpectEffectfulReason::Ignored,
def.loc_pattern.region,
)
};
let enclosing_fx_constraint = constraints.fx_call(
fx_var,
@ -3486,9 +3492,14 @@ fn constrain_stmt_def(
generalizable,
);
// Stmt expr must be effectful, otherwise it's dead code
let effectful_constraint =
Constraint::ExpectEffectful(fx_var, ExpectEffectfulReason::Stmt, region);
let effectful_constraint = if def.loc_expr.value.contains_any_early_returns() {
// If the statement has early returns, it doesn't need to be effectful to
// potentially affect the output of the containing function
Constraint::True
} else {
// If there are no early returns, it must be effectful or else it's dead code
Constraint::ExpectEffectful(fx_var, ExpectEffectfulReason::Stmt, region)
};
let fx_call_kind = match fn_name {
None => FxCallKind::Stmt,