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

Dart2js accepts "null" as argument to "!". #34147

Closed
lrhn opened this issue Aug 15, 2018 · 4 comments
Closed

Dart2js accepts "null" as argument to "!". #34147

lrhn opened this issue Aug 15, 2018 · 4 comments
Assignees
Labels
type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dart2js

Comments

@lrhn
Copy link
Member

lrhn commented Aug 15, 2018

Example:

main() {
  bool x = null;
  print(!x);  // Should throw, prints "true" in dart2js.
}
@lrhn lrhn added web-dart2js type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Aug 15, 2018
@rakudrama
Copy link
Member

Will ! accept only a nonnullable operand with NNBD?

@lrhn
Copy link
Member Author

lrhn commented Mar 14, 2019

Yes, the type expected by conditions and boolean operators will (overwhelmingly likely) be non-null bool.

All these operations currently throw if they get a null value (exept when implementations forget to check), so they are prime examples of operations whose operands will become non-nullable.

@eernstg
Copy link
Member

eernstg commented Mar 14, 2019

Note that dart2js fails to raise a dynamic error because null occurs where a bool is required in other situations as well. #36193 (comment) reports that if (e) .. will run the else branch rather than raising a dynamic error. For more detail, check #36193 (comment).

I just tried another one (using dart2js from ac4eca7):

main() {
  print("Before loop");
  while (null) {
    print("In loop!");
    break;
  }
  print("After loop");
}

Surprisingly, this prints 'Before loop', then 'After loop', and then once more 'After loop'. With do-while, null counts as false (so it prints 'Before loop', 'In loop!', 'After loop' as semi-expected):

main() {
  print("Before loop");
  do {
    print("In loop!");
    break;
  } while (null);
  print("After loop");
}

dart-bot pushed a commit that referenced this issue Apr 3, 2019
This helps triage/resolve issues in testRuntimeErrors:
* if_null_condition_test will be an approved failure for all dart2js
configurations due to #34147.
* if_runtime_error_test contains all the expected type errors. On the
dart2js-production-linux-d8 configuration (which runs with -O3), we
assume no type errors exist - even ones guarded by
Expect.throwsTypeError - so the optimized code produces runtime errors
instead. See https://dart-review.googlesource.com/c/sdk/+/97820 for a
similar discussion.

Change-Id: Iaf2df85af16cc03fcd7ec7ef2a9e57a2bc3ce081
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/98622
Reviewed-by: Bob Nystrom <rnystrom@google.com>
Commit-Queue: Mayank Patke <fishythefish@google.com>
@fishythefish fishythefish self-assigned this Apr 5, 2019
@fishythefish
Copy link
Member

fishythefish commented Apr 5, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dart2js
Projects
None yet
Development

No branches or pull requests

4 participants