8000 Cannot reinitialize inout parameter of type `MutableSpan<T>?` · Issue #81976 · swiftlang/swift · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Cannot reinitialize inout parameter of type MutableSpan<T>? #81976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
glessard opened this issue Jun 4, 2025 · 0 comments
Open

Cannot reinitialize inout parameter of type MutableSpan<T>? #81976

glessard opened this issue Jun 4, 2025 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. ownership Feature: Ownership modifiers and semantics

Comments

@glessard
Copy link
Contributor
glessard commented Jun 4, 2025

Description

An inout parameter of an Optional of a type that is ~Copyable & ~Escapable is very hard to work with. In particular, the idiomatic way to unwrap that parameter doesn't work.

Reproduction

Using swift-DEVELOPMENT-SNAPSHOT-2025-06-01-a (e.g. in godbolt)

func modify<T, U>(
    _ s: inout MutableSpan<T>?,
    _ body: (inout MutableSpan<T>) -> U
) -> U {
    var unwrapped: MutableSpan<T>
    if let uw = s {
        unwrapped = uw
    } else {
        unwrapped = MutableSpan(_unsafeElements: .init(start: nil, count: 0))
    }
    let u = body(&unwrapped)
    s = consume unwrapped
    return u
}

Does not compile. The error is:

<source>:5:9: error: lifetime-dependent variable 'unwrapped' escapes its scope
 3 |     _ body: (inout MutableSpan<T>) -> U
 4 | ) -> U {
 5 |     var unwrapped: MutableSpan<T>
   |         `- error: lifetime-dependent variable 'unwrapped' escapes its scope
 6 |     if let uw = s {
 7 |         unwrapped = uw
 8 |     } else {
 9 |         unwrapped = MutableSpan(_unsafeElements: .init(start: nil, count: 0))
   |                                                   `- note: it depends on the lifetime of this parent value
10 |     }
11 |     let u = body(&unwrapped)
12 |     s = consume unwrapped
   |       `- note: this use causes the lifetime-dependent value to escape
13 |     return u
14 | }
Compiler returned: 1

Expected behavior

This should compile. In fact, modifying line 6 to read if let uw = s.take() { allows the whole thing to compile. That changes things very little, yet satisfies the compiler.

Environment

Swift version 6.2-dev (LLVM 5e7837820b1c886, Swift d5ef256)
Target: x86_64-unknown-linux-gnu
Build config: +assertions

The compiler is called with -enable-experimental-feature LifetimeDependence

Additional information

rdar://152572002

@glessard glessard added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. ownership Feature: Ownership modifiers and semantics labels Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. ownership Feature: Ownership modifiers and semantics
Projects
None yet
Development

No branches or pull requests

1 participant
0