Skip to content

[cxx-interop] Allow old spelling of AppKit constants #83568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8680,6 +8680,26 @@ bool importer::isSpecialUIKitStructZeroProperty(const clang::NamedDecl *decl) {
return ident->isStr("UIEdgeInsetsZero") || ident->isStr("UIOffsetZero");
}

bool importer::isSpecialAppKitFunctionKeyProperty(
const clang::NamedDecl *decl) {
auto constant = dyn_cast<clang::EnumConstantDecl>(decl);
if (!constant)
return false;

auto module = constant->getOwningModule();
if (!module || module->getTopLevelModuleName() != "AppKit")
return false;

clang::DeclarationName name = constant->getDeclName();
const clang::IdentifierInfo *ident = name.getAsIdentifierInfo();
if (!ident)
return false;
auto rawName = ident->getName();

return rawName.starts_with("NS") &&
(rawName.ends_with("FunctionKey") || rawName.ends_with("Character"));
}

bool importer::hasSameUnderlyingType(const clang::Type *a,
const clang::TemplateTypeParmDecl *b) {
while (a->isPointerType() || a->isReferenceType())
Expand Down
5 changes: 4 additions & 1 deletion lib/ClangImporter/ImportName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,9 +646,12 @@ findSwiftNameAttr(const clang::Decl *decl, ImportNameVersion version) {
if (version > ImportNameVersion::swift2()) {
// FIXME: Until Apple gets a chance to update UIKit's API notes, always use
// the new name for certain properties.
if (auto *namedDecl = dyn_cast<clang::NamedDecl>(decl))
if (auto *namedDecl = dyn_cast<clang::NamedDecl>(decl)) {
if (importer::isSpecialUIKitStructZeroProperty(namedDecl))
version = ImportNameVersion::swift4_2();
if (importer::isSpecialAppKitFunctionKeyProperty(namedDecl))
return std::nullopt;
}

// Dig out the attribute that specifies the Swift name.
std::optional<AnySwiftNameAttr> activeAttr;
Expand Down
6 changes: 6 additions & 0 deletions lib/ClangImporter/ImporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1976,6 +1976,12 @@ bool shouldSuppressDeclImport(const clang::Decl *decl);
/// but are now renamed using the swift_name attribute.
bool isSpecialUIKitStructZeroProperty(const clang::NamedDecl *decl);

/// Identifies certain AppKit constants that have overlay equivalents but are
/// also annotated with SwiftName API Notes attributes at the same time. Older
/// Clang versions were silently dropping the API Notes attribute on those
/// constants.
bool isSpecialAppKitFunctionKeyProperty(const clang::NamedDecl *decl);

/// \returns true if \p a has the same underlying type as \p b after removing
/// any pointer/reference specifiers. Note that this does not currently look through
/// nested types other than pointers or references.
Expand Down
3 changes: 3 additions & 0 deletions test/Interop/Cxx/objc-correctness/appkit-uikit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import AppKit
var _: AttributeScopes.AppKitAttributes.UnderlineStyleAttribute! = nil

var _ = NSEvent.SpecialKey.upArrow.rawValue
var _ = NSEvent.SpecialKey.enter.rawValue
var _ = NSUpArrowFunctionKey
var _ = NSEnterCharacter

#elseif canImport(UIKit)
import UIKit
Expand Down