|
| 1 | +// RUN: %empty-directory(%t) |
| 2 | +// RUN: split-file %s %t |
| 3 | +// RUN: %target-swift-frontend -cxx-interoperability-mode=default -emit-ir -I %swift_src_root/lib/ClangImporter/SwiftBridging -I %t%{fs-sep}Inputs %t%{fs-sep}test.swift -Xcc -fignore-exceptions -verify -verify-additional-file %t%{fs-sep}Inputs%{fs-sep}noncopyable.h -verify-additional-prefix TEST1- -D TEST1 |
| 4 | +// RUN: %target-swift-frontend -cxx-interoperability-mode=default -emit-ir -I %swift_src_root/lib/ClangImporter/SwiftBridging -I %t%{fs-sep}Inputs %t%{fs-sep}test.swift -Xcc -fignore-exceptions -verify -verify-additional-file %t%{fs-sep}Inputs%{fs-sep}noncopyable.h -verify-additional-prefix TEST2- -D TEST2 |
| 5 | +// RUN: %target-swift-frontend -cxx-interoperability-mode=default -emit-ir -I %swift_src_root/lib/ClangImporter/SwiftBridging -I %t%{fs-sep}Inputs %t%{fs-sep}test.swift -Xcc -fignore-exceptions -verify -verify-additional-file %t%{fs-sep}Inputs%{fs-sep}noncopyable.h -verify-additional-prefix TEST3- -D TEST3 |
| 6 | +// RUN: %target-swift-frontend -cxx-interoperability-mode=default -emit-ir -I %swift_src_root/lib/ClangImporter/SwiftBridging -I %t%{fs-sep}Inputs %t%{fs-sep}test.swift -Xcc -fignore-exceptions -verify -verify-additional-file %t%{fs-sep}Inputs%{fs-sep}noncopyable.h -verify-additional-prefix TEST4- -D TEST4 -Xcc -std=c++20 |
| 7 | + |
| 8 | +//--- Inputs/module.modulemap |
| 9 | +module Test { |
| 10 | + header "noncopyable.h" |
| 11 | + requires cplusplus |
| 12 | +} |
| 13 | + |
| 14 | +//--- Inputs/noncopyable.h |
| 15 | +#include "swift/bridging" |
| 16 | +#include <string> |
| 17 | + |
| 18 | +struct NonCopyable { |
| 19 | + NonCopyable() = default; |
| 20 | + NonCopyable(const NonCopyable& other) = delete; // expected-note {{'NonCopyable' has been explicitly marked deleted here}} |
| 21 | + NonCopyable(NonCopyable&& other) = default; |
| 22 | +}; |
| 23 | + |
| 24 | +template <typename T> |
| 25 | +struct OwnsT { |
| 26 | + T element; |
| 27 | + OwnsT() {} |
| 28 | + OwnsT(const OwnsT &other) : element(other.element) {} |
| 29 | + // expected-error@-1 *{{call to deleted constructor of 'NonCopyable'}} |
| 30 | + // expected-note@-2 *{{in instantiation of member function 'OwnsT<NonCopyable>::OwnsT' requested here}} |
| 31 | + // expected-TEST1-error@-3 {{failed to copy 'OwnsT<NonCopyable>'; did you mean to import 'OwnsT<NonCopyable>' as ~Copyable?}} |
| 32 | + // expected-TEST1-note@-4 {{use 'requires' (since C++20) to specify the constraints under which the copy constructor is available}} |
| 33 | + // expected-TEST1-note@-5 {{annotate a type with 'SWIFT_COPYABLE_IF(<T>)' in C++ to specify that the type is Copyable if <T> is Copyable}} |
| 34 | + OwnsT(OwnsT&& other) {} |
| 35 | +}; |
| 36 | + |
| 37 | +using OwnsNonCopyable = OwnsT<NonCopyable>; |
| 38 | + |
| 39 | +template <typename T> struct Derived : OwnsT<T> { |
| 40 | + // expected-TEST2-error@-1 {{failed to copy 'Derived<NonCopyable>'; did you mean to import 'Derived<NonCopyable>' as ~Copyable?}} |
| 41 | + // expected-TEST2-note@-2 {{use 'requires' (since C++20) to specify the constraints under which the copy constructor is available}} |
| 42 | + // expected-TEST2-note@-3 {{annotate a type with 'SWIFT_COPYABLE_IF(<T>)' in C++ to specify that the type is Copyable if <T> is Copyable}} |
| 43 | + // expected-TEST2-note@-4 {{annotate a type with 'SWIFT_NONCOPYABLE' in C++ to import it as ~Copyable}} |
| 44 | +}; |
| 45 | + |
| 46 | +using DerivedNonCopyable = Derived<NonCopyable>; |
| 47 | + |
| 48 | +template <typename T> struct SWIFT_COPYABLE_IF(T) Annotated { |
| 49 | + T element; |
| 50 | + Annotated() : element() {} |
| 51 | + Annotated(const Annotated &other) : element(other.element) {} |
| 52 | + // expected-TEST3-error@-1 {{failed to copy 'Annotated<OwnsT<NonCopyable>>'; did you mean to import 'Annotated<OwnsT<NonCopyable>>' as ~Copyable?}} |
| 53 | + // expected-TEST3-note@-2 {{one of the types that 'Annotated<OwnsT<NonCopyable>>' depends on may need a 'requires' clause (since C++20) in the copy constructor, a 'SWIFT_COPYABLE_IF' annotation or a 'SWIFT_NONCOPYABLE' annotation'}} |
| 54 | + // expected-TEST3-note@-3 {{the 'SWIFT_COPYABLE_IF' annotation on 'Annotated<OwnsT<NonCopyable>>' may be missing a parameter}} |
| 55 | + |
| 56 | + Annotated(Annotated &&) = default; |
| 57 | +}; |
| 58 | + |
| 59 | +using AnnotatedOwnsNonCopyable = Annotated<OwnsT<NonCopyable>>; |
| 60 | + |
| 61 | +#if __cplusplus >= 202002L |
| 62 | +template <typename T> struct Requires { |
| 63 | + T element; |
| 64 | + Requires() : element() {} |
| 65 | + Requires(const Requires &other) requires std::is_copy_constructible_v<T> : element(other.element) {} |
| 66 | + // expected-TEST4-error@-1 {{failed to copy 'Requires<OwnsT<NonCopyable>>'; did you mean to import 'Requires<OwnsT<NonCopyable>>' as ~Copyable?}} |
| 67 | + // expected-TEST4-note@-2 {{one of the types that 'Requires<OwnsT<NonCopyable>>' depends on may need a 'requires' clause (since C++20) in the copy constructor, a 'SWIFT_COPYABLE_IF' annotation or a 'SWIFT_NONCOPYABLE' annotation'}} |
| 68 | + // expected-TEST4-note@-3 {{the 'requires' clause on the copy constructor of 'Requires<OwnsT<NonCopyable>>' may be missing a constraint}} |
| 69 | + |
| 70 | + Requires(Requires &&) = default; |
| 71 | +}; |
| 72 | + |
| 73 | +using RequiresOwnsNonCopyable = Requires<OwnsT<NonCopyable>>; |
| 74 | +#endif |
| 75 | + |
| 76 | +//--- test.swift |
| 77 | +import Test |
| 78 | +import CxxStdlib |
| 79 | + |
| 80 | +func takeCopyable<T: Copyable>(_ x: T) {} |
| 81 | + |
| 82 | +#if TEST1 |
| 83 | +func simpleTest() { |
| 84 | + let s = OwnsNonCopyable() |
| 85 | + takeCopyable(s) |
| 86 | +} |
| 87 | + |
| 88 | +#elseif TEST2 |
| 89 | +func derived() { |
| 90 | + let s = DerivedNonCopyable() |
| 91 | + takeCopyable(s) |
| 92 | +} |
| 93 | + |
| 94 | +#elseif TEST3 |
| 95 | +func annotated() { |
| 96 | + let s = AnnotatedOwnsNonCopyable() |
| 97 | + // Annotated has a correct SWIFT_COPYABLE_IF annotation, but OwnsT does not |
| 98 | + // Since we import OwnsT<NonCopyable> as Copyable (even though it cannot be copy constructible), |
| 99 | + // we end up also importing Annotated<OwnsT<NonCopyable>> as Copyable |
| 100 | + takeCopyable(s) |
| 101 | +} |
| 102 | + |
| 103 | +#elseif TEST4 |
| 104 | +func requires() { |
| 105 | + let s = RequiresOwnsNonCopyable() |
| 106 | + // Requires makes use of 'requires' correctly, but OwnsT is missing some information |
| 107 | + // Since we import OwnsT<NonCopyable> as Copyable (even though it cannot be copy constructible), |
| 108 | + // we end up also importing Requires<OwnsT<NonCopyable>> as Copyable |
| 109 | + takeCopyable(s) |
| 110 | +} |
| 111 | + |
| 112 | +#endif |
0 commit comments