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-214] Swift 2: Guard let can shadow func param but not let-bound local constant #42836

Closed
swift-ci opened this issue Dec 13, 2015 · 6 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-214
Radar rdar://problem/22609757
Original Reporter rsmoz (JIRA User)
Type Bug
Environment

Swift 2.2 OS X

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 57b1448c42c33942b5fa1e72da00755f

is duplicated by:

  • SR-9070 Variable shadowing inconsistency with guard

relates to:

  • SR-1687 Swift does not warn about shadowing
  • SR-8524 Shadowing in protocols leads to difficult to find bugs

Issue Description:

func a(x: Int?) {
    if let x = x { //No compiler error when if-let shadows parameter
        print(x+1)
    }
}

func b(x: Int?) {
    guard let x = x else { return } //No compiler error when guard-let shadows parameter
    print(x+1)
}

func c() {
    let x: Int? = nil
    if let x = x { //No compiler error when if-let shadows local constant
        print(x+1)
    }
}


func d() {
    let x: Int? = nil
    guard let x = x else { return } //!Compiler error! when guard-let shadows local constant
    print(x+1)
}

There's also a corresponding Radar: https://openradar.appspot.com/22609757

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jan 9, 2016

Comment by Robert S Mozayeni (JIRA)

calebd (JIRA User) makes the case that this already works as it should, even if it is mildly inconvenient that d isn't supported. I've come to agree with him, but am still leaving this open for the Swift team to decided what to do with it.

@swift-ci
Copy link
Collaborator Author

swift-ci commented Jan 9, 2016

Comment by Caleb Davenport (JIRA)

I understand the motivation for this (and have been in a situation where this would be nice) but I am not convinced that this is a bug.

Scenarios a, b, and c work because the symbol `x` is shadowing a variable in a new, more narrow scope.

`a` and `b` both create symbol `x` in a new scope by shadowing the function parameter `x` which comes from a higher scope. `c` creates a symbol `x` in a new scope by shadowing a local constant which is a higher scope.

`d` attempts to overwrite `x` in the same scope since guard does not create a new scope. In order for this to succeed, one would need the ability to universally overwrite symbols in a single scope which breaks a lot of the safety that Swift provides.

For example, this would have to be legal since it is overwriting both the type and value of a named constant just as example `d` does in the original post:

func d() {
    let x: Int? = 42
    let x = "42"
}

@dduan
Copy link
Collaborator

dduan commented Oct 24, 2018

To me the biggest violation of intuition is the scope of a parameter: it seems to be available in the same places a local variable is (not uncommon in PLs), but one can shadow it with a `guard` as well.

Unfortunately, restricting this type of shadowing is probably too source-breaking. I also understand the tradeoff with allowing same-scope shadowing.

@martinr448
Copy link

Has this bug been fixed in the meantime? func d() compiles and runs without problems with Xcode 12.5.1 and

Swift version 5.4.2.

@swift-ci
Copy link
Collaborator Author

Comment by Hristo Hristov (JIRA)

I also would like to know why local shadowing is compiling and working in current Xcode 12 and not working in another CI environment?

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis
Copy link
Collaborator

We do support this now apparently.

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

4 participants