blob: b2fac8c86b198b98139b70f399bfb52ab5f91ab1 [file] [log] [blame]
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/ast/extensions.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/member.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/generated/utilities_dart.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../../../generated/elements_types_mixin.dart';
import '../../../utils.dart';
import '../resolution/context_collection_resolution.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(AnalysisDriverResolutionTest);
});
}
final isDynamicType = TypeMatcher<DynamicTypeImpl>();
final isNeverType = TypeMatcher<NeverTypeImpl>();
final isVoidType = TypeMatcher<VoidTypeImpl>();
/// Integration tests for resolution.
@reflectiveTest
class AnalysisDriverResolutionTest extends PubPackageResolutionTest
with ElementsTypesMixin {
void assertDeclaredVariableType(SimpleIdentifier node, String expected) {
var element = node.staticElement as VariableElement;
assertType(element.type, expected);
}
void assertDeclaredVariableTypeObject(SimpleIdentifier node) {
var element = node.staticElement as VariableElement;
expect(element.type, typeProvider.objectType);
}
/// Test that [argumentList] has exactly two type items `int` and `double`.
void assertTypeArguments(
TypeArgumentList argumentList, List<DartType> expectedTypes) {
expect(argumentList.arguments, hasLength(expectedTypes.length));
for (int i = 0; i < expectedTypes.length; i++) {
_assertNamedTypeSimple(argumentList.arguments[i], expectedTypes[i]);
}
}
void assertUnresolvedInvokeType(DartType invokeType) {
expect(invokeType, isDynamicType);
}
/// Creates a function that checks that an expression is a reference to a top
/// level variable with the given [name].
void Function(Expression) checkTopVarRef(String name) {
return (Expression e) {
TopLevelVariableElement variable = _getTopLevelVariable(result, name);
SimpleIdentifier node = e as SimpleIdentifier;
expect(node.staticElement, same(variable.getter));
expect(node.staticType, variable.type);
};
}
/// Creates a function that checks that an expression is a named argument
/// that references a top level variable with the given [name], where the
/// name of the named argument is undefined.
void Function(Expression) checkTopVarUndefinedNamedRef(String name) {
return (Expression e) {
TopLevelVariableElement variable = _getTopLevelVariable(result, name);
NamedExpression named = e as NamedExpression;
expect(named.staticType, variable.type);
SimpleIdentifier nameIdentifier = named.name.label;
expect(nameIdentifier.staticElement, isNull);
expect(nameIdentifier.staticType, isNull);
var expression = named.expression as SimpleIdentifier;
expect(expression.staticElement, same(variable.getter));
expect(expression.staticType, variable.type);
};
}
test_adjacentStrings() async {
String content = r'''
void main() {
'aaa' 'bbb' 'ccc';
}
''';
addTestFile(content);
await resolveTestFile();
List<Statement> statements = _getMainStatements(result);
var statement = statements[0] as ExpressionStatement;
var expression = statement.expression as AdjacentStrings;
expect(expression.staticType, typeProvider.stringType);
expect(expression.strings, hasLength(3));
StringLiteral literal_1 = expression.strings[0];
expect(literal_1.staticType, typeProvider.stringType);
StringLiteral literal_2 = expression.strings[1];
expect(literal_2.staticType, typeProvider.stringType);
StringLiteral literal_3 = expression.strings[2];
expect(literal_3.staticType, typeProvider.stringType);
}
test_annotation() async {
String content = r'''
const myAnnotation = 1;
@myAnnotation
class C {
@myAnnotation
int field1 = 2, field2 = 3;
@myAnnotation
C() {}
@myAnnotation
void method() {}
}
@myAnnotation
int topLevelVariable1 = 4, topLevelVariable2 = 5;
@myAnnotation
void topLevelFunction() {}
''';
addTestFile(content);
await resolveTestFile();
var myDeclaration =
result.unit.declarations[0] as TopLevelVariableDeclaration;
var myVariable = myDeclaration.variables.variables[0];
var myElement = myVariable.declaredElement as TopLevelVariableElement;
void assertMyAnnotation(AnnotatedNode node) {
Annotation annotation = node.metadata[0];
expect(annotation.element, same(myElement.getter));
var identifier_1 = annotation.name as SimpleIdentifier;
expect(identifier_1.staticElement, same(myElement.getter));
expect(identifier_1.staticType, isNull);
}
{
var classNode = result.unit.declarations[1] as ClassDeclaration;
assertMyAnnotation(classNode);
{
var node = classNode.members[0] as FieldDeclaration;
assertMyAnnotation(node);
}
{
var node = classNode.members[1] as ConstructorDeclaration;
assertMyAnnotation(node);
}
{
var node = classNode.members[2] as MethodDeclaration;
assertMyAnnotation(node);
}
}
{
var node = result.unit.declarations[2] as TopLevelVariableDeclaration;
assertMyAnnotation(node);
}
{
var node = result.unit.declarations[3] as FunctionDeclaration;
assertMyAnnotation(node);
}
}
test_annotation_onDirective_export() async {
addTestFile(r'''
@a
export 'dart:math';
const a = 1;
''');
await resolveTestFile();
var directive = findNode.export('dart:math');
expect(directive.metadata, hasLength(1));
Annotation annotation = directive.metadata[0];
expect(annotation.element, findElement.topGet('a'));
var aRef = annotation.name as SimpleIdentifier;
assertElement(aRef, findElement.topGet('a'));
assertTypeNull(aRef);
}
test_annotation_onDirective_import() async {
addTestFile(r'''
@a
import 'dart:math';
const a = 1;
''');
await resolveTestFile();
var directive = findNode.import('dart:math');
expect(directive.metadata, hasLength(1));
Annotation annotation = directive.metadata[0];
expect(annotation.element, findElement.topGet('a'));
var aRef = annotation.name as SimpleIdentifier;
assertElement(aRef, findElement.topGet('a'));
assertTypeNull(aRef);
}
test_annotation_onDirective_library() async {
addTestFile(r'''
@a
library test;
const a = 1;
''');
await resolveTestFile();
var directive = findNode.libraryDirective;
expect(directive.metadata, hasLength(1));
Annotation annotation = directive.metadata[0];
expect(annotation.element, findElement.topGet('a'));
var aRef = annotation.name as SimpleIdentifier;
assertElement(aRef, findElement.topGet('a'));
assertTypeNull(aRef);
}
test_annotation_onDirective_part() async {
newFile('$testPackageLibPath/a.dart', r'''
part of 'test.dart';
''');
addTestFile(r'''
@a
part 'a.dart';
const a = 1;
''');
await resolveTestFile();
var directive = findNode.part('a.dart');
expect(directive.metadata, hasLength(1));
Annotation annotation = directive.metadata[0];
expect(annotation.element, findElement.topGet('a'));
var aRef = annotation.name as SimpleIdentifier;
assertElement(aRef, findElement.topGet('a'));
assertTypeNull(aRef);
}
test_annotation_onDirective_partOf() async {
final a = newFile('$testPackageLibPath/a.dart', r'''
part 'test.dart';
''');
addTestFile(r'''
@a
part of 'a.dart';
const a = 1;
''');
// Make sure that the part knows its library.
contextFor(a.path).currentSession.getParsedUnit(a.path);
await resolveTestFile();
var directive = findNode.partOf('a.dart');
expect(directive.metadata, hasLength(1));
Annotation annotation = directive.metadata[0];
expect(annotation.element, findElement.topGet('a'));
var aRef = annotation.name as SimpleIdentifier;
assertElement(aRef, findElement.topGet('a'));
assertTypeNull(aRef);
}
test_annotation_onFormalParameter_redirectingFactory() async {
addTestFile(r'''
class C {
factory C(@a @b x, y, {@c z}) = C.named;
C.named(int p);
}
const a = 1;
const b = 2;
const c = 2;
''');
await resolveTestFile();
void assertTopGetAnnotation(Annotation annotation, String name) {
var getter = findElement.topGet(name);
expect(annotation.element, getter);
var ref = annotation.name as SimpleIdentifier;
assertElement(ref, getter);
assertTypeNull(ref);
}
{
var parameter = findNode.simpleParameter('x, ');
expect(parameter.metadata, hasLength(2));
assertTopGetAnnotation(parameter.metadata[0], 'a');
assertTopGetAnnotation(parameter.metadata[1], 'b');
}
{
var parameter = findNode.simpleParameter('z}');
expect(parameter.metadata, hasLength(1));
assertTopGetAnnotation(parameter.metadata[0], 'c');
}
}
test_annotation_onVariableList_constructor() async {
String content = r'''
class C {
final Object x;
const C(this.x);
}
main() {
@C(C(42))
var foo = null;
}
''';
addTestFile(content);
await resolveTestFile();
var c = result.unit.declarations[0] as ClassDeclaration;
var constructor = c.members[1] as ConstructorDeclaration;
ConstructorElement element = constructor.declaredElement!;
var main = result.unit.declarations[1] as FunctionDeclaration;
var statement = (main.functionExpression.body as BlockFunctionBody)
.block
.statements[0] as VariableDeclarationStatement;
Annotation annotation = statement.variables.metadata[0];
expect(annotation.element, same(element));
var identifier_1 = annotation.name as SimpleIdentifier;
expect(identifier_1.staticElement, same(c.declaredElement));
}
test_annotation_onVariableList_topLevelVariable() async {
String content = r'''
const myAnnotation = 1;
class C {
void method() {
@myAnnotation
int var1 = 4, var2 = 5;
}
}
''';
addTestFile(content);
await resolveTestFile();
var myDeclaration =
result.unit.declarations[0] as TopLevelVariableDeclaration;
VariableDeclaration myVariable = myDeclaration.variables.variables[0];
var myElement = myVariable.declaredElement as TopLevelVariableElement;
var classNode = result.unit.declarations[1] as ClassDeclaration;
var node = classNode.members[0] as MethodDeclaration;
var statement = (node.body as BlockFunctionBody).block.statements[0]
as VariableDeclarationStatement;
Annotation annotation = statement.variables.metadata[0];
expect(annotation.element, same(myElement.getter));
var identifier_1 = annotation.name as SimpleIdentifier;
expect(identifier_1.staticElement, same(myElement.getter));
assertTypeNull(identifier_1);
}
test_annotation_prefixed_classField() async {
newFile('$testPackageLibPath/a.dart', r'''
class A {
static const a = 1;
}
''');
addTestFile(r'''
import 'a.dart' as p;
@p.A.a
main() {}
''');
await resolveTestFile();
CompilationUnit unit = result.unit;
final aImport = unit.declaredElement!.library.imports2[0];
PrefixElement aPrefix = aImport.prefix!.element;
LibraryElement aLibrary = aImport.importedLibrary!;
CompilationUnitElement aUnitElement = aLibrary.definingCompilationUnit;
ClassElement aClass = aUnitElement.getType('A')!;
var aGetter = aClass.getField('a')!.getter;
Annotation annotation = unit.declarations[0].metadata.single;
expect(annotation.element, same(aGetter));
var prefixed = annotation.name as PrefixedIdentifier;
expect(prefixed.prefix.staticElement, same(aPrefix));
expect(prefixed.prefix.staticType, isNull);
expect(prefixed.identifier.staticElement, same(aClass));
expect(prefixed.prefix.staticType, isNull);
expect(annotation.constructorName!.staticElement, aGetter);
assertTypeNull(annotation.constructorName!);
expect(annotation.arguments, isNull);
}
test_annotation_prefixed_constructor() async {
newFile('$testPackageLibPath/a.dart', r'''
class A {
const A(int a, {int b});
}
''');
addTestFile(r'''
import 'a.dart' as p;
@p.A(1, b: 2)
main() {}
''');
await resolveTestFile();
CompilationUnit unit = result.unit;
final aImport = unit.declaredElement!.library.imports2[0];
PrefixElement aPrefix = aImport.prefix!.element;
LibraryElement aLibrary = aImport.importedLibrary!;
CompilationUnitElement aUnitElement = aLibrary.definingCompilationUnit;
ClassElement aClass = aUnitElement.getType('A')!;
ConstructorElement constructor = aClass.unnamedConstructor!;
Annotation annotation = unit.declarations[0].metadata.single;
expect(annotation.element, same(constructor));
var prefixed = annotation.name as PrefixedIdentifier;
expect(prefixed.prefix.staticElement, same(aPrefix));
expect(prefixed.prefix.staticType, isNull);
expect(prefixed.identifier.staticElement, same(aClass));
expect(prefixed.prefix.staticType, isNull);
expect(annotation.constructorName, isNull);
var arguments = annotation.arguments!.arguments;
var parameters = constructor.parameters;
_assertArgumentToParameter(arguments[0], parameters[0]);
_assertArgumentToParameter(arguments[1], parameters[1]);
}
test_annotation_prefixed_constructor_named() async {
newFile('$testPackageLibPath/a.dart', r'''
class A {
const A.named(int a, {int b});
}
''');
addTestFile(r'''
import 'a.dart' as p;
@p.A.named(1, b: 2)
main() {}
''');
await resolveTestFile();
CompilationUnit unit = result.unit;
final aImport = unit.declaredElement!.library.imports2[0];
PrefixElement aPrefix = aImport.prefix!.element;
LibraryElement aLibrary = aImport.importedLibrary!;
CompilationUnitElement aUnitElement = aLibrary.definingCompilationUnit;
ClassElement aClass = aUnitElement.getType('A')!;
ConstructorElement constructor = aClass.getNamedConstructor('named')!;
Annotation annotation = unit.declarations[0].metadata.single;
expect(annotation.element, same(constructor));
var prefixed = annotation.name as PrefixedIdentifier;
expect(prefixed.prefix.staticElement, same(aPrefix));
expect(prefixed.prefix.staticType, isNull);
expect(prefixed.identifier.staticElement, same(aClass));
expect(prefixed.prefix.staticType, isNull);
var constructorName = annotation.constructorName as SimpleIdentifier;
expect(constructorName.staticElement, same(constructor));
assertTypeNull(constructorName);
var arguments = annotation.arguments!.arguments;
var parameters = constructor.parameters;
_assertArgumentToParameter(arguments[0], parameters[0]);
_assertArgumentToParameter(arguments[1], parameters[1]);
}
test_annotation_prefixed_topLevelVariable() async {
newFile('$testPackageLibPath/a.dart', r'''
const topAnnotation = 1;
''');
addTestFile(r'''
import 'a.dart' as p;
@p.topAnnotation
main() {}
''');
await resolveTestFile();
CompilationUnit unit = result.unit;
final aImport = unit.declaredElement!.library.imports2[0];
PrefixElement aPrefix = aImport.prefix!.element;
LibraryElement aLibrary = aImport.importedLibrary!;
CompilationUnitElement aUnitElement = aLibrary.definingCompilationUnit;
var topAnnotation = aUnitElement.topLevelVariables[0].getter;
Annotation annotation = unit.declarations[0].metadata.single;
expect(annotation.element, same(topAnnotation));
var prefixed = annotation.name as PrefixedIdentifier;
expect(prefixed.prefix.staticElement, same(aPrefix));
expect(prefixed.prefix.staticType, isNull);
expect(prefixed.identifier.staticElement, same(topAnnotation));
expect(prefixed.prefix.staticType, isNull);
expect(annotation.constructorName, isNull);
expect(annotation.arguments, isNull);
}
test_annotation_unprefixed_classField() async {
addTestFile(r'''
@A.a
main() {}
class A {
static const a = 1;
}
''');
await resolveTestFile();
CompilationUnit unit = result.unit;
CompilationUnitElement unitElement = unit.declaredElement!;
ClassElement aClass = unitElement.getType('A')!;
var aGetter = aClass.getField('a')!.getter;
Annotation annotation = unit.declarations[0].metadata.single;
expect(annotation.element, same(aGetter));
var prefixed = annotation.name as PrefixedIdentifier;
expect(prefixed.prefix.staticElement, same(aClass));
assertTypeNull(prefixed.prefix);
expect(prefixed.identifier.staticElement, same(aGetter));
assertTypeNull(prefixed.identifier);
expect(annotation.constructorName, isNull);
expect(annotation.arguments, isNull);
}
test_annotation_unprefixed_constructor() async {
addTestFile(r'''
@A(1, b: 2)
main() {}
class A {
const A(int a, {int b});
}
''');
await resolveTestFile();
CompilationUnit unit = result.unit;
CompilationUnitElement unitElement = unit.declaredElement!;
ClassElement aClass = unitElement.getType('A')!;
ConstructorElement constructor = aClass.unnamedConstructor!;
Annotation annotation = unit.declarations[0].metadata.single;
expect(annotation.element, same(constructor));
var name = annotation.name as SimpleIdentifier;
expect(name.staticElement, same(aClass));
expect(annotation.constructorName, isNull);
var arguments = annotation.arguments!.arguments;
var parameters = constructor.parameters;
_assertArgumentToParameter(arguments[0], parameters[0]);
_assertArgumentToParameter(arguments[1], parameters[1]);
}
test_annotation_unprefixed_constructor_named() async {
addTestFile(r'''
@A.named(1, b: 2)
main() {}
class A {
const A.named(int a, {int b});
}
''');
await resolveTestFile();
CompilationUnit unit = result.unit;
CompilationUnitElement unitElement = unit.declaredElement!;
ClassElement aClass = unitElement.getType('A')!;
ConstructorElement constructor = aClass.constructors.single;
Annotation annotation = unit.declarations[0].metadata.single;
expect(annotation.element, same(constructor));
var prefixed = annotation.name as PrefixedIdentifier;
expect(prefixed.prefix.staticElement, same(aClass));
assertTypeNull(prefixed.prefix);
expect(prefixed.identifier.staticElement, same(constructor));
assertTypeNull(prefixed.identifier);
expect(annotation.constructorName, isNull);
var arguments = annotation.arguments!.arguments;
var parameters = constructor.parameters;
_assertArgumentToParameter(arguments[0], parameters[0]);
_assertArgumentToParameter(arguments[1], parameters[1]);
}
test_annotation_unprefixed_constructor_withNestedConstructorInvocation() async {
addTestFile('''
class C {
const C();
}
class D {
final C c;
const D(this.c);
}
@D(const C())
f() {}
''');
await resolveTestFile();
var elementC = AstFinder.getClass(result.unit, 'C').declaredElement!;
var constructorC = elementC.constructors[0];
var elementD = AstFinder.getClass(result.unit, 'D').declaredElement!;
var constructorD = elementD.constructors[0];
var atD = AstFinder.getTopLevelFunction(result.unit, 'f').metadata[0];
var constC = atD.arguments!.arguments[0] as InstanceCreationExpression;
expect(atD.name.staticElement, elementD);
expect(atD.element, constructorD);
expect(constC.staticType, interfaceTypeNone(elementC));
var constructorName = constC.constructorName;
expect(constructorName.staticElement, constructorC);
expect(constructorName.type.type, interfaceTypeNone(elementC));
}
test_annotation_unprefixed_topLevelVariable() async {
String content = r'''
const annotation_1 = 1;
const annotation_2 = 1;
@annotation_1
@annotation_2
void main() {
print(42);
}
''';
addTestFile(content);
await resolveTestFile();
var declaration_1 =
result.unit.declarations[0] as TopLevelVariableDeclaration;
VariableDeclaration variable_1 = declaration_1.variables.variables[0];
var element_1 = variable_1.declaredElement as TopLevelVariableElement;
var declaration_2 =
result.unit.declarations[1] as TopLevelVariableDeclaration;
VariableDeclaration variable_2 = declaration_2.variables.variables[0];
var element_2 = variable_2.declaredElement as TopLevelVariableElement;
var main = result.unit.declarations[2] as FunctionDeclaration;
Annotation annotation_1 = main.metadata[0];
expect(annotation_1.element, same(element_1.getter));
var identifier_1 = annotation_1.name as SimpleIdentifier;
expect(identifier_1.staticElement, same(element_1.getter));
assertTypeNull(identifier_1);
Annotation annotation_2 = main.metadata[1];
expect(annotation_2.element, same(element_2.getter));
var identifier_2 = annotation_2.name as SimpleIdentifier;
expect(identifier_2.staticElement, same(element_2.getter));
assertTypeNull(identifier_2);
}
test_asExpression() async {
await assertNoErrorsInCode(r'''
void main() {
num v = 42;
v as int;
}
''');
List<Statement> statements = _getMainStatements(result);
// num v = 42;
VariableElement vElement;
{
var statement = statements[0] as VariableDeclarationStatement;
vElement = statement.variables.variables[0].name.staticElement
as VariableElement;
expect(vElement.type, typeProvider.numType);
}
// v as int;
{
var statement = statements[1] as ExpressionStatement;
var asExpression = statement.expression as AsExpression;
expect(asExpression.staticType, typeProvider.intType);
var target = asExpression.expression as SimpleIdentifier;
expect(target.staticElement, vElement);
expect(target.staticType, typeProvider.numType);
var intName = asExpression.type as NamedType;
expect(intName.name.staticElement, typeProvider.intType.element);
expect(intName.name.staticType, isNull);
}
}
test_binary_operator_with_synthetic_operands() async {
addTestFile('''
void main() {
var list = *;
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
// Note: no further expectations. We don't care how the error is recovered
// from, provided it is recovered from in a way that doesn't crash the
// analyzer/FE integration.
}
test_binaryExpression() async {
String content = r'''
main() {
var v = 1 + 2;
}
''';
addTestFile(content);
await resolveTestFile();
List<Statement> mainStatements = _getMainStatements(result);
var statement = mainStatements[0] as VariableDeclarationStatement;
VariableDeclaration vNode = statement.variables.variables[0];
VariableElement vElement = vNode.declaredElement!;
expect(vElement.type, typeProvider.intType);
var value = vNode.initializer as BinaryExpression;
expect(value.leftOperand.staticType, typeProvider.intType);
expect(value.rightOperand.staticType, typeProvider.intType);
expect(value.staticElement!.name, '+');
expect(value.staticType, typeProvider.intType);
}
test_binaryExpression_gtGtGt() async {
await resolveTestCode('''
class A {
A operator >>>(int amount) => this;
}
f(A a) {
a >>> 3;
}
''');
assertResolvedNodeText(findNode.binary('>>> 3'), r'''
BinaryExpression
leftOperand: SimpleIdentifier
token: a
staticElement: self::@function::f::@parameter::a
staticType: A
operator: >>>
rightOperand: IntegerLiteral
literal: 3
parameter: self::@class::A::@method::>>>::@parameter::amount
staticType: int
staticElement: self::@class::A::@method::>>>
staticInvokeType: A Function(int)
staticType: A
''');
}
test_binaryExpression_ifNull() async {
String content = r'''
int x = 3;
main() {
1.2 ?? x;
}
''';
addTestFile(content);
await resolveTestFile();
List<Statement> mainStatements = _getMainStatements(result);
var statement = mainStatements[0] as ExpressionStatement;
var binary = statement.expression as BinaryExpression;
expect(binary.operator.type, TokenType.QUESTION_QUESTION);
expect(binary.staticElement, isNull);
expect(binary.staticType, typeProvider.numType);
expect(binary.leftOperand.staticType, typeProvider.doubleType);
expect(binary.rightOperand.staticType, typeProvider.intType);
}
test_binaryExpression_logical() async {
addTestFile(r'''
main() {
true && true;
true || true;
}
''');
await resolveTestFile();
List<Statement> statements = _getMainStatements(result);
{
var statement = statements[0] as ExpressionStatement;
var binaryExpression = statement.expression as BinaryExpression;
expect(binaryExpression.staticElement, isNull);
expect(binaryExpression.staticType, typeProvider.boolType);
}
{
var statement = statements[1] as ExpressionStatement;
var binaryExpression = statement.expression as BinaryExpression;
expect(binaryExpression.staticElement, isNull);
expect(binaryExpression.staticType, typeProvider.boolType);
}
}
test_binaryExpression_notEqual() async {
String content = r'''
main() {
1 != 2;
}
''';
addTestFile(content);
await resolveTestFile();
List<Statement> statements = _getMainStatements(result);
var statement = statements[0] as ExpressionStatement;
var expression = statement.expression as BinaryExpression;
expect(expression.operator.type, TokenType.BANG_EQ);
expect(expression.leftOperand.staticType, typeProvider.intType);
expect(expression.rightOperand.staticType, typeProvider.intType);
expect(expression.staticElement!.name, '==');
expect(expression.staticType, typeProvider.boolType);
}
test_cascade_get_with_numeric_getter_name() async {
addTestFile('''
void f(x) {
x..42;
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
// Note: no further expectations. We don't care how the error is recovered
// from, provided it is recovered from in a way that doesn't crash the
// analyzer/FE integration.
}
test_cascade_method_call_with_synthetic_method_name() async {
addTestFile('''
void f(x) {
x..(42);
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
// Note: no further expectations. We don't care how the error is recovered
// from, provided it is recovered from in a way that doesn't crash the
// analyzer/FE integration.
}
test_cascadeExpression() async {
String content = r'''
void main() {
new A()..a()..b();
}
class A {
void a() {}
void b() {}
}
''';
addTestFile(content);
await resolveTestFile();
List<Statement> statements = _getMainStatements(result);
var statement = statements[0] as ExpressionStatement;
var expression = statement.expression as CascadeExpression;
expect(expression.target.staticType, isNotNull);
NodeList<Expression> sections = expression.cascadeSections;
var a = sections[0] as MethodInvocation;
expect(a.methodName.staticElement, isNotNull);
expect(a.staticType, isNotNull);
var b = sections[1] as MethodInvocation;
expect(b.methodName.staticElement, isNotNull);
expect(b.staticType, isNotNull);
}
test_closure() async {
addTestFile(r'''
main() {
var items = <int>[1, 2, 3];
items.forEach((item) {
item;
});
items.forEach((item) {
item;
});
}
''');
await resolveTestFile();
var mainDeclaration = result.unit.declarations[0] as FunctionDeclaration;
var mainElement = mainDeclaration.declaredElement as FunctionElement;
var mainBody = mainDeclaration.functionExpression.body as BlockFunctionBody;
List<Statement> mainStatements = mainBody.block.statements;
var itemsStatement = mainStatements[0] as VariableDeclarationStatement;
var itemsElement = itemsStatement.variables.variables[0].declaredElement!;
// First closure.
ParameterElement itemElement1;
{
var forStatement = mainStatements[1] as ExpressionStatement;
var forInvocation = forStatement.expression as MethodInvocation;
var forTarget = forInvocation.target as SimpleIdentifier;
expect(forTarget.staticElement, itemsElement);
var closureTypeStr = 'void Function(int)';
var closure =
forInvocation.argumentList.arguments[0] as FunctionExpression;
var closureElement = closure.declaredElement as FunctionElementImpl;
expect(closureElement.enclosingElement, same(mainElement));
ParameterElement itemElement = closureElement.parameters[0];
itemElement1 = itemElement;
expect(closureElement.returnType, typeProvider.voidType);
assertType(closureElement.type, closureTypeStr);
expect(closure.staticType, same(closureElement.type));
List<FormalParameter> closureParameters = closure.parameters!.parameters;
expect(closureParameters, hasLength(1));
var itemNode = closureParameters[0] as SimpleFormalParameter;
_assertSimpleParameter(itemNode, itemElement,
name: 'item',
offset: 56,
kind: ParameterKind.REQUIRED,
type: typeProvider.intType);
var closureBody = closure.body as BlockFunctionBody;
List<Statement> closureStatements = closureBody.block.statements;
var itemStatement = closureStatements[0] as ExpressionStatement;
var itemIdentifier = itemStatement.expression as SimpleIdentifier;
expect(itemIdentifier.staticElement, itemElement);
expect(itemIdentifier.staticType, typeProvider.intType);
}
// Second closure, same names, different elements.
{
var forStatement = mainStatements[2] as ExpressionStatement;
var forInvocation = forStatement.expression as MethodInvocation;
var forTarget = forInvocation.target as SimpleIdentifier;
expect(forTarget.staticElement, itemsElement);
var closureTypeStr = 'void Function(int)';
var closure =
forInvocation.argumentList.arguments[0] as FunctionExpression;
var closureElement = closure.declaredElement as FunctionElementImpl;
expect(closureElement.enclosingElement, same(mainElement));
ParameterElement itemElement = closureElement.parameters[0];
expect(itemElement, isNot(same(itemElement1)));
expect(closureElement.returnType, typeProvider.voidType);
assertType(closureElement.type, closureTypeStr);
expect(closure.staticType, same(closureElement.type));
List<FormalParameter> closureParameters = closure.parameters!.parameters;
expect(closureParameters, hasLength(1));
var itemNode = closureParameters[0] as SimpleFormalParameter;
_assertSimpleParameter(itemNode, itemElement,
name: 'item',
offset: 97,
kind: ParameterKind.REQUIRED,
type: typeProvider.intType);
var closureBody = closure.body as BlockFunctionBody;
List<Statement> closureStatements = closureBody.block.statements;
var itemStatement = closureStatements[0] as ExpressionStatement;
var itemIdentifier = itemStatement.expression as SimpleIdentifier;
expect(itemIdentifier.staticElement, itemElement);
expect(itemIdentifier.staticType, typeProvider.intType);
}
}
test_closure_generic() async {
addTestFile(r'''
main() {
foo(<T>() => new List<T>(4));
}
void foo(List<T> Function<T>() createList) {}
''');
await resolveTestFile();
var closure = findNode.functionExpression('<T>() =>');
assertType(closure, 'List<T> Function<T>()');
var closureElement = closure.declaredElement as FunctionElementImpl;
expect(closureElement.enclosingElement, findElement.function('main'));
assertType(closureElement.returnType, 'List<T>');
expect(closureElement.parameters, isEmpty);
var typeParameters = closureElement.typeParameters;
expect(typeParameters, hasLength(1));
TypeParameterElement tElement = typeParameters[0];
expect(tElement.name, 'T');
expect(tElement.nameOffset, 16);
var creation = findNode.instanceCreation('new List');
assertType(creation, 'List<T>');
var tRef = findNode.simple('T>(4)');
assertElement(tRef, tElement);
}
test_closure_inField() async {
addTestFile(r'''
class C {
var v = (() => 42)();
}
''');
await resolveTestFile();
CompilationUnit unit = result.unit;
var c = unit.declarations[0] as ClassDeclaration;
var declaration = c.members[0] as FieldDeclaration;
VariableDeclaration field = declaration.fields.variables[0];
var invocation = field.initializer as FunctionExpressionInvocation;
var closure = invocation.function.unParenthesized as FunctionExpression;
var closureElement = closure.declaredElement as FunctionElementImpl;
expect(closureElement, isNotNull);
}
test_closure_inTopLevelVariable() async {
addTestFile(r'''
var v = (() => 42)();
''');
await resolveTestFile();
CompilationUnit unit = result.unit;
var declaration = unit.declarations[0] as TopLevelVariableDeclaration;
VariableDeclaration variable = declaration.variables.variables[0];
var invocation = variable.initializer as FunctionExpressionInvocation;
var closure = invocation.function.unParenthesized as FunctionExpression;
var closureElement = closure.declaredElement as FunctionElementImpl;
expect(closureElement, isNotNull);
}
test_conditionalExpression() async {
String content = r'''
void main() {
true ? 1 : 2.3;
}
''';
addTestFile(content);
await resolveTestFile();
List<Statement> statements = _getMainStatements(result);
var statement = statements[0] as ExpressionStatement;
var expression = statement.expression as ConditionalExpression;
expect(expression.staticType, typeProvider.numType);
expect(expression.condition.staticType, typeProvider.boolType);
expect(expression.thenExpression.staticType, typeProvider.intType);
expect(expression.elseExpression.staticType, typeProvider.doubleType);
}
test_const_constructor_calls_non_const_super() async {
addTestFile('''
class A {
final a;
A(this.a);
}
class B extends A {
const B() : super(5);
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
// Note: no further expectations. We don't care how the error is recovered
// from, provided it is recovered from in a way that doesn't crash the
// analyzer/FE integration.
}
test_constructor_context() async {
addTestFile(r'''
class C {
C(int p) {
p;
}
}
''');
await resolveTestFile();
var cNode = result.unit.declarations[0] as ClassDeclaration;
var constructorNode = cNode.members[0] as ConstructorDeclaration;
ParameterElement pElement = constructorNode.declaredElement!.parameters[0];
var constructorBody = constructorNode.body as BlockFunctionBody;
var pStatement = constructorBody.block.statements[0] as ExpressionStatement;
var pIdentifier = pStatement.expression as SimpleIdentifier;
expect(pIdentifier.staticElement, same(pElement));
expect(pIdentifier.staticType, typeProvider.intType);
}
test_constructor_initializer_field() async {
addTestFile(r'''
class C {
int f;
C(int p) : f = p {
f;
}
}
''');
await resolveTestFile();
var cNode = result.unit.declarations[0] as ClassDeclaration;
ClassElement cElement = cNode.declaredElement!;
FieldElement fElement = cElement.getField('f')!;
var constructorNode = cNode.members[1] as ConstructorDeclaration;
ParameterElement pParameterElement =
constructorNode.declaredElement!.parameters[0];
{
var initializer =
constructorNode.initializers[0] as ConstructorFieldInitializer;
expect(initializer.fieldName.staticElement, same(fElement));
var expression = initializer.expression as SimpleIdentifier;
expect(expression.staticElement, same(pParameterElement));
}
}
test_constructor_initializer_super() async {
addTestFile(r'''
class A {
A(int a);
A.named(int a, {int b});
}
class B extends A {
B.one(int p) : super(p + 1);
B.two(int p) : super.named(p + 1, b: p + 2);
}
''');
await resolveTestFile();
var aNode = result.unit.declarations[0] as ClassDeclaration;
ClassElement aElement = aNode.declaredElement!;
var bNode = result.unit.declarations[1] as ClassDeclaration;
{
var constructor = bNode.members[0] as ConstructorDeclaration;
var initializer =
constructor.initializers[0] as SuperConstructorInvocation;
expect(initializer.staticElement, same(aElement.unnamedConstructor));
expect(initializer.constructorName, isNull);
}
{
var namedConstructor = aElement.getNamedConstructor('named')!;
var constructor = bNode.members[1] as ConstructorDeclaration;
var initializer =
constructor.initializers[0] as SuperConstructorInvocation;
expect(initializer.staticElement, same(namedConstructor));
var constructorName = initializer.constructorName!;
expect(constructorName.staticElement, same(namedConstructor));
expect(constructorName.staticType, isNull);
List<Expression> arguments = initializer.argumentList.arguments;
_assertArgumentToParameter(arguments[0], namedConstructor.parameters[0]);
_assertArgumentToParameter(arguments[1], namedConstructor.parameters[1]);
}
}
test_constructor_initializer_this() async {
addTestFile(r'''
class C {
C(int a, [int b]);
C.named(int a, {int b});
C.one(int p) : this(1, 2);
C.two(int p) : this.named(3, b: 4);
}
''');
await resolveTestFile();
var cNode = result.unit.declarations[0] as ClassDeclaration;
ClassElement cElement = cNode.declaredElement!;
{
var unnamedConstructor = cElement.constructors[0];
var constructor = cNode.members[2] as ConstructorDeclaration;
var initializer =
constructor.initializers[0] as RedirectingConstructorInvocation;
expect(initializer.staticElement, same(unnamedConstructor));
expect(initializer.constructorName, isNull);
List<Expression> arguments = initializer.argumentList.arguments;
_assertArgumentToParameter(
arguments[0], unnamedConstructor.parameters[0]);
_assertArgumentToParameter(
arguments[1], unnamedConstructor.parameters[1]);
}
{
var namedConstructor = cElement.constructors[1];
var constructor = cNode.members[3] as ConstructorDeclaration;
var initializer =
constructor.initializers[0] as RedirectingConstructorInvocation;
expect(initializer.staticElement, same(namedConstructor));
var constructorName = initializer.constructorName!;
expect(constructorName.staticElement, same(namedConstructor));
expect(constructorName.staticType, isNull);
List<Expression> arguments = initializer.argumentList.arguments;
_assertArgumentToParameter(arguments[0], namedConstructor.parameters[0]);
_assertArgumentToParameter(arguments[1], namedConstructor.parameters[1]);
}
}
test_constructor_redirected() async {
addTestFile(r'''
class A implements B {
A(int a);
A.named(double a);
}
class B {
factory B.one(int b) = A;
factory B.two(double b) = A.named;
}
''');
await resolveTestFile();
expect(result.errors, isEmpty);
var aNode = result.unit.declarations[0] as ClassDeclaration;
ClassElement aElement = aNode.declaredElement!;
var bNode = result.unit.declarations[1] as ClassDeclaration;
{
ConstructorElement aUnnamed = aElement.constructors[0];
var constructor = bNode.members[0] as ConstructorDeclaration;
ConstructorElement element = constructor.declaredElement!;
expect(element.redirectedConstructor, same(aUnnamed));
var constructorName = constructor.redirectedConstructor!;
expect(constructorName.staticElement, same(aUnnamed));
NamedType namedType = constructorName.type;
expect(namedType.type, interfaceTypeNone(aElement));
var identifier = namedType.name as SimpleIdentifier;
expect(identifier.staticElement, same(aElement));
expect(identifier.staticType, isNull);
expect(constructorName.name, isNull);
}
{
ConstructorElement aNamed = aElement.constructors[1];
var constructor = bNode.members[1] as ConstructorDeclaration;
ConstructorElement element = constructor.declaredElement!;
expect(element.redirectedConstructor, same(aNamed));
var constructorName = constructor.redirectedConstructor!;
expect(constructorName.staticElement, same(aNamed));
var namedType = constructorName.type;
expect(namedType.type, interfaceTypeNone(aElement));
var identifier = namedType.name as SimpleIdentifier;
expect(identifier.staticElement, same(aElement));
expect(identifier.staticType, isNull);
expect(constructorName.name!.staticElement, aNamed);
expect(constructorName.name!.staticType, isNull);
}
}
test_constructor_redirected_generic() async {
addTestFile(r'''
class A<T> implements B<T> {
A(int a);
A.named(double a);
}
class B<U> {
factory B.one(int b) = A<U>;
factory B.two(double b) = A<U>.named;
}
''');
await resolveTestFile();
expect(result.errors, isEmpty);
var aNode = result.unit.declarations[0] as ClassDeclaration;
ClassElement aElement = aNode.declaredElement!;
var bNode = result.unit.declarations[1] as ClassDeclaration;
TypeParameterType uType =
typeParameterTypeNone(bNode.declaredElement!.typeParameters[0]);
InterfaceType auType = aElement.instantiate(
typeArguments: [uType],
nullabilitySuffix: NullabilitySuffix.none,
);
{
ConstructorElement expectedElement = aElement.constructors[0];
var constructor = bNode.members[0] as ConstructorDeclaration;
ConstructorElement element = constructor.declaredElement!;
var actualMember = element.redirectedConstructor!;
assertMember(actualMember, expectedElement, {'T': 'U'});
var constructorName = constructor.redirectedConstructor!;
expect(constructorName.staticElement, same(actualMember));
NamedType namedType = constructorName.type;
expect(namedType.type, auType);
var identifier = namedType.name as SimpleIdentifier;
expect(identifier.staticElement, same(aElement));
expect(identifier.staticType, isNull);
expect(constructorName.name, isNull);
}
{
ConstructorElement expectedElement = aElement.constructors[1];
var constructor = bNode.members[1] as ConstructorDeclaration;
ConstructorElement element = constructor.declaredElement!;
var actualMember = element.redirectedConstructor!;
assertMember(actualMember, expectedElement, {'T': 'U'});
var constructorName = constructor.redirectedConstructor!;
expect(constructorName.staticElement, same(actualMember));
NamedType namedType = constructorName.type;
expect(namedType.type, auType);
var identifier = namedType.name as SimpleIdentifier;
expect(identifier.staticElement, same(aElement));
expect(identifier.staticType, isNull);
expect(constructorName.name!.staticElement, same(actualMember));
expect(constructorName.name!.staticType, isNull);
}
}
test_deferredImport_loadLibrary_invocation() async {
newFile('$testPackageLibPath/a.dart', '');
addTestFile(r'''
import 'a.dart' deferred as a;
main() {
a.loadLibrary();
}
''');
await resolveTestFile();
var import = findElement.import('package:test/a.dart');
var invocation = findNode.methodInvocation('loadLibrary');
assertType(invocation, 'Future<dynamic>');
assertInvokeType(invocation, 'Future<dynamic> Function()');
var target = invocation.target as SimpleIdentifier;
assertElement(target, import.prefix?.element);
assertType(target, null);
var name = invocation.methodName;
assertElement(name, import.importedLibrary!.loadLibraryFunction);
assertType(name, 'Future<dynamic> Function()');
}
test_deferredImport_loadLibrary_invocation_argument() async {
newFile('$testPackageLibPath/a.dart', '');
addTestFile(r'''
import 'a.dart' deferred as a;
var b = 1;
var c = 2;
main() {
a.loadLibrary(b, c);
}
''');
await resolveTestFile();
var import = findElement.import('package:test/a.dart');
var invocation = findNode.methodInvocation('loadLibrary');
assertType(invocation, 'Future<dynamic>');
assertInvokeType(invocation, 'Future<dynamic> Function()');
var target = invocation.target as SimpleIdentifier;
assertElement(target, import.prefix?.element);
assertType(target, null);
var name = invocation.methodName;
assertElement(name, import.importedLibrary!.loadLibraryFunction);
assertType(name, 'Future<dynamic> Function()');
var bRef = invocation.argumentList.arguments[0];
assertElement(bRef, findElement.topGet('b'));
assertType(bRef, 'int');
var cRef = invocation.argumentList.arguments[1];
assertElement(cRef, findElement.topGet('c'));
assertType(cRef, 'int');
}
test_deferredImport_loadLibrary_tearOff() async {
newFile('$testPackageLibPath/a.dart', '');
addTestFile(r'''
import 'a.dart' deferred as a;
main() {
a.loadLibrary;
}
''');
await resolveTestFile();
var import = findElement.import('package:test/a.dart');
var prefixed = findNode.prefixed('a.loadLibrary');
assertType(prefixed, 'Future<dynamic> Function()');
var prefix = prefixed.prefix;
assertElement(prefix, import.prefix?.element);
assertType(prefix, null);
var identifier = prefixed.identifier;
assertElement(identifier, import.importedLibrary!.loadLibraryFunction);
assertType(identifier, 'Future<dynamic> Function()');
}
test_deferredImport_variable() async {
newFile('$testPackageLibPath/a.dart', 'var v = 0;');
addTestFile(r'''
import 'a.dart' deferred as a;
main() async {
a.v;
a.v = 1;
}
''');
await resolveTestFile();
var import = findElement.import('package:test/a.dart');
var v = (import.importedLibrary!.publicNamespace.get('v')
as PropertyAccessorElement)
.variable as TopLevelVariableElement;
{
var prefixed = findNode.prefixed('a.v;');
assertElement(prefixed, v.getter);
assertType(prefixed, 'int');
assertElement(prefixed.prefix, import.prefix?.element);
assertType(prefixed.prefix, null);
assertElement(prefixed.identifier, v.getter);
assertType(prefixed.identifier, 'int');
}
{
var prefixed = findNode.prefixed('a.v = 1;');
assertElementNull(prefixed);
assertTypeNull(prefixed);
assertElement(prefixed.prefix, import.prefix?.element);
assertType(prefixed.prefix, null);
assertUnresolvedSimpleIdentifier(prefixed.identifier);
}
}
test_directive_export() async {
newFile('$testPackageLibPath/a.dart', r'''
class MyClass {}
int myVar;
int get myGetter => 0;
int set mySetter(_) {}
''');
addTestFile(r'''
export 'a.dart' show MyClass, myVar, myGetter, mySetter, Unresolved;
''');
await resolveTestFile();
var export = findElement.export('package:test/a.dart');
var namespace = export.exportedLibrary!.exportNamespace;
{
var ref = findNode.simple('MyClass');
assertElement(ref, namespace.get('MyClass'));
assertType(ref, null);
}
{
var ref = findNode.simple('myVar');
var getter = namespace.get('myVar') as PropertyAccessorElement;
assertElement(ref, getter.variable);
assertType(ref, null);
}
{
var ref = findNode.simple('myGetter');
var getter = namespace.get('myGetter') as PropertyAccessorElement;
assertElement(ref, getter.variable);
assertType(ref, null);
}
{
var ref = findNode.simple('mySetter');
var getter = namespace.get('mySetter=') as PropertyAccessorElement;
assertElement(ref, getter.variable);
assertType(ref, null);
}
{
var ref = findNode.simple('Unresolved');
assertElementNull(ref);
assertType(ref, null);
}
}
test_directive_import_hide() async {
newFile('$testPackageLibPath/a.dart', r'''
class MyClass {}
int myVar;
int get myGetter => 0;
int set mySetter(_) {}
''');
addTestFile(r'''
import 'a.dart' hide MyClass, myVar, myGetter, mySetter, Unresolved;
''');
await resolveTestFile();
var import = findElement.import('package:test/a.dart');
var namespace = import.importedLibrary!.exportNamespace;
{
var ref = findNode.simple('MyClass');
assertElement(ref, namespace.get('MyClass'));
assertType(ref, null);
}
{
var ref = findNode.simple('myVar');
var getter = namespace.get('myVar') as PropertyAccessorElement;
assertElement(ref, getter.variable);
assertType(ref, null);
}
{
var ref = findNode.simple('myGetter');
var getter = namespace.get('myGetter') as PropertyAccessorElement;
assertElement(ref, getter.variable);
assertType(ref, null);
}
{
var ref = findNode.simple('mySetter');
var getter = namespace.get('mySetter=') as PropertyAccessorElement;
assertElement(ref, getter.variable);
assertType(ref, null);
}
{
var ref = findNode.simple('Unresolved');
assertElementNull(ref);
assertType(ref, null);
}
}
test_directive_import_show() async {
newFile('$testPackageLibPath/a.dart', r'''
class MyClass {}
int myVar;
int get myGetter => 0;
int set mySetter(_) {}
''');
addTestFile(r'''
import 'a.dart' show MyClass, myVar, myGetter, mySetter, Unresolved;
''');
await resolveTestFile();
var import = findElement.import('package:test/a.dart');
var namespace = import.importedLibrary!.exportNamespace;
{
var ref = findNode.simple('MyClass');
assertElement(ref, namespace.get('MyClass'));
assertType(ref, null);
}
{
var ref = findNode.simple('myVar');
var getter = namespace.get('myVar') as PropertyAccessorElement;
assertElement(ref, getter.variable);
assertType(ref, null);
}
{
var ref = findNode.simple('myGetter');
var getter = namespace.get('myGetter') as PropertyAccessorElement;
assertElement(ref, getter.variable);
assertType(ref, null);
}
{
var ref = findNode.simple('mySetter');
var getter = namespace.get('mySetter=') as PropertyAccessorElement;
assertElement(ref, getter.variable);
assertType(ref, null);
}
{
var ref = findNode.simple('Unresolved');
assertElementNull(ref);
assertType(ref, null);
}
}
test_enum_toString() async {
addTestFile(r'''
enum MyEnum { A, B, C }
main(MyEnum e) {
e.toString();
}
''');
await resolveTestFile();
List<Statement> mainStatements = _getMainStatements(result);
var statement = mainStatements[0] as ExpressionStatement;
var invocation = statement.expression as MethodInvocation;
assertInvokeType(invocation, 'String Function()');
var methodElement = invocation.methodName.staticElement as MethodElement;
expect(methodElement.name, 'toString');
expect(methodElement.enclosingElement, same(objectElement));
}
test_error_unresolvedTypeAnnotation() async {
String content = r'''
main() {
Foo<int> v = null;
}
''';
addTestFile(content);
await resolveTestFile();
var statements = _getMainStatements(result);
var statement = statements[0] as VariableDeclarationStatement;
var namedType = statement.variables.type as NamedType;
expect(namedType.type, isDynamicType);
expect(namedType.typeArguments!.arguments[0].type, typeProvider.intType);
VariableDeclaration vNode = statement.variables.variables[0];
expect(vNode.name.staticType, isNull);
expect(vNode.declaredElement!.type, isDynamicType);
}
test_field_context() async {
addTestFile(r'''
class C<T> {
var f = <T>[];
}
''');
await resolveTestFile();
var cNode = result.unit.declarations[0] as ClassDeclaration;
var tElement = cNode.declaredElement!.typeParameters[0];
var fDeclaration = cNode.members[0] as FieldDeclaration;
VariableDeclaration fNode = fDeclaration.fields.variables[0];
var fElement = fNode.declaredElement as FieldElement;
expect(
fElement.type, typeProvider.listType(typeParameterTypeNone(tElement)));
}
test_field_generic() async {
addTestFile(r'''
class C<T> {
T f;
}
main(C<int> c) {
c.f; // ref
c.f = 1;
}
''');
await resolveTestFile();
{
var fRef = findNode.simple('f; // ref');
assertMember(fRef, findElement.getter('f'), {'T': 'int'});
assertType(fRef, 'int');
}
{
var fRef = findNode.simple('f = 1;');
assertUnresolvedSimpleIdentifier(fRef);
}
}
test_formalParameter_functionTyped() async {
addTestFile(r'''
class A {
A(String p(int a));
}
''');
await resolveTestFile();
var clazz = result.unit.declarations[0] as ClassDeclaration;
var constructor = clazz.members[0] as ConstructorDeclaration;
List<FormalParameter> parameters = constructor.parameters.parameters;
var p = parameters[0] as FunctionTypedFormalParameter;
expect(p.declaredElement, same(constructor.declaredElement!.parameters[0]));
{
var type =
(p.identifier.staticElement as ParameterElement).type as FunctionType;
expect(type.returnType, typeProvider.stringType);
expect(type.parameters, hasLength(1));
expect(type.parameters[0].type, typeProvider.intType);
}
_assertNamedTypeSimple(p.returnType!, typeProvider.stringType);
{
var a = p.parameters.parameters[0] as SimpleFormalParameter;
_assertNamedTypeSimple(a.type!, typeProvider.intType);
expect(a.identifier!.staticType, isNull);
}
}
test_formalParameter_functionTyped_fieldFormal_typed() async {
// TODO(scheglov) Add "untyped" version with precise type in field.
addTestFile(r'''
class A {
Function f;
A(String this.f(int a));
}
''');
await resolveTestFile();
var clazz = result.unit.declarations[0] as ClassDeclaration;
var fDeclaration = clazz.members[0] as FieldDeclaration;
VariableDeclaration fNode = fDeclaration.fields.variables[0];
var fElement = fNode.declaredElement as FieldElement;
var constructor = clazz.members[1] as ConstructorDeclaration;
var pElement = constructor.declaredElement!.parameters[0]
as FieldFormalParameterElement;
expect(pElement.field, same(fElement));
List<FormalParameter> parameters = constructor.parameters.parameters;
var p = parameters[0] as FieldFormalParameter;
expect(p.declaredElement, same(pElement));
expect(p.identifier.staticElement, same(pElement));
assertTypeNull(p.identifier);
_assertNamedTypeSimple(p.type!, typeProvider.stringType);
{
var a = p.parameters!.parameters[0] as SimpleFormalParameter;
_assertNamedTypeSimple(a.type!, typeProvider.intType);
expect(a.identifier!.staticType, isNull);
}
}
test_formalParameter_simple_fieldFormal() async {
addTestFile(r'''
class A {
int f;
A(this.f);
}
''');
await resolveTestFile();
var clazz = result.unit.declarations[0] as ClassDeclaration;
var fDeclaration = clazz.members[0] as FieldDeclaration;
VariableDeclaration fNode = fDeclaration.fields.variables[0];
var fElement = fNode.declaredElement as FieldElement;
var constructor = clazz.members[1] as ConstructorDeclaration;
List<FormalParameter> parameters = constructor.parameters.parameters;
var parameterElement = constructor.declaredElement!.parameters[0]
as FieldFormalParameterElement;
expect(parameterElement.field, same(fElement));
var parameterNode = parameters[0] as FieldFormalParameter;
expect(parameterNode.type, isNull);
expect(parameterNode.declaredElement, same(parameterElement));
expect(parameterNode.identifier.staticElement, same(parameterElement));
assertTypeNull(parameterNode.identifier);
}
test_formalParameter_simple_fieldFormal_typed() async {
addTestFile(r'''
class A {
int f;
A(int this.f);
}
''');
await resolveTestFile();
var clazz = result.unit.declarations[0] as ClassDeclaration;
var fDeclaration = clazz.members[0] as FieldDeclaration;
VariableDeclaration fNode = fDeclaration.fields.variables[0];
var fElement = fNode.declaredElement as FieldElement;
var constructor = clazz.members[1] as ConstructorDeclaration;
List<FormalParameter> parameters = constructor.parameters.parameters;
var parameterElement = constructor.declaredElement!.parameters[0]
as FieldFormalParameterElement;
expect(parameterElement.field, same(fElement));
var parameterNode = parameters[0] as FieldFormalParameter;
_assertNamedTypeSimple(parameterNode.type!, typeProvider.intType);
expect(parameterNode.declaredElement, same(parameterElement));
expect(parameterNode.identifier.staticElement, same(parameterElement));
assertTypeNull(parameterNode.identifier);
}
test_forwardingStub_class() async {
addTestFile(r'''
class A<T> {
void m(T t) {}
}
class B extends A<int> {}
main(B b) {
b.m(1);
}
''');
await resolveTestFile();
// var aNode = result.unit!.declarations[0] as ClassDeclaration;
// ClassElement eElement = aNode.declaredElement!;
// MethodElement mElement = eElement.getMethod('m');
List<Statement> mainStatements = _getMainStatements(result);
var statement = mainStatements[0] as ExpressionStatement;
var invocation = statement.expression as MethodInvocation;
assertInvokeType(invocation, 'void Function(int)');
// TODO(scheglov) Check for MethodElement
// expect(invocation.methodName.staticElement, same(mElement));
}
test_function_call_with_synthetic_arguments() async {
addTestFile('''
void f(x) {}
class C {
m() {
f(,);
}
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
// Note: no further expectations. We don't care how the error is recovered
// from, provided it is recovered from in a way that doesn't crash the
// analyzer/FE integration.
}
test_functionExpressionInvocation() async {
addTestFile(r'''
typedef Foo<S> = S Function<T>(T x);
void main(f) {
(f as Foo<int>)<String>('hello');
}
''');
await resolveTestFile();
List<Statement> statements = _getMainStatements(result);
var statement = statements[0] as ExpressionStatement;
var invocation = statement.expression as FunctionExpressionInvocation;
expect(invocation.staticElement, isNull);
assertInvokeType(invocation, 'int Function(String)');
expect(invocation.staticType, typeProvider.intType);
List<TypeAnnotation> typeArguments = invocation.typeArguments!.arguments;
expect(typeArguments, hasLength(1));
_assertNamedTypeSimple(typeArguments[0], typeProvider.stringType);
}
test_functionExpressionInvocation_namedArgument() async {
addTestFile(r'''
int a;
main(f) {
(f)(p: a);
}
''');
await resolveTestFile();
assertTopGetRef('a);', 'a');
}
test_generic_function_type() async {
addTestFile('''
main() {
void Function<T>(T) f;
}
''');
await resolveTestFile();
assertTypeNull(findNode.simple('f;'));
var fType = findElement.localVar('f').type as FunctionType;
var fTypeTypeParameter = fType.typeFormals[0];
var fTypeParameter = fType.normalParameterTypes[0] as TypeParameterType;
expect(fTypeParameter.element, same(fTypeTypeParameter));
var tRef = findNode.simple('T>');
var functionTypeNode = tRef.parent!.parent!.parent as GenericFunctionType;
var functionType = functionTypeNode.type as FunctionType;
assertElement(tRef, functionType.typeFormals[0]);
}
test_indexExpression() async {
String content = r'''
main() {
var items = <int>[1, 2, 3];
items[0];
}
''';
addTestFile(content);
await resolveTestFile();
InterfaceType intType = typeProvider.intType;
InterfaceType listIntType = typeProvider.listType(intType);
List<Statement> mainStatements = _getMainStatements(result);
VariableElement itemsElement;
{
var statement = mainStatements[0] as VariableDeclarationStatement;
VariableDeclaration itemsNode = statement.variables.variables[0];
itemsElement = itemsNode.declaredElement!;
expect(itemsElement.type, listIntType);
}
var statement = mainStatements[1] as ExpressionStatement;
var indexExpression = statement.expression as IndexExpression;
expect(indexExpression.staticType, intType);
var actualElement = indexExpression.staticElement as MethodMember;
var expectedElement = listIntType.getMethod('[]') as MethodMember;
expect(actualElement.name, '[]');
expect(actualElement.declaration, same(expectedElement.declaration));
expect(actualElement.returnType, intType);
expect(actualElement.parameters[0].type, intType);
}
test_instanceCreation_factory() async {
String content = r'''
class C {
factory C() => throw 0;
factory C.named() => throw 0;
}
var a = new C();
var b = new C.named();
''';
addTestFile(content);
await resolveTestFile();
CompilationUnit unit = result.unit;
var cNode = unit.declarations[0] as ClassDeclaration;
ClassElement cElement = cNode.declaredElement!;
ConstructorElement defaultConstructor = cElement.constructors[0];
ConstructorElement namedConstructor = cElement.constructors[1];
{
var aDeclaration = unit.declarations[1] as TopLevelVariableDeclaration;
VariableDeclaration aNode = aDeclaration.variables.variables[0];
var value = aNode.initializer as InstanceCreationExpression;
expect(value.staticType, interfaceTypeNone(cElement));
var constructorName = value.constructorName;
expect(constructorName.name, isNull);
expect(constructorName.staticElement, defaultConstructor);
NamedType namedType = constructorName.type;
expect(namedType.typeArguments, isNull);
Identifier typeIdentifier = namedType.name;
expect(typeIdentifier.staticElement, cElement);
expect(typeIdentifier.staticType, isNull);
}
{
var bDeclaration = unit.declarations[2] as TopLevelVariableDeclaration;
VariableDeclaration bNode = bDeclaration.variables.variables[0];
var value = bNode.initializer as InstanceCreationExpression;
expect(value.staticType, interfaceTypeNone(cElement));
var constructorName = value.constructorName;
expect(constructorName.staticElement, namedConstructor);
expect(constructorName.name!.staticType, isNull);
NamedType namedType = constructorName.type;
expect(namedType.typeArguments, isNull);
var typeIdentifier = namedType.name as SimpleIdentifier;
expect(typeIdentifier.staticElement, cElement);
expect(typeIdentifier.staticType, isNull);
}
}
test_instanceCreation_namedArgument() async {
addTestFile(r'''
class X {
X(int a, {bool b, double c});
}
var v = new X(1, b: true, c: 3.0);
''');
await resolveTestFile();
CompilationUnit unit = result.unit;
var xNode = unit.declarations[0] as ClassDeclaration;
ClassElement xElement = xNode.declaredElement!;
ConstructorElement constructorElement = xElement.constructors[0];
var vDeclaration = unit.declarations[1] as TopLevelVariableDeclaration;
VariableDeclaration vNode = vDeclaration.variables.variables[0];
var creation = vNode.initializer as InstanceCreationExpression;
List<Expression> arguments = creation.argumentList.arguments;
expect(creation.staticType, interfaceTypeNone(xElement));
var constructorName = creation.constructorName;
expect(constructorName.name, isNull);
expect(constructorName.staticElement, constructorElement);
NamedType namedType = constructorName.type;
expect(namedType.typeArguments, isNull);
Identifier typeIdentifier = namedType.name;
expect(typeIdentifier.staticElement, xElement);
expect(typeIdentifier.staticType, isNull);
_assertArgumentToParameter(arguments[0], constructorElement.parameters[0]);
_assertArgumentToParameter(arguments[1], constructorElement.parameters[1]);
_assertArgumentToParameter(arguments[2], constructorElement.parameters[2]);
}
test_instanceCreation_noTypeArguments() async {
String content = r'''
class C {
C(int p);
C.named(int p);
}
var a = new C(1);
var b = new C.named(2);
''';
addTestFile(content);
await resolveTestFile();
CompilationUnit unit = result.unit;
var cNode = unit.declarations[0] as ClassDeclaration;
ClassElement cElement = cNode.declaredElement!;
ConstructorElement defaultConstructor = cElement.constructors[0];
ConstructorElement namedConstructor = cElement.constructors[1];
{
var aDeclaration = unit.declarations[1] as TopLevelVariableDeclaration;
VariableDeclaration aNode = aDeclaration.variables.variables[0];
var value = aNode.initializer as InstanceCreationExpression;
expect(value.staticType, interfaceTypeNone(cElement));
var constructorName = value.constructorName;
expect(constructorName.name, isNull);
expect(constructorName.staticElement, defaultConstructor);
NamedType namedType = constructorName.type;
expect(namedType.typeArguments, isNull);
Identifier typeIdentifier = namedType.name;
expect(typeIdentifier.staticElement, cElement);
expect(typeIdentifier.staticType, isNull);
Expression argument = value.argumentList.arguments[0];
_assertArgumentToParameter(argument, defaultConstructor.parameters[0]);
}
{
var bDeclaration = unit.declarations[2] as TopLevelVariableDeclaration;
VariableDeclaration bNode = bDeclaration.variables.variables[0];
var value = bNode.initializer as InstanceCreationExpression;
expect(value.staticType, interfaceTypeNone(cElement));
var constructorName = value.constructorName;
expect(constructorName.staticElement, namedConstructor);
expect(constructorName.name!.staticElement, namedConstructor);
expect(constructorName.name!.staticType, isNull);
NamedType namedType = constructorName.type;
expect(namedType.typeArguments, isNull);
var typeIdentifier = namedType.name as SimpleIdentifier;
expect(typeIdentifier.staticElement, cElement);
expect(typeIdentifier.staticType, isNull);
Expression argument = value.argumentList.arguments[0];
_assertArgumentToParameter(argument, namedConstructor.parameters[0]);
}
}
test_instanceCreation_prefixed() async {
newFile('$testPackageLibPath/a.dart', r'''
class C<T> {
C(T p);
C.named(T p);
}
''');
addTestFile(r'''
import 'a.dart' as p;
main() {
new p.C(0);
new p.C.named(1.2);
new p.C<bool>.named(false);
}
''');
await resolveTestFile();
CompilationUnit unit = result.unit;
final aImport = unit.declaredElement!.library.imports2[0];
LibraryElement aLibrary = aImport.importedLibrary!;
ClassElement cElement = aLibrary.getType('C')!;
ConstructorElement defaultConstructor = cElement.constructors[0];
ConstructorElement namedConstructor = cElement.constructors[1];
var statements = _getMainStatements(result);
{
var cTypeInt = cElement.instantiate(
typeArguments: [typeProvider.intType],
nullabilitySuffix: NullabilitySuffix.none,
);
var statement = statements[0] as ExpressionStatement;
var creation = statement.expression as InstanceCreationExpression;
expect(creation.staticType, cTypeInt);
var constructorName = creation.constructorName;
expect(constructorName.name, isNull);
expect(constructorName.staticElement, defaultConstructor);
NamedType namedType = constructorName.type;
expect(namedType.typeArguments, isNull);
var typeIdentifier = namedType.name as PrefixedIdentifier;
expect(typeIdentifier.staticElement, same(cElement));
expect(typeIdentifier.staticType, isNull);
SimpleIdentifier typePrefix = typeIdentifier.prefix;
expect(typePrefix.name, 'p');
expect(typePrefix.staticElement, same(aImport.prefix?.element));
expect(typePrefix.staticType, isNull);
expect(typeIdentifier.identifier.staticElement, same(cElement));
}
{
var cTypeDouble = cElement.instantiate(
typeArguments: [typeProvider.doubleType],
nullabilitySuffix: NullabilitySuffix.none,
);
var statement = statements[1] as ExpressionStatement;
var creation = statement.expression as InstanceCreationExpression;
expect(creation.staticType, cTypeDouble);
var constructorName = creation.constructorName;
expect(constructorName.name!.staticElement, namedConstructor);
expect(constructorName.name!.staticType, isNull);
expect(constructorName.staticElement, namedConstructor);
NamedType namedType = constructorName.type;
expect(namedType.typeArguments, isNull);
var typeIdentifier = namedType.name as PrefixedIdentifier;
expect(typeIdentifier.staticElement, cElement);
expect(typeIdentifier.staticType, isNull);
SimpleIdentifier typePrefix = typeIdentifier.prefix;
expect(typePrefix.name, 'p');
expect(typePrefix.staticElement, same(aImport.prefix?.element));
expect(typePrefix.staticType, isNull);
expect(typeIdentifier.identifier.staticElement, same(cElement));
}
{
var cTypeBool = cElement.instantiate(
typeArguments: [typeProvider.boolType],
nullabilitySuffix: NullabilitySuffix.none,
);
var statement = statements[2] as ExpressionStatement;
var creation = statement.expression as InstanceCreationExpression;
expect(creation.staticType, cTypeBool);
var constructorName = creation.constructorName;
expect(constructorName.name!.staticElement, namedConstructor);
expect(constructorName.name!.staticType, isNull);
expect(constructorName.staticElement, namedConstructor);
NamedType namedType = constructorName.type;
expect(namedType.typeArguments!.arguments, hasLength(1));
_assertNamedTypeSimple(
namedType.typeArguments!.arguments[0], typeProvider.boolType);
var typeIdentifier = namedType.name as PrefixedIdentifier;
expect(typeIdentifier.staticElement, cElement);
expect(typeIdentifier.staticType, isNull);
SimpleIdentifier typePrefix = typeIdentifier.prefix;
expect(typePrefix.name, 'p');
expect(typePrefix.staticElement, same(aImport.prefix?.element));
expect(typePrefix.staticType, isNull);
expect(typeIdentifier.identifier.staticElement, same(cElement));
}
}
test_instanceCreation_unprefixed() async {
addTestFile(r'''
main() {
new C(0);
new C<bool>(false);
new C.named(1.2);
new C<bool>.named(false);
}
class C<T> {
C(T p);
C.named(T p);
}
''');
await resolveTestFile();
CompilationUnit unit = result.unit;
CompilationUnitElement unitElement = unit.declaredElement!;
ClassElement cElement = unitElement.getType('C')!;
ConstructorElement defaultConstructor = cElement.constructors[0];
ConstructorElement namedConstructor = cElement.constructors[1];
var statements = _getMainStatements(result);
{
var cTypeInt = cElement.instantiate(
typeArguments: [typeProvider.intType],
nullabilitySuffix: NullabilitySuffix.none,
);
var statement = statements[0] as ExpressionStatement;
var creation = statement.expression as InstanceCreationExpression;
expect(creation.staticType, cTypeInt);
var constructorName = creation.constructorName;
expect(constructorName.name, isNull);
expect(constructorName.staticElement, defaultConstructor);
NamedType namedType = constructorName.type;
expect(namedType.typeArguments, isNull);
var typeIdentifier = namedType.name as SimpleIdentifier;
expect(typeIdentifier.staticElement, same(cElement));
expect(typeIdentifier.staticType, isNull);
}
{
var cTypeBool = cElement.instantiate(
typeArguments: [typeProvider.boolType],
nullabilitySuffix: NullabilitySuffix.none,
);
var statement = statements[1] as ExpressionStatement;
var creation = statement.expression as InstanceCreationExpression;
expect(creation.staticType, cTypeBool);
var constructorName = creation.constructorName;
expect(constructorName.name, isNull);
expect(constructorName.staticElement, defaultConstructor);
NamedType namedType = constructorName.type;
expect(namedType.typeArguments!.arguments, hasLength(1));
_assertNamedTypeSimple(
namedType.typeArguments!.arguments[0], typeProvider.boolType);
var typeIdentifier = namedType.name as SimpleIdentifier;
expect(typeIdentifier.staticElement, same(cElement));
expect(typeIdentifier.staticType, isNull);
}
{
var cTypeDouble = cElement.instantiate(
typeArguments: [typeProvider.doubleType],
nullabilitySuffix: NullabilitySuffix.none,
);
var statement = statements[2] as ExpressionStatement;
var creation = statement.expression as InstanceCreationExpression;
expect(creation.staticType, cTypeDouble);
var constructorName = creation.constructorName;
expect(constructorName.name!.staticElement, namedConstructor);
expect(constructorName.name!.staticType, isNull);
expect(constructorName.staticElement, namedConstructor);
NamedType namedType = constructorName.type;
expect(namedType.typeArguments, isNull);
var typeIdentifier = namedType.name as SimpleIdentifier;
expect(typeIdentifier.staticElement, cElement);
expect(typeIdentifier.staticType, isNull);
}
{
var cTypeBool = cElement.instantiate(
typeArguments: [typeProvider.boolType],
nullabilitySuffix: NullabilitySuffix.none,
);
var statement = statements[3] as ExpressionStatement;
var creation = statement.expression as InstanceCreationExpression;
expect(creation.staticType, cTypeBool);
var constructorName = creation.constructorName;
expect(constructorName.name!.staticElement, namedConstructor);
expect(constructorName.name!.staticType, isNull);
expect(constructorName.staticElement, namedConstructor);
NamedType namedType = constructorName.type;
expect(namedType.typeArguments!.arguments, hasLength(1));
_assertNamedTypeSimple(
namedType.typeArguments!.arguments[0], typeProvider.boolType);
var typeIdentifier = namedType.name as SimpleIdentifier;
expect(typeIdentifier.staticElement, cElement);
expect(typeIdentifier.staticType, isNull);
}
}
test_instanceCreation_withTypeArguments() async {
addTestFile(r'''
class C<K, V> {
C(K k, V v);
C.named(K k, V v);
}
var a = new C<int, double>(1, 2.3);
var b = new C<num, String>.named(4, 'five');
''');
await resolveTestFile();
var cElement = findElement.class_('C');
var defaultConstructor = cElement.unnamedConstructor!;
var namedConstructor = cElement.getNamedConstructor('named')!;
{
var creation = findNode.instanceCreation('new C<int, double>(1, 2.3);');
assertMember(creation, defaultConstructor, {'K': 'int', 'V': 'double'});
assertType(creation, 'C<int, double>');
var namedType = creation.constructorName.type;
assertNamedType(namedType, cElement, 'C<int, double>');
var typeArguments = namedType.typeArguments!.arguments;
assertNamedType(typeArguments[0] as NamedType, intElement, 'int');
assertNamedType(typeArguments[1] as NamedType, doubleElement, 'double');
expect(creation.constructorName.name, isNull);
Expression argument = creation.argumentList.arguments[0];
_assertArgumentToParameter2(argument, 'int');
}
{
var creation = findNode.instanceCreation('new C<num, String>.named');
assertMember(creation, namedConstructor, {'K': 'num', 'V': 'String'});
assertType(creation, 'C<num, String>');
var namedType = creation.constructorName.type;
assertNamedType(namedType, cElement, 'C<num, String>');
var typeArguments = namedType.typeArguments!.arguments;
assertNamedType(typeArguments[0] as NamedType, numElement, 'num');
assertNamedType(typeArguments[1] as NamedType, stringElement, 'String');
var constructorName = creation.constructorName.name;
assertMember(
constructorName, namedConstructor, {'K': 'num', 'V': 'String'});
assertType(constructorName, null);
var argument = creation.argumentList.arguments[0];
_assertArgumentToParameter2(argument, 'num');
}
}
test_invalid_annotation_on_variable_declaration() async {
addTestFile(r'''
const a = null;
main() {
int x, @a y;
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
// Note: no further expectations. We don't care how the error is recovered
// from, provided it is recovered from in a way that doesn't crash the
// analyzer/FE integration.
}
test_invalid_annotation_on_variable_declaration_for() async {
addTestFile(r'''
const a = null;
main() {
for (var @a x = 0;;) {}
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
// Note: no further expectations. We don't care how the error is recovered
// from, provided it is recovered from in a way that doesn't crash the
// analyzer/FE integration.
}
test_invalid_catch_parameters_3() async {
addTestFile(r'''
main() {
try { } catch (x, y, z) { }
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
assertDeclaredVariableTypeObject(findNode.simple('x,'));
assertDeclaredVariableType(findNode.simple('y,'), 'StackTrace');
}
test_invalid_catch_parameters_empty() async {
addTestFile(r'''
main() {
try { } catch () { }
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
}
test_invalid_catch_parameters_named_stack() async {
addTestFile(r'''
main() {
try { } catch (e, {s}) { }
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
assertDeclaredVariableTypeObject(findNode.simple('e,'));
assertDeclaredVariableType(findNode.simple('s})'), 'StackTrace');
}
test_invalid_catch_parameters_optional_stack() async {
addTestFile(r'''
main() {
try { } catch (e, [s]) { }
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
assertDeclaredVariableTypeObject(findNode.simple('e,'));
assertDeclaredVariableType(findNode.simple('s])'), 'StackTrace');
}
test_invalid_const_as() async {
addTestFile(r'''
const num a = 1.2;
const int b = a as int;
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var aRef = findNode.simple('a as int');
assertElement(aRef, findElement.topGet('a'));
assertType(aRef, 'num');
assertNamedType(findNode.namedType('int;'), intElement, 'int');
}
test_invalid_const_constructor_initializer_field_multiple() async {
addTestFile(r'''
var a = 0;
class A {
final x = 0;
const A() : x = a;
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var xRef = findNode.simple('x = a');
assertElement(xRef, findElement.field('x'));
var aRef = findNode.simple('a;');
assertElement(aRef, findElement.topGet('a'));
assertType(aRef, 'int');
}
test_invalid_const_methodInvocation() async {
addTestFile(r'''
const a = 'foo';
const b = 0;
const c = a.codeUnitAt(b);
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var invocation = findNode.methodInvocation('codeUnitAt');
assertType(invocation, 'int');
assertInvokeType(invocation, 'int Function(int)');
assertElement(
invocation.methodName,
elementMatcher(
stringElement.getMethod('codeUnitAt'),
isLegacy: isLegacyLibrary,
),
);
var aRef = invocation.target;
assertElement(aRef, findElement.topGet('a'));
assertType(aRef, 'String');
var bRef = invocation.argumentList.arguments[0];
assertElement(bRef, findElement.topGet('b'));
assertType(bRef, 'int');
}
test_invalid_const_methodInvocation_static() async {
addTestFile(r'''
const c = A.m();
class A {
static int m() => 0;
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var invocation = findNode.methodInvocation('m();');
assertType(invocation, 'int');
assertInvokeType(invocation, 'int Function()');
assertElement(invocation.methodName, findElement.method('m'));
}
test_invalid_const_methodInvocation_topLevelFunction() async {
addTestFile(r'''
const id = identical;
const a = 0;
const b = 0;
const c = id(a, b);
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var invocation = findNode.functionExpressionInvocation('id(');
assertElement(invocation.function, findElement.topGet('id'));
assertInvokeType(invocation, 'bool Function(Object?, Object?)');
assertType(invocation, 'bool');
var aRef = invocation.argumentList.arguments[0];
assertElement(aRef, findElement.topGet('a'));
assertType(aRef, 'int');
var bRef = invocation.argumentList.arguments[1];
assertElement(bRef, findElement.topGet('b'));
assertType(bRef, 'int');
}
test_invalid_const_throw_local() async {
addTestFile(r'''
main() {
const c = throw 42;
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var throwExpression = findNode.throw_('throw 42;');
expect(throwExpression.staticType, isNeverType);
assertType(throwExpression.expression, 'int');
}
test_invalid_const_throw_topLevel() async {
addTestFile(r'''
const c = throw 42;
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var throwExpression = findNode.throw_('throw 42;');
expect(throwExpression.staticType, isNeverType);
assertType(throwExpression.expression, 'int');
}
test_invalid_constructor_initializer_field_class() async {
addTestFile(r'''
var a = 0;
class A {
A() : X = a;
}
class X {}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var xRef = findNode.simple('X = ');
assertElementNull(xRef);
var aRef = findNode.simple('a;');
assertElement(aRef, findElement.topGet('a'));
assertType(aRef, 'int');
}
test_invalid_constructor_initializer_field_getter() async {
addTestFile(r'''
var a = 0;
class A {
A() : x = a;
int get x => 0;
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var xRef = findNode.simple('x = ');
assertElement(xRef, findElement.field('x'));
var aRef = findNode.simple('a;');
assertElement(aRef, findElement.topGet('a'));
assertType(aRef, 'int');
}
test_invalid_constructor_initializer_field_importPrefix() async {
addTestFile(r'''
import 'dart:async' as x;
var a = 0;
class A {
A() : x = a;
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var xRef = findNode.simple('x = ');
assertElementNull(xRef);
var aRef = findNode.simple('a;');
assertElement(aRef, findElement.topGet('a'));
assertType(aRef, 'int');
}
test_invalid_constructor_initializer_field_method() async {
addTestFile(r'''
var a = 0;
class A {
A() : x = a;
void x() {}
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var xRef = findNode.simple('x = ');
assertElementNull(xRef);
var aRef = findNode.simple('a;');
assertElement(aRef, findElement.topGet('a'));
assertType(aRef, 'int');
}
test_invalid_constructor_initializer_field_setter() async {
addTestFile(r'''
var a = 0;
class A {
A() : x = a;
set x(_) {}
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var xRef = findNode.simple('x = ');
assertElement(xRef, findElement.field('x'));
var aRef = findNode.simple('a;');
assertElement(aRef, findElement.topGet('a'));
assertType(aRef, 'int');
}
test_invalid_constructor_initializer_field_topLevelFunction() async {
addTestFile(r'''
var a = 0;
class A {
A() : x = a;
}
void x() {}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var xRef = findNode.simple('x = ');
assertElementNull(xRef);
var aRef = findNode.simple('a;');
assertElement(aRef, findElement.topGet('a'));
assertType(aRef, 'int');
}
test_invalid_constructor_initializer_field_topLevelVar() async {
addTestFile(r'''
var a = 0;
class A {
A() : x = a;
}
int x;
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var xRef = findNode.simple('x = ');
assertElementNull(xRef);
var aRef = findNode.simple('a;');
assertElement(aRef, findElement.topGet('a'));
assertType(aRef, 'int');
}
test_invalid_constructor_initializer_field_typeParameter() async {
addTestFile(r'''
var a = 0;
class A<T> {
A() : T = a;
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var tRef = findNode.simple('T = ');
assertElementNull(tRef);
var aRef = findNode.simple('a;');
assertElement(aRef, findElement.topGet('a'));
assertType(aRef, 'int');
}
test_invalid_constructor_initializer_field_unresolved() async {
addTestFile(r'''
var a = 0;
class A {
A() : x = a;
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var aRef = findNode.simple('a;');
assertElement(aRef, findElement.topGet('a'));
assertType(aRef, 'int');
}
test_invalid_constructor_return_blockBody() async {
addTestFile(r'''
int a = 0;
class C {
C() {
return a;
}
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
assertTopGetRef('a;', 'a');
}
test_invalid_constructor_return_expressionBody() async {
addTestFile(r'''
int a = 0;
class C {
C() => a;
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
assertTopGetRef('a;', 'a');
}
test_invalid_deferred_type_localVariable() async {
addTestFile(r'''
import 'dart:async' deferred as a;
main() {
a.Future<int> v;
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
assertNamedType(
findNode.namedType('a.Future'),
futureElement,
'Future<int>',
expectedPrefix: findElement.import('dart:async').prefix?.element,
);
assertNamedType(findNode.namedType('int>'), intElement, 'int');
}
test_invalid_fieldInitializer_field() async {
addTestFile(r'''
class C {
final int a = 0;
final int b = a + 1;
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var aRef = findNode.simple('a + 1');
assertElement(aRef, findElement.getter('a'));
assertType(aRef, 'int');
}
test_invalid_fieldInitializer_getter() async {
addTestFile(r'''
class C {
int get a => 0;
final int b = a + 1;
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var aRef = findNode.simple('a + 1');
assertElement(aRef, findElement.getter('a'));
assertType(aRef, 'int');
}
test_invalid_fieldInitializer_method() async {
addTestFile(r'''
class C {
int a() => 0;
final int b = a + 1;
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var aRef = findNode.simple('a + 1');
assertElement(aRef, findElement.method('a'));
assertType(aRef, 'int Function()');
}
test_invalid_fieldInitializer_this() async {
addTestFile(r'''
class C {
final b = this;
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var thisRef = findNode.this_('this');
assertType(thisRef, 'C');
}
test_invalid_generator_async_return_blockBody() async {
addTestFile(r'''
int a = 0;
f() async* {
return a;
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var aRef = findNode.simple('a;');
assertElement(aRef, findElement.topGet('a'));
assertType(aRef, 'int');
}
test_invalid_generator_async_return_expressionBody() async {
addTestFile(r'''
int a = 0;
f() async* => a;
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var aRef = findNode.simple('a;');
assertElement(aRef, findElement.topGet('a'));
assertType(aRef, 'int');
}
test_invalid_generator_sync_return_blockBody() async {
addTestFile(r'''
int a = 0;
f() sync* {
return a;
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var aRef = findNode.simple('a;');
assertElement(aRef, findElement.topGet('a'));
assertType(aRef, 'int');
}
test_invalid_generator_sync_return_expressionBody() async {
addTestFile(r'''
int a = 0;
f() sync* => a;
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var aRef = findNode.simple('a;');
assertElement(aRef, findElement.topGet('a'));
assertType(aRef, 'int');
}
test_invalid_getter_parameters() async {
addTestFile(r'''
get m(int a, double b) {
a;
b;
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var aRef = findNode.simple('a;');
assertElement(aRef, findElement.parameter('a'));
assertType(aRef, 'int');
var bRef = findNode.simple('b;');
assertElement(bRef, findElement.parameter('b'));
assertType(bRef, 'double');
}
@failingTest
test_invalid_instanceCreation_abstract() async {
addTestFile(r'''
abstract class C<T> {
C(T a);
C.named(T a);
C.named2();
}
var a = 0;
var b = true;
main() {
new C(a);
new C.named(b);
new C<double>.named2();
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var c = findElement.class_('C');
{
var creation = findNode.instanceCreation('new C(a)');
assertType(creation, 'C<int>');
ConstructorName constructorName = creation.constructorName;
expect(constructorName.name, isNull);
NamedType type = constructorName.type;
expect(type.typeArguments, isNull);
assertElement(type.name, c);
assertTypeNull(type.name);
var aRef = creation.argumentList.arguments[0] as SimpleIdentifier;
assertElement(aRef, findElement.topGet('a'));
assertType(aRef, 'int');
}
{
var creation = findNode.instanceCreation('new C.named(b)');
assertType(creation, 'C<bool>');
ConstructorName constructorName = creation.constructorName;
expect(constructorName.name!.name, 'named');
NamedType type = constructorName.type;
expect(type.typeArguments, isNull);
assertElement(type.name, c);
assertType(type.name, 'C<bool>');
var bRef = creation.argumentList.arguments[0] as SimpleIdentifier;
assertElement(bRef, findElement.topGet('b'));
assertType(bRef, 'bool');
}
{
var creation = findNode.instanceCreation('new C<double>.named2()');
assertType(creation, 'C<double>');
ConstructorName constructorName = creation.constructorName;
expect(constructorName.name!.name, 'named2');
NamedType type = constructorName.type;
assertTypeArguments(type.typeArguments!, [doubleType]);
assertElement(type.name, c);
assertType(type.name, 'C<double>');
}
}
test_invalid_instanceCreation_arguments_named() async {
addTestFile(r'''
class C {
C();
}
var a = 0;
main() {
new C(x: a);
}
''');
await resolveTestFile();
expect(result.errors, isNotEmpty);
var classElement = findElement.class_('C');
var creation = findNode.instanceCreation('new C(x: a)');
_assertConstructorInvocation(creation, classElement);
var argument = creation.argumentList.arguments[0] as NamedExpression;
assertElementNull(argument.name.label);
var aRef = argument.expression;
assertElement(aRef, findElement.topGet('a'));
assertType(aRef, 'int');
}
test_invalid_instanceCreation_arguments_required_01() async {
addTestFile(r'''
class C {