Files
swift-mirror/test/Interop/SwiftToCxx/expose-attr/expose-swift-decls-to-cxx.swift
Gabor Horvath a13940f1c4 [cxx-interop] Do not generate aborting move constructors
In the reverse interop header we generated move constructors that call
abort at runtime. This is problematic for several reasons:
* In C++14 and below some of our own generated functions like _make
  ended up calling the move constructor. Those are calling abort and
  also trigger unreachable code warning in newer versions of Clang.
  In C++17 and up it is fine due to the guaranteed copy elision.
* Type traits are fooled and think these types are movable. As a result,
  libraries could generate calls to the aborting move ctor.

This PR removes the generation of move operations. As we generate copy
operations, the compiler will not declare or define the move operations
implicitly. Whenever the user goes out of their way and try to move an
object they will get a copy instead.

rdar://150793518
2025-07-08 16:45:58 +01:00

124 lines
4.9 KiB
Swift

// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -module-name Expose -enable-experimental-cxx-interop -clang-header-expose-decls=has-expose-attr -typecheck -verify -emit-clang-header-path %t/expose.h
// RUN: %FileCheck %s < %t/expose.h
// RUN: %check-interop-cxx-header-in-clang(%t/expose.h -Wno-error=unused-function)
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -emit-module -module-name Expose -o %t
// RUN: %target-swift-frontend -parse-as-library %t/Expose.swiftmodule -module-name Expose -enable-experimental-cxx-interop -clang-header-expose-decls=has-expose-attr -typecheck -verify -emit-clang-header-path %t/expose.h
// RUN: %FileCheck %s < %t/expose.h
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -enable-library-evolution -typecheck -emit-module-interface-path %t/Expose.swiftinterface -module-name Expose
// RUN: %target-swift-frontend -parse-as-library %t/Expose.swiftinterface -enable-library-evolution -disable-objc-attr-requires-foundation-module -module-name Expose -enable-experimental-cxx-interop -clang-header-expose-decls=has-expose-attr -typecheck -verify -emit-clang-header-path %t/expose.h
// RUN: %FileCheck %s < %t/expose.h
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend %s -enable-library-evolution -typecheck -emit-module-interface-path %t/Expose.swiftinterface -module-name Expose
// RUN: %target-swift-frontend -parse-as-library %t/Expose.swiftinterface -enable-library-evolution -disable-objc-attr-requires-foundation-module -module-name Expose -clang-header-expose-decls=has-expose-attr-or-stdlib -typecheck -verify -emit-clang-header-path %t/expose.h
// RUN: %FileCheck %s < %t/expose.h
@_expose(Cxx)
public func exposed1() {
}
public func exposed2NotExposed() {
}
@_expose(Cxx)
public func exposed3() {
}
@_expose(Cxx, "exposed4")
public func exposed4Renamed() {
}
@_expose(Cxx)
public struct ExposedStruct {
public var x: Int
public func method() {}
}
public struct NotExposedStruct {
public var x: Int
}
@_expose(Cxx, "ExposedStruct2")
public struct ExposedStructRenamed {
public var y: Int
@_expose(Cxx)
public init() { y = 42; prop = 0; prop2 = 0; }
@_expose(Cxx, "initWithValue")
public init(why x: Int) { y = x; prop = 0; prop2 = 0; }
@_expose(Cxx, "renamedProp")
public var prop: Int
@_expose(Cxx, "prop3")
public let prop2: Int
@_expose(Cxx, "renamedMethod")
public func method() {}
public func getNonExposedStruct() -> NotExposedStruct {
return NotExposedStruct(x: 2)
}
// FIXME: if 'getNonExposedStruct' has explicit @_expose we should error in Sema.
public func passNonExposedStruct(_ x: NotExposedStruct) {
}
// FIXME: if 'passNonExposedStruct' has explicit @_expose we should error in Sema.
}
@_expose(Cxx)
public final class ExposedClass {
public func method() {}
}
// CHECK: class SWIFT_SYMBOL("{{.*}}") ExposedClass final
// CHECK: class SWIFT_SYMBOL("{{.*}}") ExposedStruct final {
// CHECK: class SWIFT_SYMBOL("{{.*}}") ExposedStruct2 final {
// CHECK: SWIFT_INLINE_THUNK ExposedStruct2 &operator =(const ExposedStruct2 &other) noexcept {
// CHECK: }
// CHECK-NEXT: swift::Int getY() const SWIFT_SYMBOL("{{.*}}");
// CHECK-NEXT: void setY(swift::Int value) SWIFT_SYMBOL("{{.*}}");
// CHECK-NEXT: static SWIFT_INLINE_THUNK ExposedStruct2 init() SWIFT_SYMBOL("{{.*}}");
// CHECK-NEXT: static SWIFT_INLINE_THUNK ExposedStruct2 initWithValue(swift::Int x) SWIFT_SYMBOL("{{.*}}");
// CHECK-NEXT: swift::Int getRenamedProp() const SWIFT_SYMBOL("{{.*}}");
// CHECK-NEXT: void setRenamedProp(swift::Int value) SWIFT_SYMBOL("{{.*}}");
// CHECK-NEXT: swift::Int getProp3() const SWIFT_SYMBOL("{{.*}}");
// CHECK-NEXT: void renamedMethod() const SWIFT_SYMBOL("{{.*}}");
// CHECK-NEXT: private:
// CHECK: SWIFT_INLINE_THUNK void exposed1() noexcept SWIFT_SYMBOL("{{.*}}") {
// CHECK-NEXT: _impl::$s6Expose8exposed1yyF();
// CHECK-NEXT: }
// CHECK-EMPTY:
// CHECK-NEXT: SWIFT_INLINE_THUNK void exposed3() noexcept SWIFT_SYMBOL("{{.*}}") {
// CHECK-NEXT: _impl::$s6Expose8exposed3yyF();
// CHECK-NEXT: }
// CHECK-EMPTY:
// CHECK-NEXT: SWIFT_INLINE_THUNK void exposed4() noexcept SWIFT_SYMBOL("{{.*}}") {
// CHECK-NEXT: _impl::$s6Expose15exposed4RenamedyyF();
// CHECK-NEXT: }
// CHECK: void ExposedClass::method()
// CHECK: swift::Int ExposedStruct::getX() const {
// CHECK: void ExposedStruct::setX(swift::Int value) {
// CHECK: void ExposedStruct::method() const {
// CHECK: swift::Int ExposedStruct2::getY() const {
// CHECK: void ExposedStruct2::setY(swift::Int value) {
// CHECK: ExposedStruct2 ExposedStruct2::init() {
// CHECK: ExposedStruct2 ExposedStruct2::initWithValue(swift::Int x) {
// CHECK: swift::Int ExposedStruct2::getRenamedProp() const {
// CHECK: void ExposedStruct2::setRenamedProp(swift::Int value) {
// CHECK: swift::Int ExposedStruct2::getProp3() const {
// CHECK: void ExposedStruct2::renamedMethod() const {
// CHECK-NOT: NonExposedStruct