[Strict memory safety] Fix "unsafe" checking for the for..in loop

The `$generator` variable we create for the async for..in loop is
`nonisolated(unsafe)`, so ensure that we generate an `unsafe`
expression when we use it. This uncovered some inconsistencies in how
we do `unsafe` checking for for..in loops, so fix those.

Fixes rdar://154775389.
This commit is contained in:
Doug Gregor
2025-07-09 16:19:14 -07:00
parent 1e87656e40
commit 35628cb503
4 changed files with 23 additions and 10 deletions

View File

@@ -98,6 +98,8 @@ func testUnsafeAsSequenceForEach() {
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 }}
for unsafe _ in unsafe uas { } // okay
for unsafe _ in [1, 2, 3] { } // expected-warning{{no unsafe operations occur within 'unsafe' for-in loop}}
}
func testForInUnsafeAmbiguity(_ integers: [Int]) {

View File

@@ -55,3 +55,10 @@ open class SyntaxVisitor {
open class SyntaxAnyVisitor: SyntaxVisitor {
override open func visit(_ token: TokenSyntax) { }
}
@available(SwiftStdlib 5.1, *)
func testMemorySafetyWithForLoop() async {
let (stream, continuation) = AsyncStream<Int>.makeStream()
for await _ in stream {}
_ = continuation
}