Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4728,10 +4728,15 @@ generateForEachStmtConstraints(ConstraintSystem &cs, DeclContext *dc,
}

// Wrap the 'next' call in 'unsafe', if the for..in loop has that
// effect.
if (stmt->getUnsafeLoc().isValid()) {
nextCall = new (ctx) UnsafeExpr(
stmt->getUnsafeLoc(), nextCall, Type(), /*implicit=*/true);
// effect or if the loop is async (in which case the iterator variable
// is nonisolated(unsafe).
if (stmt->getUnsafeLoc().isValid() ||
(isAsync &&
ctx.LangOpts.StrictConcurrencyLevel == StrictConcurrency::Complete)) {
SourceLoc loc = stmt->getUnsafeLoc();
if (loc.isInvalid())
loc = stmt->getForLoc();
nextCall = new (ctx) UnsafeExpr(loc, nextCall, Type(), /*implicit=*/true);
}

// The iterator type must conform to IteratorProtocol.
Expand Down
11 changes: 5 additions & 6 deletions lib/Sema/TypeCheckEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2457,7 +2457,7 @@ class ApplyClassifier {
return ShouldRecurse;
}
ShouldRecurse_t checkUnsafe(UnsafeExpr *E) {
return E->isImplicit() ? ShouldRecurse : ShouldNotRecurse;
return ShouldNotRecurse;
}
ShouldRecurse_t checkTry(TryExpr *E) {
return ShouldRecurse;
Expand Down Expand Up @@ -4626,10 +4626,6 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
diagnoseUnsafeUse(unsafeUse);
}
}
} else if (S->getUnsafeLoc().isValid()) {
// Extraneous "unsafe" on the sequence.
Ctx.Diags.diagnose(S->getUnsafeLoc(), diag::no_unsafe_in_unsafe_for)
.fixItRemove(S->getUnsafeLoc());
}

return ShouldRecurse;
Expand Down Expand Up @@ -4689,7 +4685,10 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>
return;
}

Ctx.Diags.diagnose(E->getUnsafeLoc(), diag::no_unsafe_in_unsafe)
Ctx.Diags.diagnose(E->getUnsafeLoc(),
forEachNextCallExprs.contains(E)
? diag::no_unsafe_in_unsafe_for
: diag::no_unsafe_in_unsafe)
.fixItRemove(E->getUnsafeLoc());
}

Expand Down
2 changes: 2 additions & 0 deletions test/Unsafe/safe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down
7 changes: 7 additions & 0 deletions test/Unsafe/unsafe_concurrency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}