Skip to content

Commit f70e5cb

Browse files
committed
CxxInterop: use Unsafe*Pointer for move-only
(cherry picked from commit 5ae2f6b)
1 parent b03f34e commit f70e5cb

File tree

9 files changed

+22
-30
lines changed

9 files changed

+22
-30
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,9 @@ namespace swift {
328328
/// when importing Swift modules that enable C++ interoperability.
329329
bool RequireCxxInteropToImportCxxInteropModule = true;
330330

331+
// Workaround for bug when building SwiftCompilerSources (rdar://128013193)
332+
bool CxxInteropUseOpaquePointerForMoveOnly = false;
333+
331334
/// On Darwin platforms, use the pre-stable ABI's mark bit for Swift
332335
/// classes instead of the stable ABI's bit. This is needed when
333336
/// targeting OSes prior to macOS 10.14.4 and iOS 12.2, where

include/swift/Option/FrontendOptions.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,11 @@ def cxx_interop_disable_requirement_at_import :
935935
HelpText<"Do not require C++ interoperability to be enabled when importing a Swift module that enables C++ interoperability">,
936936
Flags<[FrontendOption, HelpHidden]>;
937937

938+
def cxx_interop_use_opaque_pointer_for_moveonly :
939+
Flag<["-"], "cxx-interop-use-opaque-pointer-for-moveonly">,
940+
HelpText<"Testing flag that will be eliminated soon. Do not use.">,
941+
Flags<[FrontendOption, HelpHidden]>;
942+
938943
def use_malloc : Flag<["-"], "use-malloc">,
939944
HelpText<"Allocate internal data structures using malloc "
940945
"(for memory debugging)">;

lib/ClangImporter/ClangImporter.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5778,16 +5778,9 @@ cloneBaseMemberDecl(ValueDecl *decl, DeclContext *newContext) {
57785778
if (auto var = dyn_cast<VarDecl>(decl)) {
57795779
auto oldContext = var->getDeclContext();
57805780
auto oldTypeDecl = oldContext->getSelfNominalTypeDecl();
5781-
// If the base type is non-copyable, and non-copyable generics are disabled,
5782-
// we cannot synthesize the accessor, because its implementation would use
5783-
// `UnsafePointer<BaseTy>`.
5784-
// We cannot use `ty->isNoncopyable()` here because that would create a
5785-
// cyclic dependency between ModuleQualifiedLookupRequest and
5786-
// LookupConformanceInModuleRequest, so we check for the presence of
5787-
// move-only attribute that is implicitly added to non-copyable C++ types by
5788-
// ClangImporter.
5781+
// FIXME: this is a workaround for rdar://128013193
57895782
if (oldTypeDecl->getAttrs().hasAttribute<MoveOnlyAttr>() &&
5790-
!context.LangOpts.hasFeature(Feature::NoncopyableGenerics))
5783+
context.LangOpts.CxxInteropUseOpaquePointerForMoveOnly)
57915784
return nullptr;
57925785

57935786
auto rawMemory = allocateMemoryForDecl<VarDecl>(var->getASTContext(),

lib/ClangImporter/ImportType.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -509,19 +509,12 @@ namespace {
509509
return importFunctionPointerLikeType(*type, pointeeType);
510510
}
511511

512-
// If non-copyable generics are disabled, we cannot specify
513-
// UnsafePointer<T> with a non-copyable type T.
514-
// We cannot use `ty->isNoncopyable()` here because that would create a
515-
// cyclic dependency between ModuleQualifiedLookupRequest and
516-
// LookupConformanceInModuleRequest, so we check for the presence of
517-
// move-only attribute that is implicitly added to non-copyable C++ types
518-
// by ClangImporter.
512+
// FIXME: this is a workaround for rdar://128013193
519513
if (pointeeType && pointeeType->getAnyNominal() &&
520514
pointeeType->getAnyNominal()
521515
->getAttrs()
522516
.hasAttribute<MoveOnlyAttr>() &&
523-
!Impl.SwiftContext.LangOpts.hasFeature(
524-
Feature::NoncopyableGenerics)) {
517+
Impl.SwiftContext.LangOpts.CxxInteropUseOpaquePointerForMoveOnly) {
525518
auto opaquePointerDecl = Impl.SwiftContext.getOpaquePointerDecl();
526519
if (!opaquePointerDecl)
527520
return Type();

lib/Frontend/CompilerInvocation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1274,6 +1274,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
12741274
Opts.CxxInteropGettersSettersAsProperties = Args.hasArg(OPT_cxx_interop_getters_setters_as_properties);
12751275
Opts.RequireCxxInteropToImportCxxInteropModule =
12761276
!Args.hasArg(OPT_cxx_interop_disable_requirement_at_import);
1277+
Opts.CxxInteropUseOpaquePointerForMoveOnly =
1278+
Args.hasArg(OPT_cxx_interop_use_opaque_pointer_for_moveonly);
12771279

12781280
Opts.VerifyAllSubstitutionMaps |= Args.hasArg(OPT_verify_all_substitution_maps);
12791281

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
// RUN: %target-swift-ide-test -print-module -module-to-print=MoveOnlyCxxValueType -I %S/Inputs -cxx-interoperability-mode=upcoming-swift -source-filename=x | %FileCheck %s --check-prefix=CHECK-NO-NCG
2-
// RUN: %target-swift-ide-test -print-module -module-to-print=MoveOnlyCxxValueType -I %S/Inputs -cxx-interoperability-mode=upcoming-swift -source-filename=x -enable-experimental-feature NoncopyableGenerics | %FileCheck %s --check-prefix=CHECK-NCG
1+
// RUN: %target-swift-ide-test -print-module -module-to-print=MoveOnlyCxxValueType -I %S/Inputs -cxx-interoperability-mode=upcoming-swift -source-filename=x | %FileCheck %s
32

4-
// CHECK-NO-NCG: func getNonCopyablePtr() -> OpaquePointer
5-
// CHECK-NO-NCG: func getNonCopyableDerivedPtr() -> OpaquePointer
6-
7-
// CHECK-NCG: func getNonCopyablePtr() -> UnsafeMutablePointer<NonCopyable>
8-
// CHECK-NCG: func getNonCopyableDerivedPtr() -> UnsafeMutablePointer<NonCopyableDerived>
3+
// CHECK: func getNonCopyablePtr() -> UnsafeMutablePointer<NonCopyable>
4+
// CHECK: func getNonCopyableDerivedPtr() -> UnsafeMutablePointer<NonCopyableDerived>

test/Interop/Cxx/class/move-only/move-only-cxx-value-type.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift)
2-
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -enable-experimental-feature NoncopyableGenerics -D HAS_NONCOPYABLE_GENERICS)
2+
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -D HAS_NONCOPYABLE_GENERICS)
33
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-5.9 -O)
44
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-6 -O)
5-
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-6 -O -enable-experimental-feature NoncopyableGenerics -D HAS_NONCOPYABLE_GENERICS)
5+
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-6 -O -D HAS_NONCOPYABLE_GENERICS)
66

77
// REQUIRES: executable_test
88

test/Interop/Cxx/operators/move-only/move-only-synthesized-properties.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-5.9)
22
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-6)
33
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift)
4-
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -enable-experimental-feature NoncopyableGenerics -D HAS_NONCOPYABLE_GENERICS)
4+
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -D HAS_NONCOPYABLE_GENERICS)
55
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-5.9 -O)
66
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=swift-6 -O)
77
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -O)
8-
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -O -enable-experimental-feature NoncopyableGenerics -D HAS_NONCOPYABLE_GENERICS)
8+
// RUN: %target-run-simple-swift(-I %S/Inputs/ -cxx-interoperability-mode=upcoming-swift -O -D HAS_NONCOPYABLE_GENERICS)
99
//
1010
// REQUIRES: executable_test
1111

test/SILOptimizer/moveonly_optional_force_unwrap.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -enable-experimental-feature NoncopyableGenerics -emit-sil -verify %s
1+
// RUN: %target-swift-frontend -emit-sil -verify %s
22

33
struct NC: ~Copyable {
44
borrowing func borrow() {}

0 commit comments

Comments
 (0)