Skip to content

Commit a9d5903

Browse files
authored
Merge pull request #76956 from swiftlang/gaborh/treat-c-struct-as-resilient
[cxx-interop] Consider extern "C" structs as resilient
2 parents fc52e27 + 625f45a commit a9d5903

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

lib/Sema/TypeCheckAccess.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,20 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "TypeCheckAccess.h"
18-
#include "TypeChecker.h"
19-
#include "TypeCheckAvailability.h"
2018
#include "TypeAccessScopeChecker.h"
19+
#include "TypeCheckAvailability.h"
20+
#include "TypeChecker.h"
2121
#include "swift/AST/ASTVisitor.h"
2222
#include "swift/AST/ASTWalker.h"
2323
#include "swift/AST/ClangModuleLoader.h"
2424
#include "swift/AST/DiagnosticsSema.h"
2525
#include "swift/AST/ExistentialLayout.h"
2626
#include "swift/AST/Import.h"
27-
#include "swift/AST/Pattern.h"
2827
#include "swift/AST/ParameterList.h"
28+
#include "swift/AST/Pattern.h"
2929
#include "swift/AST/TypeCheckRequests.h"
3030
#include "swift/Basic/Assertions.h"
31+
#include "clang/AST/DeclCXX.h"
3132
#include "clang/AST/DeclObjC.h"
3233

3334
using namespace swift;
@@ -1883,6 +1884,11 @@ bool isFragileClangNode(const ClangNode &node) {
18831884
return isFragileClangType(pd->getType());
18841885
if (auto *typedefDecl = dyn_cast<clang::TypedefNameDecl>(decl))
18851886
return isFragileClangType(typedefDecl->getUnderlyingType());
1887+
if (auto *rd = dyn_cast<clang::RecordDecl>(decl)) {
1888+
if (!isa<clang::CXXRecordDecl>(rd))
1889+
return false;
1890+
return !rd->getDeclContext()->isExternCContext();
1891+
}
18861892
return true;
18871893
}
18881894

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
// RUN: %target-swift-frontend %t/test.swift -I %t/Inputs -typecheck -enable-library-evolution -enable-experimental-cxx-interop -verify
4+
5+
//--- Inputs/module.modulemap
6+
module CxxModule {
7+
header "header.h"
8+
requires cplusplus
9+
}
10+
11+
//--- Inputs/header.h
12+
13+
class CxxStruct {
14+
public:
15+
int x; int y;
16+
17+
void method() const;
18+
};
19+
20+
#if defined(__cplusplus)
21+
extern "C" {
22+
#endif
23+
24+
struct CStruct {
25+
int x; int y;
26+
};
27+
28+
#if defined(__cplusplus)
29+
}
30+
#endif
31+
32+
//--- test.swift
33+
34+
import CxxModule
35+
36+
public func useCStruct(_ x: CStruct) {
37+
}
38+
39+
// expected-error@+1 {{cannot use struct 'CxxStruct' here; C++ types from imported module 'CxxModule' do not support library evolution}}
40+
public func usesCxxStruct(_ x: CxxStruct) {
41+
}

0 commit comments

Comments
 (0)