-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[cxx-interop] Use formal C++ interop mode to fix name lookup in module interfaces #79984
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
test/Interop/Cxx/modules/prevent-module-namespace-shadowed-by-namespace.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// XFAIL: * | ||
// This test currently fails because there's no way to explicitly refer to | ||
// a module that has been shadowed by another declaration, e.g., a namespace. | ||
// Unlike in the prevent-module-shadowed-by-namespace.swift test, the textual | ||
// interface is generated with C++ interop enabled, which means namespaces are | ||
// not filtered out during name lookup when the interface is recompiled later. | ||
|
||
// RUN: %empty-directory(%t) | ||
// RUN: %empty-directory(%t/include) | ||
// RUN: split-file %s %t | ||
|
||
// Compile shim.swift with C++ interop, check that its interface is usable | ||
// (despite using a mix of C/C++ decls): | ||
// | ||
// RUN: %empty-directory(%t/lib) | ||
// RUN: %target-swift-emit-module-interface(%t/lib/shim.swiftinterface) %t/shim.swift -cxx-interoperability-mode=default -module-name shim -I %t/include | ||
// RUN: %FileCheck %t/shim.swift < %t/lib/shim.swiftinterface | ||
// RUN: %target-swift-frontend %t/program.swift -typecheck -verify -cxx-interoperability-mode=default -I %t/include -I %t/lib | ||
|
||
//--- include/module.modulemap | ||
// A Clang module which will first be compiled in C mode, and then later compiled in C++ mode | ||
module c2cxx { | ||
header "c2cxx.h" | ||
export * | ||
} | ||
|
||
//--- include/c2cxx.h | ||
// A header file that defines a namespace with the same name as the module, | ||
// a common C++ idiom. We want to make sure that it does not shadow the module | ||
// in textual interfaces generated with C++ interop disabled, but later compiled | ||
// with C++ interop enabled. | ||
#ifndef __C2CXX_NAMESPACE_H | ||
#define __C2CXX_NAMESPACE_H | ||
typedef int c2cxx_number; // always available and resilient | ||
#ifdef __cplusplus | ||
namespace c2cxx { typedef c2cxx_number number; }; // only available in C++ | ||
#endif // __cplusplus | ||
#endif // __C2CXX_NAMESPACE_H | ||
|
||
//--- shim.swift | ||
// A shim around c2cxx that refers to a mixture of namespaced (C++) and | ||
// top-level (C) decls; requires cxx-interoperability-mode | ||
import c2cxx | ||
public func shimId(_ n: c2cxx.number) -> c2cxx_number { return n } | ||
// ^^^^^`- refers to the namespace | ||
// CHECK: public func shimId(_ n: c2cxx.c2cxx.number) -> c2cxx.c2cxx_number | ||
// ^^^^^\^^^^^`-namespace ^^^^^`-module | ||
// `-module | ||
|
||
@inlinable public func shimIdInline(_ n: c2cxx_number) -> c2cxx.number { | ||
// CHECK: public func shimIdInline(_ n: c2cxx.c2cxx_number) -> c2cxx.c2cxx.number | ||
// ^^^^^`-module ^^^^^\^^^^^`-namespace | ||
// `-module | ||
let m: c2cxx.number = n | ||
// CHECK: let m: c2cxx.number = n | ||
// ^^^^^`-namespace | ||
return m | ||
} | ||
|
||
//--- program.swift | ||
// Uses the shim and causes it to be (re)built from its interface | ||
import shim | ||
func numberwang() { let _ = shimId(42) + shimIdInline(24) } |
57 changes: 57 additions & 0 deletions
57
test/Interop/Cxx/modules/prevent-module-shadowed-by-namespace.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// RUN: %empty-directory(%t) | ||
// RUN: %empty-directory(%t/include) | ||
// RUN: split-file %s %t | ||
|
||
// Generate textual interface for shim.swift without C++ interop enabled, then | ||
// re-compile it with C++ interop enabled (because it is used by program.swift, | ||
// which is compiled with C++ interop enabled). | ||
// | ||
// RUN: %empty-directory(%t/lib) | ||
// RUN: %target-swift-emit-module-interface(%t/lib/shim.swiftinterface) %t/shim.swift -module-name shim -I %t/include | ||
// RUN: %FileCheck %t/shim.swift < %t/lib/shim.swiftinterface | ||
// RUN: %target-swift-frontend %t/program.swift -typecheck -verify -cxx-interoperability-mode=default -I %t/include -I %t/lib | ||
|
||
//--- include/module.modulemap | ||
// A Clang module which will first be compiled in C mode, and then later compiled in C++ mode | ||
module c2cxx { | ||
header "c2cxx.h" | ||
export * | ||
} | ||
|
||
//--- include/c2cxx.h | ||
// A header file that defines a namespace with the same name as the module, | ||
// a common C++ idiom. We want to make sure that it does not shadow the module | ||
// in textual interfaces generated with C++ interop disabled, but later compiled | ||
// with C++ interop enabled. | ||
#ifndef __C2CXX_NAMESPACE_H | ||
#define __C2CXX_NAMESPACE_H | ||
typedef int c2cxx_number; // always available and resilient | ||
#ifdef __cplusplus | ||
namespace c2cxx { typedef c2cxx_number number; }; // only available in C++ | ||
#endif // __cplusplus | ||
#endif // __C2CXX_NAMESPACE_H | ||
|
||
//--- shim.swift | ||
// A shim around c2cxx that exposes a c2cxx decl in its module interface | ||
import c2cxx | ||
|
||
// Exposes a (fully-qualified) c2cxx decl in its module interface. | ||
public func shimId(_ n: c2cxx_number) -> c2cxx_number { return n } | ||
// CHECK: public func shimId(_ n: c2cxx.c2cxx_number) -> c2cxx.c2cxx_number | ||
// ^^^^^`- refers to the module | ||
|
||
// @inlinable functions have their bodies splatted in the module interface file; | ||
// those verbatim bodies may contain unqualified names. | ||
@inlinable public func shimIdInline(_ n: c2cxx_number) -> c2cxx_number { | ||
// CHECK: public func shimIdInline(_ n: c2cxx.c2cxx_number) -> c2cxx.c2cxx_number | ||
// ^^^^^`- refers to the module | ||
let m: c2cxx_number = n | ||
// CHECK: let m: c2cxx_number = n | ||
// ^^^^^^^^^^^^`- verbatim (unqualified) | ||
return m | ||
} | ||
|
||
//--- program.swift | ||
// Uses the shim and causes it to be (re)built from its interface | ||
import shim | ||
func numberwang() { let _ = shimId(42) + shimIdInline(24) } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: If we only have 2 options here, maybe a
%select{
would be a better way to take the arguments.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My argument in favor of taking a
StringRef
here is that, at the call site,arg->getSpelling()
means something at face-value, rather thantrue
/false
or1
/0
. But I don't feel very strongly about this. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Orthogonally I'm even wondering whether we should combine this with this other note:
In fact,
valid arguments to <flag> are <options>
seems like a very re-usable note and it would be nice to de-duplicate some of that.But I think that can also be done in a follow-up patch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I am fine with this as is then.