swiftlang / swift-experimental-string-processing

An early experimental general-purpose pattern matching engine for Swift.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unexpected compiler error for CharacterClass.inverted

Kyle-Ye opened this issue · comments

commented

inverted defined in CharacterClass will return CharacterClass. But the compiler is emitting an error for it.

let a0 = OneOrMore(.anyOf("a")) // ✅
let a1 = OneOrMore(.anyOf("a").inverted) // ❌(Unexpected) Member 'inverted' in 'CharacterClass' produces result of type 'RegexComponent', but context expects 'CharacterClass'
let a2 = OneOrMore(CharacterClass.anyOf("a").inverted) // ✅
commented

Looks like this is an issue on the swift Compiler side. I think it was matched to Anchor.inverted which lead to the problem. But actually .anyOf will only return CharacterClass

@natecook1000 have you seen anything like this before?

I have not! Looks like there might be some interference between SE-0287 and SE-0299? I'll try a reduced example.

Edit: This is pretty much the RegexBuilder setup, but doesn't reproduce the problem. Am I missing something? We don't even have the Anchor extension methods that would allow it to be selected in that some RegexComponent context.

protocol MyComponent {}

func takesMyComponent(_ c: some MyComponent) {
  print(c)
}

struct MyCharacterClass: MyComponent {
  var id: Int
  
  var inverted: MyCharacterClass {
    MyCharacterClass(id: -self.id)
  }
}

struct MyAnchor: MyComponent {
  var id: Int
  
  var inverted: MyAnchor {
    MyAnchor(id: -self.id)
  }
}

extension MyComponent where Self == MyCharacterClass {
  static func anyOf(_ id: Int) -> MyCharacterClass {
    MyCharacterClass(id: id)
  }
}

takesMyComponent(.anyOf(1).inverted)
takesMyComponent(MyAnchor(id: 2).inverted)

I'm able to reproduce @Kyle-Ye's report with both Swift 5.8 and 5.9…