| Index: pkg/analyzer/lib/src/task/strong/rules.dart
|
| diff --git a/pkg/analyzer/lib/src/task/strong/rules.dart b/pkg/analyzer/lib/src/task/strong/rules.dart
|
| index 32236a8aaee630c3d0584190def357676a92d62e..ed92a7ecc5a7289ca30b978b3e94fcd1c66eef1e 100644
|
| --- a/pkg/analyzer/lib/src/task/strong/rules.dart
|
| +++ b/pkg/analyzer/lib/src/task/strong/rules.dart
|
| @@ -421,6 +421,7 @@ class TypeRules {
|
| var reason = null;
|
|
|
| var errors = <String>[];
|
| +
|
| var ok = inferrer.inferExpression(expr, toT, errors);
|
| if (ok) return InferredType.create(this, expr, toT);
|
| reason = (errors.isNotEmpty) ? errors.first : null;
|
| @@ -508,264 +509,12 @@ class DownwardsInference {
|
| /// Downward inference
|
| bool _inferExpression(Expression e, DartType t, List<String> errors,
|
| {cast: true}) {
|
| - if (e is ConditionalExpression) {
|
| - return _inferConditionalExpression(e, t, errors);
|
| - }
|
| - if (e is ParenthesizedExpression) {
|
| - return _inferParenthesizedExpression(e, t, errors);
|
| - }
|
| if (rules.isSubTypeOf(rules.getStaticType(e), t)) return true;
|
| if (cast && rules.getStaticType(e).isDynamic) {
|
| annotateCastFromDynamic(e, t);
|
| return true;
|
| }
|
| - if (e is FunctionExpression) return _inferFunctionExpression(e, t, errors);
|
| - if (e is ListLiteral) return _inferListLiteral(e, t, errors);
|
| - if (e is MapLiteral) return _inferMapLiteral(e, t, errors);
|
| - if (e is NamedExpression) return _inferNamedExpression(e, t, errors);
|
| - if (e is InstanceCreationExpression) {
|
| - return _inferInstanceCreationExpression(e, t, errors);
|
| - }
|
| errors.add("$e cannot be typed as $t");
|
| return false;
|
| }
|
| -
|
| - /// If t1 = I<dynamic, ..., dynamic>, then look for a supertype
|
| - /// of t1 of the form K<S0, ..., Sm> where t2 = K<S0', ..., Sm'>
|
| - /// If the supertype exists, use the constraints S0 <: S0', ... Sm <: Sm'
|
| - /// to derive a concrete instantation for I of the form <T0, ..., Tn>,
|
| - /// such that I<T0, .., Tn> <: t2
|
| - List<DartType> _matchTypes(InterfaceType t1, InterfaceType t2) {
|
| - if (t1 == t2) return t2.typeArguments;
|
| - var tArgs1 = t1.typeArguments;
|
| - var tArgs2 = t2.typeArguments;
|
| - // If t1 isn't a raw type, bail out
|
| - if (tArgs1 != null && tArgs1.any((t) => !t.isDynamic)) return null;
|
| -
|
| - // This is our inferred type argument list. We start at all dynamic,
|
| - // and fill in with inferred types when we reach a match.
|
| - var actuals =
|
| - new List<DartType>.filled(tArgs1.length, rules.provider.dynamicType);
|
| -
|
| - // When we find the supertype of t1 with the same
|
| - // classname as t2 (see below), we have the following:
|
| - // If t1 is an instantiation of a class T1<X0, ..., Xn>
|
| - // and t2 is an instantiation of a class T2<Y0, ...., Ym>
|
| - // of the form t2 = T2<S0, ..., Sm>
|
| - // then we want to choose instantiations for the Xi
|
| - // T0, ..., Tn such that T1<T0, ..., Tn> <: t2 .
|
| - // To find this, we simply instantate T1 with
|
| - // X0, ..., Xn, and then find its superclass
|
| - // T2<T0', ..., Tn'>. We then solve the constraint
|
| - // set T0' <: S0, ..., Tn' <: Sn for the Xi.
|
| - // Currently, we only handle constraints where
|
| - // the Ti' is one of the Xi'. If there are multiple
|
| - // constraints on some Xi, we choose the lower of the
|
| - // two (if it exists).
|
| - bool permute(List<DartType> permutedArgs) {
|
| - if (permutedArgs == null) return false;
|
| - var ps = t1.typeParameters;
|
| - var ts = ps.map((p) => p.type).toList();
|
| - for (int i = 0; i < permutedArgs.length; i++) {
|
| - var tVar = permutedArgs[i];
|
| - var tActual = tArgs2[i];
|
| - var index = ts.indexOf(tVar);
|
| - if (index >= 0 && rules.isSubTypeOf(tActual, actuals[index])) {
|
| - actuals[index] = tActual;
|
| - }
|
| - }
|
| - return actuals.any((x) => !x.isDynamic);
|
| - }
|
| -
|
| - // Look for the first supertype of t1 with the same class name as t2.
|
| - bool match(InterfaceType t1) {
|
| - if (t1.element == t2.element) {
|
| - return permute(t1.typeArguments);
|
| - }
|
| -
|
| - if (t1 == rules.provider.objectType) return false;
|
| -
|
| - if (match(t1.superclass)) return true;
|
| -
|
| - for (final parent in t1.interfaces) {
|
| - if (match(parent)) return true;
|
| - }
|
| -
|
| - for (final parent in t1.mixins) {
|
| - if (match(parent)) return true;
|
| - }
|
| - return false;
|
| - }
|
| -
|
| - // We have that t1 = T1<dynamic, ..., dynamic>.
|
| - // To match t1 against t2, we use the uninstantiated version
|
| - // of t1, essentially treating it as an instantiation with
|
| - // fresh variables, and solve for the variables.
|
| - // t1.element.type will be of the form T1<X0, ..., Xn>
|
| - if (!match(t1.element.type)) return null;
|
| - var newT1 = t1.element.type.substitute4(actuals);
|
| - // If we found a solution, return it.
|
| - if (rules.isSubTypeOf(newT1, t2)) return actuals;
|
| - return null;
|
| - }
|
| -
|
| - /// These assume that e is not already a subtype of t
|
| -
|
| - bool _inferConditionalExpression(
|
| - ConditionalExpression e, DartType t, errors) {
|
| - return _inferExpression(e.thenExpression, t, errors) &&
|
| - _inferExpression(e.elseExpression, t, errors);
|
| - }
|
| -
|
| - bool _inferParenthesizedExpression(
|
| - ParenthesizedExpression e, DartType t, errors) {
|
| - return _inferExpression(e.expression, t, errors);
|
| - }
|
| -
|
| - bool _inferInstanceCreationExpression(
|
| - InstanceCreationExpression e, DartType t, errors) {
|
| - var arguments = e.argumentList.arguments;
|
| - var rawType = rules.getStaticType(e);
|
| - // rawType is the instantiated type of the instance
|
| - if (rawType is! InterfaceType) return false;
|
| - var type = (rawType as InterfaceType);
|
| - if (type.typeParameters == null ||
|
| - type.typeParameters.length == 0) return false;
|
| - if (e.constructorName.type == null) return false;
|
| - // classTypeName is the type name of the class being instantiated
|
| - var classTypeName = e.constructorName.type;
|
| - // Check that we were not passed any type arguments
|
| - if (classTypeName.typeArguments != null) return false;
|
| - // Infer type arguments
|
| - if (t is! InterfaceType) return false;
|
| - var targs = _matchTypes(type, t);
|
| - if (targs == null) return false;
|
| - if (e.staticElement == null) return false;
|
| - var constructorElement = e.staticElement;
|
| - // From the constructor element get:
|
| - // the instantiated type of the constructor, then
|
| - // the uninstantiated element for the constructor, then
|
| - // the uninstantiated type for the constructor
|
| - var rawConstructorElement =
|
| - constructorElement.type.element as ConstructorElement;
|
| - var baseType = rawConstructorElement.type;
|
| - if (baseType == null) return false;
|
| - // From the interface type (instantiated), get:
|
| - // the uninstantiated element, then
|
| - // the uninstantiated type, then
|
| - // the type arguments (aka the type parameters)
|
| - var tparams = type.element.type.typeArguments;
|
| - // Take the uninstantiated constructor type, and replace the type
|
| - // parameters with the inferred arguments.
|
| - var fType = baseType.substitute2(targs, tparams);
|
| - {
|
| - var rTypes = fType.normalParameterTypes;
|
| - var oTypes = fType.optionalParameterTypes;
|
| - var pTypes = new List.from(rTypes)..addAll(oTypes);
|
| - var pArgs = arguments.where((x) => x is! NamedExpression);
|
| - var pi = 0;
|
| - for (var arg in pArgs) {
|
| - if (pi >= pTypes.length) return false;
|
| - var argType = pTypes[pi];
|
| - if (!_inferExpression(arg, argType, errors)) return false;
|
| - pi++;
|
| - }
|
| - var nTypes = fType.namedParameterTypes;
|
| - for (var arg0 in arguments) {
|
| - if (arg0 is! NamedExpression) continue;
|
| - var arg = arg0 as NamedExpression;
|
| - SimpleIdentifier nameNode = arg.name.label;
|
| - String name = nameNode.name;
|
| - var argType = nTypes[name];
|
| - if (argType == null) return false;
|
| - if (!_inferExpression(arg, argType, errors)) return false;
|
| - }
|
| - }
|
| - annotateInstanceCreationExpression(e, targs);
|
| - return true;
|
| - }
|
| -
|
| - bool _inferNamedExpression(NamedExpression e, DartType t, errors) {
|
| - return _inferExpression(e.expression, t, errors);
|
| - }
|
| -
|
| - bool _inferFunctionExpression(FunctionExpression e, DartType t, errors) {
|
| - if (t is! FunctionType) return false;
|
| - var fType = t as FunctionType;
|
| - var eType = e.staticType as FunctionType;
|
| - if (eType is! FunctionType) return false;
|
| -
|
| - // We have a function literal, so we can treat the arrow type
|
| - // as non-fuzzy. Since we're not improving on parameter types
|
| - // currently, if this check fails then we cannot succeed, so
|
| - // bail out. Otherwise, we never need to check the parameter types
|
| - // again.
|
| - if (!rules.isFunctionSubTypeOf(eType, fType,
|
| - fuzzyArrows: false, ignoreReturn: true)) return false;
|
| -
|
| - // This only entered inference because of fuzzy typing.
|
| - // The function type is already specific enough, we can just
|
| - // succeed and treat it as a successful inference
|
| - if (rules.isSubTypeOf(eType.returnType, fType.returnType)) return true;
|
| -
|
| - // Fuzzy typing again, handle the void case (not caught by the previous)
|
| - if (fType.returnType.isVoid) return true;
|
| -
|
| - if (e.body is! ExpressionFunctionBody) return false;
|
| - var body = (e.body as ExpressionFunctionBody).expression;
|
| - if (!_inferExpression(body, fType.returnType, errors)) return false;
|
| -
|
| - // TODO(leafp): Try narrowing the argument types if possible
|
| - // to get better code in the function body. This requires checking
|
| - // that the body is well-typed at the more specific type.
|
| -
|
| - // At this point, we know that the parameter types are in the appropriate subtype
|
| - // relation, and we have checked that we can type the body at the appropriate return
|
| - // type, so we can are done.
|
| - annotateFunctionExpression(e, fType.returnType);
|
| - return true;
|
| - }
|
| -
|
| - bool _inferListLiteral(ListLiteral e, DartType t, errors) {
|
| - var dyn = rules.provider.dynamicType;
|
| - var listT = rules.provider.listType.substitute4([dyn]);
|
| - // List <: t (using dart rules) must be true
|
| - if (!listT.isSubtypeOf(t)) return false;
|
| - // The list literal must have no type arguments
|
| - if (e.typeArguments != null) return false;
|
| - if (t is! InterfaceType) return false;
|
| - var targs = _matchTypes(listT, t);
|
| - if (targs == null) return false;
|
| - assert(targs.length == 1);
|
| - var etype = targs[0];
|
| - assert(!etype.isDynamic);
|
| - var elements = e.elements;
|
| - var b = elements.every((e) => _inferExpression(e, etype, errors));
|
| - if (b) annotateListLiteral(e, targs);
|
| - return b;
|
| - }
|
| -
|
| - bool _inferMapLiteral(MapLiteral e, DartType t, errors) {
|
| - var dyn = rules.provider.dynamicType;
|
| - var mapT = rules.provider.mapType.substitute4([dyn, dyn]);
|
| - // Map <: t (using dart rules) must be true
|
| - if (!mapT.isSubtypeOf(t)) return false;
|
| - // The map literal must have no type arguments
|
| - if (e.typeArguments != null) return false;
|
| - if (t is! InterfaceType) return false;
|
| - var targs = _matchTypes(mapT, t);
|
| - if (targs == null) return false;
|
| - assert(targs.length == 2);
|
| - var kType = targs[0];
|
| - var vType = targs[1];
|
| - assert(!(kType.isDynamic && vType.isDynamic));
|
| - var entries = e.entries;
|
| - bool inferEntry(MapLiteralEntry entry) {
|
| - return _inferExpression(entry.key, kType, errors) &&
|
| - _inferExpression(entry.value, vType, errors);
|
| - }
|
| - var b = entries.every(inferEntry);
|
| - if (b) annotateMapLiteral(e, targs);
|
| - return b;
|
| - }
|
| }
|
|
|