-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathissue-71495.swift
48 lines (38 loc) · 1.07 KB
/
issue-71495.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// RUN: %target-run-simple-swift(-Xfrontend -sil-verify-all)
// RUN: %target-run-simple-swift(-O -Xfrontend -sil-verify-all)
// REQUIRES: executable_test
/// A unique value represented by a heap memory location.
struct Handle: ~Copyable {
var address: UnsafeMutableRawPointer
init() {
self.address = .allocate(byteCount: 2, alignment: 2)
}
consuming func done() {
let address = self.address
discard self
address.deallocate()
print("deallocated handle via done()")
}
deinit {
address.deallocate()
print("deallocated handle via deinit")
}
}
func description(of pointer: UnsafeRawPointer) -> String {
let address = UInt(bitPattern: pointer)
return "0x" + String(address, radix: 16)
}
func borrowHandleAndCrashNoMore(_ handle: borrowing Handle) -> String {
var string = ""
return string.withUTF8 { _ in
description(of: handle.address)
}
}
let handleDescription: String
do {
let handle = Handle()
handleDescription = borrowHandleAndCrashNoMore(handle)
handle.done()
}
// CHECK: result: 0x
print("result: \(handleDescription)")