| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library analyzer.src.generated.error_verifier; | 5 library analyzer.src.generated.error_verifier; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 import "dart:math" as math; | 8 import "dart:math" as math; |
| 9 | 9 |
| 10 import 'package:analyzer/dart/ast/ast.dart'; | 10 import 'package:analyzer/dart/ast/ast.dart'; |
| (...skipping 630 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 641 return super.visitExportDirective(node); | 641 return super.visitExportDirective(node); |
| 642 } | 642 } |
| 643 | 643 |
| 644 @override | 644 @override |
| 645 Object visitExpressionFunctionBody(ExpressionFunctionBody node) { | 645 Object visitExpressionFunctionBody(ExpressionFunctionBody node) { |
| 646 bool wasInAsync = _inAsync; | 646 bool wasInAsync = _inAsync; |
| 647 bool wasInGenerator = _inGenerator; | 647 bool wasInGenerator = _inGenerator; |
| 648 try { | 648 try { |
| 649 _inAsync = node.isAsynchronous; | 649 _inAsync = node.isAsynchronous; |
| 650 _inGenerator = node.isGenerator; | 650 _inGenerator = node.isGenerator; |
| 651 FunctionType functionType = | 651 FunctionType functionType = _enclosingFunction?.type; |
| 652 _enclosingFunction == null ? null : _enclosingFunction.type; | |
| 653 DartType expectedReturnType = functionType == null | 652 DartType expectedReturnType = functionType == null |
| 654 ? DynamicTypeImpl.instance | 653 ? DynamicTypeImpl.instance |
| 655 : functionType.returnType; | 654 : functionType.returnType; |
| 656 ExecutableElement function = _enclosingFunction; | 655 ExecutableElement function = _enclosingFunction; |
| 657 bool isSetterWithImplicitReturn = function.hasImplicitReturnType && | 656 bool isSetterWithImplicitReturn = function.hasImplicitReturnType && |
| 658 function is PropertyAccessorElement && | 657 function is PropertyAccessorElement && |
| 659 function.isSetter; | 658 function.isSetter; |
| 660 if (!isSetterWithImplicitReturn) { | 659 if (!isSetterWithImplicitReturn) { |
| 661 _checkForReturnOfInvalidType(node.expression, expectedReturnType); | 660 _checkForReturnOfInvalidType(node.expression, expectedReturnType); |
| 662 } | 661 } |
| (...skipping 1126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1789 } | 1788 } |
| 1790 ExecutableElement executableElement = method.element; | 1789 ExecutableElement executableElement = method.element; |
| 1791 if (executableElement == null) { | 1790 if (executableElement == null) { |
| 1792 return; | 1791 return; |
| 1793 } | 1792 } |
| 1794 SimpleIdentifier methodName = method.name; | 1793 SimpleIdentifier methodName = method.name; |
| 1795 if (methodName.isSynthetic) { | 1794 if (methodName.isSynthetic) { |
| 1796 return; | 1795 return; |
| 1797 } | 1796 } |
| 1798 FormalParameterList formalParameterList = method.parameters; | 1797 FormalParameterList formalParameterList = method.parameters; |
| 1799 NodeList<FormalParameter> parameterList = | 1798 NodeList<FormalParameter> parameterList = formalParameterList?.parameters; |
| 1800 formalParameterList != null ? formalParameterList.parameters : null; | |
| 1801 List<AstNode> parameters = | 1799 List<AstNode> parameters = |
| 1802 parameterList != null ? new List.from(parameterList) : null; | 1800 parameterList != null ? new List.from(parameterList) : null; |
| 1803 _checkForAllInvalidOverrideErrorCodesForExecutable(executableElement, | 1801 _checkForAllInvalidOverrideErrorCodesForExecutable(executableElement, |
| 1804 executableElement.parameters, parameters, methodName); | 1802 executableElement.parameters, parameters, methodName); |
| 1805 } | 1803 } |
| 1806 | 1804 |
| 1807 /** | 1805 /** |
| 1808 * Verify that all classes of the given [withClause] are valid. | 1806 * Verify that all classes of the given [withClause] are valid. |
| 1809 * | 1807 * |
| 1810 * See [CompileTimeErrorCode.MIXIN_DECLARES_CONSTRUCTOR], | 1808 * See [CompileTimeErrorCode.MIXIN_DECLARES_CONSTRUCTOR], |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1913 * don't have `return;` if the enclosing method has a return type. | 1911 * don't have `return;` if the enclosing method has a return type. |
| 1914 * | 1912 * |
| 1915 * Check that the return type matches the type of the declared return type in | 1913 * Check that the return type matches the type of the declared return type in |
| 1916 * the enclosing method or function. | 1914 * the enclosing method or function. |
| 1917 * | 1915 * |
| 1918 * See [CompileTimeErrorCode.RETURN_IN_GENERATIVE_CONSTRUCTOR], | 1916 * See [CompileTimeErrorCode.RETURN_IN_GENERATIVE_CONSTRUCTOR], |
| 1919 * [StaticWarningCode.RETURN_WITHOUT_VALUE], and | 1917 * [StaticWarningCode.RETURN_WITHOUT_VALUE], and |
| 1920 * [StaticTypeWarningCode.RETURN_OF_INVALID_TYPE]. | 1918 * [StaticTypeWarningCode.RETURN_OF_INVALID_TYPE]. |
| 1921 */ | 1919 */ |
| 1922 void _checkForAllReturnStatementErrorCodes(ReturnStatement statement) { | 1920 void _checkForAllReturnStatementErrorCodes(ReturnStatement statement) { |
| 1923 FunctionType functionType = | 1921 FunctionType functionType = _enclosingFunction?.type; |
| 1924 _enclosingFunction == null ? null : _enclosingFunction.type; | |
| 1925 DartType expectedReturnType = functionType == null | 1922 DartType expectedReturnType = functionType == null |
| 1926 ? DynamicTypeImpl.instance | 1923 ? DynamicTypeImpl.instance |
| 1927 : functionType.returnType; | 1924 : functionType.returnType; |
| 1928 Expression returnExpression = statement.expression; | 1925 Expression returnExpression = statement.expression; |
| 1929 // RETURN_IN_GENERATIVE_CONSTRUCTOR | 1926 // RETURN_IN_GENERATIVE_CONSTRUCTOR |
| 1930 bool isGenerativeConstructor(ExecutableElement element) => | 1927 bool isGenerativeConstructor(ExecutableElement element) => |
| 1931 element is ConstructorElement && !element.isFactory; | 1928 element is ConstructorElement && !element.isFactory; |
| 1932 if (isGenerativeConstructor(_enclosingFunction)) { | 1929 if (isGenerativeConstructor(_enclosingFunction)) { |
| 1933 if (returnExpression == null) { | 1930 if (returnExpression == null) { |
| 1934 return; | 1931 return; |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2031 * This method corresponds to | 2028 * This method corresponds to |
| 2032 * [BestPracticesVerifier.checkForArgumentTypeNotAssignableForArgument]. | 2029 * [BestPracticesVerifier.checkForArgumentTypeNotAssignableForArgument]. |
| 2033 * | 2030 * |
| 2034 * See [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]. | 2031 * See [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]. |
| 2035 */ | 2032 */ |
| 2036 void _checkForArgumentTypeNotAssignableForArgument(Expression argument) { | 2033 void _checkForArgumentTypeNotAssignableForArgument(Expression argument) { |
| 2037 if (argument == null) { | 2034 if (argument == null) { |
| 2038 return; | 2035 return; |
| 2039 } | 2036 } |
| 2040 ParameterElement staticParameterElement = argument.staticParameterElement; | 2037 ParameterElement staticParameterElement = argument.staticParameterElement; |
| 2041 DartType staticParameterType = | 2038 DartType staticParameterType = staticParameterElement?.type; |
| 2042 staticParameterElement == null ? null : staticParameterElement.type; | |
| 2043 _checkForArgumentTypeNotAssignableWithExpectedTypes(argument, | 2039 _checkForArgumentTypeNotAssignableWithExpectedTypes(argument, |
| 2044 staticParameterType, StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE); | 2040 staticParameterType, StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE); |
| 2045 } | 2041 } |
| 2046 | 2042 |
| 2047 /** | 2043 /** |
| 2048 * Verify that the given [expression] can be assigned to its corresponding | 2044 * Verify that the given [expression] can be assigned to its corresponding |
| 2049 * parameters. The [expectedStaticType] is the expected static type. | 2045 * parameters. The [expectedStaticType] is the expected static type. |
| 2050 * | 2046 * |
| 2051 * This method corresponds to | 2047 * This method corresponds to |
| 2052 * [BestPracticesVerifier.checkForArgumentTypeNotAssignableWithExpectedTypes]. | 2048 * [BestPracticesVerifier.checkForArgumentTypeNotAssignableWithExpectedTypes]. |
| (...skipping 1643 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3696 if (node.identifier == null && node.loopVariable == null) { | 3692 if (node.identifier == null && node.loopVariable == null) { |
| 3697 return; | 3693 return; |
| 3698 } | 3694 } |
| 3699 | 3695 |
| 3700 DartType iterableType = getStaticType(node.iterable); | 3696 DartType iterableType = getStaticType(node.iterable); |
| 3701 if (iterableType.isDynamic) { | 3697 if (iterableType.isDynamic) { |
| 3702 return; | 3698 return; |
| 3703 } | 3699 } |
| 3704 | 3700 |
| 3705 // The type of the loop variable. | 3701 // The type of the loop variable. |
| 3706 SimpleIdentifier variable = node.identifier != null | 3702 SimpleIdentifier variable = node.identifier ?? node.loopVariable.identifier; |
| 3707 ? node.identifier | |
| 3708 : node.loopVariable.identifier; | |
| 3709 DartType variableType = getStaticType(variable); | 3703 DartType variableType = getStaticType(variable); |
| 3710 | 3704 |
| 3711 DartType loopType = node.awaitKeyword != null | 3705 DartType loopType = node.awaitKeyword != null |
| 3712 ? _typeProvider.streamType | 3706 ? _typeProvider.streamType |
| 3713 : _typeProvider.iterableType; | 3707 : _typeProvider.iterableType; |
| 3714 | 3708 |
| 3715 // Use an explicit string instead of [loopType] to remove the "<E>". | 3709 // Use an explicit string instead of [loopType] to remove the "<E>". |
| 3716 String loopTypeName = node.awaitKeyword != null ? "Stream" : "Iterable"; | 3710 String loopTypeName = node.awaitKeyword != null ? "Stream" : "Iterable"; |
| 3717 | 3711 |
| 3718 // The object being iterated has to implement Iterable<T> for some T that | 3712 // The object being iterated has to implement Iterable<T> for some T that |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3785 * | 3779 * |
| 3786 * See [StaticTypeWarningCode.INSTANCE_METHOD_NAME_COLLIDES_WITH_SUPERCLASS_ST
ATIC]. | 3780 * See [StaticTypeWarningCode.INSTANCE_METHOD_NAME_COLLIDES_WITH_SUPERCLASS_ST
ATIC]. |
| 3787 */ | 3781 */ |
| 3788 bool _checkForInstanceMethodNameCollidesWithSuperclassStatic( | 3782 bool _checkForInstanceMethodNameCollidesWithSuperclassStatic( |
| 3789 ExecutableElement executableElement, SimpleIdentifier errorNameTarget) { | 3783 ExecutableElement executableElement, SimpleIdentifier errorNameTarget) { |
| 3790 String executableElementName = executableElement.name; | 3784 String executableElementName = executableElement.name; |
| 3791 if (executableElement is! PropertyAccessorElement && | 3785 if (executableElement is! PropertyAccessorElement && |
| 3792 !executableElement.isOperator) { | 3786 !executableElement.isOperator) { |
| 3793 HashSet<ClassElement> visitedClasses = new HashSet<ClassElement>(); | 3787 HashSet<ClassElement> visitedClasses = new HashSet<ClassElement>(); |
| 3794 InterfaceType superclassType = _enclosingClass.supertype; | 3788 InterfaceType superclassType = _enclosingClass.supertype; |
| 3795 ClassElement superclassElement = | 3789 ClassElement superclassElement = superclassType?.element; |
| 3796 superclassType == null ? null : superclassType.element; | |
| 3797 bool executableElementPrivate = | 3790 bool executableElementPrivate = |
| 3798 Identifier.isPrivateName(executableElementName); | 3791 Identifier.isPrivateName(executableElementName); |
| 3799 while (superclassElement != null && | 3792 while (superclassElement != null && |
| 3800 !visitedClasses.contains(superclassElement)) { | 3793 !visitedClasses.contains(superclassElement)) { |
| 3801 visitedClasses.add(superclassElement); | 3794 visitedClasses.add(superclassElement); |
| 3802 LibraryElement superclassLibrary = superclassElement.library; | 3795 LibraryElement superclassLibrary = superclassElement.library; |
| 3803 // Check fields. | 3796 // Check fields. |
| 3804 FieldElement fieldElt = | 3797 FieldElement fieldElt = |
| 3805 superclassElement.getField(executableElementName); | 3798 superclassElement.getField(executableElementName); |
| 3806 if (fieldElt != null) { | 3799 if (fieldElt != null) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3838 .INSTANCE_METHOD_NAME_COLLIDES_WITH_SUPERCLASS_STATIC, | 3831 .INSTANCE_METHOD_NAME_COLLIDES_WITH_SUPERCLASS_STATIC, |
| 3839 errorNameTarget, | 3832 errorNameTarget, |
| 3840 [ | 3833 [ |
| 3841 executableElementName, | 3834 executableElementName, |
| 3842 methodElement.enclosingElement.displayName | 3835 methodElement.enclosingElement.displayName |
| 3843 ]); | 3836 ]); |
| 3844 return true; | 3837 return true; |
| 3845 } | 3838 } |
| 3846 } | 3839 } |
| 3847 superclassType = superclassElement.supertype; | 3840 superclassType = superclassElement.supertype; |
| 3848 superclassElement = | 3841 superclassElement = superclassType?.element; |
| 3849 superclassType == null ? null : superclassType.element; | |
| 3850 } | 3842 } |
| 3851 } | 3843 } |
| 3852 return false; | 3844 return false; |
| 3853 } | 3845 } |
| 3854 | 3846 |
| 3855 /** | 3847 /** |
| 3856 * Verify that an 'int' can be assigned to the parameter corresponding to the | 3848 * Verify that an 'int' can be assigned to the parameter corresponding to the |
| 3857 * given [argument]. This is used for prefix and postfix expressions where | 3849 * given [argument]. This is used for prefix and postfix expressions where |
| 3858 * the argument value is implicit. | 3850 * the argument value is implicit. |
| 3859 * | 3851 * |
| 3860 * See [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]. | 3852 * See [StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE]. |
| 3861 */ | 3853 */ |
| 3862 void _checkForIntNotAssignable(Expression argument) { | 3854 void _checkForIntNotAssignable(Expression argument) { |
| 3863 if (argument == null) { | 3855 if (argument == null) { |
| 3864 return; | 3856 return; |
| 3865 } | 3857 } |
| 3866 ParameterElement staticParameterElement = argument.staticParameterElement; | 3858 ParameterElement staticParameterElement = argument.staticParameterElement; |
| 3867 DartType staticParameterType = | 3859 DartType staticParameterType = staticParameterElement?.type; |
| 3868 staticParameterElement == null ? null : staticParameterElement.type; | |
| 3869 _checkForArgumentTypeNotAssignable(argument, staticParameterType, _intType, | 3860 _checkForArgumentTypeNotAssignable(argument, staticParameterType, _intType, |
| 3870 StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE); | 3861 StaticWarningCode.ARGUMENT_TYPE_NOT_ASSIGNABLE); |
| 3871 } | 3862 } |
| 3872 | 3863 |
| 3873 /** | 3864 /** |
| 3874 * Verify that the given [annotation] isn't defined in a deferred library. | 3865 * Verify that the given [annotation] isn't defined in a deferred library. |
| 3875 * | 3866 * |
| 3876 * See [CompileTimeErrorCode.INVALID_ANNOTATION_FROM_DEFERRED_LIBRARY]. | 3867 * See [CompileTimeErrorCode.INVALID_ANNOTATION_FROM_DEFERRED_LIBRARY]. |
| 3877 */ | 3868 */ |
| 3878 void _checkForInvalidAnnotationFromDeferredLibrary(Annotation annotation) { | 3869 void _checkForInvalidAnnotationFromDeferredLibrary(Annotation annotation) { |
| (...skipping 2365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6244 class _InvocationCollector extends RecursiveAstVisitor { | 6235 class _InvocationCollector extends RecursiveAstVisitor { |
| 6245 final List<String> superCalls = <String>[]; | 6236 final List<String> superCalls = <String>[]; |
| 6246 | 6237 |
| 6247 @override | 6238 @override |
| 6248 visitMethodInvocation(MethodInvocation node) { | 6239 visitMethodInvocation(MethodInvocation node) { |
| 6249 if (node.target is SuperExpression) { | 6240 if (node.target is SuperExpression) { |
| 6250 superCalls.add(node.methodName.name); | 6241 superCalls.add(node.methodName.name); |
| 6251 } | 6242 } |
| 6252 } | 6243 } |
| 6253 } | 6244 } |
| OLD | NEW |