Why don't I get a warning about my closure capture here?

I was experimenting with something, and was trying to induce a compiler warning related to a non-Sendable type. However, I didn't get the warning I was expecting and I'm having trouble figuring out why this code is warning-free. I suspect I'm missing something obvious...

Swift 5.9, Xcode 15, strict warnings enabled.

struct NonSendable {
}

@available(*, unavailable)
extension NonSendable: Sendable {
}

actor MyActor {
	let nonSendable: NonSendable

	init(nonSendable: @Sendable () -> NonSendable) {
		self.nonSendable = nonSendable()
	}
}

let nonSendable = NonSendable()

// why don't I get a warning about capturing a non-sendable value here?
let actor = MyActor(nonSendable: { nonSendable })

Full sendability checking is off by default in order to prevent causing countless (and potentially difficult to fix) warnings.

You can enable full sendability checking by enabling the StrictConcurrency experimental feature using either the .enableExperimentalFeature("StrictConcurrency") Swift compilation setting in Package.swift or passing --enable-experimental-feature 'StrictConcurrency' as a command-line argument to the compiler.

I actually did have strict checking enabled. It turns out the issue is that checking doesn’t currently apply to variables in the global scope. But it is being fixed!

2 Likes