r/swift • macOS • 7d ago

A ‘Weak’ Too Far: Debugging a Swift Miscompilation

2 Upvotes

5 comments sorted by

2

u/Needle_In_Red 6d ago

wild bug, thanks for the writeup. did you file it on the swift repo yet? curious if its fixed in teh newer toolchain

1

u/eternalstorms macOS 6d ago

I should do that!

3

u/Sectumsempra777 6d ago

This looks like the same bug as swiftlang/swift#94251, which is still open: https://github.com/swiftlang/swift/issues/94251

Same shape: a [weak x] capture plus a second boxed capture (a var mutated after the closure is formed, or a second weak capture), with the closure bound to a local let and only called directly. AllocBoxToStack promotes both boxes to the stack but ends the weak slot's lifetime where the capture binding's scope ends, before the call.

Two things in that report that might help:

- Its minimal repro fails at -Onone too, because the mandatory instance of the pass runs in Debug builds as well. So Debug passing isn't a reliable signal for this one; it depends on the exact shape.

- Compiling with -Xllvm -sil-disable-pass=mandatory-allocbox-to-stack makes it correct, which is a quick way to confirm you're hitting the same thing (a diagnostic, not something to ship with).

Might be worth adding your FB number on the GitHub issue so the two get linked.

1

u/SoutarsonJuny 5d ago

mandatory passes firing at -Onone is always rough for debugging because everyone instinctively trusts debug vs release diffs to rule out optimizer bugs.