Skip to content

Commit c0a2fcc

Browse files
committed
[sending] Do not mangle sending into function/methods but keep mangling into vars/storage.
We want to ensure that functions/methods themselves do not have sending mangled into their names, but we do want sending mangled in non-top level positions. For example: we do not want to mangle sending into a function like the following: ```swift // We don't want to mangle this. func test(_ x: sending NonSendableKlass) -> () ``` But when it comes to actually storing functions into memory, we do want to distinguish in between function values that use sending vs those that do not since we do not want to allow for them to alias. Thus we want to mangle sending into things like the following: ```swift // We want to distinguish in between Array<(sending T) -> ()> and // Array((T) -> ()> let a = Array<(sending T) -> ()> // We want to distinguish in between a global contianing (sending T) -> () and a // global containing (T) -> (). var global: (sending T) -> () ``` This commit achieves that by making changes to the ASTMangler in getDeclType which causes getDeclType to set a flag that says that we have not yet recursed through the system and thus should suppress the printing of sendable. Once we get further into the system and recurse, that flag is by default set to true, so we get the old sending parameter without having to update large amounts of code. rdar://127383107 (cherry picked from commit 2bf497d)
1 parent 3510f29 commit c0a2fcc

File tree

6 files changed

+210
-98
lines changed

6 files changed

+210
-98
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,8 @@ class ASTMangler : public Mangler {
446446
GenericSignature sig,
447447
ModuleDecl *fromModule);
448448
void appendImplFunctionType(SILFunctionType *fn, GenericSignature sig,
449-
const ValueDecl *forDecl = nullptr);
449+
const ValueDecl *forDecl = nullptr,
450+
bool isInRecursion = true);
450451
void appendOpaqueTypeArchetype(ArchetypeType *archetype,
451452
OpaqueTypeDecl *opaqueDecl,
452453
SubstitutionMap subs,
@@ -522,25 +523,29 @@ class ASTMangler : public Mangler {
522523
FunctionMangling,
523524
};
524525

525-
void appendFunction(AnyFunctionType *fn, GenericSignature sig,
526-
FunctionManglingKind functionMangling = NoFunctionMangling,
527-
const ValueDecl *forDecl = nullptr);
526+
void
527+
appendFunction(AnyFunctionType *fn, GenericSignature sig,
528+
FunctionManglingKind functionMangling = NoFunctionMangling,
529+
const ValueDecl *forDecl = nullptr,
530+
bool isRecursedInto = true);
528531
void appendFunctionType(AnyFunctionType *fn, GenericSignature sig,
529532
bool isAutoClosure = false,
530-
const ValueDecl *forDecl = nullptr);
533+
const ValueDecl *forDecl = nullptr,
534+
bool isRecursedInto = true);
531535
void appendClangType(AnyFunctionType *fn);
532536
template <typename FnType>
533537
void appendClangType(FnType *fn, llvm::raw_svector_ostream &os);
534538

535-
void appendFunctionSignature(AnyFunctionType *fn,
536-
GenericSignature sig,
539+
void appendFunctionSignature(AnyFunctionType *fn, GenericSignature sig,
537540
const ValueDecl *forDecl,
538-
FunctionManglingKind functionMangling);
541+
FunctionManglingKind functionMangling,
542+
bool isRecursedInto = true);
539543

540544
void appendFunctionInputType(ArrayRef<AnyFunctionType::Param> params,
541545
LifetimeDependenceInfo lifetimeDependenceInfo,
542546
GenericSignature sig,
543-
const ValueDecl *forDecl = nullptr);
547+
const ValueDecl *forDecl = nullptr,
548+
bool isRecursedInto = true);
544549
void appendFunctionResultType(Type resultType,
545550
GenericSignature sig,
546551
const ValueDecl *forDecl = nullptr);

