Add AST representation for coroutines
This is a proof-of-concept PR adding AST representation for coroutines
Further background at https://forums.swift.org/t/pitch-yield-once-functions-first-class-coroutines/77081
@tkremenek @rjmccall Following discussions on LLVM Dev Meeting, this is a stripped-down version of https://github.com/swiftlang/swift/pull/78508:
- No autodiff-related parts
- No silgen (besides the "standard" special path used for coroutine accessors)
- Some other fixes for SIL optimizer is omitted
Essentially the PR contains AST-related bits for coroutine declarations and coroutine function types as well as required boilerplate for AST mangling, dumping, serialization, etc.
@xedin comments from the original PR were addressed as well.
Please test with following pull request: https://github.com/swiftlang/llvm-project/pull/11396 @swift-ci please test
Please test with following pull request: https://github.com/swiftlang/llvm-project/pull/11396 @swift-ci please test
Please test with following pull request: https://github.com/swiftlang/llvm-project/pull/11396 @swift-ci please test
Please test with following pull request: https://github.com/swiftlang/llvm-project/pull/11396 @swift-ci please test windows platform
https://github.com/swiftlang/swift-syntax/pull/3225 @swift-ci please test
@slavapestov @rjmccall
The updated PR contains the support for yields w/o dedicated yield result type (I added them on top of the previous attempts to show the approach).
Things to note:
- There are no silgen changes here (as we discussed), though I have ported them separately and verified that removing special path for coroutine accessors and treating them as "ordinary coroutines" does not lead to any test breakage
- Function types support multiple yields (there was some initial boilerplate for this there), but I decided to postpone decision how we'd represent multiple yields for declarations (similar to error types), so declarations only support a single yield for now.
- It turned out that swift syntax parser treats function result arrow as a part of
result clause, so to minimize amount of changes I decided to use for yields the syntax similar to the throws, so the yielded type uses contextualyieldskeyword that (if present) should be after after specifiers likethrowsorasync - Function type construction always requires yield types to be passed (even if empty), so they won't forgotten by accident. Though, the default implementation w/o yields could be added (I left it commented out for now)
- Everything is hidden behind dedicated experimental flag
Please let me know if there is something that should be changed / done differently in this approach.
https://github.com/swiftlang/swift-syntax/pull/3225 @swift-ci please test
https://github.com/swiftlang/swift-syntax/pull/3225 https://github.com/swiftlang/llvm-project/pull/12064 @swift-ci please test
https://github.com/swiftlang/swift-syntax/pull/3225 https://github.com/swiftlang/llvm-project/pull/12064 @swift-ci please test
https://github.com/swiftlang/swift-syntax/pull/3225 https://github.com/swiftlang/llvm-project/pull/12064 @swift-ci please test macos platform
Looks like main is broken:
[2026-01-08T05:03:21.576Z] /Users/ec2-user/jenkins/workspace/swift-PR-macos/branch-main/swift/stdlib/public/core/StaticPrint.swift:866:29: error: expression uses unsafe constructs but is not marked with 'unsafe' [#]8;;https://docs.swift.org/compiler/documentation/diagnostics/strict-memory-safety\StrictMemorySafety]8;;\]
[2026-01-08T05:03:21.576Z] 864 | let argumentClosures = message.interpolation.arguments.argumentClosures
[2026-01-08T05:03:21.576Z] 865 | if Bool(_builtinBooleanLiteral: Builtin.ifdef_SWIFT_STDLIB_PRINT_DISABLED()) { return }
[2026-01-08T05:03:21.576Z] 866 | let formatStringPointer = _getGlobalStringTablePointer(formatString)
[2026-01-08T05:03:21.576Z] | |- error: expression uses unsafe constructs but is not marked with 'unsafe' [#]8;;https://docs.swift.org/compiler/documentation/diagnostics/strict-memory-safety\StrictMemorySafety]8;;\]
[2026-01-08T05:03:21.576Z] | `- note: reference to global function '_getGlobalStringTablePointer' involves unsafe type 'UnsafePointer<CChar>' (aka 'UnsafePointer<Int8>')
[2026-01-08T05:03:21.576Z] 867 | unsafe constant_vprintf_backend(
[2026-01-08T05:03:21.576Z] 868 | fmt: formatStringPointer,
[2026-01-08T05:03:21.576Z]
[2026-01-08T05:03:21.576Z] [#StrictMemorySafety]: <https://docs.swift.org/compiler/documentation/diagnostics/strict-memory-safety>
[2026-01-08T05:03:21.576Z] ninja: build stopped: subcommand failed.
[2026-01-08T05:03:21.576Z] ERROR: command `['env', '/usr/local/bin/cmake', '--build', '/Users/ec2-user/jenkins/workspace/swift-PR-macos/branch-main/build/buildbot_incremental/minimalstdlib-macosx-x86_64', '--config',
https://github.com/swiftlang/swift-syntax/pull/3225 https://github.com/swiftlang/llvm-project/pull/12064 @swift-ci please test macos platform
https://github.com/swiftlang/swift-syntax/pull/3225 https://github.com/swiftlang/llvm-project/pull/12064 @swift-ci please test
https://github.com/swiftlang/swift-syntax/pull/3225 https://github.com/swiftlang/llvm-project/pull/12064 @swift-ci please test macos platform
Hi, Anton. I'll try to get back to this in a week; I've been getting over a case of the flu and needing to budget my energy.
@rjmccall This is a new version to review. Changes:
- No metadata-related changes for now
- Default implementation for builtins (
_function()vs_coroutine()helpers) - Removed refactoring remnants
Please take a look
https://github.com/swiftlang/swift-syntax/pull/3225 https://github.com/swiftlang/llvm-project/pull/12064 @swift-ci please test