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

Side by Side Diff: pkg/analyzer/lib/src/generated/resolver.dart

Issue 2676633005: Temporarily restore ad hoc Future.then inference (Closed)
Patch Set: Address comments Created 3 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 unified diff | Download patch
« no previous file with comments | « no previous file | pkg/analyzer/lib/src/generated/static_type_analyzer.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.resolver; 5 library analyzer.src.generated.resolver;
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 5200 matching lines...) Expand 10 before | Expand all | Expand 10 after
5211 } 5211 }
5212 5212
5213 /** 5213 /**
5214 * Prepares this [ResolverVisitor] to using it for incremental resolution. 5214 * Prepares this [ResolverVisitor] to using it for incremental resolution.
5215 */ 5215 */
5216 void initForIncrementalResolution() { 5216 void initForIncrementalResolution() {
5217 _overrideManager.enterScope(); 5217 _overrideManager.enterScope();
5218 } 5218 }
5219 5219
5220 /** 5220 /**
5221 * Returns true if this method is `Future.then` or an override thereof.
5222 *
5223 * If so we will apply special typing rules in strong mode, to handle the
5224 * implicit union of `S | Future<S>`
5225 */
5226 // TODO(leafp): Eliminate this when code is switched to using FutureOr
5227 bool isFutureThen(Element element) {
5228 // If we are a method named then
5229 if (element is MethodElement && element.name == 'then') {
5230 DartType type = element.enclosingElement.type;
5231 // On Future or a subtype, then we're good.
5232 return (type.isDartAsyncFuture || isSubtypeOfFuture(type));
5233 }
5234 return false;
5235 }
5236
5237 /**
5221 * Returns true if this type is any subtype of the built in Future type. 5238 * Returns true if this type is any subtype of the built in Future type.
5222 */ 5239 */
5240 // TODO(leafp): Eliminate this when code is switched to using FutureOr
5223 bool isSubtypeOfFuture(DartType type) => 5241 bool isSubtypeOfFuture(DartType type) =>
5224 typeSystem.isSubtypeOf(type, typeProvider.futureDynamicType); 5242 typeSystem.isSubtypeOf(type, typeProvider.futureDynamicType);
5225 5243
5226 /** 5244 /**
5227 * Given a downward inference type [fnType], and the declared 5245 * Given a downward inference type [fnType], and the declared
5228 * [typeParameterList] for a function expression, determines if we can enable 5246 * [typeParameterList] for a function expression, determines if we can enable
5229 * downward inference and if so, returns the function type to use for 5247 * downward inference and if so, returns the function type to use for
5230 * inference. 5248 * inference.
5231 * 5249 *
5232 * This will return null if inference is not possible. This happens when 5250 * This will return null if inference is not possible. This happens when
(...skipping 820 matching lines...) Expand 10 before | Expand all | Expand 10 after
6053 _currentFunctionBody = node.body; 6071 _currentFunctionBody = node.body;
6054 _enclosingFunction = node.element; 6072 _enclosingFunction = node.element;
6055 _overrideManager.enterScope(); 6073 _overrideManager.enterScope();
6056 try { 6074 try {
6057 DartType functionType = InferenceContext.getType(node); 6075 DartType functionType = InferenceContext.getType(node);
6058 if (functionType is FunctionType) { 6076 if (functionType is FunctionType) {
6059 functionType = 6077 functionType =
6060 matchFunctionTypeParameters(node.typeParameters, functionType); 6078 matchFunctionTypeParameters(node.typeParameters, functionType);
6061 if (functionType is FunctionType) { 6079 if (functionType is FunctionType) {
6062 _inferFormalParameterList(node.parameters, functionType); 6080 _inferFormalParameterList(node.parameters, functionType);
6063 InferenceContext.setType( 6081 DartType returnType;
6064 node.body, _computeReturnOrYieldType(functionType.returnType)); 6082 ParameterElement parameterElement =
6083 resolutionMap.staticParameterElementForExpression(node);
6084 if (isFutureThen(parameterElement?.enclosingElement)) {
6085 var futureThenType =
6086 InferenceContext.getContext(node.parent) as FunctionType;
6087
6088 // TODO(leafp): Get rid of this once code has been updated to use
6089 // FutureOr
6090 // Introduce FutureOr<T> for backwards compatibility if it was
6091 // missing in old code.
6092 if (futureThenType.parameters.isNotEmpty) {
6093 if (!futureThenType.parameters[0].type.isDartAsyncFutureOr) {
6094 var typeParamS =
6095 futureThenType.returnType.flattenFutures(typeSystem);
6096 returnType = _createFutureOr(typeParamS);
6097 }
6098 }
6099 }
6100 returnType ??= _computeReturnOrYieldType(functionType.returnType);
6101 InferenceContext.setType(node.body, returnType);
6065 } 6102 }
6066 } 6103 }
6067 super.visitFunctionExpression(node); 6104 super.visitFunctionExpression(node);
6068 } finally { 6105 } finally {
6069 _overrideManager.exitScope(); 6106 _overrideManager.exitScope();
6070 } 6107 }
6071 } finally { 6108 } finally {
6072 _currentFunctionBody = outerFunctionBody; 6109 _currentFunctionBody = outerFunctionBody;
6073 _enclosingFunction = outerFunction; 6110 _enclosingFunction = outerFunction;
6074 } 6111 }
(...skipping 4776 matching lines...) Expand 10 before | Expand all | Expand 10 after
10851 return null; 10888 return null;
10852 } 10889 }
10853 if (identical(node.staticElement, variable)) { 10890 if (identical(node.staticElement, variable)) {
10854 if (node.inSetterContext()) { 10891 if (node.inSetterContext()) {
10855 result = true; 10892 result = true;
10856 } 10893 }
10857 } 10894 }
10858 return null; 10895 return null;
10859 } 10896 }
10860 } 10897 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/lib/src/generated/static_type_analyzer.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698