Re-land "Eliminate uses of old AST node types from pkg/analyzer/lib/src/summary"
There are two bug fixes:
- Due to dartbug.com/33441, it's possible that a set/map literal
resynthesized from a summary will have neither its `isSet` or
`isMap` boolean set to `true`. We work around the problem for the
short term assuming such literals are maps.
- The linker method ConstNode.collectDependencies hadn't been updated
to handle the new summary nodes `makeUntypedSetOrMap` and
`makeTypedMap2`.
This CL includes unit tests for these two bug fixes.
Original change description:
> Eliminate uses of old AST node types from pkg/analyzer/lib/src/summary
>
> Change-Id: Ida907040d0461d9ae723421eeac71f4e19a8d4b2
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/95284
> Commit-Queue: Paul Berry <paulberry@google.com>
> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
> Bug:
Change-Id: Ie26be4e0e241872ef4c7e183ada1f1d6b5f6e7f0
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/96264
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
diff --git a/pkg/analyzer/lib/src/dart/constant/evaluation.dart b/pkg/analyzer/lib/src/dart/constant/evaluation.dart
index 688875e..cc56403 100644
--- a/pkg/analyzer/lib/src/dart/constant/evaluation.dart
+++ b/pkg/analyzer/lib/src/dart/constant/evaluation.dart
@@ -1484,7 +1484,14 @@
@override
DartObjectImpl visitSetOrMapLiteral(SetOrMapLiteral node) {
- if (node.isMap) {
+ // Note: due to dartbug.com/33441, it's possible that a set/map literal
+ // resynthesized from a summary will have neither its `isSet` or `isMap`
+ // boolean set to `true`. We work around the problem by assuming such
+ // literals are maps.
+ // TODO(paulberry): when dartbug.com/33441 is fixed, add an assertion here
+ // to verify that `node.isSet == !node.isMap`.
+ bool isMap = !node.isSet;
+ if (isMap) {
if (!node.isConst) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.MISSING_CONST_IN_MAP_LITERAL, node);
@@ -1511,7 +1518,7 @@
InterfaceType mapType =
_typeProvider.mapType.instantiate([keyType, valueType]);
return new DartObjectImpl(mapType, new MapState(map));
- } else if (node.isSet) {
+ } else {
if (!node.isConst) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.MISSING_CONST_IN_SET_LITERAL, node);
@@ -1533,7 +1540,6 @@
InterfaceType setType = _typeProvider.setType.instantiate([elementType]);
return new DartObjectImpl(setType, new SetState(set));
}
- return null;
}
@override
diff --git a/pkg/analyzer/lib/src/generated/resolver.dart b/pkg/analyzer/lib/src/generated/resolver.dart
index a22da7a..6bd9085 100644
--- a/pkg/analyzer/lib/src/generated/resolver.dart
+++ b/pkg/analyzer/lib/src/generated/resolver.dart
@@ -8910,15 +8910,20 @@
/// Return `true` if the given collection [element] does not contain any
/// synthetic tokens.
bool _isComplete(CollectionElement element) {
- Token token = element.beginToken;
- int endOffset = element.endToken.offset;
- while (token != null && token.offset <= endOffset) {
- if (token.isSynthetic) {
- return false;
- }
- token = token.next;
- }
+ // TODO(paulberry,brianwilkerson): the code below doesn't work because it
+ // assumes access to token offsets, which aren't available when working with
+ // expressions resynthesized from summaries. For now we just assume the
+ // collection element is complete.
return true;
+// Token token = element.beginToken;
+// int endOffset = element.endToken.offset;
+// while (token != null && token.offset <= endOffset) {
+// if (token.isSynthetic) {
+// return false;
+// }
+// token = token.next;
+// }
+// return true;
}
}
diff --git a/pkg/analyzer/lib/src/summary/expr_builder.dart b/pkg/analyzer/lib/src/summary/expr_builder.dart
index 3301d95..3733565 100644
--- a/pkg/analyzer/lib/src/summary/expr_builder.dart
+++ b/pkg/analyzer/lib/src/summary/expr_builder.dart
@@ -865,7 +865,7 @@
int count = _uc.ints[intPtr++];
List<CollectionElement> elements = <CollectionElement>[];
for (int i = 0; i < count; i++) {
- elements.add(_popCollectionElement());
+ elements.insert(0, _popCollectionElement());
}
DartType staticType;
if (typeArguments != null && typeArguments.arguments.length == 2) {
diff --git a/pkg/analyzer/lib/src/summary/link.dart b/pkg/analyzer/lib/src/summary/link.dart
index bd2a422..cd6b006 100644
--- a/pkg/analyzer/lib/src/summary/link.dart
+++ b/pkg/analyzer/lib/src/summary/link.dart
@@ -1934,6 +1934,7 @@
case UnlinkedExprOperation.makeUntypedList:
case UnlinkedExprOperation.makeUntypedMap:
case UnlinkedExprOperation.makeUntypedSet:
+ case UnlinkedExprOperation.makeUntypedSetOrMap:
intPtr++;
break;
case UnlinkedExprOperation.assignToRef:
@@ -1961,6 +1962,7 @@
intPtr++;
break;
case UnlinkedExprOperation.makeTypedMap:
+ case UnlinkedExprOperation.makeTypedMap2:
refPtr += 2;
intPtr++;
break;
diff --git a/pkg/analyzer/lib/src/summary/summarize_const_expr.dart b/pkg/analyzer/lib/src/summary/summarize_const_expr.dart
index 5e93786..056a4bf 100644
--- a/pkg/analyzer/lib/src/summary/summarize_const_expr.dart
+++ b/pkg/analyzer/lib/src/summary/summarize_const_expr.dart
@@ -344,14 +344,6 @@
typeName.typeArguments != null);
} else if (expr is ListLiteral) {
_serializeListLiteral(expr);
- // ignore: deprecated_member_use_from_same_package
- } else if (expr is MapLiteral) {
- // ignore: deprecated_member_use_from_same_package
- _serializeMapLiteral(expr);
- // ignore: deprecated_member_use_from_same_package
- } else if (expr is SetLiteral) {
- // ignore: deprecated_member_use_from_same_package
- _serializeSetLiteral(expr);
} else if (expr is SetOrMapLiteral) {
_serializeSetOrMapLiteral(expr);
} else if (expr is MethodInvocation) {
@@ -573,27 +565,6 @@
}
}
- @deprecated
- void _serializeMapLiteral(MapLiteral expr) {
- if (forConst || expr.typeArguments == null) {
- for (MapLiteralEntry entry in expr.entries) {
- _serialize(entry.key);
- _serialize(entry.value);
- }
- ints.add(expr.entries.length);
- } else {
- ints.add(0);
- }
- if (expr.typeArguments != null &&
- expr.typeArguments.arguments.length == 2) {
- references.add(serializeType(expr.typeArguments.arguments[0]));
- references.add(serializeType(expr.typeArguments.arguments[1]));
- operations.add(UnlinkedExprOperation.makeTypedMap);
- } else {
- operations.add(UnlinkedExprOperation.makeUntypedMap);
- }
- }
-
void _serializeMethodInvocation(MethodInvocation invocation) {
Expression target = invocation.target;
SimpleIdentifier methodName = invocation.methodName;
@@ -673,24 +644,6 @@
}
}
- @deprecated
- void _serializeSetLiteral(SetLiteral expr) {
- if (forConst || expr.typeArguments == null) {
- List<Expression> elements = expr.elements;
- elements.forEach(_serialize);
- ints.add(elements.length);
- } else {
- ints.add(0);
- }
- if (expr.typeArguments != null &&
- expr.typeArguments.arguments.length == 1) {
- references.add(serializeType(expr.typeArguments.arguments[0]));
- operations.add(UnlinkedExprOperation.makeTypedSet);
- } else {
- operations.add(UnlinkedExprOperation.makeUntypedSet);
- }
- }
-
void _serializeSetOrMapLiteral(SetOrMapLiteral expr) {
if (forConst || expr.typeArguments == null) {
for (CollectionElement element in expr.elements2) {
diff --git a/pkg/analyzer/test/src/dart/resolution/type_inference/map_literal_test.dart b/pkg/analyzer/test/src/dart/resolution/type_inference/map_literal_test.dart
index e88c870..89f9942 100644
--- a/pkg/analyzer/test/src/dart/resolution/type_inference/map_literal_test.dart
+++ b/pkg/analyzer/test/src/dart/resolution/type_inference/map_literal_test.dart
@@ -92,6 +92,24 @@
assertType(setOrMapLiteral('{'), 'Map<String, String>');
}
+ test_default_constructor_param_typed() async {
+ addTestFile('''
+class C {
+ const C({x = const <String, int>{}});
+}
+''');
+ await resolveTestFile();
+ }
+
+ test_default_constructor_param_untyped() async {
+ addTestFile('''
+class C {
+ const C({x = const {}});
+}
+''');
+ await resolveTestFile();
+ }
+
test_noContext_noTypeArgs_expressions_lubOfIntAndString() async {
addTestFile('''
var a = {1 : 'a', 2 : 'b', 3 : 'c'};
diff --git a/pkg/analyzer/test/src/diagnostics/const_eval_throws_exception_test.dart b/pkg/analyzer/test/src/diagnostics/const_eval_throws_exception_test.dart
index f51e17b..c39bd8d 100644
--- a/pkg/analyzer/test/src/diagnostics/const_eval_throws_exception_test.dart
+++ b/pkg/analyzer/test/src/diagnostics/const_eval_throws_exception_test.dart
@@ -80,4 +80,48 @@
await resolveTestFile();
assertNoTestErrors();
}
+
+ test_default_constructor_arg_empty_map_importAnalyzedAfter() async {
+ addTestFile('''
+import 'other.dart';
+
+main() {
+ var c = const C();
+}
+''');
+ newFile('/test/lib/other.dart', content: '''
+class C {
+ final Map<String, int> m;
+ const C({this.m = const <String, int>{}})
+ : assert(m != null);
+}
+''');
+ await resolveTestFile();
+ assertNoTestErrors();
+ var otherFileResult =
+ await resolveFile(convertPath('/test/lib/other.dart'));
+ expect(otherFileResult.errors, isEmpty);
+ }
+
+ test_default_constructor_arg_empty_map_importAnalyzedBefore() async {
+ addTestFile('''
+import 'other.dart';
+
+main() {
+ var c = const C();
+}
+''');
+ newFile('/test/lib/other.dart', content: '''
+class C {
+ final Map<String, int> m;
+ const C({this.m = const <String, int>{}})
+ : assert(m != null);
+}
+''');
+ var otherFileResult =
+ await resolveFile(convertPath('/test/lib/other.dart'));
+ expect(otherFileResult.errors, isEmpty);
+ await resolveTestFile();
+ assertNoTestErrors();
+ }
}
diff --git a/pkg/analyzer/test/src/summary/summary_common.dart b/pkg/analyzer/test/src/summary/summary_common.dart
index 9d73270..b45006a 100644
--- a/pkg/analyzer/test/src/summary/summary_common.dart
+++ b/pkg/analyzer/test/src/summary/summary_common.dart
@@ -2884,11 +2884,14 @@
operators: [
UnlinkedExprOperation.pushInt,
UnlinkedExprOperation.pushString,
+ UnlinkedExprOperation.makeMapLiteralEntry,
UnlinkedExprOperation.pushInt,
UnlinkedExprOperation.pushString,
+ UnlinkedExprOperation.makeMapLiteralEntry,
UnlinkedExprOperation.pushInt,
UnlinkedExprOperation.pushString,
- UnlinkedExprOperation.makeTypedMap
+ UnlinkedExprOperation.makeMapLiteralEntry,
+ UnlinkedExprOperation.makeTypedMap2
],
ints: [
11,
@@ -2917,11 +2920,14 @@
operators: [
UnlinkedExprOperation.pushInt,
UnlinkedExprOperation.pushString,
+ UnlinkedExprOperation.makeMapLiteralEntry,
UnlinkedExprOperation.pushInt,
UnlinkedExprOperation.pushString,
+ UnlinkedExprOperation.makeMapLiteralEntry,
UnlinkedExprOperation.pushInt,
UnlinkedExprOperation.pushString,
- UnlinkedExprOperation.makeTypedMap
+ UnlinkedExprOperation.makeMapLiteralEntry,
+ UnlinkedExprOperation.makeTypedMap2
],
ints: [
11,
@@ -3078,11 +3084,14 @@
operators: [
UnlinkedExprOperation.pushInt,
UnlinkedExprOperation.pushString,
+ UnlinkedExprOperation.makeMapLiteralEntry,
UnlinkedExprOperation.pushInt,
UnlinkedExprOperation.pushString,
+ UnlinkedExprOperation.makeMapLiteralEntry,
UnlinkedExprOperation.pushInt,
UnlinkedExprOperation.pushString,
- UnlinkedExprOperation.makeUntypedMap
+ UnlinkedExprOperation.makeMapLiteralEntry,
+ UnlinkedExprOperation.makeUntypedSetOrMap
],
ints: [
11,
@@ -3108,7 +3117,7 @@
UnlinkedExprOperation.pushInt,
UnlinkedExprOperation.pushInt,
UnlinkedExprOperation.pushInt,
- UnlinkedExprOperation.makeUntypedSet
+ UnlinkedExprOperation.makeUntypedSetOrMap
],
ints: [11, 22, 33, 3],
);
@@ -7383,7 +7392,7 @@
'var v = <int, String>{11: "aaa", 22: "bbb", 33: "ccc"};');
assertUnlinkedConst(variable.initializer.bodyExpr,
'<int, String>{11: "aaa", 22: "bbb", 33: "ccc"}',
- operators: [UnlinkedExprOperation.makeTypedMap],
+ operators: [UnlinkedExprOperation.makeTypedMap2],
ints: [0],
referenceValidators: [
(EntityRef r) => checkTypeRef(r, 'dart:core', 'int',
@@ -7429,11 +7438,14 @@
operators: [
UnlinkedExprOperation.pushInt,
UnlinkedExprOperation.pushString,
+ UnlinkedExprOperation.makeMapLiteralEntry,
UnlinkedExprOperation.pushInt,
UnlinkedExprOperation.pushString,
+ UnlinkedExprOperation.makeMapLiteralEntry,
UnlinkedExprOperation.pushInt,
UnlinkedExprOperation.pushString,
- UnlinkedExprOperation.makeUntypedMap
+ UnlinkedExprOperation.makeMapLiteralEntry,
+ UnlinkedExprOperation.makeUntypedSetOrMap
],
ints: [11, 22, 33, 3],
strings: ['aaa', 'bbb', 'ccc'],
@@ -7448,7 +7460,7 @@
UnlinkedExprOperation.pushInt,
UnlinkedExprOperation.pushInt,
UnlinkedExprOperation.pushInt,
- UnlinkedExprOperation.makeUntypedSet
+ UnlinkedExprOperation.makeUntypedSetOrMap
],
ints: [11, 22, 33, 3],
forTypeInferenceOnly: true);