Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(576)

Unified Diff: pkg/analyzer/lib/src/generated/error_verifier.dart

Issue 895113002: Add static type checking of "yield" statements to analyzer. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/analyzer/lib/src/generated/error.dart ('k') | pkg/analyzer/lib/src/generated/resolver.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/analyzer/lib/src/generated/error_verifier.dart
diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart
index 56a5d10ab5040383b098ca0aedec741e011a74bd..c16cb353a1099bdb2880e61e5548cae5b3ac4ee5 100644
--- a/pkg/analyzer/lib/src/generated/error_verifier.dart
+++ b/pkg/analyzer/lib/src/generated/error_verifier.dart
@@ -1095,7 +1095,9 @@ class ErrorVerifier extends RecursiveAstVisitor<Object> {
@override
Object visitYieldStatement(YieldStatement node) {
- if (!_inGenerator) {
+ if (_inGenerator) {
+ _checkForYieldOfInvalidType(node.expression);
+ } else {
CompileTimeErrorCode errorCode;
if (node.star != null) {
errorCode = CompileTimeErrorCode.YIELD_EACH_IN_NON_GENERATOR;
@@ -5711,6 +5713,37 @@ class ErrorVerifier extends RecursiveAstVisitor<Object> {
}
/**
+ * Check for a type mis-match between the yielded type and the declared
+ * return type of a generator function.
+ *
+ * This method should only be called in generator functions.
+ */
+ bool _checkForYieldOfInvalidType(Expression yieldExpression) {
+ assert(_inGenerator);
+ if (_enclosingFunction == null) {
+ return false;
+ }
+ DartType declaredReturnType = _enclosingFunction.returnType;
+ DartType staticYieldedType = getStaticType(yieldExpression);
+ DartType impliedReturnType;
+ if (_enclosingFunction.isAsynchronous) {
+ impliedReturnType =
+ _typeProvider.streamType.substitute4(<DartType>[staticYieldedType]);
+ } else {
+ impliedReturnType =
+ _typeProvider.iterableType.substitute4(<DartType>[staticYieldedType]);
+ }
+ if (impliedReturnType.isAssignableTo(declaredReturnType)) {
+ return false;
+ }
+ _errorReporter.reportTypeErrorForNode(
+ StaticTypeWarningCode.YIELD_OF_INVALID_TYPE,
+ yieldExpression,
+ [declaredReturnType, impliedReturnType]);
+ return true;
+ }
+
+ /**
* This verifies that if the given class declaration implements the class Function that it has a
* concrete implementation of the call method.
*
« no previous file with comments | « pkg/analyzer/lib/src/generated/error.dart ('k') | pkg/analyzer/lib/src/generated/resolver.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698