Skip to content

Compiler performs implicit copies on a borrowing parameter passed-through to a local closure #75854

@karwa

Description

@karwa

Description

According to SE-377:

We propose new borrowing and consuming parameter modifiers to allow developers to explicitly choose the ownership convention that a function uses to receive immutable parameters. Applying one of these modifiers to a parameter causes that parameter binding to no longer be implicitly copyable, and potential copies need to be marked with the new copy x operator.

In the reproduction test case, the compiler performs a copy of a parameter marked as borrowing without the developer having to use the copy operator.

(there is a related bug, which I will file separately, about this crashing the compiler if Foo<T> is ~Copyable)

Reproduction

struct Foo<T> {}

func huh<T>(_ source: borrowing Foo<T>) {
  let process: (borrowing Foo<T>) -> () -> Void = {
    s in { _ = consume s }  // This should not be valid!
  }
  process(source)()
}

Expected behavior

Expect the compiler to reject the reproduction test case. It is not possible to consume a borrow!

Environment

Swift version 6.0-dev (LLVM 73a346c9caf32cc, Swift 3e21fc6)
Target: x86_64-unknown-linux-gnu

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA 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