nicklockwood / Expression

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

Xcode 16 beta: iOS 18 succeeds were iOS 17 fails #53

Closed nighthawk closed 2 months ago

nighthawk commented 2 months ago

(This might be fixed in a future beta, but I thought I'd raise it in case there's a workaround.)

After fixing compile errors related to #52, the test suite succeeds on iOS 18 but fails on iOS 17. Something iffy seems to be going on with the _Optional check.

This test for example crashes on iOS 17:

    func testCastDoubleAsOptionalString() {
        let expression = AnyExpression("5.6")
        XCTAssertEqual(try expression.evaluate() as String?, "5.6")
    }

The different behaviour is in this function:

    // Cast a value to the specified type
    static func cast<T>(_ anyValue: Any) -> T? {
        if let value = anyValue as? T {
            return value
        }
        var type: Any.Type = T.self
        if let optionalType = type as? _Optional.Type {
            type = optionalType.wrappedType
        }
        switch type { 
        // ...   
        case is Substring.Type:
            return (anyValue as? _String)?.substring as! T?
        default:
            return nil
        }
    }

On iOS 18 that type as? _Optional.Type succeeds while in iOS 17 it fails, which then returns nil in the end, which then crashes due to the force unwrap in return (AnyExpression.cast(AnyExpression.stringify(anyValue)) as T?)!

This is on 16.0 Beta 2. Downloading Beta 3 now to see if it's the same issue there.

nicklockwood commented 2 months ago

I've added a workaround on the develop branch that seems to work but it's not ideal. I'll file a bug with Apple

nicklockwood commented 2 months ago

I think I'm no longer able to to reproduce this in b3, even without my fix applied - can you confirm?

nighthawk commented 2 months ago

Same here. Looks like b3 fixed it and you could remove your workaround. Good to close this then.