Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-1804] No compiler error for redeclaration of variable bound by guard #44413

Open
martinr448 opened this issue Jun 17, 2016 · 1 comment
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@martinr448
Copy link

Previous ID SR-1804
Radar None
Original Reporter @martinr448
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 6d550c1e961ae017ab560cbb576d283c

Issue Description:

Redeclaring a variable which is already bound by a guard statement does not cause a compiler error when done on the file level. The following compiles and runs without errors or warnings:

// main.swift
import Swift

guard let number = Int("1234") else { fatalError() }
print(number) // Output: 1234
let number = 5678
print(number) // Output: 1234

It looks as if the statement `let number = 5678` is completely ignored.

The problem only occurs with code on the file level. Doing the same inside a function causes a compiler error, as expected.

func foo() {
    guard let number = Int("1234") else { fatalError() }
    print(number)
    let number = 5678 //  error: definition conflicts with previous value
}

Tested with

  • Xcode 7.3.1, "Default" and "Snapshot 2016-06-06 (a)" toolchain

  • Xcode 8 beta.

@Dante-Broggi
Copy link
Contributor

This bug still occurs in the Xcode 9.4.1 toolchain.

Edit: this also 'works':

let number = 5678
print(number) // Output: 5678
guard let number = Int("1234") else { fatalError() }
print(number) // Output: 1234

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

2 participants