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

Bugs with opaque result types in structural position #60038

Open
slavapestov opened this issue Jul 13, 2022 · 3 comments · Fixed by #60044
Open

Bugs with opaque result types in structural position #60038

slavapestov opened this issue Jul 13, 2022 · 3 comments · Fixed by #60044
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself expressions Feature: expressions opaque result types Feature → types → opaque types: opaque result types opaque types Feature → types: opaque types swift 5.7 type checker Area → compiler: Semantic analysis type inference Feature: type inference unexpected error Bug: Unexpected error

Comments

@slavapestov
Copy link
Member

Each of these three declarations should type check, but doesn't:

protocol P {
}

extension P {
  func f1() -> () -> (some P) {
    return { self } // error: cannot convert value of type 'Self' to closure result type 'some P'
  }

  func f2() -> () -> (some P) {
    return { () -> Self in self } // error: function declares an opaque return type, but has no return statements in its body from which to infer an underlying type
  }

  func f3() -> (some P).Type {
    return Self.self // error: function declares an opaque return type, but has no return statements in its body from which to infer an underlying type
  }
}
@slavapestov slavapestov added the bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. label Jul 13, 2022
@slavapestov
Copy link
Member Author

CC @jckarter

slavapestov added a commit to slavapestov/swift that referenced this issue Jul 13, 2022
…tion

Move the check where we insert UnderlyingToOpaqueExpr earlier in
coerceToType(). Otherwise, returning a metatype or function type
would attempt to perform a conversion instead of building a
UnderlyingToOpaqueExpr.

Fixes part of apple#60038.
slavapestov added a commit to slavapestov/swift that referenced this issue Jul 13, 2022
…tion

Move the check where we insert UnderlyingToOpaqueExpr earlier in
coerceToType(). Otherwise, returning a metatype or function type
would attempt to perform a conversion instead of building a
UnderlyingToOpaqueExpr.

Fixes part of apple#60038.
slavapestov added a commit to slavapestov/swift that referenced this issue Jul 13, 2022
…tion

Move the check where we insert UnderlyingToOpaqueExpr earlier in
coerceToType(). Otherwise, returning a metatype or function type
would attempt to perform a conversion instead of building a
UnderlyingToOpaqueExpr.

Fixes part of apple#60038.
slavapestov added a commit to slavapestov/swift that referenced this issue Jul 14, 2022
…tion

Move the check where we insert UnderlyingToOpaqueExpr earlier in
coerceToType(). Otherwise, returning a metatype or function type
would attempt to perform a conversion instead of building a
UnderlyingToOpaqueExpr.

Fixes part of apple#60038.
Catfish-Man pushed a commit to Catfish-Man/swift that referenced this issue Jul 28, 2022
…tion

Move the check where we insert UnderlyingToOpaqueExpr earlier in
coerceToType(). Otherwise, returning a metatype or function type
would attempt to perform a conversion instead of building a
UnderlyingToOpaqueExpr.

Fixes part of apple#60038.
@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler in itself type checker Area → compiler: Semantic analysis opaque types Feature → types: opaque types closures Feature: closures labels Dec 11, 2022
@AnthonyLatsis AnthonyLatsis added function types Feature → types: function types opaque result types Feature → types → opaque types: opaque result types unexpected error Bug: Unexpected error swift 5.7 type inference Feature: type inference expressions Feature: expressions and removed closures Feature: closures function types Feature → types: function types labels Apr 3, 2023
@vanvoorden
Copy link
Contributor

extension P {
  func f1() -> () -> (some P) {
    return { self } // error: cannot convert value of type 'Self' to closure result type 'some P'
  }
}

@slavapestov It looks like f2 and f3 are both fixed for me in Swift 5.9.2… but I'm still broken on f1. Was there any roadmap to fix that? Thanks!

@vanvoorden
Copy link
Contributor

//  https://github.com/apple/swift/blob/main/test/SILGen/opaque_result_type_structural.swift

public protocol P1 {}

extension P1 {
  public func f1() -> () -> (some P1) {
    let fn = { self }
    return fn
  }
  
  static public func f2() -> (Self) -> (some P1) {
    let fn: (Self) -> (Self) = { p in p }
    return fn
  }
}

public protocol P2 {}

extension P2 {
  public func f1() -> some P2 {
    return self
  }
  
  public func f2() -> some P2 {
    return self.f1()
  }
  
  public func f3() -> () -> (some P2) {
    let fn = { self.f2() }
    return fn
  }
  
  static public func f4() -> (Self) -> (some P2) {
    let fn: (Self) -> (Self) = { p in p.f2() }
    //  Cannot convert value of type 'some P2' to closure result type 'Self'
    return fn
  }
}

I was able to repurpose the workaround from opaque_result_type_structural here to unblock me on that… I'm still seeing a similar error for f4 when I don't know the concrete type… but that's kind of edge-casey and not blocking me at the moment.

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 expressions Feature: expressions opaque result types Feature → types → opaque types: opaque result types opaque types Feature → types: opaque types swift 5.7 type checker Area → compiler: Semantic analysis type inference Feature: type inference unexpected error Bug: Unexpected error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants