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-10752] Trouble with Generic Constraints and Associated Types #53142

Closed
stephencelis opened this issue May 22, 2019 · 5 comments
Closed

[SR-10752] Trouble with Generic Constraints and Associated Types #53142

stephencelis opened this issue May 22, 2019 · 5 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

@stephencelis
Copy link
Contributor

Previous ID SR-10752
Radar rdar://problem/51068593
Original Reporter @stephencelis
Type Bug
Status Resolved
Resolution Done
Environment

Swift 5.1

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

md5: 9d6ac07735765d370875cf592220ea2b

is duplicated by:

  • SR-13019 Bad compiler error on generic constraint of associated types

Issue Description:

Given the following parser type:

struct Parser<I, O> {
  let run: (inout I) -> O?
} 

I can define the following parse function from inout Substring:

extension Parser where I == Substring {
  func parse(_ input: String) -> O? {
    var sub = input[...]
    guard
      let result = self.run(&sub),
      sub.isEmpty
      else { return nil }
    return result
  }
} 

I'd preferably like to work more generically on any Collection, but the following doesn't compile.

extension Parser where I: Collection {
  func parse<C: Collection>(_ input: C) -> O? where I == C.SubSequence {
    var sub = input[...]
    guard
      let result = self.run(&sub), // ERROR: Use of extraneous '&'
      sub.isEmpty
      else { return nil }
    return result
  }
} 

The error is puzzling, but it seems to be that Swift isn't considering the I generic and C.SubSequence equivalent, despite the where clause.

extension Parser where I: Collection {
  func parse<C: Collection>(_ input: C) -> O? where I == C.SubSequence {
    var sub = input[...] as I // ERROR: 'C.SubSequence' is not convertible to 'I'
    guard
      let result = self.run(&sub),
      sub.isEmpty
      else { return nil }
    return result
  }
}  

We can force-cast our way to another error:

 extension Parser where I: Collection {
  func parse<C: Collection>(_ input: C) -> O? where I == C.SubSequence {
    var sub = input[...] as! I
    guard
      let result = self.run(&sub),
      sub.isEmpty // ERROR: Value of type 'I' has no member 'isEmpty'
      else { return nil }
    return result
  }
}  

But now we're stuck. I is constrained to Collection but it doesn't seem to think it has isEmpty as a member.

@belkadan
Copy link
Contributor

Definitely a bug. Casting back seems to work around it for now?

extension Parser where I: Collection {
  func parse<C: Collection>(_ input: C) -> O? where I == C.SubSequence {
    var sub = input[...] as! I
    guard
      let result = self.run(&sub),
      (sub as! C.SubSequence).isEmpty
      else { return nil }
    return result
  }
}

@stephencelis
Copy link
Contributor Author

Ah good find! Thanks for unblocking in the meantime 🙂

@stephencelis
Copy link
Contributor Author

Another failure for good measure:

public struct Parser<I, O> {
  public let run: (inout I) -> O?
}

extension Parser where I: StringProtocol {
  public func parse<S>(_ input: S) -> O? where S: StringProtocol, S.SubSequence == I {
    var input = input[...] as! I
    guard
      let match = self.run(&input),
      (input as! S.SubSequence).isEmpty
      else { return nil }
    return match
  }
} 

@stephencelis
Copy link
Contributor Author

(This succeeds, but note the force casts throughout.)

@slavapestov
Copy link
Member

#42113

@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