Skip to content

Commit e63cea6

Browse files
stereotype441Commit Bot
authored and
Commit Bot
committed
Unify named and unnamed argument handling logic for invocations.
In order to support dart-lang/language#731 (improved inference for fold etc.) I'm going to need to add logic to _inferInvocation to postpone type analysis of arguments that are function expressions. To avoid having to code up this logic twice, it will be helpful to have both named and unnamed arguments handled by the same chunk of code. In particular, this change unifies the computation of inferredFormalType, the recursive call to inferExpression, the logic for hoisting, and the update of the local variables identicalInfo, formalTypes, and actualTypes. We pay a small price by having to have multiple `if (isExpression)` checks, but these should be very fast. Change-Id: I095a7eac84237eeb878cc3dd86e76a6a871f31d5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/241041 Reviewed-by: Chloe Stefantsova <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 28654b9 commit e63cea6

File tree

1 file changed

+56
-74
lines changed

1 file changed

+56
-74
lines changed

pkg/front_end/lib/src/fasta/type_inference/type_inferrer.dart

Lines changed: 56 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,91 +2408,73 @@ class TypeInferrerImpl implements TypeInferrer {
24082408
argument is Expression || argument is NamedExpression,
24092409
"Expected the argument to be either an Expression "
24102410
"or a NamedExpression, got '${argument.runtimeType}'.");
2411-
if (argument is Expression) {
2412-
int index = positionalIndex++;
2413-
DartType formalType = getPositionalParameterType(calleeType, index);
2414-
DartType inferredFormalType = substitution != null
2415-
? substitution.substituteType(formalType)
2416-
: formalType;
2417-
DartType inferredType;
2411+
int index;
2412+
DartType formalType;
2413+
Expression argumentExpression;
2414+
bool isExpression = argument is Expression;
2415+
if (isExpression) {
2416+
index = positionalIndex++;
2417+
formalType = getPositionalParameterType(calleeType, index);
2418+
argumentExpression = arguments.positional[index];
2419+
} else {
2420+
index = namedIndex++;
2421+
NamedExpression namedArgument = arguments.named[index];
2422+
formalType = getNamedParameterType(calleeType, namedArgument.name);
2423+
argumentExpression = namedArgument.value;
2424+
}
2425+
DartType inferredFormalType = substitution != null
2426+
? substitution.substituteType(formalType)
2427+
: formalType;
2428+
if (isExpression) {
24182429
if (isImplicitExtensionMember && index == 0) {
24192430
assert(
24202431
receiverType != null,
24212432
"No receiver type provided for implicit extension member "
24222433
"invocation.");
24232434
continue;
2424-
} else {
2425-
if (isSpecialCasedBinaryOperator) {
2426-
inferredFormalType = typeSchemaEnvironment
2427-
.getContextTypeOfSpecialCasedBinaryOperator(
2428-
typeContext, receiverType!, inferredFormalType,
2429-
isNonNullableByDefault: isNonNullableByDefault);
2430-
} else if (isSpecialCasedTernaryOperator) {
2431-
inferredFormalType = typeSchemaEnvironment
2432-
.getContextTypeOfSpecialCasedTernaryOperator(
2433-
typeContext, receiverType!, inferredFormalType,
2434-
isNonNullableByDefault: isNonNullableByDefault);
2435-
}
2436-
ExpressionInferenceResult result = inferExpression(
2437-
arguments.positional[index],
2438-
isNonNullableByDefault
2439-
? inferredFormalType
2440-
: legacyErasure(inferredFormalType),
2441-
inferenceNeeded ||
2442-
isSpecialCasedBinaryOperator ||
2443-
isSpecialCasedTernaryOperator ||
2444-
typeChecksNeeded);
2445-
inferredType = identical(result.inferredType, noInferredType) ||
2446-
isNonNullableByDefault
2447-
? result.inferredType
2448-
: legacyErasure(result.inferredType);
2449-
if (localHoistedExpressions != null &&
2450-
evaluationOrderIndex >= hoistingEndIndex) {
2451-
hoistedExpressions = null;
2452-
}
2453-
Expression expression =
2454-
_hoist(result.expression, inferredType, hoistedExpressions);
2455-
identicalInfo
2456-
?.add(flowAnalysis.equalityOperand_end(expression, inferredType));
2457-
arguments.positional[index] = expression..parent = arguments;
24582435
}
2459-
if (useFormalAndActualTypes) {
2460-
formalTypes!.add(formalType);
2461-
actualTypes!.add(inferredType);
2436+
if (isSpecialCasedBinaryOperator) {
2437+
inferredFormalType =
2438+
typeSchemaEnvironment.getContextTypeOfSpecialCasedBinaryOperator(
2439+
typeContext, receiverType!, inferredFormalType,
2440+
isNonNullableByDefault: isNonNullableByDefault);
2441+
} else if (isSpecialCasedTernaryOperator) {
2442+
inferredFormalType =
2443+
typeSchemaEnvironment.getContextTypeOfSpecialCasedTernaryOperator(
2444+
typeContext, receiverType!, inferredFormalType,
2445+
isNonNullableByDefault: isNonNullableByDefault);
24622446
}
2447+
}
2448+
ExpressionInferenceResult result = inferExpression(
2449+
argumentExpression,
2450+
isNonNullableByDefault
2451+
? inferredFormalType
2452+
: legacyErasure(inferredFormalType),
2453+
inferenceNeeded ||
2454+
isSpecialCasedBinaryOperator ||
2455+
isSpecialCasedTernaryOperator ||
2456+
typeChecksNeeded);
2457+
DartType inferredType = identical(result.inferredType, noInferredType) ||
2458+
isNonNullableByDefault
2459+
? result.inferredType
2460+
: legacyErasure(result.inferredType);
2461+
if (localHoistedExpressions != null &&
2462+
evaluationOrderIndex >= hoistingEndIndex) {
2463+
hoistedExpressions = null;
2464+
}
2465+
Expression expression =
2466+
_hoist(result.expression, inferredType, hoistedExpressions);
2467+
identicalInfo
2468+
?.add(flowAnalysis.equalityOperand_end(expression, inferredType));
2469+
if (isExpression) {
2470+
arguments.positional[index] = expression..parent = arguments;
24632471
} else {
2464-
assert(argument is NamedExpression);
2465-
int index = namedIndex++;
24662472
NamedExpression namedArgument = arguments.named[index];
2467-
DartType formalType =
2468-
getNamedParameterType(calleeType, namedArgument.name);
2469-
DartType inferredFormalType = substitution != null
2470-
? substitution.substituteType(formalType)
2471-
: formalType;
2472-
ExpressionInferenceResult result = inferExpression(
2473-
namedArgument.value,
2474-
isNonNullableByDefault
2475-
? inferredFormalType
2476-
: legacyErasure(inferredFormalType),
2477-
inferenceNeeded ||
2478-
isSpecialCasedBinaryOperator ||
2479-
typeChecksNeeded);
2480-
DartType inferredType =
2481-
identical(result.inferredType, noInferredType) ||
2482-
isNonNullableByDefault
2483-
? result.inferredType
2484-
: legacyErasure(result.inferredType);
2485-
if (localHoistedExpressions != null &&
2486-
evaluationOrderIndex >= hoistingEndIndex) {
2487-
hoistedExpressions = null;
2488-
}
2489-
Expression expression =
2490-
_hoist(result.expression, inferredType, hoistedExpressions);
24912473
namedArgument.value = expression..parent = namedArgument;
2492-
if (useFormalAndActualTypes) {
2493-
formalTypes!.add(formalType);
2494-
actualTypes!.add(inferredType);
2495-
}
2474+
}
2475+
if (useFormalAndActualTypes) {
2476+
formalTypes!.add(formalType);
2477+
actualTypes!.add(inferredType);
24962478
}
24972479
}
24982480
if (identicalInfo != null) {

0 commit comments

Comments
 (0)