OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 analyzer.src.generated.static_type_analyzer; | 5 library analyzer.src.generated.static_type_analyzer; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
10 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; | 10 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; |
(...skipping 1930 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1941 | 1941 |
1942 List<ParameterElement> params = <ParameterElement>[]; | 1942 List<ParameterElement> params = <ParameterElement>[]; |
1943 List<DartType> argTypes = <DartType>[]; | 1943 List<DartType> argTypes = <DartType>[]; |
1944 for (int i = 0, length = rawParameters.length; i < length; i++) { | 1944 for (int i = 0, length = rawParameters.length; i < length; i++) { |
1945 ParameterElement parameter = rawParameters[i]; | 1945 ParameterElement parameter = rawParameters[i]; |
1946 if (parameter != null) { | 1946 if (parameter != null) { |
1947 params.add(parameter); | 1947 params.add(parameter); |
1948 argTypes.add(argumentList.arguments[i].staticType); | 1948 argTypes.add(argumentList.arguments[i].staticType); |
1949 } | 1949 } |
1950 } | 1950 } |
1951 | |
1952 // TODO(leafp): remove this again after code has been updated to | |
1953 // use FutureOr on classes that implement Future | |
1954 // Special case Future<T>.then upwards inference. It has signature: | |
1955 // | |
1956 // <S>(T -> (S | Future<S>)) -> Future<S> | |
1957 // | |
1958 // Based on the first argument type, we'll pick one of these signatures: | |
1959 // | |
1960 // <S>(T -> S) -> Future<S> | |
1961 // <S>(T -> Future<S>) -> Future<S> | |
1962 // | |
1963 // ... and finish the inference using that. | |
1964 if (argTypes.isNotEmpty && _resolver.isFutureThen(fnType.element)) { | |
1965 var firstArgType = argTypes[0]; | |
1966 var firstParamType = params[0].type; | |
1967 if (firstArgType is FunctionType && | |
1968 firstParamType is FunctionType && | |
1969 !firstParamType.returnType.isDartAsyncFutureOr) { | |
1970 var argReturnType = firstArgType.returnType; | |
1971 // Skip the inference if we have the top type. It can only lead to | |
1972 // worse inference. For example, this happens when the lambda returns | |
1973 // S or Future<S> in a conditional. | |
1974 if (!argReturnType.isObject && !argReturnType.isDynamic) { | |
1975 DartType paramReturnType = _typeProvider.futureOrType | |
1976 .instantiate([fnType.typeFormals[0].type]); | |
1977 | |
1978 // Adjust the expected parameter type to have this return type. | |
1979 var function = new FunctionElementImpl(firstParamType.name, -1) | |
1980 ..isSynthetic = true | |
1981 ..shareParameters(firstParamType.parameters.toList()) | |
1982 ..returnType = paramReturnType; | |
1983 function.type = new FunctionTypeImpl(function); | |
1984 // Use this as the expected 1st parameter type. | |
1985 params[0] = new ParameterElementImpl.synthetic( | |
1986 params[0].name, function.type, params[0].parameterKind); | |
1987 } | |
1988 } | |
1989 } | |
1990 | |
1991 return ts.inferGenericFunctionOrType( | 1951 return ts.inferGenericFunctionOrType( |
1992 fnType, params, argTypes, InferenceContext.getContext(node), | 1952 fnType, params, argTypes, InferenceContext.getContext(node), |
1993 errorReporter: _resolver.errorReporter, errorNode: errorNode); | 1953 errorReporter: _resolver.errorReporter, errorNode: errorNode); |
1994 } | 1954 } |
1995 return null; | 1955 return null; |
1996 } | 1956 } |
1997 | 1957 |
1998 /** | 1958 /** |
1999 * Given an instance creation of a possibly generic type, infer the type | 1959 * Given an instance creation of a possibly generic type, infer the type |
2000 * arguments using the current context type as well as the argument types. | 1960 * arguments using the current context type as well as the argument types. |
(...skipping 445 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2446 } | 2406 } |
2447 // merge types | 2407 // merge types |
2448 if (result == null) { | 2408 if (result == null) { |
2449 result = type; | 2409 result = type; |
2450 } else { | 2410 } else { |
2451 result = _typeSystem.getLeastUpperBound(result, type); | 2411 result = _typeSystem.getLeastUpperBound(result, type); |
2452 } | 2412 } |
2453 return null; | 2413 return null; |
2454 } | 2414 } |
2455 } | 2415 } |
OLD | NEW |