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

Using ParserPrinter+map in rawValue cause infinite loop #302

Open
seongkue opened this issue Jun 1, 2023 · 0 comments
Open

Using ParserPrinter+map in rawValue cause infinite loop #302

seongkue opened this issue Jun 1, 2023 · 0 comments

Comments

@seongkue
Copy link

seongkue commented Jun 1, 2023

When enum type conform RawRepresentable, Swift use 'var rawValue' when compute equal operator( == )

enum SampleEnum: Equatable {
case a
case b
}

extension SampleEnum: RawRepresentable {
typealias RawValue = String
init?(rawValue: String) {
switch rawValue {
case "a": self = .a
case "b": self = .b
default: return nil
}
}
var rawValue: String {
print("rawValue called")
switch self {
case .a: return "a"
case .b: return "b"
}
}
}

let sampleA: SampleEnum = .a
let sampleB: SampleEnum = .b

print(sampleA == sampleB) // print "rawValue called"

So use ParserPrinter using "a".map { SampleEnum.a } in var rawValue { } cause infinite loop., because it called the method below.
extension Parsers.MapConstant: ParserPrinter where Upstream: ParserPrinter, Output: Equatable { @inlinable public func print(_ output: Output, into input: inout Upstream.Input) throws { guard output == self.output else { throw PrintingError.failed( summary: """ expected \(self.output) """, input: input ) } try self.upstream.print((), into: &input) } }

guard output == self.output --> call rawValue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant