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-4811] Type's name can be assigned a value #47388

Open
masters3d opened this issue May 5, 2017 · 2 comments
Open

[SR-4811] Type's name can be assigned a value #47388

masters3d opened this issue May 5, 2017 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@masters3d
Copy link
Contributor

Previous ID SR-4811
Radar None
Original Reporter @masters3d
Type Bug
Environment

Swift version 3.1 (swiftlang-802.0.51 clang-802.0.41

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

md5: 91be8b3f253fd92bb0bf138ca675e353

relates to:

  • SR-1687 Swift does not warn about shadowing

Issue Description:

I recently learned that swifts allow variable names to shadow type names.
This is very surprising. Is this an intended feature?

let Void = "what?"
struct S {}
let S = 10

I've never run into this personally because I use swiftLint to force lowercase naming for variables.

@belkadan
Copy link
Contributor

belkadan commented May 8, 2017

Everything can shadow everything, but usually not in the same context. I get an error for your redeclaration of S.

<stdin>:3:5: error: invalid redeclaration of 'S'
let S = 10
    ^
<stdin>:2:8: note: 'S' previously declared here
struct S {}
       ^

We've talked a bit about having a warning for a variable or function shadowing a type, which you could silence with backticks.

@masters3d
Copy link
Contributor Author

Ah interesting, I only tried it on the REPL. Back ticks would be good. I think this only matters for built in types. Does that need a proposal?

Welcome to Apple Swift version 3.1 (swiftlang-802.0.51 clang-802.0.41). Type :help for assistance.
  1> let S = 10
S: Int = 10
  2> struct S {}
  3> print(S)
error: repl.swift:3:7: error: ambiguous use of 'S'
print(S)
      ^
  3>  

@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