Flutter 3.50.0's First P0: A Store-to-Load Shortcut That Never Checked Dominance
AOT builds on Flutter 3.50.0 die on the sort of sealed-class switch that is everywhere in modern Dart, and no hotfix has shipped yet, so affected teams have to reshape otherwise clean code. The crash is also a clean exhibit of a performance optimization quietly breaking a core compiler invariant and taking a whole release down with it.
Dart 3.14.0 introduces an AOT-only regression that lands straight in Flutter 3.50.0. The same sealed-class `switch` compiles cleanly on Dart 3.13.5 and runs fine through `dart run` on 3.14.0, but an AOT build makes `gen_snapshot` die with `linearscan.cc:3013: unreachable code`. The pattern: one `case` returns early, the other assigns a closure-captured variable before a closure runs.
Dart's front end lowers the pattern-matching switch while leaving a spurious `if (opt is Item)` guard and an extra control-flow path, even though the `Item` branch is logically guaranteed. A pre-3.14 change, commit 25ae146, added store-to-load forwarding for effectively final variables: reusing the initialization value instead of reading from the closure context. That optimization assumed the single initialization write always reaches every read. The extra switch path breaks the assumption, and the compiler substitutes a read with a value that was never defined on that path. Compiler theory names this failure mode: a dominance violation.
The fix adds an `IsDominatedBy` check to `LoadFieldInstr::Canonicalize()` so the substitution only happens when the initializing value dominates the read site. It is merged into Dart Stable and should reach Dart 3.14.1, while Flutter 3.50.1 is still pending. Until it lands, the workaround moves the variable definition and the closure's use into the same `case` branch.
A dominance violation sits at the root: the optimization held its single-initialization-write assumption implicitly rather than verifying it against the CFG, so the first CFE-generated junk path it met produced an invalid IL.
The temporary fix deliberately keeps store-to-load forwarding for the common case and only falls back when the value fails to dominate the read, a sensible costing tradeoff that preserves the speedup and closes the correctness hole.
Dart Stable already carries the fix while Flutter 3.50.x stays broken, so anyone pinned to Flutter's Dart SDK is left reshaping idiomatic code over a bug that is solved upstream.
The author's closing jab makes a practical point: AI assistants happily emit this exact compact sealed-class switch, and none of them surface what it does to the compiler's intermediate representation downstream.
Can't you just do committed = value directly, instead of run(() { committed = value })?