Skip to content

Commit

Permalink
Add more JS subtyping semantics to CFE constant evaluator.
Browse files Browse the repository at this point in the history
If we're targeting a JS backend, `isSubtype` considers every int to be a
double. We need to additionally allow any integer-valued double to be an
int. This mainly allows us to handle signed zeroes correctly.

Fixes #36562.

Change-Id: I00d1e693bb3f1268ac100a76c9bec8fcc612b182
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/99101
Commit-Queue: Mayank Patke <fishythefish@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
  • Loading branch information
fishythefish authored and commit-bot@chromium.org committed Apr 10, 2019
1 parent 585debb commit ab66a38
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/front_end/lib/src/fasta/kernel/constant_evaluator.dart
Expand Up @@ -2193,6 +2193,14 @@ class ConstantEvaluator extends RecursiveVisitor<Constant> {
// With JS semantics, an integer is also a double.
return true;
}

if (constantType == typeEnvironment.doubleType &&
type == typeEnvironment.intType) {
double value = (constant as DoubleConstant).value;
if (value.isFinite && value == value.truncateToDouble()) {
return true;
}
}
}
return typeEnvironment.isSubtypeOf(constantType, type);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/compiler/dart2js_extra/issue36562_test.dart
@@ -0,0 +1,12 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

const int x = 0;

const List<int> l = const [
1,
-x,
];

main() => print(l);

0 comments on commit ab66a38

Please sign in to comment.