swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.6k stars 10.37k forks source link

[SR-6451] Initializer is able to recursively call itself with no warnings #49001

Closed colemancda closed 6 years ago

colemancda commented 6 years ago
Previous ID SR-6451
Radar None
Original Reporter @colemancda
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment Swift 4.1 development snapshot on [Ubuntu 16.04](https://swift.org/builds/swift-4.1-branch/ubuntu1604/swift-4.1-DEVELOPMENT-SNAPSHOT-2017-11-20-a/swift-4.1-DEVELOPMENT-SNAPSHOT-2017-11-20-a-ubuntu16.04.tar.gz) [swift-4.1-DEVELOPMENT-SNAPSHOT-2017-11-20-a-ubuntu16.04.tar.gz](https://swift.org/builds/swift-4.1-branch/ubuntu1604/swift-4.1-DEVELOPMENT-SNAPSHOT-2017-11-20-a/swift-4.1-DEVELOPMENT-SNAPSHOT-2017-11-20-a-ubuntu16.04.tar.gz) Also tried in Ubuntu 16.10 [swift-4.1-DEVELOPMENT-SNAPSHOT-2017-11-20-a-ubuntu16.10.tar.gz](https://swift.org/builds/swift-4.1-branch/ubuntu1610/swift-4.1-DEVELOPMENT-SNAPSHOT-2017-11-20-a/swift-4.1-DEVELOPMENT-SNAPSHOT-2017-11-20-a-ubuntu16.10.tar.gz)
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: af8609b588fceb597731dd9852454e34

duplicates:

Issue Description:

Any initializer can be called recursively and the compiler will emit no warnings. The resulting code will crash at runtime with an non-descriptive error (Segmentation Fault). The Swift compiler should not allow initializers to call themselves recursively, unlike methods or functions there is no scenario where this will not crash.

struct Value {
    let value: Int
    init(_ value: Int) {
        self.init(value)
    }
}

let value = Value(1) // crash

Here are other reproducible examples:

https://user-images.githubusercontent.com/2727770/164962448-399c6ca2-849e-45ac-a724-8be99343d0cc.gz

http://swift.sandbox.bluemix.net/#/repl/5a15f40343e1ee1b72bcd2f9

http://swift.sandbox.bluemix.net/#/repl/5a15f8d243e1ee1b72bcd2fb

colemancda commented 6 years ago

Can also reproduce with Cacao v0.6.2 and swift-4.1-DEVELOPMENT-SNAPSHOT-2017-11-20-a-ubuntu16.10.tar.gz

swift package update
swift build
.build/x86_64-unknown-linux/debug/CacaoDemo

The same code compiles and runs fine with Xcode and on Linux with swift-DEVELOPMENT-SNAPSHOT-2017-06-26-a-ubuntu16.10.tar.

swift build
.build/debug/CacaoDemo
colemancda commented 6 years ago

Upon debugging, the crash occurs when UIView's initializer is called.