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-3316] "Script mode" globals have internal visibility and can be accessed before initialization from other files #45904

Open
jckarter opened this issue Dec 2, 2016 · 1 comment
Labels
access control Feature → modifiers: Access control and access levels bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself feature A feature request or implementation good first issue Good for newcomers swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal

Comments

@jckarter
Copy link
Member

jckarter commented Dec 2, 2016

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

md5: e092a51eda538e02ab484391d0b1b1a5

Issue Description:

Variables declared in a script default to internal visibility, like other global declarations, but this is problematic, because semantically they behave more like local variables. Another file can access the global before its initializer has even run:

// main.swift
class C {}
var x: C
foo()
x = C()
// foo.swift
func foo() {
  print(x) // loads garbage from `x` from main.swift
}

In a future version of Swift, script globals should be changed to be private by default. We could possibly also allow a script global to be declared with explicit access control, which would make it visible to other files but would also have to make the variable behave more like a library global, with a dispatch_once-d initializer, e.g.:

// main.swift
var x = 42 // private, eagerly initialized
private var y = 17 // private, initialized on demand
internal var z = 38 // internal, initialized on demand
@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added good first issue Good for newcomers access control Feature → modifiers: Access control and access levels feature A feature request or implementation swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal and removed StarterProposal labels Nov 11, 2022
@Vivek09Chahal
Copy link

I have think of this program and I came to a conclusion and I would like to do work on this problem. I will be glad if I will get chance to work on this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
access control Feature → modifiers: Access control and access levels bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself feature A feature request or implementation good first issue Good for newcomers swift evolution proposal needed Flag → feature: A feature that warrants a Swift evolution proposal
Projects
None yet
Development

No branches or pull requests

3 participants