lib/AST/ASTMangler.cpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,7 +2034,8 @@ getResultDifferentiability(SILResultInfo::Options options) {
20342034

20352035
void ASTMangler::appendImplFunctionType(SILFunctionType *fn,
20362036
GenericSignature outerGenericSig,
2037-
const ValueDecl *forDecl) {
2037+
const ValueDecl *forDecl,
2038+
bool isInRecursion) {
20382039

20392040
llvm::SmallVector<char, 32> OpArgs;
20402041

@@ -2159,7 +2160,7 @@ void ASTMangler::appendImplFunctionType(SILFunctionType *fn,
21592160
}
21602161

21612162
// Mangle if we have a transferring result.
2162-
if (fn->hasSendingResult())
2163+
if (isInRecursion && fn->hasSendingResult())
21632164
OpArgs.push_back('T');
21642165

21652166
// Mangle the results.
@@ -2942,7 +2943,7 @@ void ASTMangler::appendAnyGenericType(const GenericTypeDecl *decl,
29422943

29432944
void ASTMangler::appendFunction(AnyFunctionType *fn, GenericSignature sig,
29442945
FunctionManglingKind functionMangling,
2945-
const ValueDecl *forDecl) {
2946+
const ValueDecl *forDecl, bool isRecursedInto) {
29462947
// Append parameter labels right before the signature/type.
29472948
auto parameters = fn->getParams();
29482949
auto firstLabel = std::find_if(
@@ -2962,15 +2963,16 @@ void ASTMangler::appendFunction(AnyFunctionType *fn, GenericSignature sig,
29622963
}
29632964

29642965
if (functionMangling != NoFunctionMangling) {
2965-
appendFunctionSignature(fn, sig, forDecl, functionMangling);
2966+
appendFunctionSignature(fn, sig, forDecl, functionMangling, isRecursedInto);
29662967
} else {
2967-
appendFunctionType(fn, sig, /*autoclosure*/ false, forDecl);
2968+
appendFunctionType(fn, sig, /*autoclosure*/ false, forDecl, isRecursedInto);
29682969
}
29692970
}
29702971

29712972
void ASTMangler::appendFunctionType(AnyFunctionType *fn, GenericSignature sig,
29722973
bool isAutoClosure,
2973-
const ValueDecl *forDecl) {
2974+
const ValueDecl *forDecl,
2975+
bool isRecursedInto) {
29742976
assert((DWARFMangling || fn->isCanonical()) &&
29752977
"expecting canonical types when not mangling for the debugger");
29762978

@@ -3042,10 +3044,11 @@ void ASTMangler::appendClangType(AnyFunctionType *fn) {
30423044
void ASTMangler::appendFunctionSignature(AnyFunctionType *fn,
30433045
GenericSignature sig,
30443046
const ValueDecl *forDecl,
3045-
FunctionManglingKind functionMangling) {
3047+
FunctionManglingKind functionMangling,
3048+
bool isRecursedInto) {
30463049
appendFunctionResultType(fn->getResult(), sig, forDecl);
30473050
appendFunctionInputType(fn->getParams(), fn->getLifetimeDependenceInfo(), sig,
3048-
forDecl);
3051+
forDecl, isRecursedInto);
30493052
if (fn->isAsync())
30503053
appendOperator("Ya");
30513054
if (fn->isSendable())
@@ -3091,7 +3094,7 @@ void ASTMangler::appendFunctionSignature(AnyFunctionType *fn,
30913094
break;
30923095
}
30933096

3094-
if (fn->hasSendingResult()) {
3097+
if (isRecursedInto && fn->hasSendingResult()) {
30953098
appendOperator("YT");
30963099
}
30973100

@@ -3137,7 +3140,13 @@ getDefaultOwnership(const ValueDecl *forDecl) {
31373140

31383141
static ParameterTypeFlags
31393142
getParameterFlagsForMangling(ParameterTypeFlags flags,
3140-
ParamSpecifier defaultSpecifier) {
3143+
ParamSpecifier defaultSpecifier,
3144+
bool isInRecursion = true) {
3145+
// If we have been recursed into, then remove sending from our flags.
3146+
if (!isInRecursion) {
3147+
flags = flags.withSending(false);
3148+
}
3149+
31413150
switch (auto specifier = flags.getOwnershipSpecifier()) {
31423151
// If no parameter specifier was provided, mangle as-is, because we are by
31433152
// definition using the default convention.
@@ -3164,7 +3173,7 @@ getParameterFlagsForMangling(ParameterTypeFlags flags,
31643173
void ASTMangler::appendFunctionInputType(
31653174
ArrayRef<AnyFunctionType::Param> params,
31663175
LifetimeDependenceInfo lifetimeDependenceInfo, GenericSignature sig,
3167-
const ValueDecl *forDecl) {
3176+
const ValueDecl *forDecl, bool isRecursedInto) {
31683177
auto defaultSpecifier = getDefaultOwnership(forDecl);
31693178

31703179
switch (params.size()) {
@@ -3187,7 +3196,7 @@ void ASTMangler::appendFunctionInputType(
31873196
appendParameterTypeListElement(
31883197
Identifier(), type,
31893198
getParameterFlagsForMangling(param.getParameterFlags(),
3190-
defaultSpecifier),
3199+
defaultSpecifier, isRecursedInto),
31913200
lifetimeDependenceInfo.getLifetimeDependenceOnParam(/*paramIndex*/ 0),
31923201
sig, nullptr);
31933202
break;
@@ -3209,7 +3218,7 @@ void ASTMangler::appendFunctionInputType(
32093218
appendParameterTypeListElement(
32103219
Identifier(), param.getPlainType(),
32113220
getParameterFlagsForMangling(param.getParameterFlags(),
3212-
defaultSpecifier),
3221+
defaultSpecifier, isRecursedInto),
32133222
lifetimeDependenceInfo.getLifetimeDependenceOnParam(paramIndex), sig,
32143223
nullptr);
32153224
appendListSeparator(isFirstParam);
@@ -3873,7 +3882,8 @@ void ASTMangler::appendDeclType(const ValueDecl *decl,
38733882
: decl->getDeclContext()->getGenericSignatureOfContext());
38743883

38753884
if (AnyFunctionType *FuncTy = type->getAs<AnyFunctionType>()) {
3876-
appendFunction(FuncTy, sig, functionMangling, decl);
3885+
appendFunction(FuncTy, sig, functionMangling, decl,
3886+
false /*is recursed into*/);
38773887
} else {
38783888
appendType(type, sig, decl);
38793889
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// RUN: %target-swift-frontend %s -emit-silgen -swift-version 6 | swift-demangle | %FileCheck -check-prefix=CHECK %s
2+
3+
// REQUIRES: concurrency
4+
// REQUIRES: asserts
5+
6+
class NonSendableKlass {}
7+
8+
struct S<T> {
9+
var count: Int { 0 }
10+
}
11+
12+
// CHECK: sil hidden [ossa] @sending_mangling.testRemoveFunctionArg(__owned sending_mangling.NonSendableKlass) -> () : $@convention(thin) (@sil_sending @owned NonSendableKlass) -> () {
13+
func testRemoveFunctionArg(_ x: sending NonSendableKlass) {}
14+
15+
// CHECK: sil hidden [ossa] @sending_mangling.testNoRemoveFunctionArgSubTypeArg(__owned sending_mangling.S<(sending __owned sending_mangling.NonSendableKlass) -> ()>) -> () : $@convention(thin) (@sil_sending S<(sending NonSendableKlass) -> ()>) -> () {
16+
func testNoRemoveFunctionArgSubTypeArg(_ x: sending S<(sending NonSendableKlass) -> ()>) {}
17+
18+
// CHECK: sil hidden [ossa] @sending_mangling.testNoRemoveFunctionArgSubTypeReturn(__owned sending_mangling.S<() -> sending sending_mangling.NonSendableKlass>) -> () : $@convention(thin) (@sil_sending S<() -> sending NonSendableKlass>) -> () {
19+
func testNoRemoveFunctionArgSubTypeReturn(_ x: sending S<() -> sending NonSendableKlass>) {}
20+
21+
// CHECK: sil hidden [ossa] @sending_mangling.testRemoveFunctionResult() -> sending_mangling.NonSendableKlass : $@convention(thin) () -> @sil_sending @owned NonSendableKlass {
22+
func testRemoveFunctionResult() -> sending NonSendableKlass {
23+
}
24+
25+
// CHECK: sil hidden [ossa] @sending_mangling.testNoRemoveFunctionResultSubTypeArg() -> sending_mangling.S<(sending __owned sending_mangling.NonSendableKlass) -> ()> : $@convention(thin) () -> @sil_sending S<(sending NonSendableKlass) -> ()> {
26+
func testNoRemoveFunctionResultSubTypeArg() -> sending S<(sending NonSendableKlass) -> ()> { fatalError() }
27+
28+
// CHECK: sil hidden [ossa] @sending_mangling.testNoRemoveFunctionResultSubTypeResult() -> sending_mangling.S<() -> sending sending_mangling.NonSendableKlass> : $@convention(thin) () -> @sil_sending S<() -> sending NonSendableKlass> {
29+
func testNoRemoveFunctionResultSubTypeResult() -> sending S<() -> sending NonSendableKlass> { fatalError() }
30+
31+
// We do not remove this since the sending is in the subtype of the result.
32+
// CHECK: sil hidden [ossa] @sending_mangling.testNoRemoveFunctionResultImmediateTypedFunctionWithArg() -> (sending __owned sending_mangling.NonSendableKlass) -> () : $@convention(thin) () -> @owned @callee_guaranteed (@sil_sending @owned NonSendableKlass) -> () {
33+
func testNoRemoveFunctionResultImmediateTypedFunctionWithArg() -> ((sending NonSendableKlass) -> ()) { fatalError() }
34+
35+
// CHECK: sil hidden [ossa] @sending_mangling.testNoRemoveFunctionResultImmedateTypedFunctionWithResult() -> () -> sending sending_mangling.NonSendableKlass : $@convention(thin) () -> @owned @callee_guaranteed () -> @sil_sending @owned NonSendableKlass {
36+
func testNoRemoveFunctionResultImmedateTypedFunctionWithResult() -> (() -> sending NonSendableKlass) { fatalError() }
37+
38+
struct MethodTest {
39+
// CHECK: sil hidden [ossa] @sending_mangling.MethodTest.testMethodRemoveFunctionArg(__owned sending_mangling.NonSendableKlass) -> () : $@convention(method) (@sil_sending @owned NonSendableKlass, MethodTest) -> () {
40+
func testMethodRemoveFunctionArg(_ x: sending NonSendableKlass) {}
41+
42+
// CHECK: sil hidden [ossa] @sending_mangling.MethodTest.testNoRemoveFunctionArgSubTypeArg(__owned sending_mangling.S<(sending __owned sending_mangling.NonSendableKlass) -> ()>) -> () : $@convention(method) (@sil_sending S<(sending NonSendableKlass) -> ()>, MethodTest) -> () {
43+
func testNoRemoveFunctionArgSubTypeArg(_ x: sending S<(sending NonSendableKlass) -> ()>) {}
44+
45+
// DEMANGLE sil hidden [ossa] @sending_mangling.MethodTest.testNoRemoveFunctionArgSubTypeReturn(__owned sending_mangling.S<() -> sending sending_mangling.NonSendableKlass>) -> () : $@convention(method) (@sil_sending S<() -> sending NonSendableKlass>, MethodTest) -> () {
46+
func testNoRemoveFunctionArgSubTypeReturn(_ x: sending S<() -> sending NonSendableKlass>) {}
47+
48+
// CHECK: sil hidden [ossa] @sending_mangling.MethodTest.testMethodRemoveFunctionResult() -> sending_mangling.NonSendableKlass : $@convention(method) (MethodTest) -> @sil_sending @owned NonSendableKlass {
49+
func testMethodRemoveFunctionResult() -> sending NonSendableKlass {
50+
}
51+
52+
// CHECK: sil hidden [ossa] @sending_mangling.MethodTest.testNoRemoveFunctionResultSubTypeArg() -> sending_mangling.S<(sending __owned sending_mangling.NonSendableKlass) -> ()> : $@convention(method) (MethodTest) -> @sil_sending S<(sending NonSendableKlass) -> ()> {
53+
func testNoRemoveFunctionResultSubTypeArg() -> sending S<(sending NonSendableKlass) -> ()> { fatalError() }
54+
55+
// CHECK: sil hidden [ossa] @sending_mangling.MethodTest.testNoRemoveFunctionResultSubTypeResult() -> sending_mangling.S<() -> sending sending_mangling.NonSendableKlass> : $@convention(method) (MethodTest) -> @sil_sending S<() -> sending NonSendableKlass> {
56+
func testNoRemoveFunctionResultSubTypeResult() -> sending S<() -> sending NonSendableKlass> { fatalError() }
57+
58+
// CHECK: sil hidden [ossa] @sending_mangling.MethodTest.testNoRemoveFunctionResultImmediateTypedFunctionWithArg() -> (sending __owned sending_mangling.NonSendableKlass) -> () : $@convention(method) (MethodTest) -> @owned @callee_guaranteed (@sil_sending @owned NonSendableKlass) -> () {
59+
func testNoRemoveFunctionResultImmediateTypedFunctionWithArg() -> ((sending NonSendableKlass) -> ()) { fatalError() }
60+
61+
// CHECK: sil hidden [ossa] @sending_mangling.MethodTest.testNoRemoveFunctionResultImmedateTypedFunctionWithResult() -> () -> sending sending_mangling.NonSendableKlass : $@convention(method) (MethodTest) -> @owned @callee_guaranteed () -> @sil_sending @owned NonSendableKlass {
62+
func testNoRemoveFunctionResultImmedateTypedFunctionWithResult() -> (() -> sending NonSendableKlass) { fatalError() }
63+
}
64+
65+
protocol SendingProtocol {
66+
func sendingArg(_ x: sending NonSendableKlass)
67+
func sendingResult() -> sending NonSendableKlass
68+
func sendingArgWithFunctionSendingArg(_ x: sending (sending NonSendableKlass) -> ())
69+
func sendingArgWithFunctionSendingResult() -> sending (sending NonSendableKlass) -> ()
70+
}
71+
72+
extension SendingProtocol {
73+
// CHECK: sil hidden [ossa] @(extension in sending_mangling):sending_mangling.SendingProtocol.sendingArg(__owned sending_mangling.NonSendableKlass) -> () : $@convention(method) <Self where Self : SendingProtocol> (@sil_sending @owned NonSendableKlass, @in_guaranteed Self) -> () {
74+
func sendingArg(_ x: sending NonSendableKlass) {}
75+
76+
// CHECK: sil hidden [ossa] @(extension in sending_mangling):sending_mangling.SendingProtocol.sendingResult() -> sending_mangling.NonSendableKlass : $@convention(method) <Self where Self : SendingProtocol> (@in_guaranteed Self) -> @sil_sending @owned NonSendableKlass {
77+
func sendingResult() -> sending NonSendableKlass { fatalError() }
78+
79+
// CHECK: sil hidden [ossa] @(extension in sending_mangling):sending_mangling.SendingProtocol.sendingArgWithFunctionSendingArg(__owned (sending __owned sending_mangling.NonSendableKlass) -> ()) -> () : $@convention(method) <Self where Self : SendingProtocol> (@sil_sending @owned @noescape @callee_guaranteed (@sil_sending @owned NonSendableKlass) -> (), @in_guaranteed Self) -> () {
80+
func sendingArgWithFunctionSendingArg(_ x: sending (sending NonSendableKlass) -> ()) {}
81+
82+
// CHECK: sil hidden [ossa] @(extension in sending_mangling):sending_mangling.SendingProtocol.sendingArgWithFunctionSendingResult() -> (sending __owned sending_mangling.NonSendableKlass) -> () : $@convention(method) <Self where Self : SendingProtocol> (@in_guaranteed Self) -> @sil_sending @owned @callee_guaranteed (@sil_sending @owned NonSendableKlass) -> () {
83+
func sendingArgWithFunctionSendingResult() -> sending (sending NonSendableKlass) -> () { fatalError() }
84+
}

0 commit comments

Comments
 (0)