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
6 changes: 6 additions & 0 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3854,6 +3854,12 @@ generateForEachStmtConstraints(ConstraintSystem &cs, DeclContext *dc,
AwaitExpr::createImplicit(ctx, nextCall->getLoc(), nextCall);
}

// Wrap the 'next' call in 'unsafe', if there is one.
if (unsafeExpr) {
nextCall = new (ctx) UnsafeExpr(unsafeExpr->getLoc(), nextCall, Type(),
/*implicit=*/true);
}

// The iterator type must conform to IteratorProtocol.
{
ProtocolDecl *iteratorProto = TypeChecker::getProtocol(
Expand Down
6 changes: 6 additions & 0 deletions lib/Sema/TypeCheckEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4122,6 +4122,12 @@ class CheckEffectsCoverage : public EffectsHandlingWalker<CheckEffectsCoverage>

if (auto parsedSequence = S->getParsedSequence()) {
parentMap[typeCheckedExpr] = parsedSequence;

if (auto nextCall = S->getNextCall()) {
auto nextParentMap = nextCall->getParentMap();
parentMap.insert(nextParentMap.begin(), nextParentMap.end());
parentMap[nextCall] = parsedSequence;
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions test/Unsafe/safe.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,17 @@ func returnsExistentialP() -> any P {
// expected-note@-1{{@unsafe conformance of 'Int' to protocol 'P' involves unsafe code}}
}

struct UnsafeAsSequence: @unsafe Sequence, IteratorProtocol {
mutating func next() -> Int? { nil }
// FIXME: Should work even if the IteratorProtocol conformance is safe
struct UnsafeAsSequence: @unsafe Sequence, @unsafe IteratorProtocol {
@unsafe mutating func next() -> Int? { nil }
}

func testUnsafeAsSequenceForEach() {
let uas = UnsafeAsSequence()

// expected-warning@+1{{expression uses unsafe constructs but is not marked with 'unsafe'}}{{12-12=unsafe }}
for _ in uas { } // expected-note{{conformance}}
// expected-note@-1{{reference}}

for _ in unsafe uas { } // okay
}
Expand Down
12 changes: 12 additions & 0 deletions test/Unsafe/unsafe-suppression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,15 @@ var yieldUnsafeOkay: Int {
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' [Unsafe]}}{{12-12=unsafe }}
// expected-note@-1{{@unsafe conformance of 'UnsafeSequence' to protocol 'Sequence' involves unsafe code}}
// expected-note@-2{{reference to unsafe instance method 'next()'}}

for _ in unsafe us { }
}