Fix for resynthesizing invalid @functionName() annotations.

It should not cause an exception and resolution failure.

R=paulberry@google.com
BUG= https://github.com/dart-lang/sdk/issues/28099

Review-Url: https://codereview.chromium.org/2565173008 .
This commit is contained in:
Konstantin Shcheglov 2016-12-14 10:45:21 -08:00
parent 017257a64f
commit 871f478c63
3 changed files with 33 additions and 3 deletions

View file

@ -308,6 +308,8 @@ abstract class SummaryResynthesizer extends ElementResynthesizer {
* Builder of [Expression]s from [UnlinkedExpr]s.
*/
class _ConstExprBuilder {
static const ARGUMENT_LIST = 'ARGUMENT_LIST';
final _UnitResynthesizer resynthesizer;
final ElementImpl context;
final UnlinkedExpr uc;
@ -651,8 +653,12 @@ class _ConstExprBuilder {
} else if (info.element is ClassElement) {
constructorName = null;
} else {
throw new StateError('Unsupported element for invokeConstructor '
'${info.element?.runtimeType}');
List<Expression> arguments = _buildArguments();
SimpleIdentifier name = AstTestFactory.identifier3(info.name);
name.staticElement = info.element;
name.setProperty(ARGUMENT_LIST, AstTestFactory.argumentList(arguments));
_push(name);
return;
}
InterfaceType definingType = resynthesizer._createConstructorDefiningType(
context?.typeParameterContext, info, ref.typeArguments);
@ -1530,8 +1536,11 @@ class _UnitResynthesizer {
ElementAnnotationImpl elementAnnotation = new ElementAnnotationImpl(unit);
Expression constExpr = _buildConstExpression(context, uc);
if (constExpr is Identifier) {
ArgumentList arguments =
constExpr.getProperty(_ConstExprBuilder.ARGUMENT_LIST);
elementAnnotation.element = constExpr.staticElement;
elementAnnotation.annotationAst = AstTestFactory.annotation(constExpr);
elementAnnotation.annotationAst =
AstTestFactory.annotation2(constExpr, null, arguments);
} else if (constExpr is InstanceCreationExpression) {
elementAnnotation.element = constExpr.staticElement;
Identifier typeName = constExpr.constructorName.type.name;

View file

@ -653,6 +653,23 @@ class B extends A {
expect(_getClassMethodReturnType(result.unit, 'B', 'm'), 'int');
}
test_getResult_invalid_annotation_functionAsConstructor() async {
addTestFile(
r'''
fff() {}
@fff()
class C {}
''',
priority: true);
AnalysisResult result = await driver.getResult(testFile);
ClassDeclaration c = result.unit.declarations[1] as ClassDeclaration;
Annotation a = c.metadata[0];
expect(a.name.name, 'fff');
expect(a.name.staticElement, new isInstanceOf<FunctionElement>());
}
test_getResult_invalidUri_exports_dart() async {
String content = r'''
export 'dart:async';

View file

@ -4065,6 +4065,10 @@ class C {
checkLibrary('@a import "foo.dart"; const a = b;');
}
test_metadata_invalid_classDeclaration() {
checkLibrary('f(_) {} @f(42) class C {}');
}
test_metadata_libraryDirective() {
checkLibrary('@a library L; const a = null;');
}