Swift 5 @unknown case. Compiler bug ?

I have adapted code to Swift 5. No issues, no correction to make, compatibility is great.


However, I have a switch case for AVCaptureDevice.authorizationStatus:

        switch AVCaptureDevice.authorizationStatus(for: .video) {
        case .authorized:  accessGranted = true
  
        case .notDetermined:  AVCaptureDevice.requestAccess(for: .video) { granted in
                if granted { accessGranted = true  }
            }
        case .denied:
            return
        case .restricted:
            return
        }


I get a (logical) warning: Handle unknown values using "@unknown default"

I am proposed a fix (not always: in a second code, with the exact same sequence, on the same machine, same Switch5 setting, I don't get the fix proposed) to add at the end

@unknown default:

<#fatalError()#>

I accept and implement this:

        case .restricted:
            return
           @unknown default:
           return
       }


Then error at line 2: Expected expression in 'return' statement


If I add a semi colon after return

        case .restricted: // The user can't grant access due to restrictions.
            return;
        @unknown default:
            return
        }

Everything OK, and identation correct.


@unknown is interpretated as the argument for return

Is this a known bug ? Or expected behavior ?

Accepted Reply

I recommend that you file a bug about this. Clearly the compiler is parsing

@unknown default
as an expression rather than the start of the next case. It would be nice if there were a way to improve that, but I don’t know enough about the compiler internals to tell you whether it’s possible. The best way forward is to put a bug report in front of the Swift team and let the experts investigate.

FYI, here’s a simpler example that doesn’t rely on AVFoundation:

func test(status: Stream.Status) {
    switch status {
    case .open, .notOpen, .opening, .reading, .writing, .atEnd, .closed, .error:
        return
    @unknown default:
        return
    }
}

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

I recommend that you file a bug about this. Clearly the compiler is parsing

@unknown default
as an expression rather than the start of the next case. It would be nice if there were a way to improve that, but I don’t know enough about the compiler internals to tell you whether it’s possible. The best way forward is to put a bug report in front of the Swift team and let the experts investigate.

FYI, here’s a simpler example that doesn’t rely on AVFoundation:

func test(status: Stream.Status) {
    switch status {
    case .open, .notOpen, .opening, .reading, .writing, .atEnd, .closed, .error:
        return
    @unknown default:
        return
    }
}

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks.


I filed a bug #50280220

I filed a bug #50280220

Thanks!

Your bug got dup’d to another bug (r. 48634159), which lead me to a Swift open source bug, SR-9920. If you look at that you’ll see it’s already fixed, but the fix hasn’t made it into a shipping Xcode yet.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks, good news.


I was confident it would be fixed rapidly (was probably not very hard, already done for 'case' after return …)