[Unsafe] Teach for..in loops to let the sequence's 'unsafe' cover next()

Warnings about unsafe uses due to an @unsafe IteratorProtocol conformance
(for the implicit call to next()) could not be silenced. Follow the same
path we did for the Sequence conformance (and makeIterator() call) by
associating it with the `unsafe` on the sequence argument.

This isn't the only solution here, but it's a reasonable one.
This commit is contained in:
Doug Gregor
2025-02-10 14:51:52 -08:00
parent 62f128f6de
commit 260ade8076
3 changed files with 24 additions and 0 deletions

View File

@@ -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(