Skip to content

Conversation

@elsakeirouz
Copy link

This PR aims to lift the desugaring of ForEachStmt (when dealing with Sequences) from SILGen to the AST, and simultaneously delaying the synthesis of makeIterator and next calls until after typechecking.

The motivation for this is support for non-Copyable & non-Escapable sequences with non-Copyable elements in ForEachStmt. In order to know which variant of makeIterator and next calls we need, we must determine whether a Sequence is copyable, asynchronous, or neither. The compiler currently relies on the function choice to infer the type of the elements. However, to determine whether a Sequence is copyable, it must be typed.
For that reason, instead of matching the pattern type with the unwrapped result type of next(), we're now matching it with Sequence. This allows us to delay the decision and synthesis of those function calls until later.

Additionally, in order to avoid having to translate the rest of the complex desugar logic for the borrowing ForEachStmt in SILGen, we chose to represent it directly at the AST level, which SILGen will then use to emit the corresponding SIL code.

To make things simpler all around, we chose to start by reworking this pass for the regular/async ForEachStmts before handling borrowing statements.

We expect to be able to remove ConstraintKind::ValueWitness with this work.

@elsakeirouz elsakeirouz self-assigned this Dec 12, 2025
@elsakeirouz elsakeirouz force-pushed the rework-for-each-desugar branch 2 times, most recently from 5381915 to e7ec1ab Compare December 13, 2025 02:22
return ctx.getAsyncIteratorNext();
}

static BraceStmt *desugarForEachStmt(ForEachStmt* stmt){
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially refactor this function to make it more readable.

@elsakeirouz elsakeirouz force-pushed the rework-for-each-desugar branch from 0dc0a64 to 714b32f Compare December 15, 2025 16:50
I have not yet tested any behavior of generated SIL code but it seems
reasonable in comparison.

I was able to compile the standard library.

Here:
* include/swift/AST/ASTBridging.h,
* include/swift/AST/Expr.h,
* include/swift/AST/ExprNodes.def,
* include/swift/AST/Stmt.h,
* include/swift/AST/StmtNodes.def,
* include/swift/AST/TypeCheckRequests.h,
* include/swift/AST/TypeCheckerTypeIDZone.def,
* include/swift/Sema/ConstraintLocator.h,
* include/swift/Sema/SyntacticElementTarget.h,
* lib/AST/ASTDumper.cpp,
* lib/AST/ASTPrinter.cpp,
* lib/AST/ASTScopeCreation.cpp,
* lib/AST/ASTVerifier.cpp,
* lib/AST/ASTWalker.cpp,
* lib/AST/Bridging/StmtBridging.cpp,
* lib/AST/Expr.cpp,
* lib/AST/Stmt.cpp,
* lib/AST/TypeCheckRequests.cpp,
* lib/ASTGen/Sources/ASTGen/Stmts.swift,
* lib/Parse/ParseStmt.cpp,
* lib/SILGen/ASTVisitor.h,
* lib/SILGen/SILGenExpr.cpp,
* lib/SILGen/SILGenStmt.cpp,
* lib/SILOptimizer/Mandatory/MoveOnlyDiagnostics.cpp,
* lib/Sema/BuilderTransform.cpp,
* lib/Sema/CSApply.cpp,
* lib/Sema/CSDiagnostics.cpp,
* lib/Sema/CSGen.cpp,
* lib/Sema/CSSimplify.cpp,
* lib/Sema/CSSyntacticElement.cpp,
* lib/Sema/SyntacticElementTarget.cpp,
* lib/Sema/TypeCheckEffects.cpp,
* lib/Sema/TypeCheckStmt.cpp.
@elsakeirouz elsakeirouz force-pushed the rework-for-each-desugar branch from 714b32f to 5285f67 Compare December 16, 2025 13:30
@elsakeirouz elsakeirouz force-pushed the rework-for-each-desugar branch from 67a017d to b5e88da Compare December 19, 2025 10:02
@elsakeirouz elsakeirouz force-pushed the rework-for-each-desugar branch from a985680 to 8dbec9f Compare December 20, 2025 23:30
@elsakeirouz elsakeirouz force-pushed the rework-for-each-desugar branch from bd1a68e to 623bdc5 Compare December 22, 2025 17:10
Copy link
Contributor

@hamishknight hamishknight left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some initial comments from a quick look though, this is looking really good!

Comment on lines +4201 to +4202
// FIXME: need to find out if the stmt is part of a foreach's
// desugared
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this issue resolved by the opaque pattern fix?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it isn't

Copy link
Contributor

@hamishknight hamishknight Dec 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elsakeirouz I think the issue is that markOwnedByStatement is walking into the opaque pattern, I think forEachVariable should probably have a parameter that controls whether it should walk into them

Copy link
Contributor

@hamishknight hamishknight Dec 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might also be worth making it so we don't walk into opaque patterns by default in forEachVariable, only SILGen and maybe a couple of Sema clients should care about that case

Copy link
Contributor

@hamishknight hamishknight Dec 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the same also applies to getSemanticsProvidingPattern, I think we should see if we can get away with not recursing into opaque patterns by default

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking into all of this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants