mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 01:21:07 +00:00
Stop reporting StrongModeCode.TOP_LEVEL_INSTANCE_GETTER
Change-Id: I947e1edb042825a5cfc3394d2554ad3272f86b91 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/195302 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
parent
6ec6fd7b4a
commit
649f7cf689
|
@ -848,8 +848,6 @@ const List<ErrorCode> errorCodeValues = [
|
|||
StaticWarningCode.INVALID_OVERRIDE_DIFFERENT_DEFAULT_VALUES_POSITIONAL,
|
||||
StaticWarningCode.MISSING_ENUM_CONSTANT_IN_SWITCH,
|
||||
StaticWarningCode.UNNECESSARY_NON_NULL_ASSERTION,
|
||||
StrongModeCode.TOP_LEVEL_IDENTIFIER_NO_TYPE,
|
||||
StrongModeCode.TOP_LEVEL_INSTANCE_GETTER,
|
||||
TodoCode.TODO,
|
||||
];
|
||||
|
||||
|
|
|
@ -14180,63 +14180,3 @@ class StaticWarningCode extends AnalyzerErrorCode {
|
|||
@override
|
||||
ErrorType get type => ErrorType.STATIC_WARNING;
|
||||
}
|
||||
|
||||
/**
|
||||
* This class has Strong Mode specific error codes.
|
||||
*
|
||||
* "Strong Mode" was the prototype for Dart 2's sound type system. Many of these
|
||||
* errors became part of Dart 2. Some of them are optional flags, used for
|
||||
* stricter checking.
|
||||
*
|
||||
* These error codes tend to use the same message across different severity
|
||||
* levels, so they are grouped for clarity.
|
||||
*/
|
||||
class StrongModeCode extends ErrorCode {
|
||||
/*
|
||||
* TODO(brianwilkerson) Make the TOP_LEVEL_ error codes be errors rather than
|
||||
* hints and then clean up the function _errorSeverity in
|
||||
* test/src/task/strong/strong_test_helper.dart.
|
||||
*/
|
||||
/* TODO(leafp) Delete most of these.
|
||||
*/
|
||||
|
||||
static const StrongModeCode TOP_LEVEL_IDENTIFIER_NO_TYPE = StrongModeCode(
|
||||
ErrorType.HINT,
|
||||
'TOP_LEVEL_IDENTIFIER_NO_TYPE',
|
||||
"The type of '{0}' can't be inferred because the type of '{1}' "
|
||||
"couldn't be inferred.",
|
||||
correction:
|
||||
"Try adding an explicit type to either the variable '{0}' or the "
|
||||
"variable '{1}'.");
|
||||
|
||||
static const StrongModeCode TOP_LEVEL_INSTANCE_GETTER = StrongModeCode(
|
||||
ErrorType.STATIC_WARNING,
|
||||
'TOP_LEVEL_INSTANCE_GETTER',
|
||||
"The type of '{0}' can't be inferred because it refers to an instance "
|
||||
"getter, '{1}', which has an implicit type.",
|
||||
correction: "Add an explicit type for either '{0}' or '{1}'.");
|
||||
|
||||
@override
|
||||
final ErrorType type;
|
||||
|
||||
/**
|
||||
* Initialize a newly created error code to have the given [type] and [name].
|
||||
*
|
||||
* The message associated with the error will be created from the given
|
||||
* [message] template. The correction associated with the error will be
|
||||
* created from the optional [correction] template.
|
||||
*/
|
||||
const StrongModeCode(ErrorType type, String name, String message,
|
||||
{String? correction, bool hasPublishedDocs = false})
|
||||
: type = type,
|
||||
super(
|
||||
correction: correction,
|
||||
hasPublishedDocs: hasPublishedDocs,
|
||||
message: message,
|
||||
name: name,
|
||||
uniqueName: 'StrongModeCode.$name',
|
||||
);
|
||||
|
||||
@override
|
||||
ErrorSeverity get errorSeverity => type.severity;
|
||||
}
|
||||
|
|
|
@ -376,15 +376,6 @@ class _VariableInferenceNode extends _InferenceNode {
|
|||
return _node.name.name;
|
||||
}
|
||||
|
||||
bool get isImplicitlyTypedInstanceField {
|
||||
var variables = _node.parent as VariableDeclarationList;
|
||||
if (variables.type == null) {
|
||||
var parent = variables.parent;
|
||||
return parent is FieldDeclaration && !parent.isStatic;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
PropertyInducingElementImpl get _elementImpl {
|
||||
return _node.declaredElement as PropertyInducingElementImpl;
|
||||
}
|
||||
|
@ -400,19 +391,7 @@ class _VariableInferenceNode extends _InferenceNode {
|
|||
return const <_InferenceNode>[];
|
||||
}
|
||||
|
||||
var dependencies =
|
||||
collector._set.map(_walker.getNode).whereNotNull().toList();
|
||||
|
||||
for (var node in dependencies) {
|
||||
if (node is _VariableInferenceNode &&
|
||||
node.isImplicitlyTypedInstanceField) {
|
||||
_elementImpl.type = DynamicTypeImpl.instance;
|
||||
isEvaluated = true;
|
||||
return const <_InferenceNode>[];
|
||||
}
|
||||
}
|
||||
|
||||
return dependencies;
|
||||
return collector._set.map(_walker.getNode).whereNotNull().toList();
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
|
@ -17,8 +17,7 @@ import 'package:analyzer/src/dart/element/element.dart';
|
|||
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
|
||||
import 'package:analyzer/src/dart/element/type.dart';
|
||||
import 'package:analyzer/src/dart/element/type_system.dart';
|
||||
import 'package:analyzer/src/error/codes.dart'
|
||||
show CompileTimeErrorCode, StrongModeCode;
|
||||
import 'package:analyzer/src/error/codes.dart' show CompileTimeErrorCode;
|
||||
import 'package:analyzer/src/task/inference_error.dart';
|
||||
|
||||
Element? _getKnownElement(Expression expression) {
|
||||
|
@ -497,14 +496,21 @@ class CodeChecker extends RecursiveAstVisitor {
|
|||
|
||||
@override
|
||||
void visitVariableDeclaration(VariableDeclaration node) {
|
||||
var variableElement = node.declaredElement;
|
||||
var parent = node.parent;
|
||||
if (variableElement is PropertyInducingElement &&
|
||||
parent is VariableDeclarationList &&
|
||||
parent.type == null) {
|
||||
var initializer = node.initializer;
|
||||
if (initializer != null) {
|
||||
_validateTopLevelInitializer(variableElement.name, initializer);
|
||||
var element = node.declaredElement;
|
||||
if (element is PropertyInducingElementImpl) {
|
||||
var error = element.typeInferenceError;
|
||||
if (error != null) {
|
||||
if (error.kind == TopLevelInferenceErrorKind.dependencyCycle) {
|
||||
// Errors on const should have been reported with
|
||||
// [CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT].
|
||||
if (!element.isConst) {
|
||||
_recordMessage(
|
||||
node.name,
|
||||
CompileTimeErrorCode.TOP_LEVEL_CYCLE,
|
||||
[element.name, error.arguments],
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
node.visitChildren(this);
|
||||
|
@ -916,10 +922,6 @@ class CodeChecker extends RecursiveAstVisitor {
|
|||
reporter.onError(error);
|
||||
}
|
||||
|
||||
void _validateTopLevelInitializer(String name, Expression n) {
|
||||
n.accept(_TopLevelInitializerValidator(this, name));
|
||||
}
|
||||
|
||||
void _visitForEachParts(ForEachParts node, SimpleIdentifier loopVariable) {
|
||||
if (loopVariable.staticElement is! VariableElement) {
|
||||
return;
|
||||
|
@ -983,169 +985,3 @@ class CodeChecker extends RecursiveAstVisitor {
|
|||
throw StateError('${e.runtimeType} is unhandled type');
|
||||
}
|
||||
}
|
||||
|
||||
class _TopLevelInitializerValidator extends RecursiveAstVisitor<void> {
|
||||
final CodeChecker _codeChecker;
|
||||
final String _name;
|
||||
|
||||
_TopLevelInitializerValidator(this._codeChecker, this._name);
|
||||
|
||||
void validateHasType(AstNode n, PropertyAccessorElement e) {
|
||||
if (e.hasImplicitReturnType) {
|
||||
var variable = e.declaration.variable as PropertyInducingElementImpl;
|
||||
var error = variable.typeInferenceError;
|
||||
if (error != null) {
|
||||
if (error.kind == TopLevelInferenceErrorKind.dependencyCycle) {
|
||||
// Errors on const should have been reported with
|
||||
// [CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT].
|
||||
if (!variable.isConst) {
|
||||
_codeChecker._recordMessage(n, CompileTimeErrorCode.TOP_LEVEL_CYCLE,
|
||||
[_name, error.arguments]);
|
||||
}
|
||||
} else {
|
||||
_codeChecker._recordMessage(
|
||||
n, StrongModeCode.TOP_LEVEL_IDENTIFIER_NO_TYPE, [_name, e.name]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void validateIdentifierElement(AstNode n, Element? e,
|
||||
{bool isMethodCall = false}) {
|
||||
if (e == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
var enclosing = e.enclosingElement;
|
||||
if (enclosing is CompilationUnitElement) {
|
||||
if (e is PropertyAccessorElement) {
|
||||
validateHasType(n, e);
|
||||
}
|
||||
} else if (enclosing is ClassElement) {
|
||||
if (e is PropertyAccessorElement) {
|
||||
if (e.isStatic) {
|
||||
validateHasType(n, e);
|
||||
} else if (e.hasImplicitReturnType) {
|
||||
_codeChecker._recordMessage(
|
||||
n, StrongModeCode.TOP_LEVEL_INSTANCE_GETTER, [_name, e.name]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
visitAsExpression(AsExpression node) {
|
||||
// Nothing to validate.
|
||||
}
|
||||
|
||||
@override
|
||||
visitBinaryExpression(BinaryExpression node) {
|
||||
TokenType operator = node.operator.type;
|
||||
if (operator == TokenType.AMPERSAND_AMPERSAND ||
|
||||
operator == TokenType.BAR_BAR ||
|
||||
operator == TokenType.EQ_EQ ||
|
||||
operator == TokenType.BANG_EQ) {
|
||||
// These operators give 'bool', no need to validate operands.
|
||||
} else {
|
||||
node.leftOperand.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
visitCascadeExpression(CascadeExpression node) {
|
||||
node.target.accept(this);
|
||||
}
|
||||
|
||||
@override
|
||||
visitConditionalExpression(ConditionalExpression node) {
|
||||
// No need to validate the condition, since it can't affect type inference.
|
||||
node.thenExpression.accept(this);
|
||||
node.elseExpression.accept(this);
|
||||
}
|
||||
|
||||
@override
|
||||
visitFunctionExpressionInvocation(FunctionExpressionInvocation node) {
|
||||
if (node.typeArguments != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
var function = node.function;
|
||||
if (function is PropertyAccess) {
|
||||
var propertyName = function.propertyName;
|
||||
validateIdentifierElement(propertyName, propertyName.staticElement);
|
||||
}
|
||||
|
||||
var functionType = node.function.staticType;
|
||||
if (functionType is FunctionType && functionType.typeFormals.isNotEmpty) {
|
||||
node.argumentList.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
visitIndexExpression(IndexExpression node) {
|
||||
// Nothing to validate.
|
||||
}
|
||||
|
||||
@override
|
||||
visitInstanceCreationExpression(InstanceCreationExpression node) {
|
||||
var constructor = node.constructorName.staticElement;
|
||||
var class_ = constructor?.enclosingElement;
|
||||
if (node.constructorName.type.typeArguments == null &&
|
||||
class_ != null &&
|
||||
class_.typeParameters.isNotEmpty) {
|
||||
// Type inference might depend on the parameters
|
||||
super.visitInstanceCreationExpression(node);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
visitIsExpression(IsExpression node) {
|
||||
// Nothing to validate.
|
||||
}
|
||||
|
||||
@override
|
||||
visitListLiteral(ListLiteral node) {
|
||||
if (node.typeArguments == null) {
|
||||
super.visitListLiteral(node);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
visitMethodInvocation(MethodInvocation node) {
|
||||
node.target?.accept(this);
|
||||
var method = node.methodName.staticElement;
|
||||
validateIdentifierElement(node, method, isMethodCall: true);
|
||||
if (method is ExecutableElement) {
|
||||
if (node.typeArguments == null && method.typeParameters.isNotEmpty) {
|
||||
// Type inference might depend on the parameters
|
||||
node.argumentList.accept(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
visitPrefixExpression(PrefixExpression node) {
|
||||
if (node.operator.type == TokenType.BANG) {
|
||||
// This operator gives 'bool', no need to validate operands.
|
||||
} else {
|
||||
node.operand.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
visitSetOrMapLiteral(SetOrMapLiteral node) {
|
||||
if (node.typeArguments == null) {
|
||||
super.visitSetOrMapLiteral(node);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
visitSimpleIdentifier(SimpleIdentifier node) {
|
||||
validateIdentifierElement(node, node.staticElement);
|
||||
}
|
||||
|
||||
@override
|
||||
visitThrowExpression(ThrowExpression node) {
|
||||
// Nothing to validate.
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,7 +34,8 @@ class A {
|
|||
}
|
||||
final b = new A().a;
|
||||
''', [
|
||||
error(StrongModeCode.TOP_LEVEL_INSTANCE_GETTER, 49, 1),
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 18, 1),
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 37, 1),
|
||||
]);
|
||||
|
||||
assertTypeDynamic(findElement.field('a').type);
|
||||
|
|
|
@ -93,7 +93,6 @@ class A {
|
|||
final y = x;
|
||||
}
|
||||
''', [
|
||||
error(StrongModeCode.TOP_LEVEL_INSTANCE_GETTER, 37, 1),
|
||||
error(CompileTimeErrorCode.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER, 37, 1),
|
||||
]);
|
||||
}
|
||||
|
|
|
@ -20,8 +20,8 @@ class TopLevelCycleTest extends PubPackageResolutionTest {
|
|||
var x = y + 1;
|
||||
var y = x + 1;
|
||||
''', [
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 8, 1),
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 23, 1),
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 4, 1),
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 19, 1),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -29,7 +29,7 @@ var y = x + 1;
|
|||
await assertErrorsInCode(r'''
|
||||
var x = x;
|
||||
''', [
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 8, 1),
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 4, 1),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -41,7 +41,7 @@ var elems = [
|
|||
],
|
||||
];
|
||||
''', [
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 25, 5),
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 4, 5),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
// 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/src/error/codes.dart';
|
||||
import 'package:test_reflective_loader/test_reflective_loader.dart';
|
||||
|
||||
import '../dart/resolution/context_collection_resolution.dart';
|
||||
|
@ -82,81 +81,71 @@ var b = new A().g;
|
|||
}
|
||||
|
||||
test_implicitlyTyped() async {
|
||||
await assertErrorsInCode('''
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
get g => 0;
|
||||
}
|
||||
var b = new A().g;
|
||||
''', [
|
||||
error(StrongModeCode.TOP_LEVEL_INSTANCE_GETTER, 42, 1),
|
||||
]);
|
||||
''');
|
||||
assertTypeDynamic(findElement.topVar('b').type);
|
||||
}
|
||||
|
||||
test_implicitlyTyped_call() async {
|
||||
await assertErrorsInCode('''
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
get g => () => 0;
|
||||
}
|
||||
var a = new A();
|
||||
var b = a.g();
|
||||
''', [
|
||||
error(StrongModeCode.TOP_LEVEL_INSTANCE_GETTER, 59, 1),
|
||||
]);
|
||||
''');
|
||||
assertTypeDynamic(findElement.topVar('b').type);
|
||||
}
|
||||
|
||||
test_implicitlyTyped_field() async {
|
||||
await assertErrorsInCode('''
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
var g = 0;
|
||||
}
|
||||
var b = new A().g;
|
||||
''', [
|
||||
error(StrongModeCode.TOP_LEVEL_INSTANCE_GETTER, 41, 1),
|
||||
]);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'int');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_field_call() async {
|
||||
await assertErrorsInCode('''
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
var g = () => 0;
|
||||
}
|
||||
var a = new A();
|
||||
var b = a.g();
|
||||
''', [
|
||||
error(StrongModeCode.TOP_LEVEL_INSTANCE_GETTER, 58, 1),
|
||||
]);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'int');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_field_prefixedIdentifier() async {
|
||||
await assertErrorsInCode('''
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
var g = 0;
|
||||
}
|
||||
var a = new A();
|
||||
var b = a.g;
|
||||
''', [
|
||||
error(StrongModeCode.TOP_LEVEL_INSTANCE_GETTER, 52, 1),
|
||||
]);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'int');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_fn() async {
|
||||
// The reference to a.x triggers TOP_LEVEL_INSTANCE_GETTER because f is
|
||||
// generic, so the type of a.x might affect the type of b.
|
||||
await assertErrorsInCode('''
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
var x = 0;
|
||||
}
|
||||
int f<T>(x) => 0;
|
||||
var a = new A();
|
||||
var b = f(a.x);
|
||||
''', [
|
||||
error(StrongModeCode.TOP_LEVEL_INSTANCE_GETTER, 72, 1),
|
||||
]);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'int');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_fn_explicit_type_params() async {
|
||||
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
|
||||
// it can't possibly affect the type of b.
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
var x = 0;
|
||||
|
@ -165,11 +154,10 @@ int f<T>(x) => 0;
|
|||
var a = new A();
|
||||
var b = f<int>(a.x);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'int');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_fn_not_generic() async {
|
||||
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
|
||||
// it can't possibly affect the type of b.
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
var x = 0;
|
||||
|
@ -178,11 +166,10 @@ int f(x) => 0;
|
|||
var a = new A();
|
||||
var b = f(a.x);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'int');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_indexExpression() async {
|
||||
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
|
||||
// it can't possibly affect the type of b.
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
var x = 0;
|
||||
|
@ -191,25 +178,21 @@ class A {
|
|||
var a = new A();
|
||||
var b = a[a.x];
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'int');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_invoke() async {
|
||||
// The reference to a.x triggers TOP_LEVEL_INSTANCE_GETTER because the
|
||||
// closure is generic, so the type of a.x might affect the type of b.
|
||||
await assertErrorsInCode('''
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
var x = 0;
|
||||
}
|
||||
var a = new A();
|
||||
var b = (<T>(y) => 0)(a.x);
|
||||
''', [
|
||||
error(StrongModeCode.TOP_LEVEL_INSTANCE_GETTER, 66, 1),
|
||||
]);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'int');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_invoke_explicit_type_params() async {
|
||||
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
|
||||
// it can't possibly affect the type of b.
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
var x = 0;
|
||||
|
@ -217,11 +200,10 @@ class A {
|
|||
var a = new A();
|
||||
var b = (<T>(y) => 0)<int>(a.x);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'int');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_invoke_not_generic() async {
|
||||
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
|
||||
// it can't possibly affect the type of b.
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
var x = 0;
|
||||
|
@ -229,26 +211,22 @@ class A {
|
|||
var a = new A();
|
||||
var b = ((y) => 0)(a.x);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'int');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_method() async {
|
||||
// The reference to a.x triggers TOP_LEVEL_INSTANCE_GETTER because f is
|
||||
// generic, so the type of a.x might affect the type of b.
|
||||
await assertErrorsInCode('''
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
var x = 0;
|
||||
int f<T>(int x) => 0;
|
||||
}
|
||||
var a = new A();
|
||||
var b = a.f(a.x);
|
||||
''', [
|
||||
error(StrongModeCode.TOP_LEVEL_INSTANCE_GETTER, 80, 1),
|
||||
]);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'int');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_method_explicit_type_params() async {
|
||||
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
|
||||
// it can't possibly affect the type of b.
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
var x = 0;
|
||||
|
@ -257,11 +235,10 @@ class A {
|
|||
var a = new A();
|
||||
var b = a.f<int>(a.x);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'int');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_method_not_generic() async {
|
||||
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
|
||||
// it can't possibly affect the type of b.
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
var x = 0;
|
||||
|
@ -270,28 +247,24 @@ class A {
|
|||
var a = new A();
|
||||
var b = a.f(a.x);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'int');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_new() async {
|
||||
// The reference to a.x triggers TOP_LEVEL_INSTANCE_GETTER because B is
|
||||
// generic, so the type of a.x might affect the type of b.
|
||||
await assertErrorsInCode('''
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
var x = 0;
|
||||
}
|
||||
class B<T> {
|
||||
B(x);
|
||||
B(T x);
|
||||
}
|
||||
var a = new A();
|
||||
var b = new B(a.x);
|
||||
''', [
|
||||
error(StrongModeCode.TOP_LEVEL_INSTANCE_GETTER, 81, 1),
|
||||
]);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'B<int>');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_new_explicit_type_params() async {
|
||||
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
|
||||
// it can't possibly affect the type of b.
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
var x = 0;
|
||||
|
@ -302,11 +275,10 @@ class B<T> {
|
|||
var a = new A();
|
||||
var b = new B<int>(a.x);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'B<int>');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_new_explicit_type_params_named() async {
|
||||
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
|
||||
// it can't possibly affect the type of b.
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
var x = 0;
|
||||
|
@ -317,11 +289,10 @@ class B<T> {
|
|||
var a = new A();
|
||||
var b = new B<int>.named(a.x);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'B<int>');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_new_explicit_type_params_prefixed() async {
|
||||
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
|
||||
// it can't possibly affect the type of b.
|
||||
newFile('$testPackageLibPath/lib1.dart', content: '''
|
||||
class B<T> {
|
||||
B(x);
|
||||
|
@ -335,28 +306,24 @@ class A {
|
|||
var a = new A();
|
||||
var b = new foo.B<int>(a.x);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'B<int>');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_new_named() async {
|
||||
// The reference to a.x triggers TOP_LEVEL_INSTANCE_GETTER because B is
|
||||
// generic, so the type of a.x might affect the type of b.
|
||||
await assertErrorsInCode('''
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
var x = 0;
|
||||
}
|
||||
class B<T> {
|
||||
B.named(x);
|
||||
B.named(T x);
|
||||
}
|
||||
var a = new A();
|
||||
var b = new B.named(a.x);
|
||||
''', [
|
||||
error(StrongModeCode.TOP_LEVEL_INSTANCE_GETTER, 93, 1),
|
||||
]);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'B<int>');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_new_not_generic() async {
|
||||
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
|
||||
// it can't possibly affect the type of b.
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
var x = 0;
|
||||
|
@ -367,11 +334,10 @@ class B {
|
|||
var a = new A();
|
||||
var b = new B(a.x);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'B');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_new_not_generic_named() async {
|
||||
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
|
||||
// it can't possibly affect the type of b.
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
var x = 0;
|
||||
|
@ -382,6 +348,7 @@ class B {
|
|||
var a = new A();
|
||||
var b = new B.named(a.x);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'B');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_new_not_generic_prefixed() async {
|
||||
|
@ -390,8 +357,6 @@ class B {
|
|||
B(x);
|
||||
}
|
||||
''');
|
||||
// The reference to a.x does not trigger TOP_LEVEL_INSTANCE_GETTER because
|
||||
// it can't possibly affect the type of b.
|
||||
await assertNoErrorsInCode('''
|
||||
import 'lib1.dart' as foo;
|
||||
class A {
|
||||
|
@ -400,44 +365,39 @@ class A {
|
|||
var a = new A();
|
||||
var b = new foo.B(a.x);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'B');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_new_prefixed() async {
|
||||
newFile('$testPackageLibPath/lib1.dart', content: '''
|
||||
class B<T> {
|
||||
B(x);
|
||||
B(T x);
|
||||
}
|
||||
''');
|
||||
// The reference to a.x triggers TOP_LEVEL_INSTANCE_GETTER because B is
|
||||
// generic, so the type of a.x might affect the type of b.
|
||||
await assertErrorsInCode('''
|
||||
await assertNoErrorsInCode('''
|
||||
import 'lib1.dart' as foo;
|
||||
class A {
|
||||
var x = 0;
|
||||
}
|
||||
var a = new A();
|
||||
var b = new foo.B(a.x);
|
||||
''', [
|
||||
error(StrongModeCode.TOP_LEVEL_INSTANCE_GETTER, 89, 1),
|
||||
]);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'B<int>');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_prefixedIdentifier() async {
|
||||
await assertErrorsInCode('''
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
get g => 0;
|
||||
}
|
||||
var a = new A();
|
||||
var b = a.g;
|
||||
''', [
|
||||
error(StrongModeCode.TOP_LEVEL_INSTANCE_GETTER, 53, 1),
|
||||
]);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'dynamic');
|
||||
}
|
||||
|
||||
test_implicitlyTyped_propertyAccessLhs() async {
|
||||
// The reference to a.x triggers TOP_LEVEL_INSTANCE_GETTER because the type
|
||||
// of a.x affects the lookup of y, which in turn affects the type of b.
|
||||
await assertErrorsInCode('''
|
||||
await assertNoErrorsInCode('''
|
||||
class A {
|
||||
var x = new B();
|
||||
int operator[](int value) => 0;
|
||||
|
@ -447,9 +407,8 @@ class B {
|
|||
}
|
||||
var a = new A();
|
||||
var b = (a.x).y;
|
||||
''', [
|
||||
error(StrongModeCode.TOP_LEVEL_INSTANCE_GETTER, 118, 1),
|
||||
]);
|
||||
''');
|
||||
assertType(findElement.topVar('b').type, 'int');
|
||||
}
|
||||
|
||||
test_prefixedIdentifier() async {
|
||||
|
|
|
@ -101,8 +101,8 @@ var t = b
|
|||
var a = b;
|
||||
var b = a;
|
||||
''', [
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 8, 1),
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 19, 1),
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 4, 1),
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 15, 1),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -718,7 +718,7 @@ var x = new C().f;
|
|||
class C {
|
||||
int f;
|
||||
}
|
||||
dynamic x;
|
||||
int x;
|
||||
''');
|
||||
}
|
||||
|
||||
|
@ -735,7 +735,7 @@ var x = new C().f;
|
|||
''');
|
||||
checkElementText(library, r'''
|
||||
import 'package:test/a.dart';
|
||||
dynamic x;
|
||||
int x;
|
||||
''');
|
||||
}
|
||||
|
||||
|
|
|
@ -364,8 +364,8 @@ var v = () => null;
|
|||
var x = () => y;
|
||||
var y = () => x;
|
||||
''', [
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 14, 1),
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 31, 1),
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 4, 1),
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 21, 1),
|
||||
]);
|
||||
|
||||
var x = _resultUnitElement.topLevelVariables[0];
|
||||
|
@ -381,8 +381,8 @@ var y = () => x;
|
|||
var x = () => y;
|
||||
var y = () => x;
|
||||
''', [
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 14, 1),
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 31, 1),
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 4, 1),
|
||||
error(CompileTimeErrorCode.TOP_LEVEL_CYCLE, 21, 1),
|
||||
]);
|
||||
|
||||
var x = _resultUnitElement.topLevelVariables[0];
|
||||
|
|
|
@ -10,8 +10,6 @@ class A {
|
|||
// ^
|
||||
// [analyzer] COMPILE_TIME_ERROR.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER
|
||||
// [cfe] Can't access 'this' in a field initializer to read 'x'.
|
||||
// ^
|
||||
// [analyzer] STATIC_WARNING.TOP_LEVEL_INSTANCE_GETTER
|
||||
}
|
||||
|
||||
void main() {
|
||||
|
|
|
@ -10,8 +10,6 @@ class A {
|
|||
// ^
|
||||
// [analyzer] COMPILE_TIME_ERROR.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER
|
||||
// [cfe] Can't access 'this' in a field initializer to read 'x'.
|
||||
// ^
|
||||
// [analyzer] STATIC_WARNING.TOP_LEVEL_INSTANCE_GETTER
|
||||
}
|
||||
|
||||
void main() {
|
||||
|
|
Loading…
Reference in a new issue