Skip to content

Commit e1e1531

Browse files
committed
[cxx-interop] Allow AppKit & UIKit to be rebuilt with C++ interop enabled
This removes a workaround from the module interface loader, which was forcing AppKit and UIKit to be rebuilt from their textual interfaces with C++ interop disabled, even if the current compilation explicitly enables it. The workaround was previously put in place because of a compiler error: ``` error: type 'AttributeScopes.AppKitAttributes.StrikethroughStyleAttribute' does not conform to protocol 'AttributedStringKey' note: possibly intended match 'AttributeScopes.AppKitAttributes.StrikethroughStyleAttribute.Value' (aka 'NSUnderlineStyle') does not conform to 'Hashable' ``` `NSUnderlineStyle` is a C/C++ type from AppKit that is declared using `NS_OPTIONS` macro. `NS_OPTIONS`/`CF_OPTIONS` macros have different expansions in C vs C++ language modes. The C++ expansions weren't handled correctly by ClangImporter, resulting in two distinct Swift types being created: a `typealias NSUnderlineStyle` which was marked as unavailable in Swift, and `enum NSUnderlineStyle`. This mostly worked fine, since the lookup logic was picking the enum during regular name lookup. However, this silently broke down when rebuilding the explicit conformance from `AppKit.swiftinterface`: ``` extension AppKit.NSUnderlineStyle : Swift.Hashable {} ``` Swift was picking the (unavailable) typealias when rebuilding this extension, which means the (available) enum wasn't getting the conformance. This is verified by an existing test (`test/Interop/Cxx/objc-correctness/appkit-uikit.swift`). rdar://142961112
1 parent a172489 commit e1e1531

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

lib/ClangImporter/ImportName.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,6 +1861,25 @@ ImportedName NameImporter::importNameImpl(const clang::NamedDecl *D,
18611861
return result;
18621862
}
18631863

1864+
// In C++ language mode, CF_OPTIONS/NS_OPTIONS macro has a different
1865+
// expansion: instead of a forward-declared enum, it expands into a typedef
1866+
// that is marked as `__attribute__((availability(swift,unavailable)))`, and
1867+
// an anonymous enum that inherits from the typedef. The logic above imports
1868+
// the anonymous enum with the desired name based on the typedef's name. In
1869+
// addition to that, we should make sure the unavailable typedef isn't
1870+
// imported into Swift to avoid having two types with the same name, which
1871+
// cause subtle name lookup issues.
1872+
if (swiftCtx.LangOpts.EnableCXXInterop &&
1873+
isUnavailableInSwift(D, nullptr, true)) {
1874+
auto loc = D->getEndLoc();
1875+
if (loc.isMacroID()) {
1876+
StringRef macroName =
1877+
clangSema.getPreprocessor().getImmediateMacroName(loc);
1878+
if (macroName == "CF_OPTIONS" || macroName == "NS_OPTIONS")
1879+
return ImportedName();
1880+
}
1881+
}
1882+
18641883
/// Whether the result is a function name.
18651884
bool isFunction = false;
18661885
bool isInitializer = false;

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,10 +2106,7 @@ InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName,
21062106
// interop enabled by the Swift CI because it uses an old host SDK.
21072107
// FIXME: Hack for CoreGraphics.swiftmodule, which cannot be rebuilt because
21082108
// of a CF_OPTIONS bug (rdar://142762174).
2109-
// FIXME: Hack for AppKit.swiftmodule / UIKit.swiftmodule, which cannot be
2110-
// rebuilt because of an NS_OPTIONS bug (rdar://143033209)
2111-
if (moduleName == "Darwin" || moduleName == "CoreGraphics"
2112-
|| moduleName == "AppKit" || moduleName == "UIKit") {
2109+
if (moduleName == "Darwin" || moduleName == "CoreGraphics") {
21132110
subInvocation.getLangOptions().EnableCXXInterop = false;
21142111
subInvocation.getLangOptions().cxxInteropCompatVersion = {};
21152112
BuildArgs.erase(llvm::remove_if(BuildArgs,

0 commit comments

Comments
 (0)