swiftlang / swift

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

[SR-12401] Non-optional type disallowed in property wrapper initializer #54839

Open JaviSoto opened 4 years ago

JaviSoto commented 4 years ago
Previous ID SR-12401
Radar rdar://problem/62200979
Original Reporter @JaviSoto
Type Bug
Environment Swift 5.2
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: b26dd8a4c28f0c88eae100f6c11a6493

Issue Description:

Consider the following slightly contrived example:

@propertyWrapper
struct PropertyWrapper {
    var wrappedValue: Any? {
        didSet {
            print("\(String(describing: wrappedValue)))")
        }
    }

    init() {}

    init(wrappedValue: Any, printingInitialValue: Bool = true) {
        self.wrappedValue = wrappedValue

        if printingInitialValue {
            print("\(wrappedValue)")
        }
    }
}

This fails in Swift 5.2 / Xcode 11.4 with the following error:

error: 'init(wrappedValue:printingInitialValue:)' parameter type ('Any') must be the same as its 'wrappedValue' property type ('Any?') or an @autoclosure thereof
    init(wrappedValue: Any, printingInitialValue: Bool = true) {

I think this ought to be allowed, as Any is compatible with Any? everywhere else in the language.

beccadax commented 4 years ago

@swift-ci create