Skip to content

Commit 43cef14

Browse files
author
Gabor Horvath
committed
[cxx-interop] Do not consider std::span<T>::size and co unsafe
Since these APIs operate on unsafes we consider them unsafe by default. This PR adds APINotes to override these defaults as these APIs will not touch the underlying buffer. This should slightly reduce the number of `unsafe`s in user code. rdar://159839254
1 parent 4183eca commit 43cef14

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed

stdlib/public/Cxx/std/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ add_custom_command_target(unused_var
66
CUSTOM_TARGET_NAME CxxStdlib-apinotes
77
COMMAND ${CMAKE_COMMAND} "-E" "make_directory" "${output_dir}"
88
COMMAND ${CMAKE_COMMAND} "-E" "copy_if_different" "${CMAKE_CURRENT_SOURCE_DIR}/std.apinotes" "${output_dir}"
9+
COMMAND ${CMAKE_COMMAND} "-E" "copy_if_different" "${CMAKE_CURRENT_SOURCE_DIR}/std_span.apinotes" "${output_dir}"
10+
COMMAND ${CMAKE_COMMAND} "-E" "copy_if_different" "${CMAKE_CURRENT_SOURCE_DIR}/std_string_view.apinotes" "${output_dir}"
911
COMMENT "Copying CxxStdlib API Notes to ${output_dir}"
1012
OUTPUT "${output_dir}/std.apinotes"
11-
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/std.apinotes")
13+
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/std.apinotes"
14+
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/std_span.apinotes"
15+
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/std_string_view.apinotes")
1216

1317
swift_install_in_component(FILES std.apinotes
1418
DESTINATION "lib/swift/apinotes"

stdlib/public/Cxx/std/std.apinotes

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,20 @@ Namespaces:
1717
Methods:
1818
- Name: data
1919
SwiftName: __dataUnsafe()
20+
- Name: size
21+
SwiftSafety: safe
22+
- Name: size_bytes
23+
SwiftSafety: safe
24+
- Name: empty
25+
SwiftSafety: safe
26+
- Name: basic_string_view
27+
Methods:
28+
- Name: size
29+
SwiftSafety: safe
30+
- Name: max_size
31+
SwiftSafety: safe
32+
- Name: length
33+
SwiftSafety: safe
34+
- Name: empty
35+
SwiftSafety: safe
36+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Name: std_span
2+
Namespaces:
3+
- Name: std
4+
Tags:
5+
- Name: span
6+
Methods:
7+
- Name: data
8+
SwiftName: __dataUnsafe()
9+
- Name: size
10+
SwiftSafety: safe
11+
- Name: size_bytes
12+
SwiftSafety: safe
13+
- Name: empty
14+
SwiftSafety: safe
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Name: std_string_view
2+
Namespaces:
3+
- Name: std
4+
Tags:
5+
- Name: basic_string_view
6+
Methods:
7+
- Name: size
8+
SwiftSafety: safe
9+
- Name: max_size
10+
SwiftSafety: safe
11+
- Name: length
12+
SwiftSafety: safe
13+
- Name: empty
14+
SwiftSafety: safe

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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 -Xcc -std=c++20 -I %t/Inputs %t/test.swift -strict-memory-safety -enable-experimental-feature LifetimeDependence -cxx-interoperability-mode=default -diagnostic-style llvm 2>&1
4+
// RUN: %target-swift-frontend -typecheck -verify -I %swift_src_root/lib/ClangImporter/SwiftBridging -Xcc -iapinotes-modules -Xcc %swift_src_root/stdlib/public/Cxx/std -Xcc -std=c++20 -I %t/Inputs %t/test.swift -strict-memory-safety -enable-experimental-feature LifetimeDependence -cxx-interoperability-mode=default -diagnostic-style llvm 2>&1
55

66
// REQUIRES: objc_interop
77
// REQUIRES: swift_feature_LifetimeDependence
@@ -134,6 +134,7 @@ func useUnsafeTuple(x: UnsafeTuple) {
134134
func useCppSpan(x: SpanOfInt) {
135135
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}
136136
_ = x // expected-note{{reference to parameter 'x' involves unsafe type}}
137+
_ = x.size()
137138
}
138139

139140
func useCppSpan2(x: SpanOfIntAlias) {

0 commit comments

Comments
 (0)