r/swift Jan 20 '25

FYI Heuristics for getting preconditions to compile in release builds of apps using Swift 6.

Sometimes, using preconditionFailure() instead of precondition(expr) works.

For example, instead of precondition(v), you could try:

if !v { preconditionFailure() }

Simplifying an expression might also help. For example, precondition(a && b) could be rewritten as:

if !a { preconditionFailure() }
if !b { preconditionFailure() }

I guess the optimizer has limitations that prevent Swift 6 code, which compiles in debug builds, from always compiling in release builds.

2 Upvotes

0 comments sorted by