Skip to content

Commit f64516b

Browse files
committed
[Strict memory safety] "unsafe" expression never propagates unsafe outward
In the effects checker, we were propagating the "has an unsafe use site" outside of an `unsafe` expression. The result of this is that we would not produce a warning for silly expressions like `unsafe unsafe ptr.pointee`, where the first (outer) `unsafe` is unnecessary. Stop propagating that bit so we properly diagnose the spurious "unsafe". Fixes issue #82315 / rdar://153672668.
1 parent 9c793a8 commit f64516b

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

lib/Sema/TypeCheckEffects.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3851,7 +3851,6 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
38513851
}
38523852

38533853
void preserveCoverageFromUnsafeOperand() {
3854-
OldFlags.mergeFrom(ContextFlags::HasAnyUnsafeSite, Self.Flags);
38553854
OldFlags.mergeFrom(ContextFlags::HasAnyUnsafe, Self.Flags);
38563855
OldFlags.mergeFrom(ContextFlags::asyncAwaitFlags(), Self.Flags);
38573856
OldFlags.mergeFrom(ContextFlags::throwFlags(), Self.Flags);

test/Unsafe/safe.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,3 +384,10 @@ func testInterpolation(ptr: UnsafePointer<Int>) {
384384
// expected-note@-1{{reference to unsafe type 'UnsafePointer<Int>'}}
385385
// expected-note@-2{{argument #0 in call to instance method 'appendInterpolation' has unsafe type 'UnsafePointer<Int>'}}
386386
}
387+
388+
func superDuperUnsafe(_ bytes: UnsafeRawBufferPointer) {
389+
// expected-warning@+1{{no unsafe operations occur within 'unsafe' expression}}
390+
let byte = unsafe unsafe bytes.first ?? 0
391+
_ = byte
392+
_ = unsafe bytes.first ?? 0
393+
}

test/Unsafe/unsafe_nonstrict.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ func testItAll(ut: UnsafeType, x: X, i: Int) {
1616
unsafe acceptP(x)
1717
_ = unsafe i // expected-warning{{no unsafe operations occur within 'unsafe' expression}}
1818
}
19+
20+
func superDuperUnsafe(_ bytes: UnsafeRawBufferPointer) {
21+
// expected-warning@+1{{no unsafe operations occur within 'unsafe' expression}}
22+
let byte = unsafe unsafe bytes.first ?? 0
23+
_ = byte
24+
_ = unsafe bytes.first ?? 0
25+
}

0 commit comments

Comments
 (0)