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 642 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
653 _inGenerator = node.isGenerator; | 653 _inGenerator = node.isGenerator; |
654 FunctionType functionType = _enclosingFunction?.type; | 654 FunctionType functionType = _enclosingFunction?.type; |
655 DartType expectedReturnType = functionType == null | 655 DartType expectedReturnType = functionType == null |
656 ? DynamicTypeImpl.instance | 656 ? DynamicTypeImpl.instance |
657 : functionType.returnType; | 657 : functionType.returnType; |
658 ExecutableElement function = _enclosingFunction; | 658 ExecutableElement function = _enclosingFunction; |
659 bool isSetterWithImplicitReturn = function.hasImplicitReturnType && | 659 bool isSetterWithImplicitReturn = function.hasImplicitReturnType && |
660 function is PropertyAccessorElement && | 660 function is PropertyAccessorElement && |
661 function.isSetter; | 661 function.isSetter; |
662 if (!isSetterWithImplicitReturn) { | 662 if (!isSetterWithImplicitReturn) { |
663 _checkForReturnOfInvalidType(node.expression, expectedReturnType, | 663 _checkForReturnOfInvalidType(node.expression, expectedReturnType); |
664 isArrowFunction: true); | |
665 } | 664 } |
666 return super.visitExpressionFunctionBody(node); | 665 return super.visitExpressionFunctionBody(node); |
667 } finally { | 666 } finally { |
668 _inAsync = wasInAsync; | 667 _inAsync = wasInAsync; |
669 _inGenerator = wasInGenerator; | 668 _inGenerator = wasInGenerator; |
670 } | 669 } |
671 } | 670 } |
672 | 671 |
673 @override | 672 @override |
674 Object visitFieldDeclaration(FieldDeclaration node) { | 673 Object visitFieldDeclaration(FieldDeclaration node) { |
(...skipping 4917 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5592 /** | 5591 /** |
5593 * Check that a type mis-match between the type of the [returnExpression] and | 5592 * Check that a type mis-match between the type of the [returnExpression] and |
5594 * the [expectedReturnType] by the enclosing method or function. | 5593 * the [expectedReturnType] by the enclosing method or function. |
5595 * | 5594 * |
5596 * This method is called both by [_checkForAllReturnStatementErrorCodes] | 5595 * This method is called both by [_checkForAllReturnStatementErrorCodes] |
5597 * and [visitExpressionFunctionBody]. | 5596 * and [visitExpressionFunctionBody]. |
5598 * | 5597 * |
5599 * See [StaticTypeWarningCode.RETURN_OF_INVALID_TYPE]. | 5598 * See [StaticTypeWarningCode.RETURN_OF_INVALID_TYPE]. |
5600 */ | 5599 */ |
5601 void _checkForReturnOfInvalidType( | 5600 void _checkForReturnOfInvalidType( |
5602 Expression returnExpression, DartType expectedReturnType, | 5601 Expression returnExpression, DartType expectedReturnType) { |
5603 {bool isArrowFunction = false}) { | |
5604 if (_enclosingFunction == null) { | 5602 if (_enclosingFunction == null) { |
5605 return; | 5603 return; |
5606 } | 5604 } |
5607 if (_inGenerator) { | 5605 if (_inGenerator) { |
5608 // "return expression;" is disallowed in generators, but this is checked | 5606 // "return expression;" is disallowed in generators, but this is checked |
5609 // elsewhere. Bare "return" is always allowed in generators regardless | 5607 // elsewhere. Bare "return" is always allowed in generators regardless |
5610 // of the return type. So no need to do any further checking. | 5608 // of the return type. So no need to do any further checking. |
5611 return; | 5609 return; |
5612 } | 5610 } |
5613 DartType staticReturnType = _computeReturnTypeForMethod(returnExpression); | 5611 DartType staticReturnType = _computeReturnTypeForMethod(returnExpression); |
5614 if (expectedReturnType.isVoid) { | 5612 if (expectedReturnType.isVoid) { |
5615 if (isArrowFunction) { | |
5616 // "void f(..) => e" admits all types for "e". | |
5617 return; | |
5618 } | |
5619 if (staticReturnType.isVoid || | 5613 if (staticReturnType.isVoid || |
5620 staticReturnType.isDynamic || | 5614 staticReturnType.isDynamic || |
5621 staticReturnType.isBottom || | 5615 staticReturnType.isBottom || |
5622 staticReturnType.isDartCoreNull) { | 5616 staticReturnType.isDartCoreNull) { |
5623 return; | 5617 return; |
5624 } | 5618 } |
5625 _errorReporter.reportTypeErrorForNode( | 5619 _errorReporter.reportTypeErrorForNode( |
5626 StaticTypeWarningCode.RETURN_OF_INVALID_TYPE, returnExpression, [ | 5620 StaticTypeWarningCode.RETURN_OF_INVALID_TYPE, returnExpression, [ |
5627 staticReturnType, | 5621 staticReturnType, |
5628 expectedReturnType, | 5622 expectedReturnType, |
(...skipping 1507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7136 class _InvocationCollector extends RecursiveAstVisitor { | 7130 class _InvocationCollector extends RecursiveAstVisitor { |
7137 final List<String> superCalls = <String>[]; | 7131 final List<String> superCalls = <String>[]; |
7138 | 7132 |
7139 @override | 7133 @override |
7140 visitMethodInvocation(MethodInvocation node) { | 7134 visitMethodInvocation(MethodInvocation node) { |
7141 if (node.target is SuperExpression) { | 7135 if (node.target is SuperExpression) { |
7142 superCalls.add(node.methodName.name); | 7136 superCalls.add(node.methodName.name); |
7143 } | 7137 } |
7144 } | 7138 } |
7145 } | 7139 } |
OLD | NEW |