[Solved] `nonisolated(nonsending)` and `@escaping` can't be used together

Does anyone know why the code doesn't compile? Why can a nonisolated(nonsending) be sending but not escaping? If it's intentional, compiler doesn't produce a nice diagnostic (the output looks like unrecognized syntax error).

final class NS {
    let fn: nonisolated(nonsending) () async -> Void
    init(_ fn: @escaping nonisolated(nonsending) () async -> Void) {
        self.fn = fn
    }
}

I tested nightly build and 6.2 and 6.3 releases. All have the same error.

1 Like

You have to reverse them:

final class NS {
    let fn: nonisolated(nonsending) () async -> Void
    init(_ fn: nonisolated(nonsending) @escaping () async -> Void) {
        self.fn = fn
    }
}

I previously brought up the inflexibility of nonisolated(nonsending) here: Difference between nonisolated(nonsending) @Sendable functions and closures.

4 Likes