Skip to content

Commit 758bce9

Browse files
committed
Fix variadic arguments in keypath subscripts
1 parent d2e1f09 commit 758bce9

File tree

3 files changed

+83
-7
lines changed

3 files changed

+83
-7
lines changed

lib/SIL/SILInstructions.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2242,6 +2242,7 @@ KeyPathPattern::get(SILModule &M, CanGenericSignature signature,
22422242
case KeyPathPatternComponent::Kind::GettableProperty:
22432243
case KeyPathPatternComponent::Kind::SettableProperty:
22442244
for (auto &index : component.getSubscriptIndices()) {
2245+
index.FormalType.dump();
22452246
maxOperandNo = std::max(maxOperandNo, (int)index.Operand);
22462247
}
22472248
}

lib/SILGen/SILGenExpr.cpp

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252

5353
#include "swift/AST/DiagnosticsSIL.h"
5454

55+
#include <iostream>
56+
5557
using namespace swift;
5658
using namespace Lowering;
5759

@@ -3067,6 +3069,11 @@ getOrCreateKeyPathEqualsAndHash(SILGenModule &SGM,
30673069
hashable);
30683070
auto formalCanTy = formalTy->getCanonicalType(genericSig);
30693071

3072+
formalTy.dump();
3073+
index.FormalType.dump();
3074+
equatableProtocol->dump();
3075+
hashable.dump();
3076+
30703077
// Get the Equatable conformance from the Hashable conformance.
30713078
auto equatable = hashable.getAssociatedConformance(formalTy,
30723079
GenericTypeParamType::get(0, 0, C),
@@ -3342,23 +3349,71 @@ lowerKeyPathSubscriptIndexTypes(
33423349
};
33433350

33443351
static void
3345-
lowerKeyPathSubscriptIndexPatterns(
3352+
lowerKeyPathSubscriptEqualsIndexPatterns(
33463353
SmallVectorImpl<KeyPathPatternComponent::Index> &indexPatterns,
33473354
ArrayRef<IndexTypePair> indexTypes,
33483355
ArrayRef<ProtocolConformanceRef> indexHashables,
3349-
unsigned &baseOperand) {
3356+
unsigned baseOperand,
3357+
SILGenModule &SGM,
3358+
ResilienceExpansion expansion) {
33503359
for (unsigned i : indices(indexTypes)) {
33513360
CanType formalTy;
33523361
SILType loweredTy;
33533362
std::tie(formalTy, loweredTy) = indexTypes[i];
33543363
auto hashable = indexHashables[i].mapConformanceOutOfContext();
3364+
// TODO: this only works if varaidic parameters must come last
3365+
// We have an array of variadic parameters...
3366+
if (!hashable.getConcrete()->getType()->isEqual(formalTy)) {
3367+
// Add all the hashable types
3368+
auto arrayTy = cast<BoundGenericStructType>(formalTy.getPointer());
3369+
auto elementTy = arrayTy->getGenericArgs()[0];
3370+
// for (unsigned hashableIndex = i; hashableIndex < indexHashables.size(); ++hashableIndex);
3371+
3372+
assert(indexTypes.size() == indexHashables.size());
3373+
3374+
assert(hashable.getConcrete()->getType()->isEqual(elementTy));
3375+
auto newLoweredTy = SGM.Types.getLoweredType(
3376+
AbstractionPattern::getOpaque(), elementTy,
3377+
TypeExpansionContext::noOpaqueTypeArchetypesSubstitution(expansion));
3378+
newLoweredTy = newLoweredTy.mapTypeOutOfContext();
3379+
auto newFormalTy = elementTy->mapTypeOutOfContext()
3380+
->getCanonicalType();
3381+
3382+
std::cout << "lowered variadic arg formal type: " << std::endl;
3383+
newFormalTy.dump();
3384+
loweredTy.dump();
3385+
3386+
indexPatterns.push_back({baseOperand++, newFormalTy,
3387+
newLoweredTy, hashable});
3388+
break;
3389+
}
3390+
// hashable.dump(); // Int
3391+
// hashable.getConcrete()->getType().dump(); // Int
3392+
// formalTy.dump(); // Array<Int>
3393+
// loweredTy.dump(); // $Array<Int>
33553394
assert(hashable.isAbstract() ||
33563395
hashable.getConcrete()->getType()->isEqual(formalTy));
33573396

33583397
indexPatterns.push_back({baseOperand++, formalTy, loweredTy, hashable});
33593398
}
33603399
};
33613400

3401+
static void
3402+
lowerKeyPathSubscriptIndexPatterns(
3403+
SmallVectorImpl<KeyPathPatternComponent::Index> &indexPatterns,
3404+
ArrayRef<IndexTypePair> indexTypes,
3405+
ArrayRef<ProtocolConformanceRef> indexHashables,
3406+
unsigned &baseOperand) {
3407+
for (unsigned i : indices(indexTypes)) {
3408+
CanType formalTy;
3409+
SILType loweredTy;
3410+
std::tie(formalTy, loweredTy) = indexTypes[i];
3411+
auto hashable = indexHashables[i].mapConformanceOutOfContext();
3412+
3413+
indexPatterns.push_back({baseOperand++, formalTy, loweredTy, hashable});
3414+
}
3415+
};
3416+
33623417
KeyPathPatternComponent
33633418
SILGenModule::emitKeyPathComponentForDecl(SILLocation loc,
33643419
GenericEnvironment *genericEnv,
@@ -3500,18 +3555,22 @@ SILGenModule::emitKeyPathComponentForDecl(SILLocation loc,
35003555
expansion,
35013556
needsGenericContext);
35023557

3503-
SmallVector<KeyPathPatternComponent::Index, 4> indexPatterns;
3558+
SmallVector<KeyPathPatternComponent::Index, 4> equalsIndexPatterns;
3559+
SmallVector<KeyPathPatternComponent::Index, 4> xIndexPatterns;
35043560
SILFunction *indexEquals = nullptr, *indexHash = nullptr;
35053561
// Property descriptors get their index information from the client.
35063562
if (!forPropertyDescriptor) {
3507-
lowerKeyPathSubscriptIndexPatterns(indexPatterns,
3563+
lowerKeyPathSubscriptEqualsIndexPatterns(equalsIndexPatterns,
3564+
indexTypes, indexHashables,
3565+
baseOperand, *this, expansion);
3566+
lowerKeyPathSubscriptIndexPatterns(xIndexPatterns,
35083567
indexTypes, indexHashables,
35093568
baseOperand);
35103569

35113570
getOrCreateKeyPathEqualsAndHash(*this, loc,
35123571
needsGenericContext ? genericEnv : nullptr,
35133572
expansion,
3514-
indexPatterns,
3573+
equalsIndexPatterns,
35153574
indexEquals, indexHash);
35163575
}
35173576

@@ -3523,7 +3582,7 @@ SILGenModule::emitKeyPathComponentForDecl(SILLocation loc,
35233582
indexTypes,
35243583
baseTy, componentTy);
35253584

3526-
auto indexPatternsCopy = getASTContext().AllocateCopy(indexPatterns);
3585+
auto indexPatternsCopy = getASTContext().AllocateCopy(xIndexPatterns);
35273586
if (isSettableInComponent()) {
35283587
auto setter = getOrCreateKeyPathSetter(*this, loc,
35293588
decl, subs,
@@ -3540,6 +3599,13 @@ SILGenModule::emitKeyPathComponentForDecl(SILLocation loc,
35403599
externalSubs,
35413600
componentTy);
35423601
} else {
3602+
for (auto &i : indexPatternsCopy) {
3603+
std::cout << "formal type: " << std::endl;
3604+
i.FormalType.dump();
3605+
std::cout << "lowered type: " << std::endl;
3606+
i.LoweredType.dump();
3607+
std::cout << std::endl;
3608+
}
35433609
return KeyPathPatternComponent::forComputedGettableProperty(id,
35443610
getter,
35453611
indexPatternsCopy,
@@ -3581,7 +3647,10 @@ RValue RValueEmitter::visitKeyPathExpr(KeyPathExpr *E, SGFContext C) {
35813647
case KeyPathExpr::Component::Kind::Property:
35823648
case KeyPathExpr::Component::Kind::Subscript: {
35833649
auto decl = cast<AbstractStorageDecl>(component.getDeclRef().getDecl());
3584-
3650+
3651+
decl->dump();
3652+
component.getIndexExpr()->dump();
3653+
35853654
unsigned numOperands = operands.size();
35863655
loweredComponents.push_back(
35873656
SGF.SGM.emitKeyPathComponentForDecl(SILLocation(E),
@@ -3604,6 +3673,7 @@ RValue RValueEmitter::visitKeyPathExpr(KeyPathExpr *E, SGFContext C) {
36043673
component.getIndexExpr());
36053674

36063675
for (auto &arg : loweredArgs) {
3676+
arg.dump();
36073677
operands.push_back(arg.forward(SGF));
36083678
}
36093679

@@ -3674,6 +3744,9 @@ RValue RValueEmitter::visitKeyPathExpr(KeyPathExpr *E, SGFContext C) {
36743744
rootTy, baseTy,
36753745
loweredComponents,
36763746
objcString);
3747+
3748+
std::cout << "[pattern] num operands: " << pattern->getNumOperands() << std::endl;
3749+
std::cout << "[operand] num operands: " << operands.size() << std::endl;
36773750
auto keyPath = SGF.B.createKeyPath(SILLocation(E), pattern,
36783751
needsGenericContext
36793752
? SGF.F.getForwardingSubstitutionMap()

lib/Sema/CSApply.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4589,6 +4589,8 @@ namespace {
45894589
auto fnType = overload.openedType->castTo<FunctionType>();
45904590
SmallVector<Identifier, 4> newLabels;
45914591
for (auto &param : fnType->getParams()) {
4592+
param.getParameterType().dump();
4593+
45924594
newLabels.push_back(param.getLabel());
45934595

45944596
auto indexType = simplifyType(param.getPlainType());

0 commit comments

Comments
 (0)