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-11738] PropertyWrapper with Optional type can be used with non-optional type, results in Optional type #54145

Closed
krzyzanowskim opened this issue Nov 8, 2019 · 2 comments
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

Comments

@krzyzanowskim
Copy link
Contributor

Previous ID SR-11738
Radar None
Original Reporter @krzyzanowskim
Type Bug
Status Closed
Resolution Done
Environment

Apple Swift version 5.1.2 (swiftlang-1100.0.278 clang-1100.0.33.9)

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

md5: 44cdbf3385c675c2ca0b4f71f6fbd192

duplicates:

  • SR-11443 Property Wrapper overwrites type declaration

Issue Description:

PropertyWrapper with Optional type can be used with non-optional type, results in Optional type

    @propertyWrapper
    struct Wrap<T> {
      var wrappedValue: T?
    }
    
    class Foo {
      @Wrap()
      var name: String // Non-optional declaration
    }
    
    // What is the type of `Foo.name`
    let foo = Foo()
    type(of:foo.name) // String?

It looks like a bug to me. Compiler should throw an error about type mismatch for String and Optional<String>, shouldn't it?

@rnapier
Copy link

rnapier commented Nov 8, 2019

I believe this is a dupe of SR-11443.

@theblixguy
Copy link
Collaborator

This is fixed on Xcode 11.2.1.

Property type 'String' does not match that of the 'wrappedValue' property of its wrapper type 'Wrap'

However, there's another (separate) bug here - the wrapper is being default-initialised (by doing @Wrap()), which is somehow interfering with the diagnosis of the type mismatch, but that seems to be resolved on 5.2.

error: generic parameter 'T' could not be inferred
          @Wrap() var name: String // Non-optional declaration
           ^
note: 'T' declared as parameter to type 'Wrap'
        struct Wrap<T> {
                    ^
error: cannot convert value of type 'String' to specified type 'T?'
          @Wrap() var name: String // Non-optional declaration
           ^~~~~~

In the meantime, don't default initialize the wrapper (i.e. change @Wrap() to @Wrap).

@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
Projects
None yet
Development

No branches or pull requests

3 participants