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-8806] Swift (4.2) release compilation has code execute in unexpected order #51314

Open
swift-ci opened this issue Sep 20, 2018 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself optimized only Flag: An issue whose reproduction requires optimized compilation

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-8806
Radar rdar://problem/44643479
Original Reporter JoannisO (JIRA User)
Type Bug

Attachment: Download

Environment

Swift 4.2 (Xcode 10), macOS 10.13.6 in the iPhone X simulator or on-device (iPhone X).

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

md5: 6d1b5c045d726d64ec86996e4dd6a8fb

Issue Description:

In the attached project when compiled the expected behaviour would be that `A` is printed 4 times, then "B" is printed after which "C" is printed and a crash occurs.

This works completely normal on Debug compilation, on release however just "C" is printed and then a crash occurs.

In my own codebase, the print statements printing `A` are setup code that is required to execute first in order for the `State.default` to be initialized. The crash in my app occurs because I `try!` the result knowing it must've been set up resulting in the crash leading me to finding this.

@belkadan
Copy link
Contributor

@swift-ci create

@eeckstein
Copy link
Member

This is expected behavior.
The optimizer is free to move the initialization point of global variables.

If you need to rely on side effects, then you can do something like

static var _default: State?

static var `default`: State {
if let d = _default { return d }
let d = State()
_default = d
return d
}

@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 optimized only Flag: An issue whose reproduction requires optimized compilation
Projects
None yet
Development

No branches or pull requests

3 participants