Skip to content

Wrong order of evaluating map entries #56848

@sgrekhov

Description

@sgrekhov

https://github.com/dart-lang/co19/blob/master/LanguageFeatures/Control-flow-collections/Null-aware-elements/runtime_A02_t02.dart fails at run time on VM, dart2js and dart2wasm.

String _log = "";

int? f(int? v) {
  _log += "f($v);";
  return v;
}

main() {
  var map1 = {
    f(1): ?f(2),
    ?f(3): ?f(null),
    ?f(4): ?null
  };
  Expect.mapEquals({1: 2}, map1);
  Expect.equals("f(1);f(2);f(3);f(null);f(4);", _log); // Expect.equals(expected: <f(1);f(2);f(3);f(null);f(4);>, actual: <f(2);f(1);f(3);f(null);f(4);>) fails.
...
}

Why map value is evaluated before the key? According to the spec the key should be evaluated first.

  • Else, if element is a nullAwareMapElement with entry k: v:

  • Evaluate k to a value kv.

  • If element has a null-aware key and kv is null, then stop. Else continue...

  • Evaluate v to a value vv.

  • If element has a null-aware value and vv is null, then stop. Else continue...

  • Append an entry kv: vv to result.

Metadata

Metadata

Labels

feature-null-aware-elementsImplementation of the Null-aware elements featurelegacy-area-front-endLegacy: Use area-dart-model instead.model-featuresGeneral feature work in the analyzer and CFE.soundnesstype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions