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-11494] Inconsistent stored Type property initialization #53895

Closed
byaruhaf opened this issue Sep 20, 2019 · 3 comments
Closed

[SR-11494] Inconsistent stored Type property initialization #53895

byaruhaf opened this issue Sep 20, 2019 · 3 comments
Assignees
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

@byaruhaf
Copy link

Previous ID SR-11494
Radar rdar://problem/55560965
Original Reporter @byaruhaf
Type Bug
Status Closed
Resolution Done

Attachment: Download

Environment

Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7)
Target: x86_64-apple-darwin18.7.0

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

md5: 91486b04671c58c338d057aed49946b1

Issue Description:

Stored type properties are supposed to be lazily initialized on their first access.

As per the Documentation

However, it was observed from a StackOverflow Question that when compiled with the -O flag to optimize code during compilation. The Stored Type Property is initialized yet it was never accessed and is not reachable during execution.

Find the attached swift file to replicate this behavior.

class SomeClass {
    static let someValue: Int = {
        print("some value init")
        return 1
    }()
}

class TestClass {
    let falseValue = false
    let falseValue2 = false
    func foo() {
        if falseValue {
            print(SomeClass.someValue)
        }
        if falseValue2 {
            print(SomeClass.someValue)
        }
    }
}
let a = TestClass()
a.foo()

When complied -O flag

~ ❯ swiftc -O test.swift
~ ❯ ./test
some value init

When complied -Onone flag

~ ❯ swiftc -Onone test.swift
~ ❯ ./test
~ ❯
@belkadan
Copy link
Contributor

Yikes, thanks for filing!

@swift-ci create

@byaruhaf
Copy link
Author

This has been fixed in Swift 5.3

alphabo@Obelix  ~ 
❯ xcrun swift --version [21:15:55]
Apple Swift version 5.2.4 (swiftlang-1103.0.32.9 clang-1103.0.32.53)
Target: x86_64-apple-darwin20.0.0

alphabo@Obelix  ~ 
❯ swiftc -O test.swift [21:15:59]

alphabo@Obelix  ~ 
❯ ./test [21:16:02]
some value init

alphabo@Obelix  ~ 
❯ chxcode 12.0 [21:16:07]

alphabo@Obelix  ~ 
❯ xcrun swift --version [21:16:12]
Apple Swift version 5.3 (swiftlang-1200.0.25.2 clang-1200.0.27.1)
Target: x86_64-apple-darwin20.0.0

alphabo@Obelix  ~ 
❯ swiftc -O test.swift [21:16:18]

alphabo@Obelix  ~ 
❯ ./test [21:16:21]

alphabo@Obelix  ~ 

@byaruhaf
Copy link
Author

issue fixed in Swift 5.3

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

No branches or pull requests

2 participants