Swift 2: Guard let can shadow let-bound func param but not let-bound local name
Originator: | jeremyw.sherman | ||
Number: | rdar://22609757 | Date Originated: | 08-Sep-2015 09:48 AM |
Status: | Open | Resolved: | |
Product: | Developer Tools | Product Version: | |
Classification: | Serious Bug | Reproducible: | Always |
Summary: If I have func a(x: Int?), I can guard let x = x else { return }, and this works fine. I love this, because it lets me avoid unnecessary nesting without having to fiddle with names. But if, within a(_:), I do let b = maybeInt(); guard let b = b else { return }, I get a redefinition error for b. This is frustrating; now I need to do something like let maybeB = maybeInt(); guard let b = maybeB else { return }. It seems weird that the let-bound function parameter can be shadowed, but not the let-bound local name. Steps to Reproduce: > swift (.lldbinit loaded) fcWelcome to Apple Swift version 2.0 (700.0.57 700.0.72). Type :help for assistance. 1> func b() -> Int? { return 3 } 2> func a(x: Int?) { 3. guard let x = x else { return } 4. let y = b() 5. guard let y = y else { return } 6. print(x, y) 7. } repl.swift:5:15: error: definition conflicts with previous value guard let y = y else { return } ^ repl.swift:4:9: note: previous definition of 'y' is here let y = b() ^ Expected Results: b is shadowed for the rest of the method body with the unwrapped value of b, so that b: Int? is replaced with b: Int. This would be identical to the handling of the let-bound function argument name. Actual Results: repl.swift:5:15: error: definition conflicts with previous value guard let y = y else { return } ^ repl.swift:4:9: note: previous definition of 'y' is here let y = b() ^ Version: Apple Swift version 2.0 (swiftlang-700.0.57.3 clang-700.0.72) Target: x86_64-apple-darwin14.5.0 Xcode 7.0 Build version 7A192o ProductName: Mac OS X ProductVersion: 10.10.5 BuildVersion: 14F27 Notes: Configuration: Attachments:
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!