-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Description
Previous ID | SR-13905 |
Radar | rdar://problem/71913561 |
Original Reporter | benpious (JIRA User) |
Type | Bug |
Environment
Swift 5.3, Xcode Version 12.0 (12A7209.
Additional Detail from JIRA
Votes | 0 |
Component/s | Compiler |
Labels | Bug |
Assignee | None |
Priority | Medium |
md5: 07958dd0b29ff0cd98f83230cc3f4204
Issue Description:
Swift seems to treat vars declared in main as global vars for the purposes of scoping, and local vars in an implicit "main" function where memory safety is concerned.
As such, accessing a variable on a line before where it was declared causes programs to crash. The repro steps for this are to simply paste the following into a main.swift file in an SPM executable project:
print(directlyUsedTypes)
let directlyUsedTypes: [String: String] = [
"char": "UInt8",
]
This behavior was reported in SR-11598 in 2019, but the author of that bug says that you can't access the var in another file; while I've observed that any access of the var before it's initialized is unsafe.
It seems to me that main.swift should not be parsed in a special way and should be compiled as a normal swift file that happens to be wrapped in a declaration of a main function. I'm guessing it's more complicated than that though, or this bug would have been fixed long ago.