Skip to content

Commit d779a8f

Browse files
committed
Count lowered parameters for a formal parameter using just the
AbstractionPattern. The old logic was wrong for vanishing tuples: a non-tuple substituted type might correspond to multiple lowered parameters if it's the result of substituting into a vanishing tuple that also contains empty pack expansions. Test to follow later in this PR.
1 parent ecfc98d commit d779a8f

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

lib/SILGen/SILGenApply.cpp

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,43 +2191,35 @@ ManagedValue SILGenFunction::emitStringLiteral(SILLocation loc,
21912191

21922192
/// Count the number of SILParameterInfos that are needed in order to
21932193
/// pass the given argument.
2194-
static unsigned getFlattenedValueCount(AbstractionPattern origType,
2195-
CanType substType) {
2196-
// The count is always 1 unless the substituted type is a tuple.
2197-
auto substTuple = dyn_cast<TupleType>(substType);
2198-
if (!substTuple)
2199-
return 1;
2200-
2201-
// If the original type is opaque, the count is 1 anyway.
2202-
if (origType.isTypeParameter())
2194+
static unsigned getFlattenedValueCount(AbstractionPattern origType) {
2195+
// The count is always 1 unless the original type is a tuple.
2196+
if (!origType.isTuple())
22032197
return 1;
22042198

2205-
// Otherwise, add up the elements.
2199+
// Add up the elements.
22062200
unsigned count = 0;
2207-
origType.forEachTupleElement(substTuple, [&](TupleElementGenerator &elt) {
2208-
// Expansion components turn into a single parameter.
2209-
if (elt.isOrigPackExpansion()) {
2201+
for (auto elt : origType.getTupleElementTypes()) {
2202+
// Expansion components turn into a single pack parameter.
2203+
if (elt.isPackExpansion()) {
22102204
count++;
22112205

22122206
// Recursively expand scalar components.
22132207
} else {
2214-
count += getFlattenedValueCount(elt.getOrigType(),
2215-
elt.getSubstTypes()[0]);
2208+
count += getFlattenedValueCount(elt);
22162209
}
2217-
});
2210+
}
22182211
return count;
22192212
}
22202213

22212214
/// Count the number of SILParameterInfos that are needed in order to
22222215
/// pass the given argument.
22232216
static unsigned getFlattenedValueCount(AbstractionPattern origType,
2224-
CanType substType,
22252217
ImportAsMemberStatus foreignSelf) {
22262218
// C functions imported as static methods don't consume any real arguments.
22272219
if (foreignSelf.isStatic())
22282220
return 0;
22292221

2230-
return getFlattenedValueCount(origType, substType);
2222+
return getFlattenedValueCount(origType);
22312223
}
22322224

22332225
namespace {
@@ -3195,7 +3187,6 @@ class ArgEmitter {
31953187
auto defArg = std::move(arg).asKnownDefaultArg();
31963188

31973189
auto numParams = getFlattenedValueCount(origParamType,
3198-
substParamType,
31993190
ImportAsMemberStatus());
32003191
DelayedArguments.emplace_back(defArg,
32013192
defArg->getDefaultArgsOwner(),
@@ -3576,7 +3567,7 @@ class ArgEmitter {
35763567

35773568
void emitExpandedBorrowed(Expr *arg, AbstractionPattern origParamType) {
35783569
CanType substArgType = arg->getType()->getCanonicalType();
3579-
auto count = getFlattenedValueCount(origParamType, substArgType);
3570+
auto count = getFlattenedValueCount(origParamType);
35803571
auto claimedParams = claimNextParameters(count);
35813572

35823573
SILType loweredSubstArgType = SGF.getLoweredType(substArgType);
@@ -3626,7 +3617,7 @@ class ArgEmitter {
36263617

36273618
void emitExpandedConsumed(Expr *arg, AbstractionPattern origParamType) {
36283619
CanType substArgType = arg->getType()->getCanonicalType();
3629-
auto count = getFlattenedValueCount(origParamType, substArgType);
3620+
auto count = getFlattenedValueCount(origParamType);
36303621
auto claimedParams = claimNextParameters(count);
36313622

36323623
SILType loweredSubstArgType = SGF.getLoweredType(substArgType);
@@ -4432,10 +4423,8 @@ struct ParamLowering {
44324423
if (substParam.isInOut()) {
44334424
count += 1;
44344425
} else {
4435-
count += getFlattenedValueCount(
4436-
origParamType,
4437-
substParam.getParameterType()->getCanonicalType(),
4438-
ImportAsMemberStatus());
4426+
count += getFlattenedValueCount(origParamType,
4427+
ImportAsMemberStatus());
44394428
}
44404429
}
44414430
}

0 commit comments

Comments
 (0)