Skip to content

Inaccurate error message: missing reinitialization of inout parameter 'x' after consume while accessing memory #83581

@rayx

Description

@rayx

Description

See code below. Capturing a consuming parameter, which was consumed and reinitialized, in a escaping closure causes compiler to emit this error:

error: missing reinitialization of inout parameter 'x' after consume while accessing memory

The compile failure itself is a known limitation. Non-copyable values have a similar limitation and it's documented in "Noncopyable variables captured by escaping closures" section in SE-0390. As consuming parameter works in a similar way as non-copyable, I believe it has the same limitation.

But I think the "inout parameter" description in the error message isn't correct. SE-0390 said that capturing a mutable local binding into a nonescaping closure works in the same way as inout parameter. But in this case fn is an escaping closure, so x is captured by reference and isn't an inout parameter.

Reproduction

func test(x: consuming String) {
    _ = consume x
    x = "abc"
    let fn: () -> Void = { print(x) }
    fn()
}

Compiler output:

<source>:2:17: error: missing reinitialization of inout parameter 'x' after consume while accessing memory
1 | func test(x: consuming String) {
2 |     _ = consume x
  |                 |- error: missing reinitialization of inout parameter 'x' after consume while accessing memory
  |                 `- note: consumed here
3 |     x = "abc"
4 |     let fn: () -> Void = { print(x) }
Compiler returned: 1

Expected behavior

Replace inout with a more appropriate description.

Environment

I use "x86-64 swiftc nightly" on godbolt:

https://godbolt.org/z/5jTPe1sW5

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    SIL ownershipArea → compiler → SIL: SIL ownershipbugA 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