nicklockwood / Expression

A cross-platform Swift library for evaluating mathematical expressions at runtime
MIT License
824 stars 50 forks source link

Test suite crashes on Xcode 13.3 / Swift 5.6 #37

Closed nighthawk closed 2 years ago

nighthawk commented 2 years ago

Running this repo's test suite on macOS 12.3 with Xcode 13.3 and Swift 5.6 crashes on my M1 MacBook Air with an EXC_BAD_ACCESS.

This happens in tests such as testTooFewArgumentsForCustomFunctionWithAdvancedInitializer and a number of other ones which test a mismatching number of arguments.

#0  0x000000016fdfcd98 in 0x16fdfcd98 ()
#1  0x00000001078bc278 in thunk for @escaping @callee_guaranteed (@in_guaranteed [Double]) -> (@out Double, @error @owned Error) ()
#2  0x00000001078f92e0 in partial apply for thunk for @escaping @callee_guaranteed (@in_guaranteed [Double]) -> (@out Double, @error @owned Error) ()
#3  0x00000001078dfba8 in Subexpression.optimized(withImpureSymbols:pureSymbols:) at /Users/adrian/Development/forks/Expression/Sources/Expression.swift:892
#4  0x00000001078de7ac in Expression.init(_:impureSymbols:pureSymbols:) at /Users/adrian/Development/forks/Expression/Sources/Expression.swift:351
#5  0x00000001078de6e0 in Expression.__allocating_init(_:impureSymbols:pureSymbols:) ()
#6  0x00000001078dfe20 in Expression.__allocating_init(_:pureSymbols:) at /Users/adrian/Development/forks/Expression/Sources/Expression.swift:372
#7  0x000000010788b350 in ExpressionTests.testTooFewArgumentsForCustomFunctionWithAdvancedInitializer() at /Users/adrian/Development/forks/Expression/Tests/ExpressionTests.swift:934

I couldn't yet narrow down what might be causing it and if it's an issue introduced with Swift 5.6 or with this library, or what to do about it. It happens both when running through Xcode or with swift test from the command line. Any advise appreciated!

nighthawk commented 2 years ago

Screenshot of the crash in Xcode, in case it helps.

Screen Shot 2022-03-24 at 14 31 31
nicklockwood commented 2 years ago

Looks like a Swift 5.6 regression. I'll investigate.

nicklockwood commented 2 years ago

@nighthawk is the crash affecting your project, or just the test failure? If it's only the latter I can just disable the test for now.

nighthawk commented 2 years ago

As far as I can tell, it doesn't seem to be impacting my project; just running the tests crashed.

I commented out these tests temporarily:

bhimsenp commented 2 years ago

@nicklockwood .. Seems following line is causing the issue (Expression.swift: line 354)

if let fn = pureSymbols($0) ?? Expression.mathSymbols[$0] ?? Expression.boolSymbols[$0] {
    return fn
}

Haven't figured out the exact cause yet, but looks like there is an issue where there is cascaded nil-coalescing operation. In one of the failing test, the fn evaluates to non-nil even when all 3 expressions are nil. Consequently, when it tries to execute line 892 (where crash is happening), it tries to execute the nil function and that's why the EXEC_BAD_ACCESS.

Adding brackets to first two correctly evaluated fn to be nil solves the crash.

if let fn = (pureSymbols($0) ?? Expression.mathSymbols[$0]) ?? Expression.boolSymbols[$0] {
    return fn
}

You can add this workaround temporarily.

nicklockwood commented 2 years ago

@bhimsenp thanks, that solved it!

nicklockwood commented 2 years ago

@nighthawk fixed in 0.13.6