Skip to content

closure capture rule applied at Swift 5.8 doesn't work correctly in resultBuilder #64757

@JCSooHwanCho

Description

@JCSooHwanCho

Description
When I tried to compile with Xcode 14.3 RC 2, our company's project codes didn't compile with some reasons. One of that is this case. In resultBuilder closure, using weak capture self with if-let shorthand make capturing semantic error. I made a sample code snippet to reproduce problem.

Steps to reproduce
Try this sample

@resultBuilder
struct VoidBuilder {
    static func buildBlock() -> Void {

    }

    static func buildPartialBlock<T>(first: T) -> Void {

    }

    static func buildPartialBlock<T>(accumulated: Void, next: T) -> Void {

    }
}

final class EscapingWrapper {
    static func wrapper(_ closure: @escaping () -> Void) {
        closure()
    }
}

final class Sample {

    @VoidBuilder
    var void: Void {
        EscapingWrapper.wrapper { [weak self] in
            guard let self else { return } // Implicit use of 'self' in closure; use 'self.' to make capture semantics explicit
        }
    }
}

Expected behavior
compile success without error.

Environment

  • Swift compiler version info
    swift-driver version: 1.75.2 Apple Swift version 5.8 (swiftlang-5.8.0.124.2 clang-1403.0.22.11.100)
    Target: arm64-apple-macosx13.0

  • Xcode version info
    Xcode 14.3
    Build version 14E222b

  • Deployment target
    no specific target(It reproduced in both ios project and cmd project)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.closuresFeature: closurescompilerThe Swift compiler itselfoptional bindingFeature → patterns: optional bindingregressionresult buildersFeature: Result buildersself capturesFeature → closures: 'self' capturesswift 5.8type checkerArea → compiler: Semantic analysisunexpected errorBug: Unexpected error

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions