Fix AstRewriteVisitor to set elements/types for instance creation nodes.
R=brianwilkerson@google.com
Bug: https://github.com/flutter/flutter/issues/17429
Change-Id: I818fabb2faf1b0ffb68957a9713a22e53db25709
Reviewed-on: https://dart-review.googlesource.com/57000
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analyzer/lib/src/dart/analysis/driver.dart b/pkg/analyzer/lib/src/dart/analysis/driver.dart
index 66005ce..0563ff8 100644
--- a/pkg/analyzer/lib/src/dart/analysis/driver.dart
+++ b/pkg/analyzer/lib/src/dart/analysis/driver.dart
@@ -95,7 +95,7 @@
/**
* The version of data format, should be incremented on every format change.
*/
- static const int DATA_VERSION = 59;
+ static const int DATA_VERSION = 60;
/**
* The number of exception contexts allowed to write. Once this field is
diff --git a/pkg/analyzer/lib/src/generated/resolver.dart b/pkg/analyzer/lib/src/generated/resolver.dart
index 91509f1..6fdac95 100644
--- a/pkg/analyzer/lib/src/generated/resolver.dart
+++ b/pkg/analyzer/lib/src/generated/resolver.dart
@@ -76,17 +76,21 @@
}
Element element = nameScope.lookup(methodName, definingLibrary);
if (element is ClassElement) {
+ ConstructorElement constructorElement = element.unnamedConstructor;
AstFactory astFactory = new AstFactoryImpl();
TypeName typeName = astFactory.typeName(methodName, node.typeArguments);
+ ConstructorName constructorName =
+ astFactory.constructorName(typeName, null, null);
InstanceCreationExpression instanceCreationExpression =
astFactory.instanceCreationExpression(
- _getKeyword(node),
- astFactory.constructorName(typeName, null, null),
- node.argumentList);
+ _getKeyword(node), constructorName, node.argumentList);
DartType type = _getType(element, node.typeArguments);
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) {
@@ -98,19 +102,22 @@
Element element = nameScope.lookup(target, definingLibrary);
if (element is ClassElement) {
// Possible case: C.n()
- if (element.getNamedConstructor(methodName.name) != null) {
+ var constructorElement = element.getNamedConstructor(methodName.name);
+ if (constructorElement != null) {
AstFactory astFactory = new AstFactoryImpl();
TypeName typeName = astFactory.typeName(target, node.typeArguments);
+ ConstructorName constructorName =
+ astFactory.constructorName(typeName, node.operator, methodName);
InstanceCreationExpression instanceCreationExpression =
astFactory.instanceCreationExpression(
- _getKeyword(node),
- astFactory.constructorName(
- typeName, node.operator, methodName),
- node.argumentList);
+ _getKeyword(node), constructorName, node.argumentList);
DartType type = _getType(element, node.typeArguments);
methodName.staticElement = element;
methodName.staticType = type;
typeName.type = type;
+ constructorName.staticElement = constructorElement;
+ instanceCreationExpression.staticType = type;
+ instanceCreationExpression.staticElement = constructorElement;
NodeReplacer.replace(node, instanceCreationExpression);
}
} else if (element is PrefixElement) {
@@ -122,18 +129,23 @@
astFactory.simpleIdentifier(methodName.token));
Element prefixedElement = nameScope.lookup(identifier, definingLibrary);
if (prefixedElement is ClassElement) {
+ ConstructorElement constructorElement =
+ prefixedElement.unnamedConstructor;
TypeName typeName = astFactory.typeName(
astFactory.prefixedIdentifier(target, node.operator, methodName),
node.typeArguments);
+ ConstructorName constructorName =
+ astFactory.constructorName(typeName, null, null);
InstanceCreationExpression instanceCreationExpression =
astFactory.instanceCreationExpression(
- _getKeyword(node),
- astFactory.constructorName(typeName, null, null),
- node.argumentList);
+ _getKeyword(node), constructorName, node.argumentList);
DartType type = _getType(prefixedElement, node.typeArguments);
methodName.staticElement = element;
methodName.staticType = type;
typeName.type = type;
+ constructorName.staticElement = constructorElement;
+ instanceCreationExpression.staticType = type;
+ instanceCreationExpression.staticElement = constructorElement;
NodeReplacer.replace(node, instanceCreationExpression);
}
}
@@ -143,19 +155,22 @@
if (prefixElement is PrefixElement) {
Element element = nameScope.lookup(target, definingLibrary);
if (element is ClassElement) {
- if (element.getNamedConstructor(methodName.name) != null) {
+ var constructorElement = element.getNamedConstructor(methodName.name);
+ if (constructorElement != null) {
AstFactory astFactory = new AstFactoryImpl();
TypeName typeName = astFactory.typeName(target, node.typeArguments);
+ ConstructorName constructorName =
+ astFactory.constructorName(typeName, node.operator, methodName);
InstanceCreationExpression instanceCreationExpression =
astFactory.instanceCreationExpression(
- _getKeyword(node),
- astFactory.constructorName(
- typeName, node.operator, methodName),
- node.argumentList);
+ _getKeyword(node), constructorName, node.argumentList);
DartType type = _getType(element, node.typeArguments);
methodName.staticElement = element;
methodName.staticType = type;
typeName.type = type;
+ constructorName.staticElement = constructorElement;
+ instanceCreationExpression.staticType = type;
+ instanceCreationExpression.staticElement = constructorElement;
NodeReplacer.replace(node, instanceCreationExpression);
}
}
diff --git a/pkg/analyzer/test/generated/non_error_resolver_test.dart b/pkg/analyzer/test/generated/non_error_resolver_test.dart
index 6ef150d..73c2001 100644
--- a/pkg/analyzer/test/generated/non_error_resolver_test.dart
+++ b/pkg/analyzer/test/generated/non_error_resolver_test.dart
@@ -4786,6 +4786,49 @@
assertNoErrors(source);
}
+ test_optionalNew_rewrite() async {
+ resetWith(
+ options: new AnalysisOptionsImpl()
+ ..previewDart2 = true
+ ..strongMode = true);
+ Source source = addSource(r'''
+import 'b.dart';
+main() {
+ const B.named1();
+ const B.named2();
+ const B.named3();
+ const B.named4();
+}
+''');
+ addNamedSource("/a.dart", r'''
+class A {
+ const A();
+ const A.named();
+}
+''');
+ addNamedSource("/b.dart", r'''
+import 'a.dart';
+import 'a.dart' as p;
+
+const _a1 = A();
+const _a2 = A.named();
+const _a3 = p.A();
+const _a4 = p.A.named();
+
+class B {
+ const B.named1({this.a: _a1}) : assert(a != null);
+ const B.named2({this.a: _a2}) : assert(a != null);
+ const B.named3({this.a: _a3}) : assert(a != null);
+ const B.named4({this.a: _a4}) : assert(a != null);
+
+ final A a;
+}
+''');
+ await computeAnalysisResult(source);
+ assertNoErrors(source);
+ verify([source]);
+ }
+
test_optionalParameterInOperator_required() async {
Source source = addSource(r'''
class A {