Skip to content

[6.0] Fix two bugs with opaque return types -vs- parameter packs #72853

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
3 changes: 3 additions & 0 deletions docs/ABI/Mangling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,7 @@ Property behaviors are implemented using private protocol conformances.

any-protocol-conformance ::= concrete-protocol-conformance
any-protocol-conformance ::= dependent-protocol-conformance
any-protocol-conformance ::= pack-protocol-conformance

any-protocol-conformance-list ::= any-protocol-conformance '_' any-protocol-conformance-list
any-protocol-conformance-list ::= empty-list
Expand All @@ -980,6 +981,8 @@ Property behaviors are implemented using private protocol conformances.
dependent-associated-conformance ::= type protocol
dependent-protocol-conformance ::= dependent-protocol-conformance opaque-type 'HO'

pack-protocol-conformance ::= any-protocol-conformance-list 'HX'

A compact representation used to represent mangled protocol conformance witness
arguments at runtime. The ``module`` is only specified for conformances that
are "retroactive", meaning that the context in which the conformance is defined
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/ASTMangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,9 @@ class ASTMangler : public Mangler {
void appendConcreteProtocolConformance(
const ProtocolConformance *conformance,
GenericSignature sig);
void appendPackProtocolConformance(
const PackConformance *conformance,
GenericSignature sig);
void appendDependentProtocolConformance(const ConformancePath &path,
GenericSignature sig);
void appendOpParamForLayoutConstraint(LayoutConstraint Layout);
Expand Down
1 change: 1 addition & 0 deletions include/swift/Demangling/DemangleNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ NODE(ClangType)
CONTEXT_NODE(Class)
NODE(ClassMetadataBaseOffset)
NODE(ConcreteProtocolConformance)
NODE(PackProtocolConformance)
NODE(ConformanceAttachedMacroExpansion)
CONTEXT_NODE(Constructor)
NODE(CoroutineContinuationPrototype)
Expand Down
1 change: 1 addition & 0 deletions include/swift/Demangling/Demangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ class Demangler : public NodeFactory {
NodePointer demangleRetroactiveProtocolConformanceRef();
NodePointer popAnyProtocolConformance();
NodePointer demangleConcreteProtocolConformance();
NodePointer demanglePackProtocolConformance();
NodePointer popDependentProtocolConformance();
NodePointer demangleDependentProtocolConformanceRoot();
NodePointer demangleDependentProtocolConformanceInherited();
Expand Down
64 changes: 57 additions & 7 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "swift/AST/MacroDiscriminatorContext.h"
#include "swift/AST/Module.h"
#include "swift/AST/Ownership.h"
#include "swift/AST/PackConformance.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/PrettyStackTrace.h"
#include "swift/AST/ProtocolConformance.h"
Expand Down Expand Up @@ -1874,8 +1875,22 @@ static bool isRetroactiveConformance(const RootProtocolConformance *root) {
/// Determine whether the given protocol conformance contains a retroactive
/// protocol conformance anywhere in it.
static bool containsRetroactiveConformance(
const ProtocolConformance *conformance,
ProtocolConformanceRef conformanceRef,
ModuleDecl *module) {
if (!conformanceRef.isPack() && !conformanceRef.isConcrete())
return false;

if (conformanceRef.isPack()) {
for (auto patternConf : conformanceRef.getPack()->getPatternConformances()) {
if (containsRetroactiveConformance(patternConf, module))
return true;
}

return false;
}

auto *conformance = conformanceRef.getConcrete();

// If the root conformance is retroactive, it's retroactive.
const RootProtocolConformance *rootConformance =
conformance->getRootConformance();
Expand All @@ -1897,8 +1912,7 @@ static bool containsRetroactiveConformance(
// for indexing purposes.
continue;
}
if (conformance.isConcrete() &&
containsRetroactiveConformance(conformance.getConcrete(), module)) {
if (containsRetroactiveConformance(conformance, module)) {
return true;
}
}
Expand All @@ -1924,14 +1938,18 @@ void ASTMangler::appendRetroactiveConformances(SubstitutionMap subMap,
};

// Ignore abstract conformances.
if (!conformance.isConcrete())
if (!conformance.isConcrete() && !conformance.isPack())
continue;

// Skip non-retroactive conformances.
if (!containsRetroactiveConformance(conformance.getConcrete(), fromModule))
if (!containsRetroactiveConformance(conformance, fromModule))
continue;

appendConcreteProtocolConformance(conformance.getConcrete(), sig);
if (conformance.isConcrete())
appendConcreteProtocolConformance(conformance.getConcrete(), sig);
else
appendPackProtocolConformance(conformance.getPack(), sig);

appendOperator("g", Index(numProtocolRequirements));
}
}
Expand Down Expand Up @@ -4143,8 +4161,14 @@ void ASTMangler::appendAnyProtocolConformance(
appendDependentProtocolConformance(conformancePath, opaqueSignature);
appendType(conformingType, genericSig);
appendOperator("HO");
} else {
} else if (conformance.isConcrete()) {
appendConcreteProtocolConformance(conformance.getConcrete(), genericSig);
} else if (conformance.isPack()) {
appendPackProtocolConformance(conformance.getPack(), genericSig);
} else {
llvm::errs() << "Bad conformance in mangler: ";
conformance.dump(llvm::errs());
abort();
}
}

Expand Down Expand Up @@ -4199,6 +4223,32 @@ void ASTMangler::appendConcreteProtocolConformance(
appendOperator("HC");
}

void ASTMangler::appendPackProtocolConformance(
const PackConformance *conformance,
GenericSignature sig) {
auto conformingType = conformance->getType();
auto patternConformances = conformance->getPatternConformances();
assert(conformingType->getNumElements() == patternConformances.size());

if (conformingType->getNumElements() == 0) {
appendOperator("y");
} else {
bool firstField = true;
for (unsigned i = 0, e = conformingType->getNumElements(); i < e; ++i) {
auto type = conformingType->getElementType(i);
auto conf = patternConformances[i];

if (auto *expansionTy = type->getAs<PackExpansionType>())
type = expansionTy->getPatternType();

appendAnyProtocolConformance(sig, type->getCanonicalType(), conf);
appendListSeparator(firstField);
}
}

appendOperator("HX");
}

void ASTMangler::appendOpParamForLayoutConstraint(LayoutConstraint layout) {
assert(layout);
switch (layout->getKind()) {
Expand Down
6 changes: 3 additions & 3 deletions lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5364,10 +5364,10 @@ CanType swift::substOpaqueTypesWithUnderlyingTypes(CanType ty,
ReplaceOpaqueTypesWithUnderlyingTypes replacer(
context.getContext(), context.getResilienceExpansion(),
context.isWholeModuleContext());
SubstOptions flags = SubstFlags::SubstituteOpaqueArchetypes;
SubstOptions flags = (SubstFlags::SubstituteOpaqueArchetypes |
SubstFlags::PreservePackExpansionLevel);
if (allowLoweredTypes)
flags =
SubstFlags::SubstituteOpaqueArchetypes | SubstFlags::AllowLoweredTypes;
flags |= SubstFlags::AllowLoweredTypes;
return ty.subst(replacer, replacer, flags)->getCanonicalType();
}

Expand Down
9 changes: 9 additions & 0 deletions lib/Demangling/Demangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -996,6 +996,7 @@ NodePointer Demangler::demangleOperator() {
case 'p':
return createWithChild(
Node::Kind::ProtocolConformanceRefInProtocolModule, popProtocol());
case 'X': return demanglePackProtocolConformance();

// Runtime records (type/protocol/conformance/function)
case 'c':
Expand Down Expand Up @@ -1847,6 +1848,7 @@ NodePointer Demangler::popAnyProtocolConformance() {
return popNode([](Node::Kind kind) {
switch (kind) {
case Node::Kind::ConcreteProtocolConformance:
case Node::Kind::PackProtocolConformance:
case Node::Kind::DependentProtocolConformanceRoot:
case Node::Kind::DependentProtocolConformanceInherited:
case Node::Kind::DependentProtocolConformanceAssociated:
Expand Down Expand Up @@ -1884,6 +1886,13 @@ NodePointer Demangler::demangleConcreteProtocolConformance() {
type, conformanceRef, conditionalConformanceList);
}

NodePointer Demangler::demanglePackProtocolConformance() {
NodePointer patternConformanceList = popAnyProtocolConformanceList();

return createWithChild(Node::Kind::PackProtocolConformance,
patternConformanceList);
}

NodePointer Demangler::popDependentProtocolConformance() {
return popNode([](Node::Kind kind) {
switch (kind) {
Expand Down
25 changes: 24 additions & 1 deletion lib/Demangling/NodePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ class NodePrinter {
case Node::Kind::AnonymousContext:
case Node::Kind::AnyProtocolConformanceList:
case Node::Kind::ConcreteProtocolConformance:
case Node::Kind::PackProtocolConformance:
case Node::Kind::DependentAssociatedConformance:
case Node::Kind::DependentProtocolConformanceAssociated:
case Node::Kind::DependentProtocolConformanceInherited:
Expand Down Expand Up @@ -3106,12 +3107,31 @@ NodePointer NodePrinter::print(NodePointer Node, unsigned depth,
printChildren(Node, depth);
return nullptr;
case Node::Kind::AnyProtocolConformanceList:
printChildren(Node, depth);
if (Node->getNumChildren() > 0) {
Printer << "(";
for (unsigned i = 0; i < Node->getNumChildren(); ++i) {
if (i > 0)
Printer << ", ";
print(Node->getChild(i), depth + 1);
}
Printer << ")";
}
return nullptr;
case Node::Kind::ConcreteProtocolConformance:
Printer << "concrete protocol conformance ";
if (Node->hasIndex())
Printer << "#" << Node->getIndex() << " ";
print(Node->getChild(0), depth + 1);
Printer << " to ";
print(Node->getChild(1), depth + 1);
if (Node->getNumChildren() > 2 &&
Node->getChild(2)->getNumChildren() > 0) {
Printer << " with conditional requirements: ";
print(Node->getChild(2), depth + 1);
}
return nullptr;
case Node::Kind::PackProtocolConformance:
Printer << "pack protocol conformance ";
printChildren(Node, depth);
return nullptr;
case Node::Kind::DependentAssociatedConformance:
Expand All @@ -3122,18 +3142,21 @@ NodePointer NodePrinter::print(NodePointer Node, unsigned depth,
Printer << "dependent associated protocol conformance ";
printOptionalIndex(Node->getChild(2));
print(Node->getChild(0), depth + 1);
Printer << " to ";
print(Node->getChild(1), depth + 1);
return nullptr;
case Node::Kind::DependentProtocolConformanceInherited:
Printer << "dependent inherited protocol conformance ";
printOptionalIndex(Node->getChild(2));
print(Node->getChild(0), depth + 1);
Printer << " to ";
print(Node->getChild(1), depth + 1);
return nullptr;
case Node::Kind::DependentProtocolConformanceRoot:
Printer << "dependent root protocol conformance ";
printOptionalIndex(Node->getChild(2));
print(Node->getChild(0), depth + 1);
Printer << " to ";
print(Node->getChild(1), depth + 1);
return nullptr;
case Node::Kind::ProtocolConformanceRefInTypeModule:
Expand Down
6 changes: 6 additions & 0 deletions lib/Demangling/OldRemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,12 @@ ManglingError Remangler::mangleConcreteProtocolConformance(Node *node,
return MANGLING_ERROR(ManglingError::UnsupportedNodeKind, node);
}

ManglingError Remangler::manglePackProtocolConformance(Node *node,
unsigned depth) {
// Pack conformances aren't in the old mangling
return MANGLING_ERROR(ManglingError::UnsupportedNodeKind, node);
}

ManglingError Remangler::mangleAnyProtocolConformanceList(Node *node,
unsigned depth) {
// Conformance lists aren't in the old mangling
Expand Down
10 changes: 10 additions & 0 deletions lib/Demangling/Remangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2614,6 +2614,14 @@ ManglingError Remangler::mangleConcreteProtocolConformance(Node *node,
return ManglingError::Success;
}

ManglingError Remangler::manglePackProtocolConformance(Node *node,
unsigned depth) {
RETURN_IF_ERROR(
mangleAnyProtocolConformanceList(node->getChild(0), depth + 1));
Buffer << "HX";
return ManglingError::Success;
}

ManglingError
Remangler::mangleDependentProtocolConformanceRoot(Node *node, unsigned depth) {
RETURN_IF_ERROR(mangleType(node->getChild(0), depth + 1));
Expand Down Expand Up @@ -2663,6 +2671,8 @@ ManglingError Remangler::mangleAnyProtocolConformance(Node *node,
switch (node->getKind()) {
case Node::Kind::ConcreteProtocolConformance:
return mangleConcreteProtocolConformance(node, depth + 1);
case Node::Kind::PackProtocolConformance:
return manglePackProtocolConformance(node, depth + 1);
case Node::Kind::DependentProtocolConformanceRoot:
return mangleDependentProtocolConformanceRoot(node, depth + 1);
case Node::Kind::DependentProtocolConformanceInherited:
Expand Down
1 change: 1 addition & 0 deletions test/Demangle/Inputs/manglings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,4 @@ $sSRyxG15Synchronization19AtomicRepresentableABRi_zrlMc ---> protocol conformanc
$sSRyxG15Synchronization19AtomicRepresentableABRi0_zrlMc ---> protocol conformance descriptor for < where A: ~Swift.Escapable> Swift.UnsafeBufferPointer<A> : Synchronization.AtomicRepresentable in Synchronization
$sSRyxG15Synchronization19AtomicRepresentableABRi1_zrlMc ---> protocol conformance descriptor for < where A: ~Swift.<bit 2>> Swift.UnsafeBufferPointer<A> : Synchronization.AtomicRepresentable in Synchronization

$s23variadic_generic_opaque2G2VyAA2S1V_AA2S2VQPGAA1PHPAeA1QHPyHC_AgaJHPyHCHX_HC ---> concrete protocol conformance variadic_generic_opaque.G2<Pack{variadic_generic_opaque.S1, variadic_generic_opaque.S2}> to protocol conformance ref (type's module) variadic_generic_opaque.P with conditional requirements: (pack protocol conformance (concrete protocol conformance variadic_generic_opaque.S1 to protocol conformance ref (type's module) variadic_generic_opaque.Q, concrete protocol conformance variadic_generic_opaque.S2 to protocol conformance ref (type's module) variadic_generic_opaque.Q))
39 changes: 35 additions & 4 deletions test/IRGen/variadic_generic_opaque.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
// RUN: %target-swift-frontend -emit-ir %s -disable-availability-checking

// FIXME: Add more tests
// RUN: %target-swift-frontend -emit-ir %s -disable-availability-checking | %FileCheck %s

public protocol P {}

public struct G<each T>: P {}

public func returnsG<each T>(_ t: repeat each T) -> some P {
public func concreteG<each T>(_ t: repeat each T) -> some P {
return G<repeat each T>()
}

public func abstractG<T>(_ t: T) -> some P {
return G<T>()
}

public func variadicG<each T>(_ t: repeat each T) -> some P {
return G<repeat each T>()
}

// Opaque return type is witnessed by a conditional conformance
protocol Q {}

struct S1: Q {}
struct S2: Q {}

struct G2<each T> {}
extension G2: P where repeat each T: Q {}

func concreteG2() -> some P {
G2<S1, S2>()
}

func abstractG2<T: Q>(_: T) -> some P {
G2<S1, T>()
}

func variadicG2<each T: Q>(_: repeat each T) -> some P {
G2<repeat each T>()
}

// CHECK: define private ptr @"get_witness_table 23variadic_generic_opaque2G2VyAA2S1V_AA2S2VQPGAA1PHPAeA1QHPyHC_AgaJHPyHCHX_HC"
// CHECK: define private ptr @"get_witness_table 23variadic_generic_opaque1QRzlAA2G2VyAA2S1V_xQPGAA1PHPAfaBHPyHC_xAaBHD1_HX_HC"
// CHECK: define private ptr @"get_witness_table Rvz23variadic_generic_opaque1QRzlAA2G2VyxxQp_QPGAA1PHPxAaBHD1__HX_HC"
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public protocol P {
func f()
}

public struct G<each T>: P {
public func f() {}
}

public func callee<each T>(_: repeat each T) -> some P {
G<repeat each T>()
}
6 changes: 6 additions & 0 deletions test/SILGen/variadic_generic_opaque_multifile.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: %target-swift-frontend -emit-silgen -primary-file %s %S/Inputs/variadic_generic_opaque_multifile_other.swift -disable-availability-checking
// RUN: %target-swift-frontend -emit-silgen %s %S/Inputs/variadic_generic_opaque_multifile_other.swift -disable-availability-checking

public func caller() {
callee(1, 2, 3).f()
}