Skip to content

Commit 7f21176

Browse files
committed
Skip standard substitutions for _Concurrency types when back-deploying.
When back-deploying concurrency support, do not use the standard substitutions for _Concurrency-defined types (such as `Task`) in type metadata because older Swift runtimes will not be able to demangle them. Instead, use the full mangled names so the runtime can still demangle them appropriately. Addresses rdar://82931890.
1 parent 3abe16f commit 7f21176

File tree

9 files changed

+58
-33
lines changed

9 files changed

+58
-33
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ class ASTMangler : public Mangler {
5656
/// to fill these in.
5757
bool AllowSymbolicReferences = false;
5858

59+
/// If enabled, allows the use of standard substitutions for types in the
60+
/// concurrency library.
61+
bool AllowConcurrencyStandardSubstitutions = true;
62+
5963
public:
6064
using SymbolicReferent = llvm::PointerUnion<const NominalTypeDecl *,
6165
const OpaqueTypeDecl *>;

include/swift/Demangling/ManglingUtils.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ char translateOperatorChar(char op);
9999
std::string translateOperator(StringRef Op);
100100

101101
/// Returns the standard type kind for an 'S' substitution, e.g. 'i' for "Int".
102-
llvm::Optional<StringRef> getStandardTypeSubst(StringRef TypeName);
102+
///
103+
/// \param allowConcurrencyManglings When true, allows the standard
104+
/// substitutions for types in the _Concurrency module that were introduced in
105+
/// Swift 5.5.
106+
llvm::Optional<StringRef> getStandardTypeSubst(
107+
StringRef TypeName, bool allowConcurrencyManglings);
103108

104109
/// Mangles an identifier using a generic Mangler class.
105110
///

include/swift/Demangling/StandardTypesMangling.def

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
/// STANDARD_TYPE(KIND, MANGLING, TYPENAME)
1414
/// The 1-character MANGLING for a known TYPENAME of KIND.
1515
///
16-
/// STANDARD_TYPE_2(KIND, MANGLING, TYPENAME)
16+
/// STANDARD_TYPE_CONCURRENCY(KIND, MANGLING, TYPENAME)
1717
/// The 1-character MANGLING for a known TYPENAME of KIND that is in the
18-
/// second level of standard types, all of which are mangled with the form
19-
/// Sc<MANGLING>.
18+
/// second level of standard types in the Concurrency model, all of which are
19+
/// mangled with the form Sc<MANGLING>.
2020
///
2121
/// OBJC_INTEROP_STANDARD_TYPE(KIND, MANGLING, TYPENAME)
2222
/// The 1-character MANGLING for a known TYPENAME of KIND, for a type that's
@@ -79,25 +79,25 @@ STANDARD_TYPE(Protocol, y, StringProtocol)
7979
STANDARD_TYPE(Protocol, Z, SignedInteger)
8080
STANDARD_TYPE(Protocol, z, BinaryInteger)
8181

82-
STANDARD_TYPE_2(Protocol, A, Actor)
83-
STANDARD_TYPE_2(Structure, C, CheckedContinuation)
84-
STANDARD_TYPE_2(Structure, c, UnsafeContinuation)
85-
STANDARD_TYPE_2(Structure, E, CancellationError)
86-
STANDARD_TYPE_2(Structure, e, UnownedSerialExecutor)
87-
STANDARD_TYPE_2(Protocol, F, Executor)
88-
STANDARD_TYPE_2(Protocol, f, SerialExecutor)
89-
STANDARD_TYPE_2(Structure, G, TaskGroup)
90-
STANDARD_TYPE_2(Structure, g, ThrowingTaskGroup)
91-
STANDARD_TYPE_2(Protocol, I, AsyncIteratorProtocol)
92-
STANDARD_TYPE_2(Protocol, i, AsyncSequence)
93-
STANDARD_TYPE_2(Structure, J, UnownedJob)
94-
STANDARD_TYPE_2(Class, M, MainActor)
95-
STANDARD_TYPE_2(Structure, P, TaskPriority)
96-
STANDARD_TYPE_2(Structure, S, AsyncStream)
97-
STANDARD_TYPE_2(Structure, s, AsyncThrowingStream)
98-
STANDARD_TYPE_2(Structure, T, Task)
99-
STANDARD_TYPE_2(Structure, t, UnsafeCurrentTask)
82+
STANDARD_TYPE_CONCURRENCY(Protocol, A, Actor)
83+
STANDARD_TYPE_CONCURRENCY(Structure, C, CheckedContinuation)
84+
STANDARD_TYPE_CONCURRENCY(Structure, c, UnsafeContinuation)
85+
STANDARD_TYPE_CONCURRENCY(Structure, E, CancellationError)
86+
STANDARD_TYPE_CONCURRENCY(Structure, e, UnownedSerialExecutor)
87+
STANDARD_TYPE_CONCURRENCY(Protocol, F, Executor)
88+
STANDARD_TYPE_CONCURRENCY(Protocol, f, SerialExecutor)
89+
STANDARD_TYPE_CONCURRENCY(Structure, G, TaskGroup)
90+
STANDARD_TYPE_CONCURRENCY(Structure, g, ThrowingTaskGroup)
91+
STANDARD_TYPE_CONCURRENCY(Protocol, I, AsyncIteratorProtocol)
92+
STANDARD_TYPE_CONCURRENCY(Protocol, i, AsyncSequence)
93+
STANDARD_TYPE_CONCURRENCY(Structure, J, UnownedJob)
94+
STANDARD_TYPE_CONCURRENCY(Class, M, MainActor)
95+
STANDARD_TYPE_CONCURRENCY(Structure, P, TaskPriority)
96+
STANDARD_TYPE_CONCURRENCY(Structure, S, AsyncStream)
97+
STANDARD_TYPE_CONCURRENCY(Structure, s, AsyncThrowingStream)
98+
STANDARD_TYPE_CONCURRENCY(Structure, T, Task)
99+
STANDARD_TYPE_CONCURRENCY(Structure, t, UnsafeCurrentTask)
100100

101101
#undef STANDARD_TYPE
102102
#undef OBJC_INTEROP_STANDARD_TYPE
103-
#undef STANDARD_TYPE_2
103+
#undef STANDARD_TYPE_CONCURRENCY

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2973,7 +2973,8 @@ bool ASTMangler::tryAppendStandardSubstitution(const GenericTypeDecl *decl) {
29732973
return false;
29742974

29752975
if (isa<NominalTypeDecl>(decl)) {
2976-
if (auto Subst = getStandardTypeSubst(decl->getName().str())) {
2976+
if (auto Subst = getStandardTypeSubst(
2977+
decl->getName().str(), AllowConcurrencyStandardSubstitutions)) {
29772978
if (!SubstMerging.tryMergeSubst(*this, *Subst, /*isStandardSubst*/ true)){
29782979
appendOperator("S", *Subst);
29792980
}

lib/Demangling/Demangler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ NodePointer Demangler::createStandardSubstitution(
975975
return createSwiftType(Node::Kind::KIND, #TYPENAME); \
976976
}
977977

978-
#define STANDARD_TYPE_2(KIND, MANGLING, TYPENAME) \
978+
#define STANDARD_TYPE_CONCURRENCY(KIND, MANGLING, TYPENAME) \
979979
if (SecondLevel && Subst == #MANGLING[0]) { \
980980
return createSwiftType(Node::Kind::KIND, #TYPENAME); \
981981
}

lib/Demangling/ManglingUtils.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,16 @@ std::string Mangle::translateOperator(StringRef Op) {
6666
return Encoded;
6767
}
6868

69-
llvm::Optional<StringRef> Mangle::getStandardTypeSubst(StringRef TypeName) {
69+
llvm::Optional<StringRef> Mangle::getStandardTypeSubst(
70+
StringRef TypeName, bool allowConcurrencyManglings) {
7071
#define STANDARD_TYPE(KIND, MANGLING, TYPENAME) \
7172
if (TypeName == #TYPENAME) { \
7273
return StringRef(#MANGLING); \
7374
}
7475

75-
#define STANDARD_TYPE_2(KIND, MANGLING, TYPENAME) \
76-
if (TypeName == #TYPENAME) { \
77-
return StringRef("c" #MANGLING); \
76+
#define STANDARD_TYPE_CONCURRENCY(KIND, MANGLING, TYPENAME) \
77+
if (allowConcurrencyManglings && TypeName == #TYPENAME) { \
78+
return StringRef("c" #MANGLING); \
7879
}
7980

8081
#include "swift/Demangling/StandardTypesMangling.def"

lib/Demangling/Remangler.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,8 @@ bool Remangler::mangleStandardSubstitution(Node *node) {
397397
if (node->getChild(1)->getKind() != Node::Kind::Identifier)
398398
return false;
399399

400-
if (auto Subst = getStandardTypeSubst(node->getChild(1)->getText())) {
400+
if (auto Subst = getStandardTypeSubst(
401+
node->getChild(1)->getText(), /*allowConcurrencyManglings=*/true)) {
401402
if (!SubstMerging.tryMergeSubst(*this, *Subst, /*isStandardSubst*/ true)) {
402403
Buffer << 'S' << *Subst;
403404
}

lib/IRGen/IRGenMangler.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/AST/IRGenOptions.h"
1717
#include "swift/AST/ProtocolAssociations.h"
1818
#include "swift/AST/ProtocolConformance.h"
19+
#include "swift/Basic/Platform.h"
1920
#include "swift/Demangling/ManglingMacros.h"
2021
#include "swift/Demangling/Demangle.h"
2122
#include "swift/ABI/MetadataValues.h"
@@ -98,7 +99,8 @@ IRGenMangler::withSymbolicReferences(IRGenModule &IGM,
9899
// manglings already, and the runtime ought to have a lookup table for
99100
// them. Symbolic referencing would be wasteful.
100101
if (type->getModuleContext()->hasStandardSubstitutions()
101-
&& Mangle::getStandardTypeSubst(type->getName().str())) {
102+
&& Mangle::getStandardTypeSubst(
103+
type->getName().str(), AllowConcurrencyStandardSubstitutions)) {
102104
return false;
103105
}
104106

@@ -146,6 +148,17 @@ SymbolicMangling
146148
IRGenMangler::mangleTypeForReflection(IRGenModule &IGM,
147149
CanGenericSignature Sig,
148150
CanType Ty) {
151+
// If our target predates Swift 5.5, we cannot apply the standard
152+
// substitutions for types defined in the Concurrency module.
153+
ASTContext &ctx = Ty->getASTContext();
154+
llvm::SaveAndRestore<bool> savedConcurrencyStandardSubstitutions(
155+
AllowConcurrencyStandardSubstitutions);
156+
if (auto runtimeCompatVersion = getSwiftRuntimeCompatibilityVersionForTarget(
157+
ctx.LangOpts.Target)) {
158+
if (*runtimeCompatVersion < llvm::VersionTuple(5, 5))
159+
AllowConcurrencyStandardSubstitutions = false;
160+
}
161+
149162
return withSymbolicReferences(IGM, [&]{
150163
appendType(Ty, Sig);
151164
});

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ _searchTypeMetadataRecords(TypeMetadataPrivateState &T,
675675

676676
// FIXME: When the _Concurrency library gets merged into the Standard Library,
677677
// we will be able to reference those symbols directly as well.
678-
#define STANDARD_TYPE_2(KIND, MANGLING, TYPENAME)
678+
#define STANDARD_TYPE_CONCURRENCY(KIND, MANGLING, TYPENAME)
679679

680680
#if !SWIFT_OBJC_INTEROP
681681
# define OBJC_INTEROP_STANDARD_TYPE(KIND, MANGLING, TYPENAME)
@@ -709,7 +709,7 @@ _findContextDescriptor(Demangle::NodePointer node,
709709
}
710710
// FIXME: When the _Concurrency library gets merged into the Standard Library,
711711
// we will be able to reference those symbols directly as well.
712-
#define STANDARD_TYPE_2(KIND, MANGLING, TYPENAME)
712+
#define STANDARD_TYPE_CONCURRENCY(KIND, MANGLING, TYPENAME)
713713
#if !SWIFT_OBJC_INTEROP
714714
# define OBJC_INTEROP_STANDARD_TYPE(KIND, MANGLING, TYPENAME)
715715
#endif

0 commit comments

Comments
 (0)