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

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

Issue 1678313002: fix part of #25200, reject non-generic function subtype of generic function (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 unified diff | Download patch
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/visitor.dart'; 10 import 'package:analyzer/dart/ast/visitor.dart';
(...skipping 7275 matching lines...) Expand 10 before | Expand all | Expand 10 after
7286 if (declaration != null) { 7286 if (declaration != null) {
7287 Element element = declaration.element; 7287 Element element = declaration.element;
7288 if (element is ExecutableElement) { 7288 if (element is ExecutableElement) {
7289 _enclosingFunction = element; 7289 _enclosingFunction = element;
7290 } 7290 }
7291 } 7291 }
7292 _overrideManager.enterScope(); 7292 _overrideManager.enterScope();
7293 } 7293 }
7294 7294
7295 /** 7295 /**
7296 * Given a downward inference type [fnType], and the declared
7297 * [typeParameterList] for a function expression, determines if we can enable
7298 * downward inference and if so, returns the function type to use for
7299 * inference.
7300 *
7301 * This will return null if inference is not possible. This happens when
7302 * there is no way we can find a subtype of the function type, given the
7303 * provided type parameter list.
7304 */
7305 FunctionType matchFunctionTypeParameters(
Jennifer Messerly 2016/02/08 21:54:27 I feel like there's got to be a better way to fact
7306 TypeParameterList typeParameterList, FunctionType fnType) {
7307 if (typeParameterList == null) {
7308 if (fnType.typeFormals.isEmpty) {
7309 return fnType;
7310 }
7311
7312 // A non-generic function cannot be a subtype of a generic one.
7313 return null;
7314 }
7315
7316 NodeList<TypeParameter> typeParameters = typeParameterList.typeParameters;
7317 if (fnType.typeFormals.isEmpty) {
7318 // TODO(jmesserly): this is a legal subtype. We don't currently infer
7319 // here, but we could. This is similar to
7320 // StrongTypeSystemImpl.inferFunctionTypeInstantiation, but we don't
7321 // have the FunctionType yet for the current node, so it's not quite
7322 // straightforward to apply.
7323 return null;
7324 }
7325
7326 if (fnType.typeFormals.length != typeParameters.length) {
7327 // A subtype cannot have different number of type formals.
7328 return null;
7329 }
7330
7331 // Same number of type formals. Instantiate the function type so its
7332 // parameter and return type are in terms of the surrounding context.
7333 return fnType.instantiate(
Jennifer Messerly 2016/02/08 21:54:27 This is a case like: // declare generic funct
7334 typeParameters.map((t) => t.name.staticElement.type).toList());
7335 }
7336
7337 /**
7296 * If it is appropriate to do so, override the current type of the static and propagated elements 7338 * If it is appropriate to do so, override the current type of the static and propagated elements
7297 * associated with the given expression with the given type. Generally speakin g, it is appropriate 7339 * associated with the given expression with the given type. Generally speakin g, it is appropriate
7298 * if the given type is more specific than the current type. 7340 * if the given type is more specific than the current type.
7299 * 7341 *
7300 * @param expression the expression used to access the static and propagated e lements whose types 7342 * @param expression the expression used to access the static and propagated e lements whose types
7301 * might be overridden 7343 * might be overridden
7302 * @param potentialType the potential type of the elements 7344 * @param potentialType the potential type of the elements
7303 * @param allowPrecisionLoss see @{code overrideVariable} docs 7345 * @param allowPrecisionLoss see @{code overrideVariable} docs
7304 */ 7346 */
7305 void overrideExpression(Expression expression, DartType potentialType, 7347 void overrideExpression(Expression expression, DartType potentialType,
(...skipping 763 matching lines...) Expand 10 before | Expand all | Expand 10 after
8069 8111
8070 @override 8112 @override
8071 Object visitFunctionExpression(FunctionExpression node) { 8113 Object visitFunctionExpression(FunctionExpression node) {
8072 ExecutableElement outerFunction = _enclosingFunction; 8114 ExecutableElement outerFunction = _enclosingFunction;
8073 try { 8115 try {
8074 _enclosingFunction = node.element; 8116 _enclosingFunction = node.element;
8075 _overrideManager.enterScope(); 8117 _overrideManager.enterScope();
8076 try { 8118 try {
8077 DartType functionType = InferenceContext.getType(node); 8119 DartType functionType = InferenceContext.getType(node);
8078 if (functionType is FunctionType) { 8120 if (functionType is FunctionType) {
8079 _inferFormalParameterList(node.parameters, functionType); 8121 functionType =
8080 DartType returnType = _computeReturnOrYieldType( 8122 matchFunctionTypeParameters(node.typeParameters, functionType);
8081 functionType.returnType, 8123 if (functionType is FunctionType) {
8082 _enclosingFunction.isGenerator, 8124 _inferFormalParameterList(node.parameters, functionType);
8083 _enclosingFunction.isAsynchronous); 8125 DartType returnType = _computeReturnOrYieldType(
8084 InferenceContext.setType(node.body, returnType); 8126 functionType.returnType,
8127 _enclosingFunction.isGenerator,
8128 _enclosingFunction.isAsynchronous);
8129 InferenceContext.setType(node.body, returnType);
8130 }
8085 } 8131 }
8086 super.visitFunctionExpression(node); 8132 super.visitFunctionExpression(node);
8087 } finally { 8133 } finally {
8088 _overrideManager.exitScope(); 8134 _overrideManager.exitScope();
8089 } 8135 }
8090 } finally { 8136 } finally {
8091 _enclosingFunction = outerFunction; 8137 _enclosingFunction = outerFunction;
8092 } 8138 }
8093 return null; 8139 return null;
8094 } 8140 }
(...skipping 4585 matching lines...) Expand 10 before | Expand all | Expand 10 after
12680 nonFields.add(node); 12726 nonFields.add(node);
12681 return null; 12727 return null;
12682 } 12728 }
12683 12729
12684 @override 12730 @override
12685 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); 12731 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this);
12686 12732
12687 @override 12733 @override
12688 Object visitWithClause(WithClause node) => null; 12734 Object visitWithClause(WithClause node) => null;
12689 } 12735 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698