OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 dev_compiler.src.checker.rules; | 5 library dev_compiler.src.checker.rules; |
6 | 6 |
7 import 'package:analyzer/src/generated/ast.dart'; | 7 import 'package:analyzer/src/generated/ast.dart'; |
8 import 'package:analyzer/src/generated/element.dart'; | 8 import 'package:analyzer/src/generated/element.dart'; |
9 import 'package:analyzer/src/generated/resolver.dart'; | 9 import 'package:analyzer/src/generated/resolver.dart'; |
10 | 10 |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 return getCallMethodType(t); | 69 return getCallMethodType(t); |
70 } | 70 } |
71 if (t is FunctionType) return t; | 71 if (t is FunctionType) return t; |
72 return null; | 72 return null; |
73 } | 73 } |
74 | 74 |
75 DartType elementType(Element e); | 75 DartType elementType(Element e); |
76 | 76 |
77 bool isDynamicTarget(Expression expr); | 77 bool isDynamicTarget(Expression expr); |
78 bool isDynamicCall(Expression call); | 78 bool isDynamicCall(Expression call); |
| 79 |
| 80 /// Gets the expected return type of the given function [body], either from |
| 81 /// a normal return/yield, or from a yield*. |
| 82 DartType getExpectedReturnType(FunctionBody body, {bool yieldStar: false}) { |
| 83 FunctionType functionType; |
| 84 var parent = body.parent; |
| 85 if (parent is Declaration) { |
| 86 functionType = elementType(parent.element); |
| 87 } else { |
| 88 assert(parent is FunctionExpression); |
| 89 functionType = getStaticType(parent); |
| 90 } |
| 91 |
| 92 var type = functionType.returnType; |
| 93 |
| 94 InterfaceType expectedType = null; |
| 95 if (body.isAsynchronous) { |
| 96 if (body.isGenerator) { |
| 97 // Stream<T> -> T |
| 98 expectedType = provider.streamType; |
| 99 } else { |
| 100 // Future<T> -> T |
| 101 // TODO(vsm): Revisit with issue #228. |
| 102 expectedType = provider.futureType; |
| 103 } |
| 104 } else { |
| 105 if (body.isGenerator) { |
| 106 // Iterable<T> -> T |
| 107 expectedType = provider.iterableType; |
| 108 } else { |
| 109 // T -> T |
| 110 return type; |
| 111 } |
| 112 } |
| 113 if (yieldStar) { |
| 114 if (type.isDynamic) { |
| 115 // Ensure it's at least a Stream / Iterable. |
| 116 return expectedType.substitute4([provider.dynamicType]); |
| 117 } else { |
| 118 // Analyzer will provide a separate error if expected type |
| 119 // is not compatible with type. |
| 120 return type; |
| 121 } |
| 122 } |
| 123 if (type.isDynamic) { |
| 124 return type; |
| 125 } else if (type is InterfaceType && type.element == expectedType.element) { |
| 126 return type.typeArguments[0]; |
| 127 } else { |
| 128 // Malformed type - fallback on analyzer error. |
| 129 return null; |
| 130 } |
| 131 } |
79 } | 132 } |
80 | 133 |
81 // TODO(jmesserly): this is unused. | 134 // TODO(jmesserly): this is unused. |
82 class DartRules extends TypeRules { | 135 class DartRules extends TypeRules { |
83 DartRules(TypeProvider provider) : super(provider); | 136 DartRules(TypeProvider provider) : super(provider); |
84 | 137 |
85 MissingTypeReporter reportMissingType = null; | 138 MissingTypeReporter reportMissingType = null; |
86 | 139 |
87 bool isSubTypeOf(DartType t1, DartType t2) { | 140 bool isSubTypeOf(DartType t1, DartType t2) { |
88 return t1.isSubtypeOf(t2); | 141 return t1.isSubtypeOf(t2); |
(...skipping 686 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
775 var entries = e.entries; | 828 var entries = e.entries; |
776 bool inferEntry(MapLiteralEntry entry) { | 829 bool inferEntry(MapLiteralEntry entry) { |
777 return _inferExpression(entry.key, kType, errors) && | 830 return _inferExpression(entry.key, kType, errors) && |
778 _inferExpression(entry.value, vType, errors); | 831 _inferExpression(entry.value, vType, errors); |
779 } | 832 } |
780 var b = entries.every(inferEntry); | 833 var b = entries.every(inferEntry); |
781 if (b) annotateMapLiteral(e, targs); | 834 if (b) annotateMapLiteral(e, targs); |
782 return b; | 835 return b; |
783 } | 836 } |
784 } | 837 } |
OLD | NEW |