-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathunsafe-suppression.swift
150 lines (116 loc) · 3.85 KB
/
unsafe-suppression.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// RUN: %target-typecheck-verify-swift -strict-memory-safety -print-diagnostic-groups
@unsafe
func iAmUnsafe() { }
@unsafe
struct UnsafeType { }
func iAmImpliedUnsafe() -> UnsafeType? { nil }
@unsafe
func labeledUnsafe(_: UnsafeType) {
unsafe iAmUnsafe()
let _ = unsafe iAmImpliedUnsafe()
}
class C {
func method() { } // expected-note{{overridden declaration is here}}
}
class D1: C { // expected-note{{make class 'D1' @unsafe to allow unsafe overrides of safe superclass methods}}{{1-1=@unsafe }}
@unsafe
override func method() { } // expected-warning{{override of safe instance method with unsafe instance method [StrictMemorySafety]}}
}
@unsafe class D2: C {
@unsafe // do not diagnose
override func method() { }
}
protocol P {
func protoMethod()
}
struct S1: P {
// expected-warning@-1{{conformance of 'S1' to protocol 'P' involves unsafe code; use '@unsafe' to indicate that the conformance is not memory-safe [StrictMemorySafety]}}{{12-12=@unsafe }}
@unsafe
func protoMethod() { } // expected-note{{unsafe instance method 'protoMethod()' cannot satisfy safe requirement}}
}
@unsafe
struct S2: P {
@unsafe
func protoMethod() { }
}
struct S3 { }
extension S3: P {
// expected-warning@-1{{conformance of 'S3' to protocol 'P' involves unsafe code; use '@unsafe' to indicate that the conformance is not memory-safe [StrictMemorySafety]}}{{15-15=@unsafe }}
@unsafe
func protoMethod() { } // expected-note{{unsafe instance method 'protoMethod()' cannot satisfy safe requirement}}
}
struct S4 { }
extension S4: @unsafe P {
@unsafe
func protoMethod() { } // okay
}
protocol P2 {
func proto2Method()
}
@unsafe
extension S4: P2 { // expected-warning{{conformance of 'S4' to protocol 'P2' involves unsafe code; use '@unsafe' to indicate that the conformance is not memory-safe}}
@unsafe
func proto2Method() { } // expected-note{{unsafe instance method}}
}
@unsafe
class SendableC1: @unchecked Sendable { }
class SendableC2 { }
@unsafe
extension SendableC2: @unchecked Sendable { }
struct Wrapper {
@unsafe
unowned(unsafe) var reference: C
}
@_nonSendable
class NonSendable { }
@unsafe nonisolated(unsafe) var notSendable: NonSendable = .init() // okay
@unsafe
struct UnsafeOuter {
func f(_: UnsafeType) { } // okay
@unsafe func g(_ y: UnsafeType) {
@unsafe let x: UnsafeType = unsafe y
let _ = unsafe x
}
}
extension UnsafeOuter {
func h(_: UnsafeType) { }
}
@unsafe
extension UnsafeOuter {
func i(_: UnsafeType) { }
}
// -----------------------------------------------------------------------
// Miscellaneous issues
// -----------------------------------------------------------------------
var yieldUnsafe: Int {
_read {
@unsafe let x = 5
yield x // expected-warning{{expression uses unsafe constructs but is not marked with 'unsafe' [StrictMemorySafety]}}
// expected-note@-1{{reference to unsafe let 'x'}}
}
_modify {
@unsafe var x = 5
yield &x // expected-warning{{expression uses unsafe constructs but is not marked with 'unsafe' [StrictMemorySafety]}}
// expected-note@-1{{reference to unsafe var 'x'}}
}
}
var yieldUnsafeOkay: Int {
_read {
@unsafe let x = 5
yield unsafe x
}
_modify {
@unsafe var x = 5
yield unsafe &x
}
}
struct UnsafeSequence: @unsafe IteratorProtocol, @unsafe Sequence {
@unsafe func next() -> Int? { nil }
}
func forEachLoop(us: UnsafeSequence) {
for _ in us { } // expected-warning{{expression uses unsafe constructs but is not marked with 'unsafe' [StrictMemorySafety]}}{{12-12=unsafe }}
// expected-note@-1{{@unsafe conformance of 'UnsafeSequence' to protocol 'Sequence' involves unsafe code}}
// expected-warning@-2{{for-in loop uses unsafe constructs but is not marked with 'unsafe' [StrictMemorySafety]}}
for _ in unsafe us { }
// expected-warning@-1{{for-in loop uses unsafe constructs but is not marked with 'unsafe' [StrictMemorySafety]}}
}