Skip to content

Commit 4ee8e0d

Browse files
authored
Merge pull request #78115 from swiftlang/gaborh/cpp-span-unsafe
[cxx-interop] Make sure C++ span is imported as @unsafe
2 parents 1bdefa1 + 985c3a4 commit 4ee8e0d

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5097,8 +5097,6 @@ static const llvm::StringMap<std::vector<int>> STLConditionalEscapableParams{
50975097
{"multimap", {0, 1}},
50985098
{"flat_multimap", {0, 1}},
50995099
{"unordered_multimap", {0, 1}},
5100-
{"span", {0}}, // TODO: remove when span is non-escapable by default
5101-
{"mdspan", {0}}, // TODO: remove when mdspan is non-escapable by default
51025100
};
51035101

51045102
static std::set<StringRef>

lib/ClangImporter/ImportDecl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,6 +1516,9 @@ namespace {
15161516
/*genericparams*/nullptr, DC);
15171517

15181518
Result->setUnderlyingType(SwiftType);
1519+
if (SwiftType->isUnsafe())
1520+
Result->getAttrs().add(new (Impl.SwiftContext)
1521+
UnsafeAttr(/*implicit=*/true));
15191522

15201523
// Make Objective-C's 'id' unavailable.
15211524
if (Impl.SwiftContext.LangOpts.EnableObjCInterop && isObjCId(Decl)) {

lib/Sema/TypeCheckDecl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "swift/AST/Attr.h"
3535
#include "swift/AST/ClangModuleLoader.h"
3636
#include "swift/AST/ConformanceLookup.h"
37+
#include "swift/AST/Decl.h"
3738
#include "swift/AST/DiagnosticsParse.h"
3839
#include "swift/AST/ExistentialLayout.h"
3940
#include "swift/AST/Expr.h"
@@ -47,6 +48,7 @@
4748
#include "swift/AST/PropertyWrappers.h"
4849
#include "swift/AST/ProtocolConformance.h"
4950
#include "swift/AST/SourceFile.h"
51+
#include "swift/AST/Type.h"
5052
#include "swift/AST/TypeCheckRequests.h"
5153
#include "swift/AST/TypeWalker.h"
5254
#include "swift/Basic/Assertions.h"

test/Interop/Cxx/class/safe-interop-mode.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11

22
// RUN: rm -rf %t
33
// RUN: split-file %s %t
4-
// RUN: %target-swift-frontend -typecheck -verify -I %swift_src_root/lib/ClangImporter/SwiftBridging -I %t/Inputs %t/test.swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe -enable-experimental-feature SafeInterop -cxx-interoperability-mode=default -diagnostic-style llvm 2>&1
4+
// RUN: %target-swift-frontend -typecheck -verify -I %swift_src_root/lib/ClangImporter/SwiftBridging -Xcc -std=c++20 -I %t/Inputs %t/test.swift -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature WarnUnsafe -enable-experimental-feature SafeInterop -enable-experimental-feature LifetimeDependence -cxx-interoperability-mode=default -diagnostic-style llvm 2>&1
55

66
// REQUIRES: objc_interop
77
// REQUIRES: swift_feature_AllowUnsafeAttribute
88
// REQUIRES: swift_feature_SafeInterop
99
// REQUIRES: swift_feature_WarnUnsafe
10+
// REQUIRES: swift_feature_LifetimeDependence
1011

1112
//--- Inputs/module.modulemap
1213
module Test {
@@ -16,6 +17,7 @@ module Test {
1617

1718
//--- Inputs/nonescapable.h
1819
#include "swift/bridging"
20+
#include <span>
1921

2022
struct SWIFT_NONESCAPABLE View {
2123
__attribute__((swift_attr("@lifetime(immortal)")))
@@ -50,6 +52,9 @@ struct MyContainer {
5052
int end() const { return -1; }
5153
};
5254

55+
using SpanOfInt = Unannotated;
56+
using SpanOfIntAlias = SpanOfInt;
57+
5358
//--- test.swift
5459

5560
import Test
@@ -75,3 +80,11 @@ func useSafeParams(x: Owner, y: View, z: SafeEscapableAggregate, c: MyContainer)
7580

7681
func useCfType(x: CFArray) {
7782
}
83+
84+
// expected-note@+1{{make global function 'useCppSpan' @unsafe to indicate that its use is not memory-safe}}
85+
func useCppSpan(x: SpanOfInt) { // expected-warning{{reference to unsafe type alias 'SpanOfInt'}}
86+
}
87+
88+
// expected-note@+1{{make global function 'useCppSpan2' @unsafe to indicate that its use is not memory-safe}}
89+
func useCppSpan2(x: SpanOfIntAlias) { // expected-warning{{reference to unsafe type alias 'SpanOfIntAlias'}}
90+
}

0 commit comments

Comments
 (0)