Can’t use consume keyword with defer statement

hopefully the pattern i’m using here should be self-explanatory:

mutating
func with<Success>(
    do body:(inout Outliner) throws -> Success) rethrows -> Success
{
    var outliner:Outliner = .init(
        diagnostics: (consume self).diagnostics)
    
    defer 
    {
        self = .init(diagnostics: outliner.diagnostics())
    }

    return try body(&outliner)
}

at least, this is what i did back when the only way to temporarily rebind mutable storage to a different type was to use subscripts and _modify. so i figured it would work with consume self too. but it doesn’t.

missing reinitialization of closure capture 'outliner' after consume

instead i have to do

mutating
func with<Success>(
    do body:(inout Outliner) throws -> Success) rethrows -> Success
{
    var outliner:StaticOutliner = .init(
        diagnostics: (consume self).diagnostics)

    do
    {
        let success:Success = try body(&outliner)
        self = .init(diagnostics: outliner.diagnostics())
        return success
    }
    catch let error
    {
        self = .init(diagnostics: outliner.diagnostics())
        throw error
    }
}

as if we were using async! why can’t we just use defer here?

2 Likes