Skip to content

Commit ca4b0a1

Browse files
authored
Merge pull request #83361 from DougGregor/noncopyable-structs-c-interop
[Clang importer] Allow C structs to be noncopyable, too
2 parents d0cf535 + 21b9b9f commit ca4b0a1

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8232,6 +8232,9 @@ CxxRecordSemantics::evaluate(Evaluator &evaluator,
82328232

82338233
auto cxxDecl = dyn_cast<clang::CXXRecordDecl>(decl);
82348234
if (!cxxDecl) {
8235+
if (hasNonCopyableAttr(decl))
8236+
return CxxRecordSemanticsKind::MoveOnly;
8237+
82358238
return CxxRecordSemanticsKind::Trivial;
82368239
}
82378240

test/Interop/C/struct/Inputs/module.modulemap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ module ForeignReference {
1010
module StructAsOptionSet {
1111
header "struct-as-option-set.h"
1212
}
13+
14+
module NoncopyableStructs {
15+
header "noncopyable-struct.h"
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: %target-typecheck-verify-swift -I %S/Inputs/
2+
3+
typedef struct __attribute__((swift_attr("~Copyable"))) NonCopyable {
4+
float x, y;
5+
} NonCopyable;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
// Check that we get the expected errors for incorrect uses of noncopyable
3+
// imported types with both C and C++ interoperability.
4+
5+
// RUN: %target-swift-frontend -emit-sil -I %S/Inputs/ %s -verify -DERRORS
6+
// RUN: %target-swift-frontend -emit-sil -I %S/Inputs/ %s -verify -DERRORS -cxx-interoperability-mode=default
7+
8+
// Check that we get the expected IR
9+
10+
// RUN: %target-swift-frontend -emit-ir -I %S/Inputs/ %s -o - | %FileCheck %s
11+
// RUN: %target-swift-frontend -emit-ir -I %S/Inputs/ %s -o - -cxx-interoperability-mode=default| %FileCheck %s
12+
13+
import NoncopyableStructs
14+
15+
// CHECK-LABEL: define hidden swiftcc void @"$s19noncopyable_structs9consumeNCyySo11NonCopyableVnF"(float %0, float %1) #0 {
16+
// CHECK: call ptr @"$sSo11NonCopyableVWOh"
17+
// CHECK: define linkonce_odr hidden ptr @"$sSo11NonCopyableVWOh"(ptr %0)
18+
// CHECK-NEXT: entry:
19+
// CHECK-NEXT: ret ptr
20+
func consumeNC(_ nc: consuming NonCopyable) { }
21+
22+
func testNC() {
23+
let nc = NonCopyable() // expected-error{{'nc' consumed more than once}}
24+
consumeNC(nc) // expected-note{{consumed here}}
25+
26+
#if ERRORS
27+
consumeNC(nc) // expected-note{{consumed again here}}
28+
#endif
29+
}

0 commit comments

Comments
 (0)