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-2727] Script mode rules for top-level bindings are confusing and broken #45331

Closed
swift-ci opened this issue Sep 22, 2016 · 3 comments
Closed
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-2727
Radar None
Original Reporter netfnet (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 051659726cbef6b8c0f2029b8342062b

duplicates:

  • SR-284 Strange error caused by let/var at specific place in main file.

is duplicated by:

  • SR-2730 Failure to initialize global variable

Issue Description:

Dear Swift Team!
I have a very simple project. It is Command Line Tool written on Swift 3.0 using Xcode 8.0. This program is:

import Foundation
func aaa() {
    print(a)
}
let a = "a"
aaa() 

This is working perfectly well and printing "a" in console, but lets do this program more complex:

import Foundation
func aaa() {
    print(a)
    print(b)
}
let a = "a"
let b = "b"
aaa()

And line
print(b)
is marked with error:
Use of unresolved identifier 'b'

We can make even easier:

import Foundation
func aaa() {
    print(a)
}
aaa()
let a = "a"

And again, line
print(a)
is marked with error:
Use of unresolved identifier 'a'

I am not newbie and I undertand that I can easily fix this error like putting all variables in the beginning of the program. Question is: why is it happening? I thought each file with extension .swift, it is a class and I can put variable and functions, call functions in any order (all variables and constants would be global)... And one last thing, I don't have ability to test this on Swift 2.2, but I don't remember I faced this bug before, so can it be a error of Swift 3.0 compiler? Thank you for any answer!

@swift-ci
Copy link
Collaborator Author

Comment by Janek Spaderna (JIRA)

Just formatted the code for readability

@belkadan
Copy link
Contributor

@jepers just brought this up on swift-users as well; here's my explanation from there: https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20160919/003383.html

@belkadan
Copy link
Contributor

I'm pretty sure it's not a behavior change, though. Swift 2 should have done the same thing.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
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