Skip to content

Commit a75f94e

Browse files
[6.2] use RespectOriginallyDefinedIn when mangling extension contexts (#82657)
- **Explanation**: USR mangling can include an extension context infix (`AAE`) when an extended type uses `@_originallyDefinedIn` on platforms other than the active one. This adds a check for the `RespectOriginallyDefinedIn` flag when checking extension decls against their extended type. - **Scope**: Changes USR mangling in these situations so that USRs are the same for the same code regardless of platform. - **Issues**: rdar://152598492 - **Original PRs**: #82348 - **Risk**: Low. The change is limited to situations where the name mangler is already disrespecting the alternate module name, and only additionally turns on that flag for any USR mangling. - **Testing**: Automated tests - **Reviewers**: @edymtt @augusto2112
1 parent d9bc9a2 commit a75f94e

File tree

7 files changed

+50
-7
lines changed

7 files changed

+50
-7
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2135,7 +2135,10 @@ class ExtensionDecl final : public GenericContext, public Decl,
21352135

21362136
/// Determine whether this extension context is in the same defining module as
21372137
/// the original nominal type context.
2138-
bool isInSameDefiningModule() const;
2138+
///
2139+
/// \param RespectOriginallyDefinedIn Whether to respect
2140+
/// \c @_originallyDefinedIn attributes or the actual location of the decls.
2141+
bool isInSameDefiningModule(bool RespectOriginallyDefinedIn = true) const;
21392142

21402143
/// Determine whether this extension is equivalent to one that requires at
21412144
/// at least some constraints to be written in the source.

lib/AST/ASTMangler.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,8 @@ std::string ASTMangler::mangleObjCRuntimeName(const NominalTypeDecl *Nominal) {
946946

947947
std::string ASTMangler::mangleTypeAsContextUSR(const NominalTypeDecl *type) {
948948
beginManglingWithoutPrefix();
949+
llvm::SaveAndRestore<bool> respectOriginallyDefinedInRAII(
950+
RespectOriginallyDefinedIn, false);
949951
llvm::SaveAndRestore<bool> allowUnnamedRAII(AllowNamelessEntities, true);
950952
BaseEntitySignature base(type);
951953
appendContext(type, base, type->getAlternateModuleName());
@@ -1014,6 +1016,8 @@ ASTMangler::mangleAnyDecl(const ValueDecl *Decl,
10141016

10151017
std::string ASTMangler::mangleDeclAsUSR(const ValueDecl *Decl,
10161018
StringRef USRPrefix) {
1019+
llvm::SaveAndRestore<bool> respectOriginallyDefinedInRAII(
1020+
RespectOriginallyDefinedIn, false);
10171021
return (llvm::Twine(USRPrefix) + mangleAnyDecl(Decl, false)).str();
10181022
}
10191023

@@ -1022,6 +1026,8 @@ std::string ASTMangler::mangleAccessorEntityAsUSR(AccessorKind kind,
10221026
StringRef USRPrefix,
10231027
bool isStatic) {
10241028
beginManglingWithoutPrefix();
1029+
llvm::SaveAndRestore<bool> respectOriginallyDefinedInRAII(
1030+
RespectOriginallyDefinedIn, false);
10251031
llvm::SaveAndRestore<bool> allowUnnamedRAII(AllowNamelessEntities, true);
10261032
Buffer << USRPrefix;
10271033
appendAccessorEntity(getCodeForAccessorKind(kind), decl, isStatic);
@@ -3052,9 +3058,9 @@ void ASTMangler::appendExtension(const ExtensionDecl* ext,
30523058
// "extension is to a protocol" would no longer be a reason to use the
30533059
// extension mangling, because an extension method implementation could be
30543060
// resiliently moved into the original protocol itself.
3055-
if (ext->isInSameDefiningModule() // case 1
3056-
&& !sigParts.hasRequirements() // case 2
3057-
&& !ext->getDeclaredInterfaceType()->isExistentialType()) { // case 3
3061+
if (ext->isInSameDefiningModule(RespectOriginallyDefinedIn) // case 1
3062+
&& !sigParts.hasRequirements() // case 2
3063+
&& !ext->getDeclaredInterfaceType()->isExistentialType()) { // case 3
30583064
// skip extension mangling
30593065
return appendAnyGenericType(decl);
30603066
}

lib/AST/Decl.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,10 +2165,13 @@ bool ExtensionDecl::isWrittenWithConstraints() const {
21652165
return false;
21662166
}
21672167

2168-
bool ExtensionDecl::isInSameDefiningModule() const {
2168+
bool ExtensionDecl::isInSameDefiningModule(
2169+
bool RespectOriginallyDefinedIn) const {
21692170
auto decl = getExtendedNominal();
2170-
auto extensionAlterName = getAlternateModuleName();
2171-
auto typeAlterName = decl->getAlternateModuleName();
2171+
auto extensionAlterName =
2172+
RespectOriginallyDefinedIn ? getAlternateModuleName() : "";
2173+
auto typeAlterName =
2174+
RespectOriginallyDefinedIn ? decl->getAlternateModuleName() : "";
21722175

21732176
if (!extensionAlterName.empty()) {
21742177
if (!typeAlterName.empty()) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %empty-directory(%t/macos)
3+
// RUN: %empty-directory(%t/ios)
4+
5+
// RUN: %target-swift-frontend -target %target-cpu-apple-macos %s -module-name OriginallyDefinedInExtension -emit-module -emit-module-path %t/macos/OriginallyDefinedInExtension.swiftmodule -emit-symbol-graph -emit-symbol-graph-dir %t/macos/
6+
// RUN: %FileCheck %s --input-file %t/macos/OriginallyDefinedInExtension.symbols.json
7+
// RUN: %target-swift-frontend -target %target-cpu-apple-ios-simulator %s -module-name OriginallyDefinedInExtension -emit-module -emit-module-path %t/ios/OriginallyDefinedInExtension.swiftmodule -emit-symbol-graph -emit-symbol-graph-dir %t/ios/
8+
// RUN: %FileCheck %s --input-file %t/ios/OriginallyDefinedInExtension.symbols.json
9+
10+
// CHECK: "precise":"s:28OriginallyDefinedInExtension12SimpleStructV05InnerF0V"
11+
12+
// REQUIRES: SWIFT_SDK=osx
13+
// REQUIRES: SWIFT_SDK=ios_simulator
14+
15+
@available(macOS 10.8, *)
16+
@_originallyDefinedIn(module: "another", macOS 11.0)
17+
public struct SimpleStruct {}
18+
19+
@available(macOS 12.0, iOS 13.0, *)
20+
public extension SimpleStruct {
21+
struct InnerStruct {}
22+
}

test/lit.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,11 @@ config.available_features.add("SWIFT_VERSION=" + swift_version)
852852

853853
config.available_features.add("STDLIB_VARIANT={}".format(config.variant_suffix[1:]))
854854

855+
if "target-same-as-host" in config.available_features:
856+
# Only add SWIFT_SDKS features if we're building host tools
857+
for sdk in config.swift_sdks:
858+
config.available_features.add("SWIFT_SDK=" + sdk.lower())
859+
855860
if "optimized_stdlib" in config.available_features:
856861
config.available_features.add("optimized_stdlib_" + run_cpu)
857862

test/lit.site.cfg.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ config.freestanding_sdk_name = "@SWIFT_SDK_FREESTANDING_LIB_SUBDIR@"
173173
if '@SWIFT_BUILD_SWIFT_SYNTAX@' == 'TRUE':
174174
config.available_features.add('swift_swift_parser')
175175

176+
config.swift_sdks = "@SWIFT_SDKS@".split(";")
177+
176178
# Let the main config do the real work.
177179
if config.test_exec_root is None:
178180
config.test_exec_root = os.path.dirname(lit.util.abs_path_preserve_drive(__file__))

validation-test/lit.site.cfg.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ config.swift_stdlib_enable_objc_interop = "@SWIFT_STDLIB_ENABLE_OBJC_INTEROP@" =
137137
# Configured in DarwinSDKs.cmake
138138
config.freestanding_sdk_name = "@SWIFT_SDK_FREESTANDING_LIB_SUBDIR@"
139139

140+
config.swift_sdks = "@SWIFT_SDKS@".split(";")
141+
140142
# Let the main config do the real work.
141143
config.test_exec_root = os.path.dirname(os.path.realpath(__file__))
142144
lit_config.load_config(config, os.path.join(config.test_exec_root, "lit.swift-features.cfg"))

0 commit comments

Comments
 (0)