swift
SE-0444: Fix interactions with Cxx interop
#76756
Merged

SE-0444: Fix interactions with Cxx interop #76756

tshortli
tshortli287 days ago (edited 287 days ago)

With the upcoming MemberImportVisibility feature enabled, code built with Cxx interop also enabled could be rejected by the compiler with cryptic errors about the __ObjC module not being imported. This is the result of a surprising implementation detail of Cxx interop. When importing C++ namespaces and their members, the Clang importer puts these declarations in the Clang header import module (a.k.a. the bridging header module, __ObjC). C++ namespaces don't have a logical modular home in the Swift AST because they can span multiple modules, so it's understandable why this implementation was chosen. However, the concrete members of namespaces also get placed in the __ObjC module too, and this really confuses things.

To work around this idiosyncrasy of Cxx interop, I've introduced Decl::getModuleContextForNameLookup() which returns the module that a declaration would ideally belong to if Cxx interop didn't have this behavior. This alternative to Decl::getModuleContext() is now used everywhere that MemberImportVisibility rules are enforced to provide consistency.

Additionally, I found that I also had to further special-case the header import module for Cxx interop because it turns out that there are some additional declarations, beyond imported namespaces, that also live there and need to be implicitly visible in every source file. The __ObjC module is not implicitly imported in source files when Cxx interop is enabled, so these declarations are not deemed visible under normal name lookup rules. When I tried to add an implicit import of __ObjC when Cxx interop is enabled, it broke a bunch tests. So for now, when a decl really belongs to the __ObjC module in Cxx interop mode, we just always allow it to be referenced.

This Cxx interop behavior really needs a re-think in my opinion, but that will require larger discussions.

Resolves rdar://136600598.

tshortli AST: Introduce ModuleDecl::isClangHeaderImportModule() convenience.
07b84fcc
tshortli tshortli requested a review from beccadax beccadax 287 days ago
tshortli tshortli requested a review from ahoppen ahoppen 287 days ago
tshortli tshortli requested a review from bnbarham bnbarham 287 days ago
tshortli tshortli requested a review from hamishknight hamishknight 287 days ago
tshortli tshortli requested a review from rintaro rintaro 287 days ago
tshortli tshortli requested a review from zoecarver zoecarver 287 days ago
tshortli tshortli requested a review from hyp hyp 287 days ago
tshortli tshortli requested a review from egorzhdan egorzhdan 287 days ago
tshortli tshortli requested a review from Xazax-hun Xazax-hun 287 days ago
tshortli tshortli requested a review from xymus xymus 287 days ago
tshortli tshortli requested a review from hborla hborla 287 days ago
tshortli tshortli requested a review from slavapestov slavapestov 287 days ago
tshortli tshortli requested a review from xedin xedin 287 days ago
tshortli SE-0444: Fix interactions with Cxx interop.
b11bb1ce
tshortli tshortli force pushed from c18f72c3 to b11bb1ce 287 days ago
tshortli
tshortli287 days ago

@swift-ci please smoke test

tshortli tshortli merged d2b562ac into main 286 days ago
tshortli tshortli deleted the member-import-visibility-cxx branch 286 days ago

Login to write a write a comment.

Login via GitHub

Assignees
No one assigned
Labels
Milestone