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

Unified Diff: pkg/analyzer/lib/src/task/strong/checker.dart

Issue 1700523002: fixes #24507, allow known functions to be treated as strict arrows (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 10 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/type_system.dart ('k') | pkg/analyzer/lib/src/task/strong/info.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
« no previous file with comments | « pkg/analyzer/lib/src/generated/type_system.dart ('k') | pkg/analyzer/lib/src/task/strong/info.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698