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-11722] Property wrapper Optional promotion causes type mismatch #54131

Closed
rnapier opened this issue Nov 6, 2019 · 1 comment
Closed

[SR-11722] Property wrapper Optional promotion causes type mismatch #54131

rnapier opened this issue Nov 6, 2019 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself property wrappers Feature: property wrappers type checker Area → compiler: Semantic analysis

Comments

@rnapier
Copy link

rnapier commented Nov 6, 2019

Previous ID SR-11722
Radar None
Original Reporter @rnapier
Type Bug
Status Resolved
Resolution Duplicate
Environment

Xcode 11.2 (11B52)

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

md5: 6aec3fe2d3fefdd4d0d79ed0065e5191

duplicates:

  • SR-11443 Property Wrapper overwrites type declaration

Issue Description:

If a property wrapper promotes a type to an Optional of that type, this can lead to a confusing mismatch of types. For example:

@propertyWrapper
class Default<T> {
    var wrappedValue: T


    init(_ defaultValue: T) {
        self.wrappedValue = defaultValue
    }
}


struct S {
    @Default(nil)
    var x: Int
}

Reading this code, you would expect `x` to be of type Int, but it's actually `Int?`. IMO this code should be invalid, and `x` should require that its type be `Int?` (in the same way that this code would be rejected if the type of `x` were String).

For a more complete example "in the wild," see https://stackoverflow.com/q/58656960/97337.

@theblixguy
Copy link
Collaborator

This was fixed by one of my PRs a while ago and now gets correctly diagnosed on master:

/Users/suyashsrijan/Desktop/test.swift:13:14: error: 'nil' is not compatible with expected argument type 'Int'
    @Default(nil)
             ^

@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 property wrappers Feature: property wrappers type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

2 participants