Variable redefinition via guard

On swift.godbolt.org this code:

let x: Int = 1
guard let x: String = Optional("a") else { fatalError() }

At global scope compiles on all available versions of the Swift compiler (going back to 3.1.1).

However, that exact same code inside a smaller scope (eg. a do block or a function body) first began compiling in Swift 5.4.

So I surmise that it was always meant to be valid, and the fact that it was not valid in nested scopes through Swift 5.3 was a bug which has now been fixed.

4 Likes