Skip to content

DDC: labeled break out of a switch whose last case has an empty body does not exit the enclosing loop (Dart 3.14.0 / Flutter 3.50.0) #64941

Open

Description

@mboe

Summary

In code compiled by the Dart Development Compiler (DDC), a break <label>; inside a switch statement only leaves the switch and not the labeled enclosing loop when the last case (or default:) of that switch has an empty body. The same code behaves correctly on the VM and with dart2js, and it becomes correct on DDC as soon as the last case gets an explicit break;.

The real-world symptom is fleaflet/flutter_map#2234 ("Infinite loop when using feature layers on web", open since 2026-07-22): workAcrossWorlds() in lib/src/layer/shared/feature_layer_utils.dart keeps iterating after break negativeWorldsLoop; until its own guard throws Assertion failed: "Infinite loop going beyond 30 for world width …". Every PolygonLayer/PolylineLayer/CircleLayer is therefore invisible in debug web builds since Flutter 3.50; release builds (dart2js) are fine.

Environment

  • Dart SDK 3.14.0 (stable), as shipped with Flutter 3.50.0 (stable)
  • macOS 27.2, arm64; Google Chrome 157.0.8103.51
  • Observed via flutter test --platform chrome (DDC). dart test -p chrome compiles with dart2js (package:test offers no DDC compiler) and passes, so I could not produce a Flutter-free DDC run; the functions below are plain Dart without any Flutter dependency.

Minimal reproduction

enum Ctl { stop, go }

/// Expected 2: loop runs for 'go', leaves the loop at 'stop'.
/// Returns -1 if the loop body kept running past the labeled break.
int forEndless(List<Ctl> seq) {
  var n = 0;
  outer:
  for (var i = 0;; i++) {
    if (i >= seq.length) return -1;
    n++;
    switch (seq[i]) {
      case Ctl.stop:
        break outer;
      case Ctl.go:          // empty body -> DDC: `break outer` only leaves the switch
    }
  }
  return n;
}

/// Identical, but the last case has an explicit `break;` -> correct on DDC.
int forEndlessExplicitBreak(List<Ctl> seq) {
  var n = 0;
  outer:
  for (var i = 0;; i++) {
    if (i >= seq.length) return -1;
    n++;
    switch (seq[i]) {
      case Ctl.stop:
        break outer;
      case Ctl.go:
        break;
    }
  }
  return n;
}

Test (flutter_test, or package:test on the VM):

final seq = [Ctl.go, Ctl.stop, Ctl.go, Ctl.go];
test('for(;;) + switch(enum) + break label', () => expect(forEndless(seq), 2));
test('… with explicit break in last case',   () => expect(forEndlessExplicitBreak(seq), 2));

Results

Variant VM dart2js (dart test -p chrome) DDC (flutter test --platform chrome)
for (;;) + switch(enum) + break label, last case empty 2 ✓ 2 ✓ -1 ✗ (loop never left)
for (; i < n;) + same switch 2 ✓ 2 ✓ 4 ✗ (ran to the end)
while (true) + same switch 2 ✓ 2 ✓ -1 ✗
switch(int) instead of enum 2 ✓ 2 ✓ -1 ✗
same switch with empty default: as last case 2 ✓ 2 ✓ -1 ✗
same switch, last case break; (explicit) 2 ✓ 2 ✓ 2 ✓
continue label instead of break label 3 ✓ 3 ✓ 3 ✓
if/else instead of switch 2 ✓ 2 ✓ 2 ✓

So the trigger is specifically: labeled break in one case and an empty trailing case. The compiled JS was not inspected.

Expected

break outer; leaves the labeled loop regardless of whether the following case has a body.

Workaround

Give the trailing case an explicit break; (or rewrite the switch as if/else). Verified against flutter_map 8.3.2: adding break; to the two empty case WorldWorkControl.visible: branches in workAcrossWorlds makes all affected layers render again under DDC.

Activity

  1. ZaraclaJ commented on Nov 17, 2026

    @ZaraclaJ

    I can confirm this with dartdevc directly, and here is the generated JavaScript, which was missing above.

    Compiled with dartaotruntime bin/snapshots/dartdevc_aot.dart.snapshot --dart-sdk-summary=lib/_internal/ddc_outline.dill -o probe.js probe.dart, on a copy of flutter_map's workAcrossWorlds:

    negativeWorldsLoop:
    for (double shift = -worldWidth;; shift -= worldWidth) {
      protectInfiniteLoop();
      switch (work(shift)) {
        case WorldWorkControl.hit:
          return true;
        case WorldWorkControl.invisible:
          break negativeWorldsLoop;
        case WorldWorkControl.visible:
      }
    }

    Dart 3.10.9 (Flutter 3.38.10): correct, the label is emitted:

    L0:
      for (let shift = -worldWidth;; shift = shift - worldWidth) {
        protectInfiniteLoop();
        switch (work(shift)) {
          case C[0] || CT.C0: { return true; }
          case C[2] || CT.C2: { break L0; }
          case C[1] || CT.C1:
        }
      }

    Dart 3.14.0 (Flutter 3.50.0): wrong, the label is gone and the break only leaves the switch:

    for (let shift = -worldWidth;; shift = shift - worldWidth) {
      protectInfiniteLoop();
      switch (work(shift)) {
        case C[0] || CT.C0: { return true; }
        case C[2] || CT.C2: { break; }
        case C[1] || CT.C1:
      }
    }

    Same output with --canary and with --modules=ddc. With 3.14.0, adding an explicit break; to the trailing case WorldWorkControl.visible: brings L0: / break L0; back, which matches your analysis.

    So this is a regression between 3.10.9 and 3.14.0 (3.11 to 3.13 not tested).

    A fix on the flutter_map side is proposed in fleaflet/flutter_map#2299.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions