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-11443] Property Wrapper overwrites type declaration #53844

Closed
yutailang0119 opened this issue Sep 10, 2019 · 2 comments
Closed

[SR-11443] Property Wrapper overwrites type declaration #53844

yutailang0119 opened this issue Sep 10, 2019 · 2 comments
Assignees
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

@yutailang0119
Copy link

Previous ID SR-11443
Radar rdar://problem/55229986
Original Reporter @yutailang0119
Type Bug
Status Resolved
Resolution Done
Environment
  • macOS Mojave Version 10.14.6

  • Xcode 11 beta 7 (11M392r)

    • Apple Swift version 5.1 (swiftlang-1100.0.270.6 clang-1100.0.32.1)

    • Target: x86_64-apple-darwin18.7.0

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

md5: 58673a13bac0cd1a00f5a5960372cfe1

duplicates:

  • SR-10900 propertyDelegate type supersedes explicit type annotation
  • SR-11381 Property wrappers make the compiler trap

is duplicated by:

  • SR-11722 Property wrapper Optional promotion causes type mismatch
  • SR-11738 PropertyWrapper with Optional type can be used with non-optional type, results in Optional type

Issue Description:

UserDefaults Wrapper in the Property Wrapper example breaks the implicit type check with the following code:

import Foundation

@propertyWrapper
struct UserDefault<Value> {
    private let key: String
    private let defaultValue: Value
    private let defaults: UserDefaults

    init(key: String, defaultValue: Value, defaults: UserDefaults = .standard) {
        self.key = key
        self.defaultValue = defaultValue
        self.defaults = defaults
    }

    var wrappedValue: Value {
        get {
            (defaults.object(forKey: key) as? Value) ?? defaultValue
        }
        set {
            defaults.set(newValue, forKey: key)
        }
    }
}

final class Playground {
    @UserDefault(key: "foo", defaultValue: nil, defaults: .standard)
    var foo: String?

    @UserDefault(key: "bar", defaultValue: nil, defaults: .standard)
    var bar: String

   func printFooType() {
        print(type(of: self.foo))   // Optional<String>
    }

   func printBarType() {
        print(type(of: self.bar))   // Optional<String>
    }
}

This can be reproduced in the following simple case.

import Foundation

var value: Any = "value"

@propertyWrapper
struct Wrapper<Value> {
    private let defaultValue: Value

    init(defaultValue: Value) {
        self.defaultValue = defaultValue
    }    var wrappedValue: Value {
        get {
            (value as? Value) ?? defaultValue
        }
        set {
            value = newValue
        }
    }
}

final class Playground {
    @Wrapper(defaultValue: nil)
    var explicitlyOptional: String?

    @Wrapper(defaultValue: nil)
    var implicitlyOptional: String

    @Wrapper(defaultValue: "NonOptional")
    var nonOptional: String

    func printExplicitlyOptionalType() {
        print(type(of: self.explicitlyOptional))
    }

    func printImplicitlyOptionalType() {
        print(type(of: self.implicitlyOptional))
    }

    func printNonOptionalType() {
        print(type(of: self.nonOptional))
    }
}

let playground = Playground()
playground.printExplicitlyOptionalType()   // Optional<String>
playground.printImplicitlyOptionalType()   // Optional<String>
playground.printNonOptionalType()  // String

In this case, I think implicitlyOptional should be a compile error.

Similar to https://bugs.swift.org/browse/SR-11381 .

@belkadan
Copy link
Contributor

Yikes, thanks for reporting!

@swift-ci create

@theblixguy
Copy link
Collaborator

This was the same issue as SR-11381. Fixed on master by #27226 Please verify using the next available snapshot!

@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