Skip to content

Commit 838a754

Browse files
author
Gabor Horvath
committed
[cxx-interop] Consider std::string safe
The std::basic_string class is escapable only if its template argument is escapable. This change helps us consider the regular std::string type with the non-escapable char template argument as self-contained and a safe type to use. This prevents spurious warnings in strict memory safety mode.
1 parent 0ba886d commit 838a754

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5075,6 +5075,7 @@ TinyPtrVector<ValueDecl *> CXXNamespaceMemberLookup::evaluate(
50755075
}
50765076

50775077
static const llvm::StringMap<std::vector<int>> STLConditionalEscapableParams{
5078+
{"basic_string", {0}},
50785079
{"vector", {0}},
50795080
{"array", {0}},
50805081
{"inplace_vector", {0}},

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,20 @@ struct MyContainer {
5252
int end() const { return -1; }
5353
};
5454

55-
using SpanOfInt = Unannotated;
55+
using SpanOfInt = std::span<int>;
5656
using SpanOfIntAlias = SpanOfInt;
5757

5858
//--- test.swift
5959

6060
import Test
6161
import CoreFoundation
62+
import CxxStdlib
6263

6364
// expected-warning@+1{{global function 'useUnsafeParam' involves unsafe code; use '@unsafe' to indicate that its use is not memory-safe}}{{1-1=@unsafe }}
6465
func useUnsafeParam(x: Unannotated) { // expected-note{{reference to unsafe struct 'Unannotated'}}
6566
}
6667

67-
// expected-warning@+2{{global function 'useUnsafeParam2' involves unsafe code; use '@unsafe' to indicate that its use is not memory-safe}}{{10:1-1=@unsafe }}
68+
// expected-warning@+2{{global function 'useUnsafeParam2' involves unsafe code; use '@unsafe' to indicate that its use is not memory-safe}}{{11:1-1=@unsafe }}
6869
@available(SwiftStdlib 5.8, *)
6970
func useUnsafeParam2(x: UnsafeReference) { // expected-note{{reference to unsafe class 'UnsafeReference'}}
7071
}
@@ -81,6 +82,9 @@ func useSafeParams(x: Owner, y: View, z: SafeEscapableAggregate, c: MyContainer)
8182
func useCfType(x: CFArray) {
8283
}
8384

85+
func useString(x: std.string) {
86+
}
87+
8488
// expected-warning@+1{{global function 'useCppSpan' involves unsafe code; use '@unsafe' to indicate that its use is not memory-safe}}
8589
func useCppSpan(x: SpanOfInt) { // expected-note{{reference to unsafe type alias 'SpanOfInt'}}
8690
}

0 commit comments

Comments
 (0)