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-1353] Shadowing variable name with Optional binding doesn't work inside a while loop #43961

Open
swift-ci opened this issue Apr 28, 2016 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-1353
Radar None
Original Reporter jaybuff (JIRA User)
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: f9b23c669a0aecc42cc654aeb5f30753

Issue Description:

This compiles:

let str = "DEADBEEF"
var result: [UInt8] = []
var pos = str.startIndex
let end = pos.advanced(by: 2)
let byteStr = str[pos..<end]
let byte = UInt8(byteStr, radix: 16)
guard let byte = byte else {
    fatalError("could convert \(byteStr) to UInt8")
}
result.append(byte)

But when I wrap it in a while loop, it doesn't compile:

let str = "DEADBEEF"
var result: [UInt8] = []
var pos = str.startIndex
while pos != str.endIndex {
    let end = pos.advanced(by: 2)
    let byteStr = str[pos..<end]
    let byte = UInt8(byteStr, radix: 16)
    guard let byte = byte else {
        fatalError("could convert \(byteStr) to UInt8")
    }
    result.append(byte)
    pos = end
}

The compiler fails with this error:

$ swift t.swift
t.swift:8:15: error: definition conflicts with previous value
    guard let byte = byte else {
              ^
t.swift:7:9: note: previous definition of 'byte' is here
    let byte = UInt8(byteStr, radix: 16)
        ^
t.swift:11:19: error: value of optional type 'UInt8?' not unwrapped; did you mean to use '!' or '?'?
    result.append(byte)
                  ^
                      !

$ swift --version
Swift version 3.0-dev (LLVM 752e1430fc, Clang 3987718dae, Swift 36739f7b57)
Target: x86_64-unknown-linux-gnu
@belkadan
Copy link
Contributor

I think we don't actually allow same-scope shadowing anywhere, and then it coincidentally works with parameters and at the top-level because we're not checking it properly. It's probably worth coming up with the model for how you want it to work, and then making that happen everywhere. That may be language-related enough to go through the Swift Evolution Process.

(As an example of a tricky case, what if the name being shadowed is a var?)

@Dante-Broggi
Copy link
Contributor

This still occurs in the Xcode 10 toolchain.

@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

3 participants