blob: 81e2c6a3cf7eed36b585ad62d58c19cd392cedc4 [file] [log] [blame]
// Copyright (c) 2014, 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 'dart:collection';
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/syntactic_entity.dart';
import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/dart/ast/visitor.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/dart/element/type_provider.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/error/listener.dart';
import 'package:analyzer/src/dart/analysis/session.dart';
import 'package:analyzer/src/dart/ast/ast.dart';
import 'package:analyzer/src/dart/element/class_hierarchy.dart';
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/dart/resolver/scope.dart';
import 'package:analyzer/src/dart/resolver/variance.dart';
import 'package:analyzer/src/diagnostic/diagnostic_factory.dart';
import 'package:analyzer/src/error/codes.dart';
import 'package:analyzer/src/error/constructor_fields_verifier.dart';
import 'package:analyzer/src/error/correct_override.dart';
import 'package:analyzer/src/error/duplicate_definition_verifier.dart';
import 'package:analyzer/src/error/getter_setter_types_verifier.dart';
import 'package:analyzer/src/error/literal_element_verifier.dart';
import 'package:analyzer/src/error/required_parameters_verifier.dart';
import 'package:analyzer/src/error/return_type_verifier.dart';
import 'package:analyzer/src/error/type_arguments_verifier.dart';
import 'package:analyzer/src/generated/element_resolver.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/java_engine.dart';
import 'package:analyzer/src/generated/parser.dart' show ParserErrorCode;
import 'package:analyzer/src/generated/sdk.dart' show DartSdk, SdkLibrary;
import 'package:analyzer/src/generated/this_access_tracker.dart';
import 'package:meta/meta.dart';
class EnclosingExecutableContext {
final ExecutableElement element;
final bool isAsynchronous;
final bool isConstConstructor;
final bool isFactoryConstructor;
final bool isGenerativeConstructor;
final bool isGenerator;
final bool inStaticMethod;
/// The return statements that have a value.
final List<ReturnStatement> _returnsWith = [];
/// The return statements that do not have a value.
final List<ReturnStatement> _returnsWithout = [];
/// This flag is set to `false` when the declared return type is not legal
/// for the kind of the function body, e.g. not `Future` for `async`.
bool hasLegalReturnType = true;
EnclosingExecutableContext(this.element)
: isAsynchronous = element != null && element.isAsynchronous,
isConstConstructor = element is ConstructorElement && element.isConst,
isFactoryConstructor =
element is ConstructorElement && element.isFactory,
isGenerativeConstructor =
element is ConstructorElement && !element.isFactory,
isGenerator = element != null && element.isGenerator,
inStaticMethod = _inStaticMethod(element);
EnclosingExecutableContext.empty() : this(null);
String get displayName {
var element = this.element;
if (element is ConstructorElement) {
var className = element.enclosingElement.displayName;
var constructorName = element.displayName;
return constructorName.isEmpty
? className
: '$className.$constructorName';
} else {
return element.displayName;
}
}
bool get isClosure {
return element is FunctionElement && element.displayName.isEmpty;
}
bool get isConstructor => element is ConstructorElement;
bool get isFunction {
if (element is FunctionElement) {
return element.displayName.isNotEmpty;
}
return element is PropertyAccessorElement;
}
bool get isMethod => element is MethodElement;
bool get isSynchronous => !isAsynchronous;
DartType get returnType => element.returnType;
static bool _inStaticMethod(ExecutableElement element) {
var enclosing = element?.enclosingElement;
if (enclosing is ClassElement || enclosing is ExtensionElement) {
return element.isStatic;
}
if (enclosing is ExecutableElement) {
return _inStaticMethod(enclosing);
}
return false;
}
}
/// A visitor used to traverse an AST structure looking for additional errors
/// and warnings not covered by the parser and resolver.
class ErrorVerifier extends RecursiveAstVisitor<void> {
/// The error reporter by which errors will be reported.
final ErrorReporter _errorReporter;
/// The current library that is being analyzed.
final LibraryElement _currentLibrary;
/// The type representing the type 'int'.
InterfaceType _intType;
/// The options for verification.
AnalysisOptionsImpl _options;
/// The object providing access to the types defined by the language.
final TypeProvider _typeProvider;
/// The type system primitives
TypeSystemImpl _typeSystem;
/// The manager for the inheritance mappings.
final InheritanceManager3 _inheritanceManager;
/// A flag indicating whether the visitor is currently within a catch clause.
///
/// See [visitCatchClause].
bool _isInCatchClause = false;
/// A flag indicating whether the visitor is currently within a comment.
bool _isInComment = false;
/// The stack of flags, where `true` at the top (last) of the stack indicates
/// that the visitor is in the initializer of a lazy local variable. When the
/// top is `false`, we might be not in a local variable, or it is not `lazy`,
/// etc.
final List<bool> _isInLateLocalVariable = [false];
/// A flag indicating whether the visitor is currently within a native class
/// declaration.
bool _isInNativeClass = false;
/// A flag indicating whether the visitor is currently within a static
/// variable declaration.
bool _isInStaticVariableDeclaration = false;
/// A flag indicating whether the visitor is currently within an instance
/// variable declaration, which is not `late`.
bool _isInInstanceNotLateVariableDeclaration = false;
/// A flag indicating whether the visitor is currently within a constructor
/// initializer.
bool _isInConstructorInitializer = false;
/// This is set to `true` iff the visitor is currently within a function typed
/// formal parameter.
bool _isInFunctionTypedFormalParameter = false;
/// A flag indicating whether the visitor is currently within code in the SDK.
bool _isInSystemLibrary = false;
/// A flag indicating whether the current library contains at least one import
/// directive with a URI that uses the "dart-ext" scheme.
bool _hasExtUri = false;
/// The class containing the AST nodes being visited, or `null` if we are not
/// in the scope of a class.
ClassElementImpl _enclosingClass;
/// The enum containing the AST nodes being visited, or `null` if we are not
/// in the scope of an enum.
ClassElement _enclosingEnum;
/// The element of the extension being visited, or `null` if we are not
/// in the scope of an extension.
ExtensionElement _enclosingExtension;
/// The helper for tracking if the current location has access to `this`.
final ThisAccessTracker _thisAccessTracker = ThisAccessTracker.unit();
/// The context of the method or function that we are currently visiting, or
/// `null` if we are not inside a method or function.
EnclosingExecutableContext _enclosingExecutable =
EnclosingExecutableContext.empty();
/// A table mapping names to the exported elements.
final Map<String, Element> _exportedElements = HashMap<String, Element>();
/// A set of the names of the variable initializers we are visiting now.
final HashSet<String> _namesForReferenceToDeclaredVariableInInitializer =
HashSet<String>();
/// The elements that will be defined later in the current scope, but right
/// now are not declared.
HiddenElements _hiddenElements;
final _UninstantiatedBoundChecker _uninstantiatedBoundChecker;
/// The features enabled in the unit currently being checked for errors.
FeatureSet _featureSet;
final RequiredParametersVerifier _requiredParametersVerifier;
final DuplicateDefinitionVerifier _duplicateDefinitionVerifier;
TypeArgumentsVerifier _typeArgumentsVerifier;
ConstructorFieldsVerifier _constructorFieldsVerifier;
ReturnTypeVerifier _returnTypeVerifier;
/// Initialize a newly created error verifier.
ErrorVerifier(ErrorReporter errorReporter, this._currentLibrary,
this._typeProvider, this._inheritanceManager)
: _errorReporter = errorReporter,
_uninstantiatedBoundChecker =
_UninstantiatedBoundChecker(errorReporter),
_requiredParametersVerifier = RequiredParametersVerifier(errorReporter),
_duplicateDefinitionVerifier =
DuplicateDefinitionVerifier(_currentLibrary, errorReporter) {
_isInSystemLibrary = _currentLibrary.source.isInSystemLibrary;
_hasExtUri = _currentLibrary.hasExtUri;
_isInCatchClause = false;
_isInStaticVariableDeclaration = false;
_isInConstructorInitializer = false;
_intType = _typeProvider.intType;
_typeSystem = _currentLibrary.typeSystem;
_options = _currentLibrary.context.analysisOptions;
_typeArgumentsVerifier =
TypeArgumentsVerifier(_options, _currentLibrary, _errorReporter);
_constructorFieldsVerifier = ConstructorFieldsVerifier(
typeSystem: _typeSystem,
errorReporter: _errorReporter,
);
_returnTypeVerifier = ReturnTypeVerifier(
typeProvider: _typeProvider,
typeSystem: _typeSystem,
errorReporter: _errorReporter,
);
}
ClassElement get enclosingClass => _enclosingClass;
/// For consumers of error verification as a library, (currently just the
/// angular plugin), expose a setter that can make the errors reported more
/// accurate when dangling code snippets are being resolved from a class
/// context. Note that this setter is very defensive for potential misuse; it
/// should not be modified in the middle of visiting a tree and requires an
/// analyzer-provided Impl instance to work.
set enclosingClass(ClassElement classElement) {
assert(classElement is ClassElementImpl);
assert(_enclosingClass == null);
assert(_enclosingEnum == null);
assert(_enclosingExecutable.element == null);
_enclosingClass = classElement;
}
/// The language team is thinking about adding abstract fields, or external
/// fields. But for now we will ignore such fields in `Struct` subtypes.
bool get _isEnclosingClassFfiStruct {
var superClass = _enclosingClass?.supertype?.element;
return superClass != null &&
superClass.library.name == 'dart.ffi' &&
superClass.name == 'Struct';
}
bool get _isNonNullableByDefault =>
_featureSet?.isEnabled(Feature.non_nullable) ?? false;
@override
void visitAnnotation(Annotation node) {
_checkForInvalidAnnotationFromDeferredLibrary(node);
_checkForMissingJSLibAnnotation(node);
super.visitAnnotation(node);
}
@override
void visitArgumentList(ArgumentList node) {
if (node.parent is! ExtensionOverride) {
_checkForArgumentTypesNotAssignableInList(node);
}
super.visitArgumentList(node);
}
@override
void visitAsExpression(AsExpression node) {
_checkForTypeAnnotationDeferredClass(node.type);
super.visitAsExpression(node);
}
@override
void visitAssertInitializer(AssertInitializer node) {
_isInConstructorInitializer = true;
try {
super.visitAssertInitializer(node);
} finally {
_isInConstructorInitializer = false;
}
}
@override
void visitAssignmentExpression(AssignmentExpression node) {
TokenType operatorType = node.operator.type;
Expression lhs = node.leftHandSide;
Expression rhs = node.rightHandSide;
if (operatorType == TokenType.EQ ||
operatorType == TokenType.QUESTION_QUESTION_EQ) {
// Already handled in the assignment resolver.
if (lhs is! SimpleIdentifier) {
_checkForInvalidAssignment(lhs, rhs);
}
} else {
_checkForArgumentTypeNotAssignableForArgument(rhs);
}
if (operatorType == TokenType.QUESTION_QUESTION_EQ) {
_checkForDeadNullCoalesce(node.readType, node.rightHandSide);
}
_checkForAssignmentToFinal(lhs);
if (lhs is IndexExpression) {
_checkIndexExpressionIndex(
lhs.index,
readElement: node.readElement,
writeElement: node.writeElement,
);
}
super.visitAssignmentExpression(node);
}
@override
void visitAwaitExpression(AwaitExpression node) {
if (!_enclosingExecutable.isAsynchronous) {
_errorReporter.reportErrorForToken(
CompileTimeErrorCode.AWAIT_IN_WRONG_CONTEXT, node.awaitKeyword);
}
if (_isNonNullableByDefault) {
_checkForUseOfVoidResult(node.expression);
}
_checkForAwaitInLateLocalVariableInitializer(node);
super.visitAwaitExpression(node);
}
@override
void visitBinaryExpression(BinaryExpression node) {
Token operator = node.operator;
TokenType type = operator.type;
if (type == TokenType.AMPERSAND_AMPERSAND || type == TokenType.BAR_BAR) {
_checkForUseOfVoidResult(node.rightOperand);
} else if (type == TokenType.EQ_EQ || type == TokenType.BANG_EQ) {
_checkForArgumentTypeNotAssignableForArgument(node.rightOperand,
promoteParameterToNullable: true);
} else if (type != TokenType.QUESTION_QUESTION) {
_checkForArgumentTypeNotAssignableForArgument(node.rightOperand);
} else {
_checkForArgumentTypeNotAssignableForArgument(node.rightOperand);
}
if (type == TokenType.QUESTION_QUESTION) {
_checkForDeadNullCoalesce(node.leftOperand.staticType, node.rightOperand);
}
_checkForUseOfVoidResult(node.leftOperand);
super.visitBinaryExpression(node);
}
@override
void visitBlock(Block node) {
_withHiddenElements(node.statements, () {
_duplicateDefinitionVerifier.checkStatements(node.statements);
super.visitBlock(node);
});
}
@override
void visitBlockFunctionBody(BlockFunctionBody node) {
_thisAccessTracker.enterFunctionBody(node);
try {
super.visitBlockFunctionBody(node);
} finally {
_thisAccessTracker.exitFunctionBody(node);
}
}
@override
void visitBreakStatement(BreakStatement node) {
SimpleIdentifier labelNode = node.label;
if (labelNode != null) {
Element labelElement = labelNode.staticElement;
if (labelElement is LabelElementImpl && labelElement.isOnSwitchMember) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.BREAK_LABEL_ON_SWITCH_MEMBER, labelNode);
}
}
}
@override
void visitCatchClause(CatchClause node) {
_duplicateDefinitionVerifier.checkCatchClause(node);
bool previousIsInCatchClause = _isInCatchClause;
try {
_isInCatchClause = true;
_checkForTypeAnnotationDeferredClass(node.exceptionType);
super.visitCatchClause(node);
} finally {
_isInCatchClause = previousIsInCatchClause;
}
}
@override
void visitClassDeclaration(ClassDeclaration node) {
ClassElementImpl outerClass = _enclosingClass;
try {
_isInNativeClass = node.nativeClause != null;
_enclosingClass = node.declaredElement;
List<ClassMember> members = node.members;
_duplicateDefinitionVerifier.checkClass(node);
_checkForBuiltInIdentifierAsName(
node.name, CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_NAME);
_checkForConflictingClassTypeVariableErrorCodes();
TypeName superclass = node.extendsClause?.superclass;
ImplementsClause implementsClause = node.implementsClause;
WithClause withClause = node.withClause;
// Only do error checks on the clause nodes if there is a non-null clause
if (implementsClause != null ||
superclass != null ||
withClause != null) {
_checkClassInheritance(node, superclass, withClause, implementsClause);
}
_checkForConflictingClassMembers();
_constructorFieldsVerifier.enterClass(node);
_checkForFinalNotInitializedInClass(members);
_checkForBadFunctionUse(node);
_checkForWrongTypeParameterVarianceInSuperinterfaces();
_checkForMainFunction(node.name);
super.visitClassDeclaration(node);
} finally {
_isInNativeClass = false;
_constructorFieldsVerifier.leaveClass();
_enclosingClass = outerClass;
}
}
@override
void visitClassTypeAlias(ClassTypeAlias node) {
_checkForBuiltInIdentifierAsName(
node.name, CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPEDEF_NAME);
ClassElementImpl outerClassElement = _enclosingClass;
try {
_enclosingClass = node.declaredElement;
_checkClassInheritance(
node, node.superclass, node.withClause, node.implementsClause);
_checkForMainFunction(node.name);
_checkForWrongTypeParameterVarianceInSuperinterfaces();
} finally {
_enclosingClass = outerClassElement;
}
super.visitClassTypeAlias(node);
}
@override
void visitComment(Comment node) {
_isInComment = true;
try {
super.visitComment(node);
} finally {
_isInComment = false;
}
}
@override
void visitCompilationUnit(CompilationUnit node) {
_featureSet = node.featureSet;
_duplicateDefinitionVerifier.checkUnit(node);
_checkForDeferredPrefixCollisions(node);
super.visitCompilationUnit(node);
_featureSet = null;
}
@override
void visitConstructorDeclaration(ConstructorDeclaration node) {
ConstructorElement element = node.declaredElement;
_withEnclosingExecutable(element, () {
_checkForInvalidModifierOnBody(
node.body, CompileTimeErrorCode.INVALID_MODIFIER_ON_CONSTRUCTOR);
_checkForConstConstructorWithNonFinalField(node, element);
_checkForConstConstructorWithNonConstSuper(node);
_constructorFieldsVerifier.verify(node);
_checkForRedirectingConstructorErrorCodes(node);
_checkForMultipleSuperInitializers(node);
_checkForRecursiveConstructorRedirect(node, element);
if (!_checkForRecursiveFactoryRedirect(node, element)) {
_checkForAllRedirectConstructorErrorCodes(node);
}
_checkForUndefinedConstructorInInitializerImplicit(node);
_checkForReturnInGenerativeConstructor(node);
super.visitConstructorDeclaration(node);
});
}
@override
void visitConstructorFieldInitializer(ConstructorFieldInitializer node) {
_isInConstructorInitializer = true;
try {
SimpleIdentifier fieldName = node.fieldName;
Element staticElement = fieldName.staticElement;
_checkForInvalidField(node, fieldName, staticElement);
if (staticElement is FieldElement) {
_checkForFieldInitializerNotAssignable(node, staticElement);
_checkForAbstractOrExternalFieldConstructorInitializer(
node.fieldName, staticElement);
}
super.visitConstructorFieldInitializer(node);
} finally {
_isInConstructorInitializer = false;
}
}
@override
void visitContinueStatement(ContinueStatement node) {
SimpleIdentifier labelNode = node.label;
if (labelNode != null) {
Element labelElement = labelNode.staticElement;
if (labelElement is LabelElementImpl &&
labelElement.isOnSwitchStatement) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.CONTINUE_LABEL_ON_SWITCH, labelNode);
}
}
}
@override
void visitDefaultFormalParameter(DefaultFormalParameter node) {
_checkForInvalidAssignment(node.identifier, node.defaultValue);
super.visitDefaultFormalParameter(node);
}
@override
void visitEnumDeclaration(EnumDeclaration node) {
ClassElement outerEnum = _enclosingEnum;
try {
_enclosingEnum = node.declaredElement;
_duplicateDefinitionVerifier.checkEnum(node);
super.visitEnumDeclaration(node);
} finally {
_enclosingEnum = outerEnum;
}
}
@override
void visitExportDirective(ExportDirective node) {
ExportElement exportElement = node.element;
if (exportElement != null) {
LibraryElement exportedLibrary = exportElement.exportedLibrary;
_checkForAmbiguousExport(node, exportElement, exportedLibrary);
_checkForExportInternalLibrary(node, exportElement);
_checkForExportLegacySymbol(node);
}
super.visitExportDirective(node);
}
@override
void visitExpressionFunctionBody(ExpressionFunctionBody node) {
_thisAccessTracker.enterFunctionBody(node);
try {
_returnTypeVerifier.verifyExpressionFunctionBody(node);
super.visitExpressionFunctionBody(node);
} finally {
_thisAccessTracker.exitFunctionBody(node);
}
}
@override
void visitExtensionDeclaration(ExtensionDeclaration node) {
_enclosingExtension = node.declaredElement;
_duplicateDefinitionVerifier.checkExtension(node);
_checkForConflictingExtensionTypeVariableErrorCodes();
_checkForFinalNotInitializedInClass(node.members);
GetterSetterTypesVerifier(
typeSystem: _typeSystem,
errorReporter: _errorReporter,
).checkExtension(node);
final name = node.name;
if (name != null) {
_checkForBuiltInIdentifierAsName(
name, CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_EXTENSION_NAME);
}
super.visitExtensionDeclaration(node);
_enclosingExtension = null;
}
@override
void visitFieldDeclaration(FieldDeclaration node) {
var fields = node.fields;
_thisAccessTracker.enterFieldDeclaration(node);
_isInStaticVariableDeclaration = node.isStatic;
_isInInstanceNotLateVariableDeclaration =
!node.isStatic && !node.fields.isLate;
if (!_isInStaticVariableDeclaration) {
if (fields.isConst) {
_errorReporter.reportErrorForToken(
CompileTimeErrorCode.CONST_INSTANCE_FIELD, fields.keyword);
}
}
try {
_checkForNotInitializedNonNullableStaticField(node);
_checkForWrongTypeParameterVarianceInField(node);
_checkForLateFinalFieldWithConstConstructor(node);
super.visitFieldDeclaration(node);
} finally {
_isInStaticVariableDeclaration = false;
_isInInstanceNotLateVariableDeclaration = false;
_thisAccessTracker.exitFieldDeclaration(node);
}
}
@override
void visitFieldFormalParameter(FieldFormalParameter node) {
_checkForValidField(node);
_checkForConstFormalParameter(node);
_checkForPrivateOptionalParameter(node);
_checkForFieldInitializingFormalRedirectingConstructor(node);
_checkForTypeAnnotationDeferredClass(node.type);
ParameterElement element = node.declaredElement;
if (element is FieldFormalParameterElement) {
FieldElement fieldElement = element.field;
if (fieldElement != null) {
_checkForAbstractOrExternalFieldConstructorInitializer(
node.identifier, fieldElement);
}
}
super.visitFieldFormalParameter(node);
}
@override
void visitForEachPartsWithDeclaration(ForEachPartsWithDeclaration node) {
DeclaredIdentifier loopVariable = node.loopVariable;
if (loopVariable == null) {
// Ignore malformed for statements.
return;
}
if (_checkForEachParts(node, loopVariable.identifier)) {
if (loopVariable.isConst) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.FOR_IN_WITH_CONST_VARIABLE, loopVariable);
}
}
super.visitForEachPartsWithDeclaration(node);
}
@override
void visitForEachPartsWithIdentifier(ForEachPartsWithIdentifier node) {
SimpleIdentifier identifier = node.identifier;
if (identifier == null) {
// Ignore malformed for statements.
return;
}
if (_checkForEachParts(node, identifier)) {
_checkForAssignmentToFinal(identifier);
}
super.visitForEachPartsWithIdentifier(node);
}
@override
void visitFormalParameterList(FormalParameterList node) {
_duplicateDefinitionVerifier.checkParameters(node);
_checkUseOfCovariantInParameters(node);
_checkUseOfDefaultValuesInParameters(node);
super.visitFormalParameterList(node);
}
@override
void visitForPartsWithDeclarations(ForPartsWithDeclarations node) {
if (node.variables != null) {
_duplicateDefinitionVerifier.checkForVariables(node.variables);
}
super.visitForPartsWithDeclarations(node);
}
@override
void visitFunctionDeclaration(FunctionDeclaration node) {
ExecutableElement functionElement = node.declaredElement;
if (functionElement != null &&
functionElement.enclosingElement is! CompilationUnitElement) {
_hiddenElements.declare(functionElement);
}
_withEnclosingExecutable(functionElement, () {
SimpleIdentifier identifier = node.name;
TypeAnnotation returnType = node.returnType;
if (node.isGetter) {
GetterSetterTypesVerifier(
typeSystem: _typeSystem,
errorReporter: _errorReporter,
).checkGetter(node.name, node.declaredElement);
}
if (node.isSetter) {
FunctionExpression functionExpression = node.functionExpression;
if (functionExpression != null) {
_checkForWrongNumberOfParametersForSetter(
identifier, functionExpression.parameters);
}
_checkForNonVoidReturnTypeForSetter(returnType);
_checkForInvalidModifierOnBody(node.functionExpression.body,
CompileTimeErrorCode.INVALID_MODIFIER_ON_SETTER);
}
_checkForTypeAnnotationDeferredClass(returnType);
_returnTypeVerifier.verifyReturnType(returnType);
_checkForImplicitDynamicReturn(node.name, node.declaredElement);
_checkForMainFunction(node.name);
super.visitFunctionDeclaration(node);
});
}
@override
void visitFunctionExpression(FunctionExpression node) {
_isInLateLocalVariable.add(false);
if (node.parent is! FunctionDeclaration) {
_withEnclosingExecutable(node.declaredElement, () {
super.visitFunctionExpression(node);
});
} else {
super.visitFunctionExpression(node);
}
_isInLateLocalVariable.removeLast();
}
@override
void visitFunctionExpressionInvocation(FunctionExpressionInvocation node) {
Expression functionExpression = node.function;
if (functionExpression is ExtensionOverride) {
return super.visitFunctionExpressionInvocation(node);
}
DartType expressionType = functionExpression.staticType;
if (!_checkForUseOfVoidResult(functionExpression) &&
!_checkForUseOfNever(functionExpression) &&
node.staticElement == null &&
!_isFunctionType(expressionType)) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.INVOCATION_OF_NON_FUNCTION_EXPRESSION,
functionExpression);
} else if (expressionType is FunctionType) {
_typeArgumentsVerifier.checkFunctionExpressionInvocation(node);
}
_requiredParametersVerifier.visitFunctionExpressionInvocation(node);
super.visitFunctionExpressionInvocation(node);
}
@override
void visitFunctionTypeAlias(FunctionTypeAlias node) {
_checkForBuiltInIdentifierAsName(
node.name, CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPEDEF_NAME);
_checkForMainFunction(node.name);
_checkForTypeAliasCannotReferenceItself(node, node.declaredElement);
super.visitFunctionTypeAlias(node);
}
@override
void visitFunctionTypedFormalParameter(FunctionTypedFormalParameter node) {
bool old = _isInFunctionTypedFormalParameter;
_isInFunctionTypedFormalParameter = true;
try {
_checkForTypeAnnotationDeferredClass(node.returnType);
// TODO(jmesserly): ideally we'd use _checkForImplicitDynamicReturn, and
// we can get the function element via `node?.element?.type?.element` but
// it doesn't have hasImplicitReturnType set correctly.
if (!_options.implicitDynamic && node.returnType == null) {
DartType parameterType = node.declaredElement.type;
if (parameterType is FunctionType &&
parameterType.returnType.isDynamic) {
_errorReporter.reportErrorForNode(
LanguageCode.IMPLICIT_DYNAMIC_RETURN,
node.identifier,
[node.identifier]);
}
}
super.visitFunctionTypedFormalParameter(node);
} finally {
_isInFunctionTypedFormalParameter = old;
}
}
@override
void visitGenericTypeAlias(GenericTypeAlias node) {
_checkForBuiltInIdentifierAsName(
node.name, CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPEDEF_NAME);
_checkForMainFunction(node.name);
_checkForTypeAliasCannotReferenceItself(node, node.declaredElement);
super.visitGenericTypeAlias(node);
}
@override
void visitImplementsClause(ImplementsClause node) {
node.interfaces.forEach(_checkForImplicitDynamicType);
super.visitImplementsClause(node);
}
@override
void visitImportDirective(ImportDirective node) {
ImportElement importElement = node.element;
if (node.prefix != null) {
_checkForBuiltInIdentifierAsName(
node.prefix, CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_PREFIX_NAME);
}
if (importElement != null) {
_checkForImportInternalLibrary(node, importElement);
if (importElement.isDeferred) {
_checkForDeferredImportOfExtensions(node, importElement);
}
}
super.visitImportDirective(node);
}
@override
void visitIndexExpression(IndexExpression node) {
_checkIndexExpressionIndex(
node.index,
readElement: node.staticElement,
writeElement: null,
);
if (node.isNullAware) {
_checkForUnnecessaryNullAware(
node.realTarget,
node.question ?? node.period ?? node.leftBracket,
);
}
super.visitIndexExpression(node);
}
@override
void visitInstanceCreationExpression(InstanceCreationExpression node) {
ConstructorName constructorName = node.constructorName;
TypeName typeName = constructorName.type;
DartType type = typeName.type;
if (type is InterfaceType) {
_checkForConstOrNewWithAbstractClass(node, typeName, type);
_checkForConstOrNewWithEnum(node, typeName, type);
_checkForConstOrNewWithMixin(node, typeName, type);
_requiredParametersVerifier.visitInstanceCreationExpression(node);
if (node.isConst) {
_checkForConstWithNonConst(node);
_checkForConstWithUndefinedConstructor(node, constructorName, typeName);
_checkForConstDeferredClass(node, constructorName, typeName);
} else {
_checkForNewWithUndefinedConstructor(node, constructorName, typeName);
}
_checkForListConstructor(node, type);
}
_checkForImplicitDynamicType(typeName);
super.visitInstanceCreationExpression(node);
}
@override
void visitIntegerLiteral(IntegerLiteral node) {
_checkForOutOfRange(node);
super.visitIntegerLiteral(node);
}
@override
void visitInterpolationExpression(InterpolationExpression node) {
_checkForUseOfVoidResult(node.expression);
super.visitInterpolationExpression(node);
}
@override
void visitIsExpression(IsExpression node) {
_checkForTypeAnnotationDeferredClass(node.type);
_checkForUseOfVoidResult(node.expression);
super.visitIsExpression(node);
}
@override
void visitListLiteral(ListLiteral node) {
_typeArgumentsVerifier.checkListLiteral(node);
_checkForListElementTypeNotAssignable(node);
super.visitListLiteral(node);
}
@override
void visitMethodDeclaration(MethodDeclaration node) {
_withEnclosingExecutable(node.declaredElement, () {
TypeAnnotation returnType = node.returnType;
if (node.isStatic && node.isGetter) {
GetterSetterTypesVerifier(
typeSystem: _typeSystem,
errorReporter: _errorReporter,
).checkGetter(node.name, node.declaredElement);
}
if (node.isSetter) {
_checkForInvalidModifierOnBody(
node.body, CompileTimeErrorCode.INVALID_MODIFIER_ON_SETTER);
_checkForWrongNumberOfParametersForSetter(node.name, node.parameters);
_checkForNonVoidReturnTypeForSetter(returnType);
} else if (node.isOperator) {
_checkForOptionalParameterInOperator(node);
_checkForWrongNumberOfParametersForOperator(node);
_checkForNonVoidReturnTypeForOperator(node);
}
_checkForExtensionDeclaresMemberOfObject(node);
_checkForTypeAnnotationDeferredClass(returnType);
_returnTypeVerifier.verifyReturnType(returnType);
_checkForImplicitDynamicReturn(node, node.declaredElement);
_checkForWrongTypeParameterVarianceInMethod(node);
super.visitMethodDeclaration(node);
});
}
@override
void visitMethodInvocation(MethodInvocation node) {
Expression target = node.realTarget;
SimpleIdentifier methodName = node.methodName;
if (target != null) {
ClassElement typeReference = ElementResolver.getTypeReference(target);
_checkForStaticAccessToInstanceMember(typeReference, methodName);
_checkForInstanceAccessToStaticMember(
typeReference, node.target, methodName);
_checkForUnnecessaryNullAware(target, node.operator);
} else {
_checkForUnqualifiedReferenceToNonLocalStaticMember(methodName);
}
_typeArgumentsVerifier.checkMethodInvocation(node);
_requiredParametersVerifier.visitMethodInvocation(node);
super.visitMethodInvocation(node);
}
@override
void visitMixinDeclaration(MixinDeclaration node) {
// TODO(scheglov) Verify for all mixin errors.
ClassElementImpl outerClass = _enclosingClass;
try {
_enclosingClass = node.declaredElement;
List<ClassMember> members = node.members;
_duplicateDefinitionVerifier.checkMixin(node);
_checkForBuiltInIdentifierAsName(
node.name, CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_NAME);
_checkForConflictingClassTypeVariableErrorCodes();
OnClause onClause = node.onClause;
ImplementsClause implementsClause = node.implementsClause;
// Only do error checks only if there is a non-null clause.
if (onClause != null || implementsClause != null) {
_checkMixinInheritance(node, onClause, implementsClause);
}
_checkForConflictingClassMembers();
_checkForFinalNotInitializedInClass(members);
_checkForMainFunction(node.name);
_checkForWrongTypeParameterVarianceInSuperinterfaces();
// _checkForBadFunctionUse(node);
super.visitMixinDeclaration(node);
} finally {
_enclosingClass = outerClass;
}
}
@override
void visitNativeClause(NativeClause node) {
// TODO(brianwilkerson) Figure out the right rule for when 'native' is
// allowed.
if (!_isInSystemLibrary) {
_errorReporter.reportErrorForNode(
ParserErrorCode.NATIVE_CLAUSE_IN_NON_SDK_CODE, node);
}
super.visitNativeClause(node);
}
@override
void visitNativeFunctionBody(NativeFunctionBody node) {
_checkForNativeFunctionBodyInNonSdkCode(node);
super.visitNativeFunctionBody(node);
}
@override
void visitPostfixExpression(PostfixExpression node) {
var operand = node.operand;
if (node.operator.type == TokenType.BANG) {
_checkForUseOfVoidResult(node);
_checkForUnnecessaryNullAware(operand, node.operator);
} else {
_checkForAssignmentToFinal(operand);
_checkForIntNotAssignable(operand);
}
if (operand is IndexExpression) {
_checkIndexExpressionIndex(
operand.index,
readElement: node.readElement,
writeElement: node.writeElement,
);
}
super.visitPostfixExpression(node);
}
@override
void visitPrefixedIdentifier(PrefixedIdentifier node) {
if (node.parent is! Annotation) {
ClassElement typeReference =
ElementResolver.getTypeReference(node.prefix);
SimpleIdentifier name = node.identifier;
_checkForStaticAccessToInstanceMember(typeReference, name);
_checkForInstanceAccessToStaticMember(typeReference, node.prefix, name);
}
super.visitPrefixedIdentifier(node);
}
@override
void visitPrefixExpression(PrefixExpression node) {
TokenType operatorType = node.operator.type;
Expression operand = node.operand;
if (operatorType != TokenType.BANG) {
if (operatorType.isIncrementOperator) {
_checkForAssignmentToFinal(operand);
}
_checkForUseOfVoidResult(operand);
_checkForIntNotAssignable(operand);
}
if (operand is IndexExpression) {
_checkIndexExpressionIndex(
operand.index,
readElement: node.readElement,
writeElement: node.writeElement,
);
}
super.visitPrefixExpression(node);
}
@override
void visitPropertyAccess(PropertyAccess node) {
var target = node.realTarget;
ClassElement typeReference = ElementResolver.getTypeReference(target);
SimpleIdentifier propertyName = node.propertyName;
_checkForStaticAccessToInstanceMember(typeReference, propertyName);
_checkForInstanceAccessToStaticMember(
typeReference, node.target, propertyName);
_checkForUnnecessaryNullAware(target, node.operator);
super.visitPropertyAccess(node);
}
@override
void visitRedirectingConstructorInvocation(
RedirectingConstructorInvocation node) {
_requiredParametersVerifier.visitRedirectingConstructorInvocation(node);
_isInConstructorInitializer = true;
try {
super.visitRedirectingConstructorInvocation(node);
} finally {
_isInConstructorInitializer = false;
}
}
@override
void visitRethrowExpression(RethrowExpression node) {
_checkForRethrowOutsideCatch(node);
super.visitRethrowExpression(node);
}
@override
void visitReturnStatement(ReturnStatement node) {
if (node.expression == null) {
_enclosingExecutable._returnsWithout.add(node);
} else {
_enclosingExecutable._returnsWith.add(node);
}
_returnTypeVerifier.verifyReturnStatement(node);
super.visitReturnStatement(node);
}
@override
void visitSetOrMapLiteral(SetOrMapLiteral node) {
if (node.isMap) {
_typeArgumentsVerifier.checkMapLiteral(node);
_checkForMapTypeNotAssignable(node);
_checkForNonConstMapAsExpressionStatement3(node);
} else if (node.isSet) {
_typeArgumentsVerifier.checkSetLiteral(node);
_checkForSetElementTypeNotAssignable3(node);
}
super.visitSetOrMapLiteral(node);
}
@override
void visitSimpleFormalParameter(SimpleFormalParameter node) {
_checkForConstFormalParameter(node);
_checkForPrivateOptionalParameter(node);
_checkForTypeAnnotationDeferredClass(node.type);
// Checks for an implicit dynamic parameter type.
//
// We can skip other parameter kinds besides simple formal, because:
// - DefaultFormalParameter contains a simple one, so it gets here,
// - FieldFormalParameter error should be reported on the field,
// - FunctionTypedFormalParameter is a function type, not dynamic.
_checkForImplicitDynamicIdentifier(node, node.identifier);
super.visitSimpleFormalParameter(node);
}
@override
void visitSimpleIdentifier(SimpleIdentifier node) {
_checkForAmbiguousImport(node);
_checkForReferenceBeforeDeclaration(node);
_checkForImplicitThisReferenceInInitializer(node);
_checkForTypeParameterReferencedByStatic(node);
if (!_isUnqualifiedReferenceToNonLocalStaticMemberAllowed(node)) {
_checkForUnqualifiedReferenceToNonLocalStaticMember(node);
}
super.visitSimpleIdentifier(node);
}
@override
void visitSpreadElement(SpreadElement node) {
if (node.isNullAware) {
_checkForUnnecessaryNullAware(node.expression, node.spreadOperator);
}
super.visitSpreadElement(node);
}
@override
void visitSuperConstructorInvocation(SuperConstructorInvocation node) {
_requiredParametersVerifier.visitSuperConstructorInvocation(node);
_isInConstructorInitializer = true;
try {
super.visitSuperConstructorInvocation(node);
} finally {
_isInConstructorInitializer = false;
}
}
@override
void visitSwitchCase(SwitchCase node) {
_withHiddenElements(node.statements, () {
_duplicateDefinitionVerifier.checkStatements(node.statements);
super.visitSwitchCase(node);
});
}
@override
void visitSwitchDefault(SwitchDefault node) {
_withHiddenElements(node.statements, () {
_duplicateDefinitionVerifier.checkStatements(node.statements);
super.visitSwitchDefault(node);
});
}
@override
void visitSwitchStatement(SwitchStatement node) {
_checkForSwitchExpressionNotAssignable(node);
_checkForCaseBlocksNotTerminated(node);
_checkForMissingEnumConstantInSwitch(node);
super.visitSwitchStatement(node);
}
@override
void visitThisExpression(ThisExpression node) {
_checkForInvalidReferenceToThis(node);
super.visitThisExpression(node);
}
@override
void visitThrowExpression(ThrowExpression node) {
_checkForConstEvalThrowsException(node);
_checkForUseOfVoidResult(node.expression);
_checkForThrowOfInvalidType(node);
super.visitThrowExpression(node);
}
@override
void visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) {
_checkForFinalNotInitialized(node.variables);
_checkForNotInitializedNonNullableVariable(node.variables);
for (var declaration in node.variables.variables) {
_checkForMainFunction(declaration.name);
}
super.visitTopLevelVariableDeclaration(node);
}
@override
void visitTypeArgumentList(TypeArgumentList node) {
NodeList<TypeAnnotation> list = node.arguments;
for (TypeAnnotation type in list) {
_checkForTypeAnnotationDeferredClass(type);
}
super.visitTypeArgumentList(node);
}
@override
void visitTypeName(TypeName node) {
_typeArgumentsVerifier.checkTypeName(node);
super.visitTypeName(node);
}
@override
void visitTypeParameter(TypeParameter node) {
_checkForBuiltInIdentifierAsName(node.name,
CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_PARAMETER_NAME);
_checkForTypeAnnotationDeferredClass(node.bound);
_checkForImplicitDynamicType(node.bound);
_checkForGenericFunctionType(node.bound);
node.bound?.accept(_uninstantiatedBoundChecker);
super.visitTypeParameter(node);
}
@override
void visitTypeParameterList(TypeParameterList node) {
_duplicateDefinitionVerifier.checkTypeParameters(node);
_checkForTypeParameterBoundRecursion(node.typeParameters);
super.visitTypeParameterList(node);
}
@override
void visitVariableDeclaration(VariableDeclaration node) {
SimpleIdentifier nameNode = node.name;
Expression initializerNode = node.initializer;
// do checks
_checkForInvalidAssignment(nameNode, initializerNode);
_checkForImplicitDynamicIdentifier(node, nameNode);
_checkForAbstractOrExternalVariableInitializer(node);
// visit name
nameNode.accept(this);
// visit initializer
String name = nameNode.name;
_namesForReferenceToDeclaredVariableInInitializer.add(name);
try {
if (initializerNode != null) {
initializerNode.accept(this);
}
} finally {
_namesForReferenceToDeclaredVariableInInitializer.remove(name);
}
// declare the variable
AstNode grandparent = node.parent.parent;
if (grandparent is! TopLevelVariableDeclaration &&
grandparent is! FieldDeclaration) {
VariableElement element = node.declaredElement;
if (element != null) {
// There is no hidden elements if we are outside of a function body,
// which will happen for variables declared in control flow elements.
_hiddenElements?.declare(element);
}
}
}
@override
void visitVariableDeclarationList(VariableDeclarationList node) {
_checkForTypeAnnotationDeferredClass(node.type);
super.visitVariableDeclarationList(node);
}
@override
void visitVariableDeclarationStatement(VariableDeclarationStatement node) {
_isInLateLocalVariable.add(node.variables.isLate);
_checkForFinalNotInitialized(node.variables);
super.visitVariableDeclarationStatement(node);
_isInLateLocalVariable.removeLast();
}
@override
void visitWithClause(WithClause node) {
node.mixinTypes.forEach(_checkForImplicitDynamicType);
super.visitWithClause(node);
}
/// Checks the class for problems with the superclass, mixins, or implemented
/// interfaces.
void _checkClassInheritance(
NamedCompilationUnitMember node,
TypeName superclass,
WithClause withClause,
ImplementsClause implementsClause) {
// Only check for all of the inheritance logic around clauses if there
// isn't an error code such as "Cannot extend double" already on the
// class.
if (!_checkForExtendsDisallowedClass(superclass) &&
!_checkForImplementsClauseErrorCodes(implementsClause) &&
!_checkForAllMixinErrorCodes(withClause) &&
!_checkForNoGenerativeConstructorsInSuperclass(superclass)) {
_checkForImplicitDynamicType(superclass);
_checkForExtendsDeferredClass(superclass);
_checkForRepeatedType(implementsClause?.interfaces,
CompileTimeErrorCode.IMPLEMENTS_REPEATED);
_checkImplementsSuperClass(implementsClause);
_checkMixinsSuperClass(withClause);
_checkMixinInference(node, withClause);
_checkForMixinWithConflictingPrivateMember(withClause, superclass);
_checkForConflictingGenerics(node);
if (node is ClassDeclaration) {
_checkForNoDefaultSuperConstructorImplicit(node);
}
}
}
/// Given a list of [directives] that have the same prefix, generate an error
/// if there is more than one import and any of those imports is deferred.
///
/// See [CompileTimeErrorCode.SHARED_DEFERRED_PREFIX].
void _checkDeferredPrefixCollision(List<ImportDirective> directives) {
int count = directives.length;
if (count > 1) {
for (int i = 0; i < count; i++) {
Token deferredToken = directives[i].deferredKeyword;
if (deferredToken != null) {
_errorReporter.reportErrorForToken(
CompileTimeErrorCode.SHARED_DEFERRED_PREFIX, deferredToken);
}
}
}
}
void _checkForAbstractOrExternalFieldConstructorInitializer(
AstNode node, FieldElement fieldElement) {
if (fieldElement.isAbstract) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.ABSTRACT_FIELD_CONSTRUCTOR_INITIALIZER, node);
}
if (fieldElement.isExternal) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.EXTERNAL_FIELD_CONSTRUCTOR_INITIALIZER, node);
}
}
void _checkForAbstractOrExternalVariableInitializer(
VariableDeclaration node) {
var declaredElement = node.declaredElement;
if (node.initializer != null) {
if (declaredElement is FieldElement) {
if (declaredElement.isAbstract) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.ABSTRACT_FIELD_INITIALIZER, node.name);
}
if (declaredElement.isExternal) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.EXTERNAL_FIELD_INITIALIZER, node.name);
}
} else if (declaredElement is TopLevelVariableElement) {
if (declaredElement.isExternal) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.EXTERNAL_VARIABLE_INITIALIZER, node.name);
}
}
}
}
/// Verify that all classes of the given [withClause] are valid.
///
/// See [CompileTimeErrorCode.MIXIN_CLASS_DECLARES_CONSTRUCTOR],
/// [CompileTimeErrorCode.MIXIN_INHERITS_FROM_NOT_OBJECT].
bool _checkForAllMixinErrorCodes(WithClause withClause) {
if (withClause == null) {
return false;
}
bool problemReported = false;
int mixinTypeIndex = -1;
for (int mixinNameIndex = 0;
mixinNameIndex < withClause.mixinTypes.length;
mixinNameIndex++) {
TypeName mixinName = withClause.mixinTypes[mixinNameIndex];
DartType mixinType = mixinName.type;
if (mixinType is InterfaceType) {
mixinTypeIndex++;
if (_checkForExtendsOrImplementsDisallowedClass(
mixinName, CompileTimeErrorCode.MIXIN_OF_DISALLOWED_CLASS)) {
problemReported = true;
} else {
ClassElement mixinElement = mixinType.element;
if (_checkForExtendsOrImplementsDeferredClass(
mixinName, CompileTimeErrorCode.MIXIN_DEFERRED_CLASS)) {
problemReported = true;
}
if (mixinElement.isMixin) {
if (_checkForMixinSuperclassConstraints(
mixinNameIndex, mixinName)) {
problemReported = true;
} else if (_checkForMixinSuperInvokedMembers(
mixinTypeIndex, mixinName, mixinElement, mixinType)) {
problemReported = true;
}
} else {
if (_checkForMixinClassDeclaresConstructor(
mixinName, mixinElement)) {
problemReported = true;
}
if (_checkForMixinInheritsNotFromObject(mixinName, mixinElement)) {
problemReported = true;
}
}
}
}
}
return problemReported;
}
/// Check for errors related to the redirected constructors.
void _checkForAllRedirectConstructorErrorCodes(
ConstructorDeclaration declaration) {
// Prepare redirected constructor node
ConstructorName redirectedConstructor = declaration.redirectedConstructor;
if (redirectedConstructor == null) {
return;
}
// Prepare redirected constructor type
ConstructorElement redirectedElement = redirectedConstructor.staticElement;
if (redirectedElement == null) {
// If the element is null, we check for the
// REDIRECT_TO_MISSING_CONSTRUCTOR case
TypeName constructorTypeName = redirectedConstructor.type;
DartType redirectedType = constructorTypeName.type;
if (redirectedType != null &&
redirectedType.element != null &&
!redirectedType.isDynamic) {
// Prepare the constructor name
String constructorStrName = constructorTypeName.name.name;
if (redirectedConstructor.name != null) {
constructorStrName += ".${redirectedConstructor.name.name}";
}
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.REDIRECT_TO_MISSING_CONSTRUCTOR,
redirectedConstructor,
[constructorStrName, redirectedType]);
}
return;
}
FunctionType redirectedType = redirectedElement.type;
DartType redirectedReturnType = redirectedType.returnType;
// Report specific problem when return type is incompatible
FunctionType constructorType = declaration.declaredElement.type;
DartType constructorReturnType = constructorType.returnType;
if (!_typeSystem.isAssignableTo2(
redirectedReturnType, constructorReturnType)) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.REDIRECT_TO_INVALID_RETURN_TYPE,
redirectedConstructor,
[redirectedReturnType, constructorReturnType]);
return;
} else if (!_typeSystem.isSubtypeOf2(redirectedType, constructorType)) {
// Check parameters.
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.REDIRECT_TO_INVALID_FUNCTION_TYPE,
redirectedConstructor,
[redirectedType, constructorType]);
}
}
/// Verify that the export namespace of the given export [directive] does not
/// export any name already exported by another export directive. The
/// [exportElement] is the [ExportElement] retrieved from the node. If the
/// element in the node was `null`, then this method is not called. The
/// [exportedLibrary] is the library element containing the exported element.
///
/// See [CompileTimeErrorCode.AMBIGUOUS_EXPORT].
void _checkForAmbiguousExport(ExportDirective directive,
ExportElement exportElement, LibraryElement exportedLibrary) {
if (exportedLibrary == null) {
return;
}
// check exported names
Namespace namespace =
NamespaceBuilder().createExportNamespaceForDirective(exportElement);
Map<String, Element> definedNames = namespace.definedNames;
for (String name in definedNames.keys) {
Element element = definedNames[name];
Element prevElement = _exportedElements[name];
if (element != null && prevElement != null && prevElement != element) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.AMBIGUOUS_EXPORT, directive, [
name,
prevElement.library.definingCompilationUnit.source.uri,
element.library.definingCompilationUnit.source.uri
]);
return;
} else {
_exportedElements[name] = element;
}
}
}
/// Check the given node to see whether it was ambiguous because the name was
/// imported from two or more imports.
void _checkForAmbiguousImport(SimpleIdentifier node) {
Element element = node.staticElement;
if (element is MultiplyDefinedElementImpl) {
String name = element.displayName;
List<Element> conflictingMembers = element.conflictingElements;
int count = conflictingMembers.length;
List<String> libraryNames = List<String>(count);
for (int i = 0; i < count; i++) {
libraryNames[i] = _getLibraryName(conflictingMembers[i]);
}
libraryNames.sort();
_errorReporter.reportErrorForNode(CompileTimeErrorCode.AMBIGUOUS_IMPORT,
node, [name, StringUtilities.printListOfQuotedNames(libraryNames)]);
}
}
/// Verify that the given [expression] can be assigned to its corresponding
/// parameters. The [expectedStaticType] is the expected static type of the
/// parameter. The [actualStaticType] is the actual static type of the
/// argument.
void _checkForArgumentTypeNotAssignable(
Expression expression,
DartType expectedStaticType,
DartType actualStaticType,
ErrorCode errorCode) {
// Warning case: test static type information
if (actualStaticType != null && expectedStaticType != null) {
if (!expectedStaticType.isVoid && _checkForUseOfVoidResult(expression)) {
return;
}
_checkForAssignableExpressionAtType(
expression, actualStaticType, expectedStaticType, errorCode);
}
}
/// Verify that the given [argument] can be assigned to its corresponding
/// parameter.
///
/// This method corresponds to
/// [BestPracticesVerifier.checkForArgumentTypeNotAssignableForArgument].
///
/// See [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE].
void _checkForArgumentTypeNotAssignableForArgument(Expression argument,
{bool promoteParameterToNullable = false}) {
// TODO(scheglov) probably cannot happen
if (argument == null) {
return;
}
_checkForArgumentTypeNotAssignableForArgument2(
argument: argument,
parameter: argument.staticParameterElement,
promoteParameterToNullable: promoteParameterToNullable,
);
}
void _checkForArgumentTypeNotAssignableForArgument2({
@required Expression argument,
@required ParameterElement parameter,
@required bool promoteParameterToNullable,
}) {
DartType staticParameterType = parameter?.type;
if (promoteParameterToNullable && staticParameterType != null) {
staticParameterType = _typeSystem.makeNullable(staticParameterType);
}
_checkForArgumentTypeNotAssignableWithExpectedTypes(argument,
staticParameterType, CompileTimeErrorCode.ARGUMENT_TYPE_NOT_ASSIGNABLE);
}
/// Verify that the given [expression] can be assigned to its corresponding
/// parameters. The [expectedStaticType] is the expected static type.
///
/// This method corresponds to
/// [BestPracticesVerifier.checkForArgumentTypeNotAssignableWithExpectedTypes].
///
/// See [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE],
/// [CompileTimeErrorCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE],
/// [StaticWarningCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE],
/// [CompileTimeErrorCode.MAP_KEY_TYPE_NOT_ASSIGNABLE],
/// [CompileTimeErrorCode.MAP_VALUE_TYPE_NOT_ASSIGNABLE],
/// [StaticWarningCode.MAP_KEY_TYPE_NOT_ASSIGNABLE], and
/// [StaticWarningCode.MAP_VALUE_TYPE_NOT_ASSIGNABLE].
void _checkForArgumentTypeNotAssignableWithExpectedTypes(
Expression expression, DartType expectedStaticType, ErrorCode errorCode) {
_checkForArgumentTypeNotAssignable(
expression, expectedStaticType, expression.staticType, errorCode);
}
/// Verify that the arguments in the given [argumentList] can be assigned to
/// their corresponding parameters.
///
/// This method corresponds to
/// [BestPracticesVerifier.checkForArgumentTypesNotAssignableInList].
///
/// See [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE].
void _checkForArgumentTypesNotAssignableInList(ArgumentList argumentList) {
if (argumentList == null) {
return;
}
for (Expression argument in argumentList.arguments) {
_checkForArgumentTypeNotAssignableForArgument(argument);
}
}
bool _checkForAssignableExpression(
Expression expression, DartType expectedStaticType, ErrorCode errorCode) {
DartType actualStaticType = expression.staticType;
return actualStaticType != null &&
_checkForAssignableExpressionAtType(
expression, actualStaticType, expectedStaticType, errorCode);
}
bool _checkForAssignableExpressionAtType(
Expression expression,
DartType actualStaticType,
DartType expectedStaticType,
ErrorCode errorCode) {
if (!_typeSystem.isAssignableTo2(actualStaticType, expectedStaticType)) {
AstNode getErrorNode(AstNode node) {
if (node is CascadeExpression) {
return getErrorNode(node.target);
}
if (node is ParenthesizedExpression) {
return getErrorNode(node.expression);
}
return node;
}
_errorReporter.reportErrorForNode(
errorCode,
getErrorNode(expression),
[actualStaticType, expectedStaticType],
);
return false;
}
return true;
}
/// Verify that the given [expression] is not final.
///
/// See [StaticWarningCode.ASSIGNMENT_TO_CONST],
/// [StaticWarningCode.ASSIGNMENT_TO_FINAL], and
/// [StaticWarningCode.ASSIGNMENT_TO_METHOD].
void _checkForAssignmentToFinal(Expression expression) {
// TODO(scheglov) Check SimpleIdentifier(s) as all other nodes.
if (expression is! SimpleIdentifier) return;
// Already handled in the assignment resolver.
if (expression is SimpleIdentifier &&
expression.parent is AssignmentExpression) {
return;
}
// prepare element
Element element;
AstNode highlightedNode = expression;
if (expression is Identifier) {
element = expression.staticElement;
if (expression is PrefixedIdentifier) {
highlightedNode = expression.identifier;
}
} else if (expression is PropertyAccess) {
element = expression.propertyName.staticElement;
highlightedNode = expression.propertyName;
}
// check if element is assignable
if (element is VariableElement) {
if (element.isConst) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.ASSIGNMENT_TO_CONST,
expression,
);
} else if (element.isFinal) {
if (_isNonNullableByDefault) {
// Handled during resolution, with flow analysis.
} else {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.ASSIGNMENT_TO_FINAL_LOCAL,
expression,
[element.name],
);
}
}
} else if (element is PropertyAccessorElement && element.isGetter) {
var variable = element.variable;
if (variable.isConst) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.ASSIGNMENT_TO_CONST,
expression,
);
} else if (variable is FieldElement && variable.isSynthetic) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.ASSIGNMENT_TO_FINAL_NO_SETTER,
highlightedNode,
[variable.name, variable.enclosingElement.displayName],
);
} else {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.ASSIGNMENT_TO_FINAL,
highlightedNode,
[variable.name],
);
}
} else if (element is FunctionElement) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.ASSIGNMENT_TO_FUNCTION, expression);
} else if (element is MethodElement) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.ASSIGNMENT_TO_METHOD, expression);
} else if (element is ClassElement ||
element is DynamicElementImpl ||
element is FunctionTypeAliasElement ||
element is TypeParameterElement) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.ASSIGNMENT_TO_TYPE, expression);
}
}
void _checkForAwaitInLateLocalVariableInitializer(AwaitExpression node) {
if (_isInLateLocalVariable.last) {
_errorReporter.reportErrorForToken(
CompileTimeErrorCode.AWAIT_IN_LATE_LOCAL_VARIABLE_INITIALIZER,
node.awaitKeyword,
);
}
}
/// Verifies that the class is not named `Function` and that it doesn't
/// extends/implements/mixes in `Function`.
void _checkForBadFunctionUse(ClassDeclaration node) {
ExtendsClause extendsClause = node.extendsClause;
WithClause withClause = node.withClause;
if (node.name.name == "Function") {
_errorReporter.reportErrorForNode(
HintCode.DEPRECATED_FUNCTION_CLASS_DECLARATION, node.name);
}
if (extendsClause != null) {
Element superElement = extendsClause.superclass.name.staticElement;
if (superElement != null && superElement.name == "Function") {
_errorReporter.reportErrorForNode(
HintCode.DEPRECATED_EXTENDS_FUNCTION, extendsClause.superclass);
}
}
if (withClause != null) {
for (TypeName type in withClause.mixinTypes) {
Element mixinElement = type.name.staticElement;
if (mixinElement != null && mixinElement.name == "Function") {
_errorReporter.reportErrorForNode(
HintCode.DEPRECATED_MIXIN_FUNCTION, type);
}
}
}
}
/// Verify that the given [identifier] is not a keyword, and generates the
/// given [errorCode] on the identifier if it is a keyword.
///
/// See [CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_EXTENSION_NAME],
/// [CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_NAME],
/// [CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPE_PARAMETER_NAME], and
/// [CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPEDEF_NAME].
void _checkForBuiltInIdentifierAsName(
SimpleIdentifier identifier, ErrorCode errorCode) {
Token token = identifier.token;
if (token.type.isKeyword && token.keyword?.isPseudo != true) {
_errorReporter
.reportErrorForNode(errorCode, identifier, [identifier.name]);
}
}
/// Verify that the given [switchCase] is terminated with 'break', 'continue',
/// 'return' or 'throw'.
///
/// see [StaticWarningCode.CASE_BLOCK_NOT_TERMINATED].
void _checkForCaseBlockNotTerminated(SwitchCase switchCase) {
NodeList<Statement> statements = switchCase.statements;
if (statements.isEmpty) {
// fall-through without statements at all
AstNode parent = switchCase.parent;
if (parent is SwitchStatement) {
NodeList<SwitchMember> members = parent.members;
int index = members.indexOf(switchCase);
if (index != -1 && index < members.length - 1) {
return;
}
}
// no other switch member after this one
} else {
Statement statement = statements.last;
if (statement is Block && statement.statements.isNotEmpty) {
Block block = statement;
statement = block.statements.last;
}
// terminated with statement
if (statement is BreakStatement ||
statement is ContinueStatement ||
statement is ReturnStatement) {
return;
}
// terminated with 'throw' expression
if (statement is ExpressionStatement) {
Expression expression = statement.expression;
if (expression is ThrowExpression || expression is RethrowExpression) {
return;
}
}
}
_errorReporter.reportErrorForToken(
CompileTimeErrorCode.CASE_BLOCK_NOT_TERMINATED, switchCase.keyword);
}
/// Verify that the switch cases in the given switch [statement] are
/// terminated with 'break', 'continue', 'rethrow', 'return' or 'throw'.
///
/// See [StaticWarningCode.CASE_BLOCK_NOT_TERMINATED].
void _checkForCaseBlocksNotTerminated(SwitchStatement statement) {
if (_isNonNullableByDefault) return;
NodeList<SwitchMember> members = statement.members;
int lastMember = members.length - 1;
for (int i = 0; i < lastMember; i++) {
SwitchMember member = members[i];
if (member is SwitchCase) {
_checkForCaseBlockNotTerminated(member);
}
}
}
/// Verify that the [_enclosingClass] does not have a method and getter pair
/// with the same name, via inheritance.
///
/// See [CompileTimeErrorCode.CONFLICTING_STATIC_AND_INSTANCE],
/// [CompileTimeErrorCode.CONFLICTING_METHOD_AND_FIELD], and
/// [CompileTimeErrorCode.CONFLICTING_FIELD_AND_METHOD].
void _checkForConflictingClassMembers() {
if (_enclosingClass == null) {
return;
}
Uri libraryUri = _currentLibrary.source.uri;
// method declared in the enclosing class vs. inherited getter/setter
for (MethodElement method in _enclosingClass.methods) {
String name = method.name;
// find inherited property accessor
ExecutableElement inherited = _inheritanceManager.getInherited2(
_enclosingClass, Name(libraryUri, name));
inherited ??= _inheritanceManager.getInherited2(
_enclosingClass, Name(libraryUri, '$name='));
if (method.isStatic && inherited != null) {
_errorReporter.reportErrorForElement(
CompileTimeErrorCode.CONFLICTING_STATIC_AND_INSTANCE, method, [
_enclosingClass.displayName,
name,
inherited.enclosingElement.displayName,
]);
} else if (inherited is PropertyAccessorElement) {
_errorReporter.reportErrorForElement(
CompileTimeErrorCode.CONFLICTING_METHOD_AND_FIELD, method, [
_enclosingClass.displayName,
name,
inherited.enclosingElement.displayName
]);
}
}
// getter declared in the enclosing class vs. inherited method
for (PropertyAccessorElement accessor in _enclosingClass.accessors) {
String name = accessor.displayName;
// find inherited method or property accessor
ExecutableElement inherited = _inheritanceManager.getInherited2(
_enclosingClass, Name(libraryUri, name));
inherited ??= _inheritanceManager.getInherited2(
_enclosingClass, Name(libraryUri, '$name='));
if (accessor.isStatic && inherited != null) {
_errorReporter.reportErrorForElement(
CompileTimeErrorCode.CONFLICTING_STATIC_AND_INSTANCE, accessor, [
_enclosingClass.displayName,
name,
inherited.enclosingElement.displayName,
]);
} else if (inherited is MethodElement) {
_errorReporter.reportErrorForElement(
CompileTimeErrorCode.CONFLICTING_FIELD_AND_METHOD, accessor, [
_enclosingClass.displayName,
name,
inherited.enclosingElement.displayName
]);
}
}
}
/// Verify all conflicts between type variable and enclosing class.
/// TODO(scheglov)
///
/// See [CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_CLASS], and
/// [CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_MEMBER].
void _checkForConflictingClassTypeVariableErrorCodes() {
for (TypeParameterElement typeParameter in _enclosingClass.typeParameters) {
String name = typeParameter.name;
// name is same as the name of the enclosing class
if (_enclosingClass.name == name) {
_errorReporter.reportErrorForElement(
CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_CLASS,
typeParameter,
[name]);
}
// check members
if (_enclosingClass.getMethod(name) != null ||
_enclosingClass.getGetter(name) != null ||
_enclosingClass.getSetter(name) != null) {
_errorReporter.reportErrorForElement(
CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_MEMBER_CLASS,
typeParameter,
[name]);
}
}
}
/// Verify all conflicts between type variable and enclosing extension.
///
/// See [CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_EXTENSION], and
/// [CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_EXTENSION_MEMBER].
void _checkForConflictingExtensionTypeVariableErrorCodes() {
for (TypeParameterElement typeParameter
in _enclosingExtension.typeParameters) {
String name = typeParameter.name;
// name is same as the name of the enclosing class
if (_enclosingExtension.name == name) {
_errorReporter.reportErrorForElement(
CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_EXTENSION,
typeParameter,
[name]);
}
// check members
if (_enclosingExtension.getMethod(name) != null ||
_enclosingExtension.getGetter(name) != null ||
_enclosingExtension.getSetter(name) != null) {
_errorReporter.reportErrorForElement(
CompileTimeErrorCode.CONFLICTING_TYPE_VARIABLE_AND_MEMBER_EXTENSION,
typeParameter,
[name]);
}
}
}
void _checkForConflictingGenerics(NamedCompilationUnitMember node) {
var element = node.declaredElement as ClassElement;
var analysisSession = _currentLibrary.session as AnalysisSessionImpl;
var errors = analysisSession.classHierarchy.errors(element);
for (var error in errors) {
if (error is IncompatibleInterfacesClassHierarchyError) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.CONFLICTING_GENERIC_INTERFACES,
node,
[
_enclosingClass.name,
error.first.getDisplayString(withNullability: true),
error.second.getDisplayString(withNullability: true),
],
);
} else {
throw UnimplementedError('${error.runtimeType}');
}
}
}
/// Verify that if the given [constructor] declaration is 'const' then there
/// are no invocations of non-'const' super constructors, and that there are
/// no instance variables mixed in.
///
/// See [CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_NON_CONST_SUPER], and
/// [CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_MIXIN_WITH_FIELD].
void _checkForConstConstructorWithNonConstSuper(
ConstructorDeclaration constructor) {
if (!_enclosingExecutable.isConstConstructor) {
return;
}
// OK, const factory, checked elsewhere
if (constructor.factoryKeyword != null) {
return;
}
// check for mixins
var instanceFields = <FieldElement>[];
for (var mixin in _enclosingClass.mixins) {
instanceFields.addAll(mixin.element.fields
.where((field) => !field.isStatic && !field.isSynthetic));
}
if (instanceFields.length == 1) {
var field = instanceFields.single;
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_MIXIN_WITH_FIELD,
constructor.returnType,
["'${field.enclosingElement.name}.${field.name}'"]);
return;
} else if (instanceFields.length > 1) {
var fieldNames = instanceFields
.map((field) => "'${field.enclosingElement.name}.${field.name}'")
.join(', ');
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_MIXIN_WITH_FIELDS,
constructor.returnType,
[fieldNames]);
return;
}
// try to find and check super constructor invocation
for (ConstructorInitializer initializer in constructor.initializers) {
if (initializer is SuperConstructorInvocation) {
ConstructorElement element = initializer.staticElement;
if (element == null || element.isConst) {
return;
}
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_NON_CONST_SUPER,
initializer,
[element.enclosingElement.displayName]);
return;
}
}
// no explicit super constructor invocation, check default constructor
InterfaceType supertype = _enclosingClass.supertype;
if (supertype == null) {
return;
}
if (supertype.isDartCoreObject) {
return;
}
ConstructorElement unnamedConstructor =
supertype.element.unnamedConstructor;
if (unnamedConstructor == null || unnamedConstructor.isConst) {
return;
}
// default constructor is not 'const', report problem
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_NON_CONST_SUPER,
constructor.returnType,
[supertype]);
}
/// Verify that if the given [constructor] declaration is 'const' then there
/// are no non-final instance variable. The [constructorElement] is the
/// constructor element.
void _checkForConstConstructorWithNonFinalField(
ConstructorDeclaration constructor,
ConstructorElement constructorElement) {
if (!_enclosingExecutable.isConstConstructor) {
return;
}
// check if there is non-final field
ClassElement classElement = constructorElement.enclosingElement;
if (!classElement.hasNonFinalField) {
return;
}
// TODO(brianwilkerson) Stop generating
// CONST_CONSTRUCTOR_WITH_NON_FINAL_FIELD when either
// CONST_CONSTRUCTOR_WITH_NON_CONST_SUPER or
// CONST_CONSTRUCTOR_WITH_MIXIN_WITH_FIELD is also generated.
_errorReporter.reportErrorForName(
CompileTimeErrorCode.CONST_CONSTRUCTOR_WITH_NON_FINAL_FIELD,
constructor);
}
/// Verify that the given 'const' instance creation [expression] is not
/// creating a deferred type. The [constructorName] is the constructor name,
/// always non-`null`. The [typeName] is the name of the type defining the
/// constructor, always non-`null`.
///
/// See [CompileTimeErrorCode.CONST_DEFERRED_CLASS].
void _checkForConstDeferredClass(InstanceCreationExpression expression,
ConstructorName constructorName, TypeName typeName) {
if (typeName.isDeferred) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.CONST_DEFERRED_CLASS,
constructorName,
[typeName.name.name]);
}
}
/// Verify that the given throw [expression] is not enclosed in a 'const'
/// constructor declaration.
///
/// See [CompileTimeErrorCode.CONST_CONSTRUCTOR_THROWS_EXCEPTION].
void _checkForConstEvalThrowsException(ThrowExpression expression) {
if (_enclosingExecutable.isConstConstructor) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.CONST_CONSTRUCTOR_THROWS_EXCEPTION, expression);
}
}
/// Verify that the given normal formal [parameter] is not 'const'.
///
/// See [CompileTimeErrorCode.CONST_FORMAL_PARAMETER].
void _checkForConstFormalParameter(NormalFormalParameter parameter) {
if (parameter.isConst) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.CONST_FORMAL_PARAMETER, parameter);
}
}
/// Verify that the given instance creation [expression] is not being invoked
/// on an abstract class. The [typeName] is the [TypeName] of the
/// [ConstructorName] from the [InstanceCreationExpression], this is the AST
/// node that the error is attached to. The [type] is the type being
/// constructed with this [InstanceCreationExpression].
void _checkForConstOrNewWithAbstractClass(
InstanceCreationExpression expression,
TypeName typeName,
InterfaceType type) {
if (type.element.isAbstract && !type.element.isMixin) {
ConstructorElement element = expression.constructorName.staticElement;
if (element != null && !element.isFactory) {
bool isImplicit =
(expression as InstanceCreationExpressionImpl).isImplicit;
if (!isImplicit) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.INSTANTIATE_ABSTRACT_CLASS, typeName);
} else {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.INSTANTIATE_ABSTRACT_CLASS, typeName);
}
}
}
}
/// Verify that the given instance creation [expression] is not being invoked
/// on an enum. The [typeName] is the [TypeName] of the [ConstructorName] from
/// the [InstanceCreationExpression], this is the AST node that the error is
/// attached to. The [type] is the type being constructed with this
/// [InstanceCreationExpression].
///
/// See [CompileTimeErrorCode.INSTANTIATE_ENUM].
void _checkForConstOrNewWithEnum(InstanceCreationExpression expression,
TypeName typeName, InterfaceType type) {
if (type.element.isEnum) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.INSTANTIATE_ENUM, typeName);
}
}
/// Verify that the given [expression] is not a mixin instantiation.
void _checkForConstOrNewWithMixin(InstanceCreationExpression expression,
TypeName typeName, InterfaceType type) {
if (type.element.isMixin) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.MIXIN_INSTANTIATE, typeName);
}
}
/// Verify that the given 'const' instance creation [expression] is not being
/// invoked on a constructor that is not 'const'.
///
/// This method assumes that the instance creation was tested to be 'const'
/// before being called.
///
/// See [CompileTimeErrorCode.CONST_WITH_NON_CONST].
void _checkForConstWithNonConst(InstanceCreationExpression expression) {
ConstructorElement constructorElement =
expression.constructorName.staticElement;
if (constructorElement != null && !constructorElement.isConst) {
if (expression.keyword != null) {
_errorReporter.reportErrorForToken(
CompileTimeErrorCode.CONST_WITH_NON_CONST, expression.keyword);
} else {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.CONST_WITH_NON_CONST, expression);
}
}
}
/// Verify that if the given 'const' instance creation [expression] is being
/// invoked on the resolved constructor. The [constructorName] is the
/// constructor name, always non-`null`. The [typeName] is the name of the
/// type defining the constructor, always non-`null`.
///
/// This method assumes that the instance creation was tested to be 'const'
/// before being called.
///
/// See [CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR], and
/// [CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR_DEFAULT].
void _checkForConstWithUndefinedConstructor(
InstanceCreationExpression expression,
ConstructorName constructorName,
TypeName typeName) {
// OK if resolved
if (constructorName.staticElement != null) {
return;
}
DartType type = typeName.type;
if (type is InterfaceType) {
ClassElement element = type.element;
if (element != null && element.isEnum) {
// We have already reported the error.
return;
}
}
Identifier className = typeName.name;
// report as named or default constructor absence
SimpleIdentifier name = constructorName.name;
if (name != null) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR,
name,
[className, name]);
} else {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.CONST_WITH_UNDEFINED_CONSTRUCTOR_DEFAULT,
constructorName,
[className]);
}
}
void _checkForDeadNullCoalesce(TypeImpl lhsType, Expression rhs) {
if (!_isNonNullableByDefault) return;
if (_typeSystem.isStrictlyNonNullable(lhsType)) {
_errorReporter.reportErrorForNode(
StaticWarningCode.DEAD_NULL_AWARE_EXPRESSION,
rhs,
);
}
}
/// Report a diagnostic if there are any extensions in the imported library
/// that are not hidden.
void _checkForDeferredImportOfExtensions(
ImportDirective directive, ImportElement importElement) {
for (var element in importElement.namespace.definedNames.values) {
if (element is ExtensionElement) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.DEFERRED_IMPORT_OF_EXTENSION,
directive.uri,
);
return;
}
}
}
/// Verify that any deferred imports in the given compilation [unit] have a
/// unique prefix.
///
/// See [CompileTimeErrorCode.SHARED_DEFERRED_PREFIX].
void _checkForDeferredPrefixCollisions(CompilationUnit unit) {
NodeList<Directive> directives = unit.directives;
int count = directives.length;
if (count > 0) {
Map<PrefixElement, List<ImportDirective>> prefixToDirectivesMap =
HashMap<PrefixElement, List<ImportDirective>>();
for (int i = 0; i < count; i++) {
Directive directive = directives[i];
if (directive is ImportDirective) {
SimpleIdentifier prefix = directive.prefix;
if (prefix != null) {
Element element = prefix.staticElement;
if (element is PrefixElement) {
List<ImportDirective> elements = prefixToDirectivesMap[element];
if (elements == null) {
elements = <ImportDirective>[];
prefixToDirectivesMap[element] = elements;
}
elements.add(directive);
}
}
}
}
for (List<ImportDirective> imports in prefixToDirectivesMap.values) {
_checkDeferredPrefixCollision(imports);
}
}
}
/// Return `true` if the caller should continue checking the rest of the
/// information in the for-each part.
bool _checkForEachParts(ForEachParts node, SimpleIdentifier variable) {
if (_checkForUseOfVoidResult(node.iterable)) {
return false;
}
DartType iterableType = node.iterable.staticType;
// TODO(scheglov) use NullableDereferenceVerifier
if (_isNonNullableByDefault) {
if (_typeSystem.isNullable(iterableType)) {
return false;
}
}
// The type of the loop variable.
DartType variableType;
var variableElement = variable.staticElement;
if (variableElement is VariableElement) {
variableType = variableElement.type;
} else {
return false;
}
AstNode parent = node.parent;
Token awaitKeyword;
if (parent is ForStatement) {
awaitKeyword = parent.awaitKeyword;
} else if (parent is ForElement) {
awaitKeyword = parent.awaitKeyword;
}
// Use an explicit string instead of [loopType] to remove the "<E>".
String loopTypeName = awaitKeyword != null ? "Stream" : "Iterable";
// The object being iterated has to implement Iterable<T> for some T that
// is assignable to the variable's type.
// TODO(rnystrom): Move this into mostSpecificTypeArgument()?
iterableType = iterableType.resolveToBound(_typeProvider.objectType);
var requiredSequenceType = awaitKeyword != null
? _typeProvider.streamDynamicType
: _typeProvider.iterableDynamicType;
if (_typeSystem.isTop(iterableType)) {
iterableType = requiredSequenceType;
}
if (!_typeSystem.isAssignableTo2(iterableType, requiredSequenceType)) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.FOR_IN_OF_INVALID_TYPE,
node.iterable,
[iterableType, loopTypeName],
);
return false;
}
DartType sequenceElementType;
{
var sequenceElement = awaitKeyword != null
? _typeProvider.streamElement
: _typeProvider.iterableElement;
var sequenceType = iterableType.asInstanceOf(sequenceElement);
if (sequenceType != null) {
sequenceElementType = sequenceType.typeArguments[0];
}
}
if (sequenceElementType == null) {
return true;
}
if (!_typeSystem.isAssignableTo2(sequenceElementType, variableType)) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.FOR_IN_OF_INVALID_ELEMENT_TYPE,
node.iterable,
[iterableType, loopTypeName, variableType],
);
}
return true;
}
/// Check that if the visiting library is not system, then any given library
/// should not be SDK internal library. The [exportElement] is the
/// [ExportElement] retrieved from the node, if the element in the node was
/// `null`, then this method is not called.
///
/// See [CompileTimeErrorCode.EXPORT_INTERNAL_LIBRARY].
void _checkForExportInternalLibrary(
ExportDirective directive, ExportElement exportElement) {
if (_isInSystemLibrary) {
return;
}
LibraryElement exportedLibrary = exportElement.exportedLibrary;
if (exportedLibrary == null) {
return;
}
// should be private
DartSdk sdk = _currentLibrary.context.sourceFactory.dartSdk;
String uri = exportedLibrary.source.uri.toString();
SdkLibrary sdkLibrary = sdk.getSdkLibrary(uri);
if (sdkLibrary == null) {
return;
}
if (!sdkLibrary.isInternal) {
return;
}
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.EXPORT_INTERNAL_LIBRARY,
directive,
[directive.uri]);
}
/// See [CompileTimeErrorCode.EXPORT_LEGACY_SYMBOL].
void _checkForExportLegacySymbol(ExportDirective node) {
if (!_isNonNullableByDefault) {
return;
}
var element = node.element as ExportElement;
// TODO(scheglov) Expose from ExportElement.
var namespace =
NamespaceBuilder().createExportNamespaceForDirective(element);
for (var element in namespace.definedNames.values) {
if (element == DynamicElementImpl.instance ||
element == NeverElementImpl.instance) {
continue;
}
if (!element.library.isNonNullableByDefault) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.EXPORT_LEGACY_SYMBOL,
node.uri,
[element.displayName],
);
// Stop after the first symbol.
// We don't want to list them all.
break;
}
}
}
/// Verify that the given extends [clause] does not extend a deferred class.
///
/// See [CompileTimeErrorCode.EXTENDS_DEFERRED_CLASS].
void _checkForExtendsDeferredClass(TypeName superclass) {
if (superclass == null) {
return;
}
_checkForExtendsOrImplementsDeferredClass(
superclass, CompileTimeErrorCode.EXTENDS_DEFERRED_CLASS);
}
/// Verify that the given extends [clause] does not extend classes such as
/// 'num' or 'String'.
///
/// See [CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS].
bool _checkForExtendsDisallowedClass(TypeName superclass) {
if (superclass == null) {
return false;
}
return _checkForExtendsOrImplementsDisallowedClass(
superclass, CompileTimeErrorCode.EXTENDS_DISALLOWED_CLASS);
}
/// Verify that the given [typeName] does not extend, implement or mixin
/// classes that are deferred.
///
/// See [_checkForExtendsDeferredClass],
/// [_checkForExtendsDeferredClassInTypeAlias],
/// [_checkForImplementsDeferredClass],
/// [_checkForAllMixinErrorCodes],
/// [CompileTimeErrorCode.EXTENDS_DEFERRED_CLASS],
/// [CompileTimeErrorCode.IMPLEMENTS_DEFERRED_CLASS], and
/// [CompileTimeErrorCode.MIXIN_DEFERRED_CLASS].
bool _checkForExtendsOrImplementsDeferredClass(
TypeName typeName, ErrorCode errorCode) {
if (typeName.isSynthetic) {
return false;
}
if (typeName.isDeferred) {
_errorReporter.reportErrorForNode(errorCode, typeName);
return true;
}
return false;
}
/// Verify that the given [typeName] does not extend, implement or mixin
/// classes such as 'num' or 'String'.
///
/// TODO(scheglov) Remove this method, when all inheritance / override
/// is concentrated. We keep it for now only because we need to know when
/// inheritance is completely wrong, so that we don't need to check anything
/// else.
bool _checkForExtendsOrImplementsDisallowedClass(
TypeName typeName, ErrorCode errorCode) {
if (typeName.isSynthetic) {
return false;
}
// The SDK implementation may implement disallowed types. For example,
// JSNumber in dart2js and _Smi in Dart VM both implement int.
if (_currentLibrary.source.isInSystemLibrary) {
return false;
}
return typeName.type is InterfaceType &&
_typeProvider.nonSubtypableClasses.contains(typeName.type.element);
}
void _checkForExtensionDeclaresMemberOfObject(MethodDeclaration node) {
if (_enclosingExtension == null) return;
var name = node.name.name;
if (name == '==' ||
name == 'hashCode' ||
name == 'toString' ||
name == 'runtimeType' ||
name == 'noSuchMethod') {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.EXTENSION_DECLARES_MEMBER_OF_OBJECT,
node.name,
);
}
}
/// Verify that the given constructor field [initializer] has compatible field
/// and initializer expression types. The [fieldElement] is the static element
/// from the name in the [ConstructorFieldInitializer].
///
/// See [CompileTimeErrorCode.CONST_FIELD_INITIALIZER_NOT_ASSIGNABLE], and
/// [StaticWarningCode.FIELD_INITIALIZER_NOT_ASSIGNABLE].
void _checkForFieldInitializerNotAssignable(
ConstructorFieldInitializer initializer, FieldElement fieldElement) {
// prepare field type
DartType fieldType = fieldElement.type;
// prepare expression type
Expression expression = initializer.expression;
if (expression == null) {
return;
}
// test the static type of the expression
DartType staticType = expression.staticType;
if (staticType == null) {
return;
}
if (_typeSystem.isAssignableTo2(staticType, fieldType)) {
return;
}
// report problem
if (_enclosingExecutable.isConstConstructor) {
// TODO(paulberry): this error should be based on the actual type of the
// constant, not the static type. See dartbug.com/21119.
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.CONST_FIELD_INITIALIZER_NOT_ASSIGNABLE,
expression,
[staticType, fieldType]);
}
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.FIELD_INITIALIZER_NOT_ASSIGNABLE,
expression,
[staticType, fieldType]);
// TODO(brianwilkerson) Define a hint corresponding to these errors and
// report it if appropriate.
// // test the propagated type of the expression
// Type propagatedType = expression.getPropagatedType();
// if (propagatedType != null && propagatedType.isAssignableTo(fieldType)) {
// return false;
// }
// // report problem
// if (isEnclosingConstructorConst) {
// errorReporter.reportTypeErrorForNode(
// CompileTimeErrorCode.CONST_FIELD_INITIALIZER_NOT_ASSIGNABLE,
// expression,
// propagatedType == null ? staticType : propagatedType,
// fieldType);
// } else {
// errorReporter.reportTypeErrorForNode(
// StaticWarningCode.FIELD_INITIALIZER_NOT_ASSIGNABLE,
// expression,
// propagatedType == null ? staticType : propagatedType,
// fieldType);
// }
// return true;
}
/// Verify that the given field formal [parameter] is in a constructor
/// declaration.
///
/// See [CompileTimeErrorCode.FIELD_INITIALIZER_OUTSIDE_CONSTRUCTOR].
void _checkForFieldInitializingFormalRedirectingConstructor(
FieldFormalParameter parameter) {
// prepare the node that should be a ConstructorDeclaration
AstNode formalParameterList = parameter.parent;
if (formalParameterList is! FormalParameterList) {
formalParameterList = formalParameterList?.parent;
}
AstNode constructor = formalParameterList?.parent;
// now check whether the node is actually a ConstructorDeclaration
if (constructor is ConstructorDeclaration) {
// constructor cannot be a factory
if (constructor.factoryKeyword != null) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.FIELD_INITIALIZER_FACTORY_CONSTRUCTOR,
parameter);
return;
}
// constructor cannot have a redirection
for (ConstructorInitializer initializer in constructor.initializers) {
if (initializer is RedirectingConstructorInvocation) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.FIELD_INITIALIZER_REDIRECTING_CONSTRUCTOR,
parameter);
return;
}
}
} else {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.FIELD_INITIALIZER_OUTSIDE_CONSTRUCTOR,
parameter);
}
}
/// Verify that the given variable declaration [list] has only initialized
/// variables if the list is final or const.
///
/// See [CompileTimeErrorCode.CONST_NOT_INITIALIZED], and
/// [StaticWarningCode.FINAL_NOT_INITIALIZED].
void _checkForFinalNotInitialized(VariableDeclarationList list) {
if (_isInNativeClass || list.isSynthetic) {
return;
}
// Handled during resolution, with flow analysis.
if (_isNonNullableByDefault &&
list.isFinal &&
list.parent is VariableDeclarationStatement) {
return;
}
bool isConst = list.isConst;
if (!(isConst || list.isFinal)) {
return;
}
NodeList<VariableDeclaration> variables = list.variables;
for (VariableDeclaration variable in variables) {
if (variable.initializer == null) {
if (isConst) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.CONST_NOT_INITIALIZED,
variable.name,
[variable.name.name]);
} else {
var variableElement = variable.declaredElement;
if (variableElement is FieldElement &&
(variableElement.isAbstract || variableElement.isExternal)) {
// Abstract and external fields can't be initialized, so no error.
} else if (variableElement is TopLevelVariableElement &&
variableElement.isExternal) {
// External top level variables can't be initialized, so no error.
} else if (!_isNonNullableByDefault || !variable.isLate) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.FINAL_NOT_INITIALIZED,
variable.name,
[variable.name.name]);
}
}
}
}
}
/// If there are no constructors in the given [members], verify that all
/// final fields are initialized. Cases in which there is at least one
/// constructor are handled in [_checkForAllFinalInitializedErrorCodes].
///
/// See [CompileTimeErrorCode.CONST_NOT_INITIALIZED], and
/// [StaticWarningCode.FINAL_NOT_INITIALIZED].
void _checkForFinalNotInitializedInClass(List<ClassMember> members) {
for (ClassMember classMember in members) {
if (classMember is ConstructorDeclaration) {
if (_isNonNullableByDefault) {
if (classMember.factoryKeyword == null) {
return;
}
} else {
return;
}
}
}
for (ClassMember classMember in members) {
if (classMember is FieldDeclaration) {
var fields = classMember.fields;
_checkForFinalNotInitialized(fields);
_checkForNotInitializedNonNullableInstanceFields(classMember);
}
}
}
void _checkForGenericFunctionType(TypeAnnotation node) {
if (node == null) {
return;
}
DartType type = node.type;
if (type is FunctionType && type.typeFormals.isNotEmpty) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.GENERIC_FUNCTION_TYPE_CANNOT_BE_BOUND,
node,
[type]);
}
}
/// Verify that the given implements [clause] does not implement classes such
/// as 'num' or 'String'.
///
/// See [CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS],
/// [CompileTimeErrorCode.IMPLEMENTS_DEFERRED_CLASS].
bool _checkForImplementsClauseErrorCodes(ImplementsClause clause) {
if (clause == null) {
return false;
}
bool foundError = false;
for (TypeName type in clause.interfaces) {
if (_checkForExtendsOrImplementsDisallowedClass(
type, CompileTimeErrorCode.IMPLEMENTS_DISALLOWED_CLASS)) {
foundError = true;
} else if (_checkForExtendsOrImplementsDeferredClass(
type, CompileTimeErrorCode.IMPLEMENTS_DEFERRED_CLASS)) {
foundError = true;
}
}
return foundError;
}
void _checkForImplicitDynamicIdentifier(AstNode node, Identifier id) {
if (_options.implicitDynamic) {
return;
}
VariableElement variable = getVariableElement(id);
if (variable != null &&
variable.hasImplicitType &&
variable.type.isDynamic) {
ErrorCode errorCode;
if (variable is FieldElement) {
errorCode = LanguageCode.IMPLICIT_DYNAMIC_FIELD;
} else if (variable is ParameterElement) {
errorCode = LanguageCode.IMPLICIT_DYNAMIC_PARAMETER;
} else {
errorCode = LanguageCode.IMPLICIT_DYNAMIC_VARIABLE;
}
_errorReporter.reportErrorForNode(errorCode, node, [id]);
}
}
void _checkForImplicitDynamicReturn(
AstNode functionName, ExecutableElement element) {
if (_options.implicitDynamic) {
return;
}
if (element is PropertyAccessorElement && element.isSetter) {
return;
}
if (element != null &&
element.hasImplicitReturnType &&
element.returnType.isDynamic) {
_errorReporter.reportErrorForNode(LanguageCode.IMPLICIT_DYNAMIC_RETURN,
functionName, [element.displayName]);
}
}
void _checkForImplicitDynamicType(TypeAnnotation node) {
if (_options.implicitDynamic ||
node == null ||
(node is TypeName && node.typeArguments != null)) {
return;
}
DartType type = node.type;
if (type is ParameterizedType &&
type.typeArguments.isNotEmpty &&
type.typeArguments.any((t) => t.isDynamic)) {
_errorReporter
.reportErrorForNode(LanguageCode.IMPLICIT_DYNAMIC_TYPE, node, [type]);
}
}
/// Verify that if the given [identifier] is part of a constructor
/// initializer, then it does not implicitly reference 'this' expression.
///
/// See [CompileTimeErrorCode.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER], and
/// [CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_STATIC].
/// TODO(scheglov) rename thid method
void _checkForImplicitThisReferenceInInitializer(
SimpleIdentifier identifier) {
if (_isInComment) {
return;
}
if (!_isInConstructorInitializer &&
!_enclosingExecutable.inStaticMethod &&
!_enclosingExecutable.isFactoryConstructor &&
!_isInInstanceNotLateVariableDeclaration &&
!_isInStaticVariableDeclaration) {
return;
}
// prepare element
Element element = identifier.staticElement;
if (!(element is MethodElement || element is PropertyAccessorElement)) {
return;
}
// static element
ExecutableElement executableElement = element as ExecutableElement;
if (executableElement.isStatic) {
return;
}
// not a class member
Element enclosingElement = element.enclosingElement;
if (enclosingElement is! ClassElement &&
enclosingElement is! ExtensionElement) {
return;
}
// qualified method invocation
AstNode parent = identifier.parent;
if (parent is MethodInvocation) {
if (identical(parent.methodName, identifier) &&
parent.realTarget != null) {
return;
}
}
// qualified property access
if (parent is PropertyAccess) {
if (identical(parent.propertyName, identifier) &&
parent.realTarget != null) {
return;
}
}
if (parent is PrefixedIdentifier) {
if (identical(parent.identifier, identifier)) {
return;
}
}
if (_enclosingExecutable.inStaticMethod) {
// TODO
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_STATIC, identifier);
} else if (_enclosingExecutable.isFactoryConstructor) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.INSTANCE_MEMBER_ACCESS_FROM_FACTORY, identifier);
} else {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.IMPLICIT_THIS_REFERENCE_IN_INITIALIZER,
identifier,
[identifier.name]);
}
}
/// Check that if the visiting library is not system, then any given library
/// should not be SDK internal library. The [importElement] is the
/// [ImportElement] retrieved from the node, if the element in the node was
/// `null`, then this method is not called
///
/// See [CompileTimeErrorCode.IMPORT_INTERNAL_LIBRARY].
void _checkForImportInternalLibrary(
ImportDirective directive, ImportElement importElement) {
if (_isInSystemLibrary) {
return;
}
LibraryElement importedLibrary = importElement.importedLibrary;
if (importedLibrary == null) {
return;
}
// should be private
DartSdk sdk = _currentLibrary.context.sourceFactory.dartSdk;
String uri = importedLibrary.source.uri.toString();
SdkLibrary sdkLibrary = sdk.getSdkLibrary(uri);
if (sdkLibrary == null || !sdkLibrary.isInternal) {
return;
}
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.IMPORT_INTERNAL_LIBRARY,
directive.uri,
[directive.uri.stringValue]);
}
/// Check that the given [typeReference] is not a type reference and that then
/// the [name] is reference to an instance member.
///
/// See [CompileTimeErrorCode.INSTANCE_ACCESS_TO_STATIC_MEMBER].
void _checkForInstanceAccessToStaticMember(
ClassElement typeReference, Expression target, SimpleIdentifier name) {
if (_isInComment) {
// OK, in comment
return;
}
// prepare member Element
Element element = name.staticElement;
if (element is ExecutableElement) {
if (!element.isStatic) {
// OK, instance member
return;
}
Element enclosingElement = element.enclosingElement;
if (enclosingElement is ExtensionElement) {
if (target is ExtensionOverride) {
// OK, target is an extension override
return;
} else if (target is SimpleIdentifier &&
target.staticElement is ExtensionElement) {
return;
} else if (target is PrefixedIdentifier &&
target.staticElement is ExtensionElement) {
return;
}
} else {
if (typeReference != null) {
// OK, target is a type
return;
}
if (enclosingElement is! ClassElement) {
// OK, top-level element
return;
}
}
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.INSTANCE_ACCESS_TO_STATIC_MEMBER,
name,
[name.name, _getKind(element), element.enclosingElement.name]);
}
}
/// Verify that an 'int' can be assigned to the parameter corresponding to the
/// given [argument]. This is used for prefix and postfix expressions where
/// the argument value is implicit.
///
/// See [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE].
void _checkForIntNotAssignable(Expression argument) {
if (argument == null) {
return;
}
ParameterElement staticParameterElement = argument.staticParameterElement;
DartType staticParameterType = staticParameterElement?.type;
_checkForArgumentTypeNotAssignable(argument, staticParameterType, _intType,
CompileTimeErrorCode.ARGUMENT_TYPE_NOT_ASSIGNABLE);
}
/// Verify that the given [annotation] isn't defined in a deferred library.
///
/// See [CompileTimeErrorCode.INVALID_ANNOTATION_FROM_DEFERRED_LIBRARY].
void _checkForInvalidAnnotationFromDeferredLibrary(Annotation annotation) {
Identifier nameIdentifier = annotation.name;
if (nameIdentifier is PrefixedIdentifier && nameIdentifier.isDeferred) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.INVALID_ANNOTATION_FROM_DEFERRED_LIBRARY,
annotation.name);
}
}
/// Verify that the given left hand side ([lhs]) and right hand side ([rhs])
/// represent a valid assignment.
///
/// See [CompileTimeErrorCode.INVALID_ASSIGNMENT].
void _checkForInvalidAssignment(Expression lhs, Expression rhs) {
if (lhs == null || rhs == null) {
return;
}
if (lhs is IndexExpression &&
identical(lhs.realTarget.staticType, NeverTypeImpl.instance) ||
lhs is PrefixedIdentifier &&
identical(lhs.prefix.staticType, NeverTypeImpl.instance) ||
lhs is PropertyAccess &&
identical(lhs.realTarget.staticType, NeverTypeImpl.instance)) {
return;
}
DartType leftType;
var parent = lhs.parent;
if (parent is AssignmentExpression && parent.leftHandSide == lhs) {
leftType = parent.writeType;
} else {
VariableElement leftVariableElement = getVariableElement(lhs);
leftType = (leftVariableElement == null)
? lhs.staticType
: leftVariableElement.type;
}
if (!leftType.isVoid && _checkForUseOfVoidResult(rhs)) {
return;
}
_checkForAssignableExpression(
rhs, leftType, CompileTimeErrorCode.INVALID_ASSIGNMENT);
}
/// Check the given [initializer] to ensure that the field being initialized
/// is a valid field. The [fieldName] is the field name from the
/// [ConstructorFieldInitializer]. The [staticElement] is the static element
/// from the name in the [ConstructorFieldInitializer].
void _checkForInvalidField(ConstructorFieldInitializer initializer,
SimpleIdentifier fieldName, Element staticElement) {
if (staticElement is FieldElement) {
if (staticElement.isSynthetic) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.INITIALIZER_FOR_NON_EXISTENT_FIELD,
initializer,
[fieldName]);
} else if (staticElement.isStatic) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.INITIALIZER_FOR_STATIC_FIELD,
initializer,
[fieldName]);
}
} else {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.INITIALIZER_FOR_NON_EXISTENT_FIELD,
initializer,
[fieldName]);
return;
}
}
/// Check to see whether the given function [body] has a modifier associated
/// with it, and report it as an error if it does.
void _checkForInvalidModifierOnBody(
FunctionBody body, CompileTimeErrorCode errorCode) {
Token keyword = body.keyword;
if (keyword != null) {
_errorReporter.reportErrorForToken(errorCode, keyword, [keyword.lexeme]);
}
}
/// Verify that the usage of the given 'this' is valid.
///
/// See [CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS].
void _checkForInvalidReferenceToThis(ThisExpression expression) {
if (!_thisAccessTracker.hasAccess) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.INVALID_REFERENCE_TO_THIS, expression);
}
}
void _checkForLateFinalFieldWithConstConstructor(FieldDeclaration node) {
if (node.isStatic) return;
var variableList = node.fields;
if (!variableList.isFinal) return;
var lateKeyword = variableList.lateKeyword;
if (lateKeyword == null) return;
var hasConstConstructor =
_enclosingClass.constructors.any((c) => c.isConst);
if (!hasConstConstructor) return;
_errorReporter.reportErrorForToken(
CompileTimeErrorCode.LATE_FINAL_FIELD_WITH_CONST_CONSTRUCTOR,
lateKeyword,
);
}
void _checkForListConstructor(
InstanceCreationExpression node, InterfaceType type) {
if (!_isNonNullableByDefault) return;
if (node.constructorName.name == null && type.isDartCoreList) {
_errorReporter.reportErrorForNode(
CompileTimeErrorCode.DEFAULT_LIST_CONSTRUCTOR,
node.constructorName,
);
}
}
/// Verify that the elements of the given list [literal] are subtypes of the
/// list's static type.
///
/// See [CompileTimeErrorCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE], and
/// [StaticWarningCode.LIST_ELEMENT_TYPE_NOT_ASSIGNABLE].
void _checkForListElementTypeNotAssignable(ListLiteral literal) {
// Determine the list's element type. We base this on the static type and
// not the literal's type arguments because in strong mode, the type
// arguments may be inferred.
DartType listType = literal.staticType;
assert(listType is InterfaceTypeImpl);
List<DartType> typeArguments =
(listType as InterfaceTypeImpl).typeArguments;
assert(typeArguments.length == 1);
DartType listElementType = typeArguments[0];