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

Unified Diff: pkg/front_end/lib/src/fasta/type_inference/type_inferrer.dart

Issue 2899833002: Add some type inference for "yield" statements. (Closed)
Patch Set: Created 3 years, 7 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
Index: pkg/front_end/lib/src/fasta/type_inference/type_inferrer.dart
diff --git a/pkg/front_end/lib/src/fasta/type_inference/type_inferrer.dart b/pkg/front_end/lib/src/fasta/type_inference/type_inferrer.dart
index d6258de42710abf3ac131dd6e07f1cf4fd44cb6e..b74bc4c6cccf2d4a9fb921ed36161ea552996418 100644
--- a/pkg/front_end/lib/src/fasta/type_inference/type_inferrer.dart
+++ b/pkg/front_end/lib/src/fasta/type_inference/type_inferrer.dart
@@ -51,7 +51,28 @@ class ClosureContext {
/// Updates the inferred return type based on the presence of a return
/// statement returning the given [type].
- void updateInferredReturnType(TypeInferrerImpl inferrer, DartType type) {
+ void handleReturn(TypeInferrerImpl inferrer, DartType type) {
+ if (isGenerator) return;
+ if (isAsync) {
+ type = inferrer.typeSchemaEnvironment.flattenFutures(type);
+ }
+ _updateInferredReturnType(inferrer, type);
+ }
+
+ void handleYield(TypeInferrerImpl inferrer, bool isYieldStar, DartType type) {
+ if (!isGenerator) return;
+ if (isYieldStar) {
+ type = inferrer.getTypeArgumentOf(
+ type,
+ isAsync
+ ? inferrer.coreTypes.streamClass
+ : inferrer.coreTypes.iterableClass);
+ if (type == null) return;
+ }
+ _updateInferredReturnType(inferrer, type);
+ }
+
+ void _updateInferredReturnType(TypeInferrerImpl inferrer, DartType type) {
if (_inferredReturnType == null) {
_inferredReturnType = type;
} else {
@@ -194,6 +215,17 @@ abstract class TypeInferrerImpl extends TypeInferrer {
}
}
+ DartType getDerivedTypeArgumentOf(DartType type, Class class_) {
+ if (type is InterfaceType) {
+ var typeAsInstanceOfClass =
+ classHierarchy.getTypeAsInstanceOf(type, class_);
+ if (typeAsInstanceOfClass != null) {
+ return typeAsInstanceOfClass.typeArguments[0];
+ }
+ }
+ return null;
+ }
+
/// Modifies a type as appropriate when inferring a declared variable's type.
DartType inferDeclarationType(DartType initializerType) {
if (initializerType is BottomType ||
« no previous file with comments | « pkg/front_end/lib/src/fasta/type_inference/type_inference_listener.dart ('k') | pkg/front_end/test/fasta/strong.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698