Don't set elements and types during AstRewriteVisitor.
Resolution is incomplete at the time when we perform rewriting.
For example, in `C<int>` the type of `int` is not set, so we get
incomplete InterfaceType of `C` with `null` as the type argument.
This was crashing summary2 in one situation internally.
R=brianwilkerson@google.com
Change-Id: Ifcff68f007a90b5694cf3e4e4d3825309c04e2b8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/103382
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analyzer/lib/src/generated/resolver.dart b/pkg/analyzer/lib/src/generated/resolver.dart
index f66ceb7..0382710 100644
--- a/pkg/analyzer/lib/src/generated/resolver.dart
+++ b/pkg/analyzer/lib/src/generated/resolver.dart
@@ -92,15 +92,6 @@
InstanceCreationExpression instanceCreationExpression =
astFactory.instanceCreationExpression(
_getKeyword(node), constructorName, node.argumentList);
- InterfaceType type = getType(typeSystem, element, node.typeArguments);
- ConstructorElement constructorElement =
- type.lookUpConstructor(null, definingLibrary);
- methodName.staticElement = element;
- methodName.staticType = type;
- typeName.type = type;
- constructorName.staticElement = constructorElement;
- instanceCreationExpression.staticType = type;
- instanceCreationExpression.staticElement = constructorElement;
NodeReplacer.replace(node, instanceCreationExpression);
}
} else if (target is SimpleIdentifier) {
@@ -130,17 +121,6 @@
astFactory.instanceCreationExpression(
_getKeyword(node), constructorName, node.argumentList,
typeArguments: typeArguments);
- InterfaceType type = getType(typeSystem, element, null);
- constructorElement =
- type.lookUpConstructor(methodName.name, definingLibrary);
- methodName.staticElement = element;
- methodName.staticType = type;
- target.staticElement = element;
- target.staticType = type; // TODO(scheglov) remove this
- typeName.type = type;
- constructorName.staticElement = constructorElement;
- instanceCreationExpression.staticType = type;
- instanceCreationExpression.staticElement = constructorElement;
NodeReplacer.replace(node, instanceCreationExpression);
}
} else if (element is PrefixElement) {
@@ -160,16 +140,6 @@
InstanceCreationExpression instanceCreationExpression =
astFactory.instanceCreationExpression(
_getKeyword(node), constructorName, node.argumentList);
- InterfaceType type =
- getType(typeSystem, prefixedElement, node.typeArguments);
- ConstructorElement constructorElement =
- type.lookUpConstructor(null, definingLibrary);
- methodName.staticElement = element;
- methodName.staticType = type;
- typeName.type = type;
- constructorName.staticElement = constructorElement;
- instanceCreationExpression.staticType = type;
- instanceCreationExpression.staticElement = constructorElement;
NodeReplacer.replace(node, instanceCreationExpression);
}
}
@@ -197,16 +167,6 @@
InstanceCreationExpression instanceCreationExpression =
astFactory.instanceCreationExpression(
_getKeyword(node), constructorName, node.argumentList);
- InterfaceType type = getType(typeSystem, element, typeArguments);
- constructorElement =
- type.lookUpConstructor(methodName.name, definingLibrary);
- methodName.staticElement = element;
- methodName.staticType = type;
- target.identifier.staticElement = element;
- typeName.type = type;
- constructorName.staticElement = constructorElement;
- instanceCreationExpression.staticType = type;
- instanceCreationExpression.staticElement = constructorElement;
NodeReplacer.replace(node, instanceCreationExpression);
}
}
diff --git a/pkg/analyzer/test/src/dart/analysis/driver_resolution_test.dart b/pkg/analyzer/test/src/dart/analysis/driver_resolution_test.dart
index 73fc758..49d54a8 100644
--- a/pkg/analyzer/test/src/dart/analysis/driver_resolution_test.dart
+++ b/pkg/analyzer/test/src/dart/analysis/driver_resolution_test.dart
@@ -5876,7 +5876,6 @@
}
}
- @failingTest
test_optionalConst_prefixed() async {
newFile('/test/lib/a.dart', content: r'''
class C {
diff --git a/pkg/analyzer/test/src/dart/resolution/optional_const_test.dart b/pkg/analyzer/test/src/dart/resolution/optional_const_test.dart
index 813eec5..5a47b69 100644
--- a/pkg/analyzer/test/src/dart/resolution/optional_const_test.dart
+++ b/pkg/analyzer/test/src/dart/resolution/optional_const_test.dart
@@ -114,6 +114,46 @@
);
}
+ test_prefixed_unnamed_generic() async {
+ newFile('/test/lib/a.dart', content: r'''
+class C<T> {
+ const C();
+}
+''');
+ await assertNoErrorsInCode(r'''
+import 'a.dart' as p;
+
+const x = p.C<int>();
+''');
+ _fillLibraries();
+
+ var element_C = libraryA.getType('C');
+ var element_p = findElement.prefix('p');
+
+ var creation = findNode.instanceCreation('p.C<int>()');
+ assertType(creation, 'C<int>');
+
+ var constructorName = creation.constructorName;
+
+ var typeName = constructorName.type;
+ assertType(typeName, 'C<int>');
+
+ var pC = typeName.name as PrefixedIdentifier;
+ assertElement(pC, element_C);
+ // TODO(scheglov) enforce
+// assertTypeNull(pC);
+
+ var ref_p = pC.prefix;
+ assertElement(ref_p, element_p);
+ assertTypeNull(ref_p);
+
+ var ref_C = pC.identifier;
+ assertElement(ref_C, element_C);
+ assertTypeNull(ref_C);
+
+ assertType(typeName.typeArguments.arguments[0], 'int');
+ }
+
void _fillLibraries([LibraryElement library]) {
library ??= result.unit.declaredElement.library;
var uriStr = library.source.uri.toString();