Create a double value from an int literal when it was promoted
Change-Id: I17b076e1b4e4d11ac35cc2e5d6750ec5c5fab548
Reviewed-on: https://dart-review.googlesource.com/c/87607
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Mike Fairhurst <mfairhurst@google.com>
diff --git a/pkg/analyzer/lib/src/dart/constant/evaluation.dart b/pkg/analyzer/lib/src/dart/constant/evaluation.dart
index b7771d5..1ca5d3f 100644
--- a/pkg/analyzer/lib/src/dart/constant/evaluation.dart
+++ b/pkg/analyzer/lib/src/dart/constant/evaluation.dart
@@ -1301,8 +1301,13 @@
}
@override
- DartObjectImpl visitIntegerLiteral(IntegerLiteral node) =>
- new DartObjectImpl(_typeProvider.intType, new IntState(node.value));
+ DartObjectImpl visitIntegerLiteral(IntegerLiteral node) {
+ if (node.staticType == _typeProvider.doubleType) {
+ return new DartObjectImpl(
+ _typeProvider.doubleType, new DoubleState(node.value?.toDouble()));
+ }
+ return new DartObjectImpl(_typeProvider.intType, new IntState(node.value));
+ }
@override
DartObjectImpl visitInterpolationExpression(InterpolationExpression node) {
diff --git a/pkg/analyzer/test/src/dart/constant/evaluation_test.dart b/pkg/analyzer/test/src/dart/constant/evaluation_test.dart
index 14189bb..f6af9c7 100644
--- a/pkg/analyzer/test/src/dart/constant/evaluation_test.dart
+++ b/pkg/analyzer/test/src/dart/constant/evaluation_test.dart
@@ -426,6 +426,15 @@
experiments: [EnableString.constant_update_2018]);
}
+ test_visitIntegerLiteral() async {
+ CompilationUnit compilationUnit = await resolveSource('''
+const double d = 3;
+''');
+ DartObjectImpl result = _evaluateConstant(compilationUnit, 'd');
+ expect(result.type, typeProvider.doubleType);
+ expect(result.toDoubleValue(), 3.0);
+ }
+
test_visitIsExpression_is_instanceOfSameClass() async {
CompilationUnit compilationUnit = await resolveSource('''
const a = const A();