Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
corwinsheahan-wf opened this issue Feb 27, 2019 · 6 comments
Assignees

Comments

@corwinsheahan-wf
Copy link

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?

@kevmoo kevmoo transferred this issue from dart-lang/build Feb 27, 2019
@jakemac53
Copy link
Contributor

cc @jmesserly

@jmesserly
Copy link

This is a known issue. This should be addressed in DDC's map implementations, which may or may not have a measurable performance impact on access times (I suspect it will not make much difference).

@jmesserly jmesserly self-assigned this Feb 27, 2019
@jmesserly
Copy link

https://dart-review.googlesource.com/c/sdk/+/94920

@rakudrama rakudrama reopened this Mar 5, 2019
@rakudrama
Copy link
Member

I believe the issue is not that map returns null or undefined, but how DDC and dart2js differ in the defaulting of optional arguments.
There are many ways of getting a JavaScript undefined value, consider new List(100000)[2].

@jmesserly
Copy link

Would you mind opening another issue about that, if you'd like to discuss further? I'd rather keep this issue focused on the report, and discuss bigger design questions about calling conventions elsewhere.

(In general, DDC tries to avoid common pitfalls related to undefined, by using null where it is practical to do so. There are indeed cases where it breaks down, though these seem rare in practice. I don't think changing the calling convention in DDC is practical, but if you'd like to discuss further, we can certainly revisit it.)

@rakudrama
Copy link
Member

@jmesserly Done: #36116

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

No branches or pull requests

5 participants