PackratParsers#phrase: explicit return type (fixes #369) #370
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This fixes #369.
TL;DR A missed result type for
PackratParsers#phrase
results in a different behaviour (Scala 2.x. vs Scala 3.x) for edgy cases.Long explanation:
It seems (I was not able to quickly find a corresponding reference) that in Scala 3.0 if an overriding method doesn't explicitly specify the result type, the result type of the base method is taken (even if the compiler can figure out a more concrete type).
This leads to a corner case with PackratParsers#phrase, which in 2.x has the effective/inferred type
def phrase[T](p: Parser[T]): PackratParser[T]
and in 3.x has the effective/inferred typedef phrase[T](p: Parser[T]): Parser[T]
.This results into an extra implicit conversion
parser2packrat
being inserted even if the current parser is already a Packrat Parser. This, in turn, breaks assumptions of thememo
method.Providing explicit return type fixes #369 subtlety.
The added test is OK for Scala 2.x, but fails for Scala 3.x - as can be seen from this run - https://github.com/ilya-klyuchnikov/scala-parser-combinators/commit/92a338ea19567614d30b1f6f1b8b7953849779a5.
Correspondingly, after the fix, the test is OK for both Scala 2.x and Scala 3.x
I have been bitten by this subtlety - while migrating https://github.com/ilya-klyuchnikov/tapl-scala to Scala 3.