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

Property wrappers unable to infer generic types when wrappedValue is an opaque type. #61274

Open
CraigSiemens opened this issue Sep 23, 2022 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself generics Feature: generic declarations and types property wrappers Feature: property wrappers type checker Area → compiler: Semantic analysis

Comments

@CraigSiemens
Copy link

Describe the bug

When a generic property wrapper's wrappedValue is an opaque type (ie using the some keyword), the generic types are unable to be inferred.

Steps To Reproduce

protocol _Publisher<Output, Failure> {
  associatedtype Output
  associatedtype Failure: Error
}

final class _PassthroughSubject<Output, Failure>: _Publisher where Failure: Error {}

@propertyWrapper
struct NewPropertyWrapper<Output, Failure: Error> {
  let subject = _PassthroughSubject<Output, Failure>()

  var wrappedValue: some _Publisher<Output, Failure> {
    subject
  }
}

struct Foo {
  @NewPropertyWrapper var doesntWork: some _Publisher<Int, Error>
}

Gives the following errors

expression failed to parse:
error: macOS.playground:157:4: error: generic parameter 'Output' could not be inferred
  @NewPropertyWrapper var doesntWork: some _Publisher<Int, Error>
   ^

macOS.playground:148:27: note: 'Output' declared as parameter to type 'NewPropertyWrapper'
struct NewPropertyWrapper<Output, Failure: Error> {
                          ^

error: macOS.playground:157:4: error: generic parameter 'Failure' could not be inferred
  @NewPropertyWrapper var doesntWork: some _Publisher<Int, Error>
   ^

macOS.playground:148:35: note: 'Failure' declared as parameter to type 'NewPropertyWrapper'
struct NewPropertyWrapper<Output, Failure: Error> {
                                  ^

macOS.playground:157:4: note: explicitly specify the generic arguments to fix this issue
  @NewPropertyWrapper var doesntWork: some _Publisher<Int, Error>
   ^
                     <Any, <#Failure: Error#>>

error: macOS.playground:157:27: error: property type 'some _Publisher' (type of 'Foo.doesntWork') does not match 'wrappedValue' type 'some _Publisher' (type of 'NewPropertyWrapper<Output, Failure>.wrappedValue')
  @NewPropertyWrapper var doesntWork: some _Publisher<Int, Error>
                          ^~~~~~~~~~

Explicitly adding the generics to the property wrapper type removes all but the last error.

  @NewPropertyWrapper<Int, Error> var doesntWork: some _Publisher<Int, Error>
error: macOS.playground:157:39: error: property type 'some _Publisher' (type of 'Foo.doesntWork') does not match 'wrappedValue' type 'some _Publisher' (type of 'NewPropertyWrapper<Output, Failure>.wrappedValue')
  @NewPropertyWrapper<Int, Error> var doesntWork: some _Publisher<Int, Error>
                                      ^~~~~~~~~~

Expected behavior

The generics for the property wrapper should be inferred from the generics used for the wrapped value.

Using any previously had the same issue in swift 5.7 but

  • Adding the generics to the property wrapper type was a workaround even tough it duplicated the generics declarations.
  • It has been fixed in swift 5.7.1.
protocol _Publisher<Output, Failure> {
  associatedtype Output
  associatedtype Failure: Error
}

final class _PassthroughSubject<Output, Failure> where Failure: Error {}

@propertyWrapper
struct NewPropertyWrapper<Output, Failure: Error> {
  let subject = _PassthroughSubject<Output, Failure>()
    
  var wrappedValue: any _Publisher<Output, Failure> {
    fatalError()
   }
}

struct Foo {
  @NewPropertyWrapper var doesntWork: any _Publisher<Int, Error>
}

Screenshots
n/a

Environment (please fill out the following information)

  • OS: macOS 12.5.1
  • Xcode Version/Tag/Branch: Version 14.1 beta 2 (14B5024i)

Additional context
n/a

@CraigSiemens CraigSiemens added the bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. label Sep 23, 2022
@theblixguy theblixguy added type checker Area → compiler: Semantic analysis generics Feature: generic declarations and types property wrappers Feature: property wrappers labels Sep 23, 2022
@AnthonyLatsis AnthonyLatsis added the compiler The Swift compiler itself label Dec 13, 2022
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 itself generics Feature: generic declarations and types property wrappers Feature: property wrappers type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

3 participants