Skip to content

Borrowed parameters cannot be used in autoclosure #77235

@glessard

Description

@glessard

Description

A function's borrowed parameter cannot be used in a precondition's autoclosure. The error claims that the parameter "cannot be captured by an escaping closure since it is a borrowed parameter". It's not clear why an autoclosure should be considered escaping. Note that this error occurs after type checking.

Reproduction

public func test(_ i: borrowing Int) -> Bool {
    precondition(i > 0)
    return true
}

Expected behavior

This should compile.
However,

<source>:1:20: error: 'i' cannot be captured by an escaping closure since it is a borrowed parameter
1 | public func test(_ i: borrowing Int) -> Bool {
  |                    `- error: 'i' cannot be captured by an escaping closure since it is a borrowed parameter
2 |     precondition(i > 0)
  |                  `- note: closure capturing 'i' here
3 |     return true
4 | }

https://swift.godbolt.org/z/v4d3YWrPv

Environment

Apple Swift version 6.1-dev (LLVM f4b733c38006ec1, Swift f4bea5f)
Target: arm64-apple-macosx15.0
(https://github.com/swiftlang/swift/releases/tag/swift-DEVELOPMENT-SNAPSHOT-2024-10-27-a)

Additional information

rdar://138670128

Metadata

Metadata

Assignees

No one assigned

    Labels

    @autoclosureFeature → attributes: the @autoclosure type attributebugA deviation from expected or documented behavior. Also: expected but undesirable behavior.ownershipFeature: Ownership modifiers and semantics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions