Index: pkg/analyzer/lib/src/task/strong/checker.dart |
diff --git a/pkg/analyzer/lib/src/task/strong/checker.dart b/pkg/analyzer/lib/src/task/strong/checker.dart |
index b391517909f416da1bcb99f8a56f3d16bf3b5fc9..8f0c9defbe4c4ba77b03e48983abe0d27e77d988 100644 |
--- a/pkg/analyzer/lib/src/task/strong/checker.dart |
+++ b/pkg/analyzer/lib/src/task/strong/checker.dart |
@@ -12,6 +12,7 @@ import 'package:analyzer/dart/ast/token.dart' show Token, TokenType; |
import 'package:analyzer/dart/ast/visitor.dart'; |
import 'package:analyzer/dart/element/element.dart'; |
import 'package:analyzer/dart/element/type.dart'; |
+import 'package:analyzer/src/dart/element/element.dart'; |
import 'package:analyzer/src/dart/element/type.dart'; |
import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; |
import 'package:analyzer/src/generated/type_system.dart'; |
@@ -565,7 +566,7 @@ class CodeChecker extends RecursiveAstVisitor { |
} |
StaticInfo _checkAssignment(Expression expr, DartType toT) { |
- final fromT = expr.staticType ?? DynamicTypeImpl.instance; |
+ final fromT = _getStaticType(expr); |
final Coercion c = _coerceTo(fromT, toT); |
if (c is Identity) return null; |
if (c is CoercionError) return new StaticTypeError(rules, expr, toT); |
@@ -770,7 +771,63 @@ class CodeChecker extends RecursiveAstVisitor { |
} |
DartType _getStaticType(Expression expr) { |
- return expr.staticType ?? DynamicTypeImpl.instance; |
+ DartType t = expr.staticType ?? DynamicTypeImpl.instance; |
+ |
+ // Remove fuzzy arrow if possible. |
+ if (t is FunctionType && StaticInfo.isKnownFunction(expr)) { |
+ t = _removeFuzz(t); |
+ } |
+ |
+ return t; |
+ } |
+ |
+ /// Remove "fuzzy arrow" in this function type. |
+ /// |
+ /// Normally we treat dynamically typed parameters as bottom for function |
+ /// types. This allows type tests such as `if (f is SingleArgFunction)`. |
+ /// It also requires a dynamic check on the parameter type to call these |
+ /// functions. |
+ /// |
+ /// When we convert to a strict arrow, dynamically typed parameters become |
+ /// top. This is safe to do for known functions, like top-level or local |
+ /// functions and static methods. Those functions must already be essentially |
+ /// treating dynamic as top. |
+ /// |
+ /// Only the outer-most arrow can be strict. Any others must be fuzzy, because |
+ /// we don't know what function value will be passed there. |
+ // TODO(jmesserly): should we use a real "fuzzyArrow" bit on the function |
+ // type? That would allow us to implement this in the subtype relation. |
+ // TODO(jmesserly): we'll need to factor this differently if we want to |
+ // move CodeChecker's functionality into existing analyzer. Likely we can |
+ // let the Expression have a strict arrow, then in places were we do |
+ // inference, convert back to a fuzzy arrow. |
+ FunctionType _removeFuzz(FunctionType t) { |
+ bool foundFuzz = false; |
+ List<ParameterElement> parameters = <ParameterElement>[]; |
+ for (ParameterElement p in t.parameters) { |
+ ParameterElement newP = _removeParameterFuzz(p); |
+ parameters.add(newP); |
+ if (p != newP) foundFuzz = true; |
+ } |
+ if (!foundFuzz) { |
+ return t; |
+ } |
+ |
+ FunctionElementImpl function = new FunctionElementImpl("", -1); |
+ function.synthetic = true; |
+ function.returnType = t.returnType; |
+ function.shareTypeParameters(t.typeFormals); |
+ function.shareParameters(parameters); |
+ return function.type = new FunctionTypeImpl(function); |
+ } |
+ |
+ /// Removes fuzzy arrow, see [_removeFuzz]. |
+ ParameterElement _removeParameterFuzz(ParameterElement p) { |
+ if (p.type.isDynamic) { |
+ return new ParameterElementImpl.synthetic( |
+ p.name, typeProvider.objectType, p.parameterKind); |
+ } |
+ return p; |
} |
/// Given an expression, return its type assuming it is |