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-12023] Generics with Self on class type failing compiler #54460

Closed
swift-ci opened this issue Jan 13, 2020 · 3 comments
Closed

[SR-12023] Generics with Self on class type failing compiler #54460

swift-ci opened this issue Jan 13, 2020 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-12023
Radar None
Original Reporter ebg (JIRA User)
Type Bug
Status Resolved
Resolution Duplicate
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: 807330616794d991a0e0b64873f80e22

duplicates:

  • SR-11176 Covariant 'Self' type cannot be referenced from a stored property initializer
  • SR-11414 Final class type and Self should be treated as equivalent in class body

Issue Description:

See forum thread for more details:

https://forums.swift.org/t/property-wrappers-access-to-both-enclosing-self-and-wrapper-instance/32526

This property wrapper compiles, but not when used:

@propertyWrapper
public final class Wrapper {
    
    public static subscript<EnclosingSelf>(
      _enclosingInstance observed: EnclosingSelf,
      wrapped wrappedKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Int?>,
      storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Self>
    ) -> Int? {
      get {
        return observed[keyPath: storageKeyPath].stored
      }
      set {
        let oldValue = observed[keyPath: storageKeyPath].stored
        if newValue != oldValue {
            // TODO: call wrapper instance with enclosing self
        }
        observed[keyPath: storageKeyPath].stored = newValue
      }
    }
    
    public var wrappedValue: Int? {
      get { fatalError("called wrappedValue getter") }
      set { fatalError("called wrappedValue setter") }
    }
    
    public init(str: String) {
        self.str = str
    }
    
    // MARK: - Private
    
    private let str: String
    private var stored: Int?
}

When used, the compiler fails:

open class TestView: UIView {
    @Wrapper(str: "HelloWorld") public var testProp: Int?
}

/** 
    Swift Compiler Error
    Type '_' has no member 'testProp'
*/

Replacing `Self` with `Wrapper` in the code fixes the issue. If I keep `Self`, I can also change the class to a struct to fix the issue.

@theblixguy
Copy link
Collaborator

The diagnostic is bad, but luckily its improved on 5.2 and it now tells you exactly what's wrong: covariant 'Self' can only appear as the type of a property, subscript or method result; did you mean 'Wrapper'?.

@theblixguy
Copy link
Collaborator

Self and Wrapper should be equivalent in this case since Wrapper is a final class, however it's just something that isn't implemented yet. There are some dups for this bug, which I have linked to this issue.

@beccadax
Copy link
Contributor

@swift-ci create

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

No branches or pull requests

3 participants