Skip to content

PackratParsers#phrase: explicit return type (fixes #369) #370

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

Merged
merged 2 commits into from
Apr 12, 2021
Merged

PackratParsers#phrase: explicit return type (fixes #369) #370

merged 2 commits into from
Apr 12, 2021

Conversation

ilya-klyuchnikov
Copy link
Contributor

@ilya-klyuchnikov ilya-klyuchnikov commented Apr 8, 2021

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 type def 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 the memo method.


Providing explicit return type fixes #369 subtlety.

  • A corresponding test case was added.

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.

@Philippus
Copy link
Member

Nice find, @SethTisue is this something that may be of interest to the scala 3 team?

@Philippus Philippus requested a review from SethTisue April 9, 2021 11:35
@SethTisue
Copy link
Member

the Scala 2 behavior is usually considered undesirable here, and I believe the Scala 3 behavior is intentional: scala/bug#7212

I've opened a ticket suggesting the Scala 3 migration guide mention this: scalacenter/scala-3-migration-guide#181

@SethTisue SethTisue merged commit 7f86709 into scala:1.2.x Apr 12, 2021
@SethTisue
Copy link
Member

thank you Ilya!

@SethTisue
Copy link
Member

Scala 3.0.0-RC3 is around the corner, so we'll publish the fix then.

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

Successfully merging this pull request may close these issues.

PackratParsers#phrase - different behaviour in Scala 2 vs Scala 3
3 participants