Skip to content

Commit 792fb87

Browse files
committed
[Diagnostics] Add an edu note explaining @_nonEphemeral diags
1 parent b2a742c commit 792fb87

File tree

4 files changed

+80
-3
lines changed

4 files changed

+80
-3
lines changed

include/swift/AST/EducationalNotes.def

+13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@
2424
EDUCATIONAL_NOTES(unsupported_existential_type,
2525
"associated-type-requirements.md")
2626

27+
EDUCATIONAL_NOTES(cannot_pass_type_to_non_ephemeral, "temporary-pointers.md")
28+
EDUCATIONAL_NOTES(cannot_pass_type_to_non_ephemeral_warning,
29+
"temporary-pointers.md")
30+
EDUCATIONAL_NOTES(cannot_use_inout_non_ephemeral,
31+
"temporary-pointers.md")
32+
EDUCATIONAL_NOTES(cannot_use_inout_non_ephemeral_warning,
33+
"temporary-pointers.md")
34+
EDUCATIONAL_NOTES(cannot_construct_dangling_pointer, "temporary-pointers.md")
35+
EDUCATIONAL_NOTES(cannot_construct_dangling_pointer_warning,
36+
"temporary-pointers.md")
37+
38+
39+
2740
EDUCATIONAL_NOTES(non_nominal_no_initializers, "nominal-types.md")
2841
EDUCATIONAL_NOTES(non_nominal_extension, "nominal-types.md")
2942
EDUCATIONAL_NOTES(associated_type_witness_conform_impossible,

test/Sema/diag_non_ephemeral.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ var optionalArr: [Int8]?
2323
// We cannot use array-to-pointer and string-to-pointer conversions with
2424
// non-ephemeral parameters.
2525

26-
takesMutableRaw(&arr, 5) // expected-error {{cannot use inout expression here; argument #1 must be a pointer that outlives the call to 'takesMutableRaw'}}
26+
takesMutableRaw(&arr, 5) // expected-error {{cannot use inout expression here; argument #1 must be a pointer that outlives the call to 'takesMutableRaw'}} {{educational-notes=temporary-pointers}}
2727
// expected-note@-1 {{implicit argument conversion from '[Int8]' to 'UnsafeMutableRawPointer' produces a pointer valid only for the duration of the call to 'takesMutableRaw'}}
2828
// expected-note@-2 {{use the 'withUnsafeMutableBytes' method on Array in order to explicitly convert argument to buffer pointer valid for a defined scope}}
2929

30-
takesConst(str, 5) // expected-error {{cannot pass 'String' to parameter; argument #1 must be a pointer that outlives the call to 'takesConst'}}
30+
takesConst(str, 5) // expected-error {{cannot pass 'String' to parameter; argument #1 must be a pointer that outlives the call to 'takesConst'}} {{educational-notes=temporary-pointers}}
3131
// expected-note@-1 {{implicit argument conversion from 'String' to 'UnsafePointer<Int8>' produces a pointer valid only for the duration of the call to 'takesConst'}}
3232
// expected-note@-2 {{use the 'withCString' method on String in order to explicitly convert argument to pointer valid for a defined scope}}
3333

test/stdlib/UnsafePointerDiagnostics_warning.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ func unsafePointerInitEphemeralConversions() {
99
var optionalArr: [Int]? = [0]
1010
var c: C?
1111

12-
_ = UnsafePointer(&foo) // expected-warning {{initialization of 'UnsafePointer<Int>' results in a dangling pointer}}
12+
_ = UnsafePointer(&foo) // expected-warning {{initialization of 'UnsafePointer<Int>' results in a dangling pointer}} {{educational-notes=temporary-pointers}}
1313
// expected-note@-1 {{implicit argument conversion from 'Int' to 'UnsafePointer<Int>' produces a pointer valid only for the duration of the call to 'init(_:)'}}
1414
// expected-note@-2 {{use 'withUnsafePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
1515

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Temporary Pointers
2+
A temporary, or ephemeral, pointer in Swift is a pointer which is introduced by an implicit function argument conversion and is only valid for the lifetime of the function call it appears in. There are a few ways to create a temporary pointer:
3+
4+
- Using an inout-to-pointer conversion by passing an argument with `&`:
5+
6+
```swift
7+
func foo(bar: UnsafePointer<Int>) { /*...*/ }
8+
var x: Int = 42
9+
foo(bar: &x)
10+
```
11+
12+
In the example above, the `bar` passed to `foo` is a temporary pointer to `x` which is only valid until `foo` returns.
13+
14+
Not all inout-to-pointer conversions result in a temporary pointer. Passing global variables and static properties inout can produce non-ephemeral pointers, as long as they are stored and have no observers. Additionally, if they are of a tuple or struct type, their stored members without observers may also be passed inout as non-ephemeral pointers.
15+
16+
- Using a string-to-pointer conversion:
17+
18+
```swift
19+
func foo(bar: UnsafePointer<Int8>) { /*...*/ }
20+
var x: String = "hello, world!"
21+
foo(bar: x)
22+
```
23+
24+
In the example above, the `bar` passed to `foo` is a temporary pointer to a buffer containing the UTF-8 code units of `x` which is only valid until `foo` returns.
25+
26+
- Using an array-to-pointer conversion:
27+
28+
```swift
29+
func foo(bar: UnsafePointer<Bool>) { /*...*/ }
30+
var x: [Bool] = [true, false, true]
31+
foo(bar: x)
32+
```
33+
34+
In the example above, the `bar` passed to `foo` is a temporary pointer to the elements of `x` which is only valid until `foo` returns.
35+
36+
Temporary pointers may only be passed as arguments to functions which do not store the pointer value or otherwise allow it to escape the function's scope. The Swift compiler is able to diagnose some, but not all, violations of this rule. Misusing a temporary pointer by allowing it to outlive the enclosing function call results in undefined behavior. For example, consider the following incorrect code:
37+
38+
```swift
39+
var x = 42
40+
let ptr = UnsafePointer(&x)
41+
// Do something with ptr.
42+
```
43+
44+
This code is invalid because the initializer of `UnsafePointer` stores its argument, causing it to outlive the `UnsafePointer` initializer call. Instead, this code should use `withUnsafePointer` to access a pointer to `x` with an explicitly defined scope:
45+
46+
```swift
47+
var x = 42
48+
withUnsafePointer(to: &x) { ptr in
49+
// Do something with ptr, but don't allow it to escape this closure!
50+
}
51+
```
52+
53+
It's important to note that the `withUnsafe*` functions can also result in undefined behavior if used improperly. For example, the following incorrect code is equivalent to the original temporary pointer example:
54+
55+
```swift
56+
var x = 42
57+
let ptr = withUnsafePointer(to: &x) { $0 }
58+
// Do something with ptr.
59+
```
60+
61+
This code is invalid because the pointer to `x` is only valid until `withUnsafePointer` returns, but it escapes the closure when it is returned and assigned to `ptr`.
62+
63+
To learn more about correctly using unsafe pointer APIs, see the Swift standard library documentation of `UnsafePointer` and related types.
64+

0 commit comments

Comments
 (0)