yourkarma / JWT

A JSON Web Token implementation in Objective-C.
MIT License
350 stars 107 forks source link

Xcode 10 Swift optional chaining bug #204

Open MarcoFiletti opened 5 years ago

MarcoFiletti commented 5 years ago

Issue Info

Info Value
Platform Name iOS
Platform Version 11.4
Integration Method CocoaPods
Xcode Version Xcode 10 (does not apply to Xcode 9)
Repro rate 100%
Demo project link See code below

Issue Description and Steps

Hello, I'm wondering why optional chaining (in a Swift project, integrated with CocoaPods) always fails by returning nil. However, step-by-step (or chaining done in the debugger) works. For example, try this:

let set = JWTClaimsSet()
set.issuer = "testIssuer"
set.subject = "testSubjectx"

let secret = "aaabbbcccddd"

let heads = ["customKey": "customValue"]

let algo = JWTAlgorithmHSBase.algorithm384()

let e = JWTBuilder.encode(set)!
let h = e.headers(heads)!
let s = h.secret(secret)!
let b = s.algorithm(algo)!
let enc = b.encode

guard let encoded = enc else {
    fatalError("No encoded")
}

print(encoded)  // succeeds

print("success")  // note that if we insert a breakpoint here and run `po JWTBuilder.encode(set)?.headers(heads)?.secret(secret)?.algorithm(algo)?.encode` we get a non-nil value

guard let encoded2 = JWTBuilder.encode(set)?.headers(heads)?.secret(secret)?.algorithm(algo)?.encode else {
   fatalError("No encoded 2")  // fails
}

print(encoded2)

Note that this only happens in Xcode 10 (not 9) so maybe it's a compiler bug?

michaelspecht commented 5 years ago

I'm seeing this on Xcode 10.1 as well on the latest release (3.0.0-beta.11, though it was happening on 3.0.0-beta.5 as well)

lolgear commented 5 years ago

@MarcoFiletti @michaelspecht Hi! I have figure out what is going on. It is a swift correct behavior with fluent-ish blocks that captures weakSelf. It is fixed in latest master for Version 3 only.