Map value for uninitialized key is different in DDC output vs Dart2js output

dart --version
Dart VM version: 2.2.0 (Tue Feb 26 15:04:32 2019 +0100) on "macos_x64"

Hi There!

We’re seeing in issue where the default value for a map lookup with a key that has not been initialized behaves differently in DDC output vs dart2js output. Here’s a test which will pass when compiled via dart2js, but will fail when when compiled via DDC:

import 'package:test/test.dart';

main() {
  test('default param', () {
    // passes in dart2js and ddc
    expect(someFunc(null), null);

    final emptyMap = {};
    // fails in ddc, passes in dart2js
    expect(someFunc(emptyMap['should_be_null']), null);
  });
}

dynamic someFunc([c1 = 'some_default_value']) {
  return c1;
}

If you print out the value of emptyMap['should_be_null'], it is null on both DDC compiled output and dart2js compiled output. However, the return value in DDC from someFunc is actually some_default_value, and not null. One workaround is to pass the expression emptyMap['should_be_null'] ?? null to someFunc.

Could it be that emptyMap['should_be_null'] is undefined in the DDC output, but is explicitly null in the dart2js output?

Author: Fantashit

1 thought on “Map value for uninitialized key is different in DDC output vs Dart2js output

Comments are closed.