Skip to content

Commit 9604019

Browse files
committed
Drop typed throws from the mangling in closure and field reflection metadata
This follows what we just did for `@isolated(any)` function types. Part of rdar://130858222.
1 parent 2c1925d commit 9604019

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ class ASTMangler : public Mangler {
8383
/// a critical role.
8484
bool AllowIsolatedAny = true;
8585

86+
/// If enabled, typed throws can be encoded in the mangled name.
87+
/// Suppressing type attributes this way is generally questionable ---
88+
/// for example, it does not interact properly with substitutions ---
89+
/// and should only be done in situations where it is just going to be
90+
/// interpreted as a type and the exact string value does not play
91+
/// a critical role.
92+
bool AllowTypedThrows = true;
93+
8694
/// If enabled, declarations annotated with @_originallyDefinedIn are mangled
8795
/// as if they're part of their original module. Disabled for debug mangling,
8896
/// because lldb wants to find declarations in the modules they're currently

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3062,7 +3062,8 @@ void ASTMangler::appendFunctionSignature(AnyFunctionType *fn,
30623062
if (fn->isSendable())
30633063
appendOperator("Yb");
30643064
if (auto thrownError = fn->getEffectiveThrownErrorType()) {
3065-
if ((*thrownError)->isEqual(fn->getASTContext().getErrorExistentialType())){
3065+
if ((*thrownError)->isEqual(fn->getASTContext().getErrorExistentialType())
3066+
|| !AllowTypedThrows) {
30663067
appendOperator("K");
30673068
} else {
30683069
appendType(*thrownError, sig);

lib/IRGen/IRGenMangler.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,23 @@ IRGenMangler::mangleTypeForReflection(IRGenModule &IGM,
162162
llvm::SaveAndRestore<bool> savedConcurrencyStandardSubstitutions(
163163
AllowConcurrencyStandardSubstitutions);
164164
llvm::SaveAndRestore<bool> savedIsolatedAny(AllowIsolatedAny);
165+
llvm::SaveAndRestore<bool> savedTypedThrows(AllowTypedThrows);
165166
if (auto runtimeCompatVersion = getSwiftRuntimeCompatibilityVersionForTarget(
166167
ctx.LangOpts.Target)) {
168+
167169
if (*runtimeCompatVersion < llvm::VersionTuple(5, 5))
168170
AllowConcurrencyStandardSubstitutions = false;
169171

170-
// Suppress @isolated(any) if we're mangling for pre-6.0 runtimes.
172+
// Suppress @isolated(any) and typed throws if we're mangling for pre-6.0
173+
// runtimes.
171174
// This is unprincipled but, because of the restrictions in e.g.
172175
// mangledNameIsUnknownToDeployTarget, should only happen when
173176
// mangling for certain reflective uses where we have to hope that
174177
// the exact type identity is generally unimportant.
175-
if (*runtimeCompatVersion < llvm::VersionTuple(6, 0))
178+
if (*runtimeCompatVersion < llvm::VersionTuple(6, 0)) {
176179
AllowIsolatedAny = false;
180+
AllowTypedThrows = false;
181+
}
177182
}
178183

179184
llvm::SaveAndRestore<bool> savedAllowStandardSubstitutions(
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: %target-swift-frontend -emit-ir -target %target-cpu-apple-macos99.99 %s | %FileCheck --check-prefix=CHECK --check-prefix=CHECK-PRESENT %s
2+
// RUN: %target-swift-frontend -emit-ir -target %target-cpu-apple-macos14.4 %s | %FileCheck --check-prefix=CHECK --check-prefix=CHECK-SUPPRESSED %s
3+
4+
// REQUIRES: OS=macosx
5+
// UNSUPPORTED: CPU=arm64e
6+
7+
// Make sure that a dangling-based type description involving typed throws is
8+
// only used when deploying to runtimes that support it.
9+
10+
// Closure capture metadata:
11+
12+
enum MyError: Error {
13+
case fail
14+
}
15+
16+
// CHECK-LABEL: @"\01l__swift5_reflection_descriptor" = private constant
17+
// CHECK-PRESENT-SAME: ptr @"symbolic Si_____Ieghdzo_ 32reflection_metadata_typed_throws7MyErrorO"
18+
// CHECK-SUPPRESSED-SAME: ptr @"symbolic Si_____Ieghdzo_ 32reflection_metadata_typed_throws7MyErrorO"
19+
// CHECK-LABEL: @metadata = private constant %swift.full_boxmetadata { {{.*}}, ptr @"\01l__swift5_reflection_descriptor" }, align
20+
func makeClosure(fn: @escaping @Sendable () throws(MyError) -> Int) -> (() throws(MyError) -> Int) {
21+
return { try fn() + 1 }
22+
}
23+
24+
// Struct field metadata:
25+
26+
public struct MyStruct {
27+
let fn: () throws (MyError) -> ()
28+
}
29+
30+
// CHECK-LABEL: @"$s32reflection_metadata_typed_throws8MyStructVMF" = internal constant
31+
// CHECK-PRESENT-SAME: ptr @"symbolic yy_____YKc 32reflection_metadata_typed_throws7MyErrorO"
32+
// CHECK-SUPPRESSED-SAME: ptr @"get_type_metadata yy32reflection_metadata_typed_throws7MyErrorOYKc.1"

0 commit comments

Comments
 (0)