Skip to content

Commit e511c07

Browse files
authored
Merge pull request #81885 from DougGregor/unsafe-for-loop-uses-6.2
[6.2] [Strict memory safety] Show issues with unsafe constructs in the for..in loop
2 parents 76eac9b + d08187a commit e511c07

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

lib/Sema/TypeCheckEffects.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4555,6 +4555,10 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
45554555
auto insertionLoc = S->getPattern()->getStartLoc();
45564556
Ctx.Diags.diagnose(S->getForLoc(), diag::for_unsafe_without_unsafe)
45574557
.fixItInsert(insertionLoc, "unsafe ");
4558+
4559+
for (const auto &unsafeUse : classification.getUnsafeUses()) {
4560+
diagnoseUnsafeUse(unsafeUse);
4561+
}
45584562
}
45594563
} else if (S->getUnsafeLoc().isValid()) {
45604564
// Extraneous "unsafe" on the sequence.

test/Unsafe/safe.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,12 @@ struct UnsafeAsSequence: @unsafe Sequence, @unsafe IteratorProtocol {
8989
func testUnsafeAsSequenceForEach() {
9090
let uas = UnsafeAsSequence()
9191

92+
// expected-note@+2{{reference to unsafe instance method 'next()'}}
9293
// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}{{12-12=unsafe }}
9394
for _ in uas { } // expected-note{{conformance}}
9495
// expected-warning@-1{{for-in loop uses unsafe constructs but is not marked with 'unsafe'}}{{documentation-file=strict-memory-safety}}{{7-7=unsafe }}
9596

97+
// expected-note@+1{{reference to unsafe instance method 'next()'}}
9698
for _ in unsafe uas { } // expected-warning{{for-in loop uses unsafe constructs but is not marked with 'unsafe'}}{{documentation-file=strict-memory-safety}}{{7-7=unsafe }}
9799

98100
for unsafe _ in unsafe uas { } // okay
@@ -118,6 +120,7 @@ struct SequenceWithUnsafeIterator: Sequence {
118120
func testUnsafeIteratorForEach() {
119121
let swui = SequenceWithUnsafeIterator()
120122

123+
// expected-note@+1{{reference to unsafe instance method 'next()'}}
121124
for _ in swui { } // expected-warning{{for-in loop uses unsafe constructs but is not marked with 'unsafe'}}{{7-7=unsafe }}
122125
for unsafe _ in swui { } // okay, it's only the iterator that's unsafe
123126
}

test/Unsafe/unsafe-suppression.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,12 @@ struct UnsafeSequence: @unsafe IteratorProtocol, @unsafe Sequence {
144144
}
145145

146146
func forEachLoop(us: UnsafeSequence) {
147+
// expected-note@+1{{reference to unsafe instance method 'next()'}}
147148
for _ in us { } // expected-warning{{expression uses unsafe constructs but is not marked with 'unsafe'}}{{documentation-file=strict-memory-safety}}{{12-12=unsafe }}
148149
// expected-note@-1{{'@unsafe' conformance of 'UnsafeSequence' to protocol 'Sequence' involves unsafe code}}
149150
// expected-warning@-2{{for-in loop uses unsafe constructs but is not marked with 'unsafe'}}{{documentation-file=strict-memory-safety}}
151+
152+
// expected-note@+1{{reference to unsafe instance method 'next()'}}
150153
for _ in unsafe us { }
151154
// expected-warning@-1{{for-in loop uses unsafe constructs but is not marked with 'unsafe'}}{{documentation-file=strict-memory-safety}}
152155
}

0 commit comments

Comments
 (0)