Skip to content

Commit 0c7987c

Browse files
hypmeg-gupta
authored andcommitted
[cxx-interop] implicitly imported CxxShim module should not force client modules to enable C++ interoperability
Fixes swiftlang#65831
1 parent f51f8e8 commit 0c7987c

File tree

6 files changed

+49
-1
lines changed

6 files changed

+49
-1
lines changed

lib/Frontend/Frontend.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,7 @@ ImplicitImportInfo CompilerInstance::getImplicitImportInfo() const {
11761176
}
11771177

11781178
if (Invocation.getLangOptions().EnableCXXInterop && canImportCxxShim()) {
1179-
pushImport(CXX_SHIM_NAME);
1179+
pushImport(CXX_SHIM_NAME, {ImportFlags::ImplementationOnly});
11801180
}
11811181

11821182
imports.ShouldImportUnderlyingModule = frontendOpts.ImportUnderlyingModule;

test/Interop/Cxx/implementation-only-imports/Inputs/helper.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@ class MagicWrapper {
1111
MagicWrapper operator - (MagicWrapper other) {
1212
return MagicWrapper{_number - other._number};
1313
}
14+
15+
int baseMethod() const { return 42; }
1416
};
1517

1618
inline MagicWrapper operator + (MagicWrapper lhs, MagicWrapper rhs) {
1719
return MagicWrapper{lhs._number + rhs._number};
1820
}
1921

22+
class MagicWrapperDerived: public MagicWrapper {
23+
public:
24+
MagicWrapperDerived() { };
25+
};
26+
2027
#endif // TEST_INTEROP_CXX_IMPLEMENTATION_ONLY_IMPORTS_INPUTS_HELPER_H
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@_implementationOnly import CxxStdlib
2+
3+
public func testUsesCxxStdlib() {
4+
let string = std.string()
5+
print(String(string))
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@_implementationOnly import UserA
2+
3+
public func testUsesA() {
4+
let wrapper = MagicWrapperDerived()
5+
// This will force Swift to use __swift_interopStaticCast from CxxShim here.
6+
print(wrapper.baseMethod())
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend %S/Inputs/use-module-a-impl-only.swift -I %S/Inputs/ -module-name UseModuleAImplOnly -emit-module -emit-module-path %t/UseModuleAImplOnly.swiftmodule -cxx-interoperability-mode=default
4+
5+
// RUN: %target-swift-frontend %s -typecheck -module-name TestMod -I %t -I %S/Inputs
6+
7+
// Check that we have used something from CxxShim in 'UseModuleAImplOnly'
8+
// RUN: %target-swift-frontend %S/Inputs/use-module-a-impl-only.swift -I %S/Inputs/ -module-name UseModuleAImplOnly -emit-module -emit-module-path %t/UseModuleAImplOnly.swiftmodule -cxx-interoperability-mode=default -emit-sil -o - | %FileCheck %s
9+
// CHECK: __swift_interopStaticCast
10+
11+
import UseModuleAImplOnly
12+
13+
public func testCallsAPI() {
14+
testUsesA()
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-frontend %S/Inputs/use-cxx-stdlib-impl-only.swift -I %S/Inputs/ -module-name UseCxxStdlibImplOnly -emit-module -emit-module-path %t/UseCxxStdlibImplOnly.swiftmodule -cxx-interoperability-mode=default
4+
5+
// RUN: %target-swift-frontend %s -typecheck -module-name TestMod -I %t -I %S/Inputs
6+
7+
// REQUIRES: OS=macosx || OS=linux-gnu
8+
9+
import UseCxxStdlibImplOnly
10+
11+
public func testCallsAPI() {
12+
testUsesCxxStdlib()
13+
}

0 commit comments

Comments
 (0)