Skip to content

[ddc] Unexpected async ordering on switch cases with no await #54050

@nshahan

Description

@nshahan

The following code has one case with an await and one that is synchronous. It should and does always execute the synchronous case but it does not execute synchronously as shown by the ordering of the elements in order.

If you replace the switch expression with a ternary expression, the execution appears to happen in the expected order.

import 'dart:async';

void main() async {
  FutureOr<int> value = 3;
  var order = <String>[];
  run() async {
    order.add('Before switch');
    final result = switch (value) {
      Future<int>() => await value,
      int() => value,
    };
    print(result == 3); // Expected: 3
    order.add('After switch');
  }
  var runFuture = run();
  order.add('After run()');
  await runFuture;
  print(order); // Expected: ['Before switch', 'After switch', 'After run()']
}

See flutter/flutter#138306

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-web-jsIssues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop.dart2js-ddc-discrepancyWhen dev and production compilations have different semanticsweb-dev-compiler

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions