| @@ -343,17 +343,22 @@ impl Simplify<'_> { |
| 343 | 343 | // A block that is a plain `let`-chain is structural nesting: |
| 344 | 344 | // splice it (a block ending in a switch yields its value |
| 345 | 345 | // through the branches' returns, so only chain ends flatten). |
| 346 | | − if let Value::Block(b) = &value { |
| 347 | | − let (blets, bend) = split((**b).clone()); |
| 348 | | − if let Expr::Return(a) = bend { |
| 349 | | − let mut pasted = blets; |
| 350 | | − pasted.push((v, Value::Atom(a), span)); |
| 351 | | − for item in pasted.into_iter().rev() { |
| 352 | | − lets.push_front(item); |
| 353 | | − } |
| 354 | | − self.changed = true; |
| 355 | | − continue; |
| 346 | + if let Value::Block(b) = &value |
| 347 | + && ends_in_return(b) |
| 348 | + { |
| 349 | + let Value::Block(b) = value else { |
| 350 | + unreachable!("matched above") |
| 351 | + }; |
| 352 | + let (mut pasted, bend) = split(*b); |
| 353 | + let Expr::Return(a) = bend else { |
| 354 | + unreachable!("the chain ends in a return") |
| 355 | + }; |
| 356 | + pasted.push((v, Value::Atom(a), span)); |
| 357 | + for item in pasted.into_iter().rev() { |
| 358 | + lets.push_front(item); |
| 356 | 359 | } |
| 360 | + self.changed = true; |
| 361 | + continue; |
| 357 | 362 | } |
| 358 | 363 | let mut value = self.value(value, uses); |
| 359 | 364 | // Beta reduction: a call of a known closure becomes a direct |
| @@ -572,26 +577,31 @@ impl Simplify<'_> { |
| 572 | 577 | } |
| 573 | 578 | }); |
| 574 | 579 | } |
| 575 | | − // The arms are raw (the chain walk substituted only |
| 576 | | − // `on`): normalise them through the substitution map |
| 577 | | − // first, so their scrutinee references are the same |
| 578 | | − // variable as `x` (aliases of it become it). |
| 579 | | − let subst = self.subst.clone(); |
| 580 | | − let norm = |b: &Expr| { |
| 581 | | − rename(b, &mut |w| subst.get(&w).cloned().unwrap_or(Atom::Var(w))) |
| 582 | | − }; |
| 583 | | − let norm_cases: Vec<(u32, Expr)> = |
| 584 | | − cases.iter().map(|(t, b)| (*t, norm(b))).collect(); |
| 585 | | − let norm_default = default.as_deref().map(norm); |
| 586 | 580 | if uses.get(x) == Some(&inside) |
| 587 | 581 | && let Some(inner) = self.switch_blocks.get(x).cloned() |
| 588 | 582 | && block_pure(&inner) |
| 589 | | − && let Some(lifted) = |
| 590 | | − self.case_of_case(&inner, *x, &norm_cases, norm_default.as_ref()) |
| 591 | 583 | { |
| 592 | | − self.dead.insert(*x); |
| 593 | | − self.changed = true; |
| 594 | | − return self.expr(lifted, uses, exits_ok); |
| 584 | + // The arms are raw (the chain walk substituted |
| 585 | + // only `on`): normalise them through the |
| 586 | + // substitution map first, so their scrutinee |
| 587 | + // references are the same variable as `x` |
| 588 | + // (aliases of it become it). Only once the cheap |
| 589 | + // conditions hold (issue #316: it copied every |
| 590 | + // switch on a variable). |
| 591 | + let subst = self.subst.clone(); |
| 592 | + let norm = |b: &Expr| { |
| 593 | + rename(b, &mut |w| subst.get(&w).cloned().unwrap_or(Atom::Var(w))) |
| 594 | + }; |
| 595 | + let norm_cases: Vec<(u32, Expr)> = |
| 596 | + cases.iter().map(|(t, b)| (*t, norm(b))).collect(); |
| 597 | + let norm_default = default.as_deref().map(norm); |
| 598 | + if let Some(lifted) = |
| 599 | + self.case_of_case(&inner, *x, &norm_cases, norm_default.as_ref()) |
| 600 | + { |
| 601 | + self.dead.insert(*x); |
| 602 | + self.changed = true; |
| 603 | + return self.expr(lifted, uses, exits_ok); |
| 604 | + } |
| 595 | 605 | } |
| 596 | 606 | } |
| 597 | 607 | Expr::Switch { |
| @@ -1178,6 +1188,19 @@ fn end_of(e: &Expr) -> End { |
| 1178 | 1188 | } |
| 1179 | 1189 | |
| 1180 | 1190 | /// Whether the expression's chain ends in a switch. |
| 1191 | +/// Whether `e` is a chain of `let`s ending in a return: a block that is |
| 1192 | +/// structural nesting only. |
| 1193 | +fn ends_in_return(e: &Expr) -> bool { |
| 1194 | + let mut e = e; |
| 1195 | + loop { |
| 1196 | + match e { |
| 1197 | + Expr::Let(_, _, rest, _) => e = rest, |
| 1198 | + Expr::Return(_) => return true, |
| 1199 | + _ => return false, |
| 1200 | + } |
| 1201 | + } |
| 1202 | +} |
| 1203 | + |
| 1181 | 1204 | fn ends_in_switch(e: &Expr) -> bool { |
| 1182 | 1205 | let mut e = e; |
| 1183 | 1206 | loop { |