Skip to content

Fix issues with binding local element archetypes [5.9] #65342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/AST/ParameterPack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,12 @@ PackType *PackType::getSingletonPackExpansion(Type param) {
}

CanPackType CanPackType::getSingletonPackExpansion(CanType param) {
return CanPackType(PackType::getSingletonPackExpansion(param));
// Note: You can't just wrap the result in CanPackType() here because
// PackExpansionType has the additional requirement that the count type
// must be a reduced shape.
return cast<PackType>(
PackType::getSingletonPackExpansion(param)
->getCanonicalType());
}

PackExpansionType *PackType::unwrapSingletonPackExpansion() const {
Expand Down
14 changes: 13 additions & 1 deletion lib/IRGen/GenPack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,7 @@ void irgen::bindOpenedElementArchetypesAtIndex(IRGenFunction &IGF,
return openablePackParams.contains(
ty->getRootGenericParam()->getCanonicalType());
};

enumerateGenericSignatureRequirements(
environment->getGenericSignature().getCanonicalSignature(),
[&](GenericRequirement requirement) {
Expand Down Expand Up @@ -996,7 +997,18 @@ void irgen::bindOpenedElementArchetypesAtIndex(IRGenFunction &IGF,
llvm::SmallVector<llvm::Value *, 2> wtables;
auto *metadata = emitTypeMetadataPackElementRef(
IGF, packType, conformances, index, MetadataState::Complete, wtables);
IGF.bindArchetype(archetype, metadata, MetadataState::Complete, wtables);

auto reqt = GenericRequirement::forMetadata(archetype);
bindGenericRequirement(IGF, reqt, metadata, MetadataState::Complete,
SubstitutionMap());

assert(conformances.size() == wtables.size());
for (unsigned i : indices(wtables)) {
auto reqt = GenericRequirement::forWitnessTable(
archetype, conformances[i].getRequirement());
bindGenericRequirement(IGF, reqt, wtables[i], MetadataState::Complete,
SubstitutionMap());
}
}
}

Expand Down
26 changes: 16 additions & 10 deletions lib/IRGen/GenProto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3753,18 +3753,24 @@ void irgen::bindGenericRequirement(IRGenFunction &IGF,
case GenericRequirement::Kind::WitnessTable:
case GenericRequirement::Kind::WitnessTablePack: {
auto proto = requirement.getProtocol();
auto conf = subs.lookupConformance(requirement.getTypeParameter(), proto);
setProtocolWitnessTableName(IGF.IGM, value, type, proto);

// FIXME: Remove this
if (conf.isPack() && isa<PackArchetypeType>(type)) {
assert(wasUnwrappedPack);
assert(conf.getPack()->getPatternConformances().size() == 1);
conf = conf.getPack()->getPatternConformances()[0];
}
if (subs) {
auto conf = subs.lookupConformance(requirement.getTypeParameter(), proto);

setProtocolWitnessTableName(IGF.IGM, value, type, proto);
auto kind = LocalTypeDataKind::forProtocolWitnessTable(conf);
IGF.setUnscopedLocalTypeData(type, kind, value);
// FIXME: Remove this
if (conf.isPack() && isa<PackArchetypeType>(type)) {
assert(wasUnwrappedPack);
assert(conf.getPack()->getPatternConformances().size() == 1);
conf = conf.getPack()->getPatternConformances()[0];
}

auto kind = LocalTypeDataKind::forProtocolWitnessTable(conf);
IGF.setUnscopedLocalTypeData(type, kind, value);
} else {
auto kind = LocalTypeDataKind::forAbstractProtocolWitnessTable(proto);
IGF.setUnscopedLocalTypeData(type, kind, value);
}
break;
}
}
Expand Down
19 changes: 19 additions & 0 deletions test/IRGen/bind_element_archetype.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %target-swift-frontend -emit-ir %s

public protocol P {}

public protocol Q {
associatedtype A: P
}

public func f<T: P>(_: T) {}

public func foo1<each T: Q>(t: repeat each T, u: repeat each T.A) {
repeat f(each u)
}

public func foo2<each T: Q, each U>(t: repeat each T, u: repeat each U)
where repeat (each U) == (each T).A, (repeat (each T, each U)): Any {
repeat f(each u)
}

25 changes: 22 additions & 3 deletions test/SILOptimizer/tuples_from_packs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,29 @@ public func makeEmptyTuple() {
return makeTuple()
}

// FIXME: This crashes in SILGen
/*public func makeOne<T>(_ t: T) -> T {
// FIXME: Useless pack_element_set/pack_element_get

// CHECK-LABEL: sil @$s17tuples_from_packs7makeOneyxxlF : $@convention(thin) <T> (@in_guaranteed T) -> @out T {
// CHECK: bb0(%0 : $*T, %1 : $*T):
// CHECK: [[PACK:%.*]] = alloc_pack $Pack{T}
// CHECK-NEXT: [[IDX:%.*]] = scalar_pack_index 0 of $Pack{T}
// CHECK-NEXT: pack_element_set %0 : $*T into [[IDX]] of [[PACK]] : $*Pack{T}
// CHECK-NEXT: [[PACK2:%.*]] = alloc_pack $Pack{T}
// CHECK-NEXT: [[BOX:%.*]] = alloc_stack $T
// CHECK-NEXT: copy_addr %1 to [init] [[BOX]] : $*T
// CHECK-NEXT: pack_element_set [[BOX]] : $*T into [[IDX]] of [[PACK2]] : $*Pack{T}
// CHECK-NEXT: [[ELT:%.*]] = pack_element_get [[IDX]] of [[PACK]] : $*Pack{T} as $*T
// CHECK-NEXT: [[ELT2:%.*]] = pack_element_get [[IDX]] of [[PACK2]] : $*Pack{T} as $*T
// CHECK-NEXT: copy_addr [[ELT2]] to [init] [[ELT]] : $*T
// CHECK-NEXT: destroy_addr [[BOX]] : $*T
// CHECK-NEXT: dealloc_stack [[BOX]] : $*T
// CHECK-NEXT: dealloc_pack [[PACK2]] : $*Pack{T}
// CHECK-NEXT: dealloc_pack [[PACK]] : $*Pack{T}
// CHECK-NEXT: [[RET:%.*]] = tuple ()
// CHECK-NEXT: return [[RET]] : $()
public func makeOne<T>(_ t: T) -> T {
return makeTuple(t)
}*/
}

// FIXME: Useless pack_element_set/pack_element_get

Expand Down