Skip to content

Commit

Permalink
[cxx-interop] Workaround name lookup issues with namespace os
Browse files Browse the repository at this point in the history
On Apple platforms, a system module `os` declares a `namespace os` under `#if defined(__cplusplus)`. This causes ClangImporter to import it as `enum os` when C++ interop is enabled. This causes name lookup ambiguity (module os vs namespace os) which is resolved in namespace's favor, breaking existing usages.

rdar://119044493
  • Loading branch information
egorzhdan committed Jan 14, 2025
1 parent 5f79319 commit 430809d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,14 @@ namespace {
// Do not import namespace declarations marked as 'swift_private'.
if (decl->hasAttr<clang::SwiftPrivateAttr>())
return nullptr;
// Workaround for os module declaring `namespace os` on Darwin, causing
// name lookup issues. That namespace only declares utility functions that
// are not supposed to be used from Swift, so let's just not import the
// namespace (rdar://119044493).
if (decl->getIdentifier() && decl->getName() == "os" &&
decl->getOwningModule() &&
decl->getOwningModule()->getTopLevelModuleName() == "os")
return nullptr;
// If this is a top-level namespace, don't put it in the module we're
// importing, put it in the "__ObjC" module that is implicitly imported.
if (!decl->getParent()->isNamespace())
Expand Down
8 changes: 8 additions & 0 deletions test/Interop/Cxx/objc-correctness/os-logger.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// RUN: %target-swift-frontend -typecheck -verify -I %S/Inputs -cxx-interoperability-mode=default %s

// REQUIRES: objc_interop
// REQUIRES: VENDOR=apple

import os

var _: os.Logger! = nil

0 comments on commit 430809d

Please sign in to comment.