Skip to content

Commit 01aa575

Browse files
committed
[cxx-interop] mark C++ classes with trivial_abi attribute as unavailable in Swift
(cherry picked from commit 2c4188b)
1 parent d1a3d6a commit 01aa575

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,6 +2712,17 @@ namespace {
27122712
if (!result)
27132713
return nullptr;
27142714

2715+
if (decl->hasAttr<clang::TrivialABIAttr>()) {
2716+
// We cannot yet represent trivial_abi C++ records in Swift.
2717+
// Clang tells us such type can be passed in registers, so
2718+
// we avoid using AddressOnly type-layout for such type, which means
2719+
// that it then does not use C++'s copy and destroy semantics from
2720+
// Swift.
2721+
Impl.markUnavailable(cast<ValueDecl>(result),
2722+
"C++ classes with `trivial_abi` Clang attribute "
2723+
"are not yet available in Swift");
2724+
}
2725+
27152726
if (auto classDecl = dyn_cast<ClassDecl>(result)) {
27162727
validateForeignReferenceType(decl, classDecl);
27172728

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
4+
// RUN: %target-swift-ide-test -print-module -module-to-print=Test -I %t/Inputs -source-filename=x -enable-experimental-cxx-interop | %FileCheck %s
5+
// RUN: %target-swift-frontend -typecheck -I %t/Inputs %t/test.swift -enable-experimental-cxx-interop -verify
6+
7+
//--- Inputs/module.modulemap
8+
module Test {
9+
header "test.h"
10+
requires cplusplus
11+
}
12+
13+
//--- Inputs/test.h
14+
15+
class TrivialABIRecord {
16+
int x = 0;
17+
public:
18+
TrivialABIRecord() {}
19+
~TrivialABIRecord() {
20+
}
21+
}
22+
__attribute__((trivial_abi));
23+
24+
// CHECK: @available(*, unavailable, message: "C++ classes with `trivial_abi` Clang attribute are not yet available in Swift")
25+
// CHECK-NEXT: struct TrivialABIRecord {
26+
27+
//--- test.swift
28+
29+
import Test
30+
31+
func test() {
32+
let _ = TrivialABIRecord() // expected-error{{'TrivialABIRecord' is unavailable: C++ classes with `trivial_abi` Clang attribute are not yet available in Swift}}
33+
}

0 commit comments

Comments
 (0)