Skip to content

Use predictions instead of post-hoc conversions when dealing with : and => #15251

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

Closed
wants to merge 2 commits into from

Conversation

odersky
Copy link
Contributor

@odersky odersky commented May 20, 2022

This PR does some cleanups in the parser base that should make it easier to implement the changed
fewerBraces rules where the lambda is now preceded by :.

The problem is that this creates two ambiguities:

  1. With self types:

     class C { self: T => 
        x = x + 1
     }

    can be alternatively read as a function application of self to a lambda. The first commit solves this
    by defaulting to a self type if the code appears at the start of a template. In the long run, this would not
    be a problem since we want to move away from self types and replace them by a This type declaration.
    But for now we need to fix the ambiguity.

  2. With the way formal parameters of lambdas are parsed. Consider the expression:

    (x: Int => Int) => Int

    Here, we used to parse (x: Int => Int) as an expression (i.e. an ascription with a Typed node) and then converted this to a parameter ValDef when the second => was encountered. Because of this hack, we
    had to allow function types like Int => Int in type ascriptions inside parentheses, but not elsewhere, even though this relaxation is not backed by the official Scala syntax. With fewerBraces the first expression inside (...) would be parsed as an application of the form x(y => z), which would complicate things further.

    The second commit solves this by lookahead scanning when a : ascription inside parentheses is encountered. If the closing parenthesis is followed by => or ?=> we classify the prefix as a list of bindings and parse appropriately. No need to convert type ascriptions to parameters.

The third commit enforces the official Scala grammar, requiring (...) around function types in ascriptions. It is enabled from source version 3.2.

Fixes #15260

@odersky odersky marked this pull request as draft May 20, 2022 14:59
@odersky odersky force-pushed the change-fewerbraces branch from c9361e9 to 5851c62 Compare May 20, 2022 16:39
Instead of parsing all formal parameters of a lambda as expressions
and converting to parameters at the end, we now scan ahead the first time
we see a `:` to determine whether the list is followed by `=>` or `?=>`.
If that's the case we parse this parameter and all following ones as
bindings instead of expressions.
@odersky odersky force-pushed the change-fewerbraces branch 2 times, most recently from ee1f600 to 12938d6 Compare May 21, 2022 17:26
@odersky odersky changed the title Predict self type instead of converting after the fact Use predictions instead of post-hoc conversions when dealing with : and => May 21, 2022
@odersky odersky marked this pull request as ready for review May 21, 2022 17:39
@odersky odersky force-pushed the change-fewerbraces branch 4 times, most recently from 40cc861 to 90c9a27 Compare May 22, 2022 09:19
@odersky odersky force-pushed the change-fewerbraces branch from 90c9a27 to f35df41 Compare May 23, 2022 08:15
@odersky
Copy link
Contributor Author

odersky commented May 23, 2022

The third commit, which changed parser behavior for type ascriptions is now dropped, which means the codebase should compile as-is under this PR.

I'll add the commit instead to the PR that supports inline lambdas following a colon.

@odersky
Copy link
Contributor Author

odersky commented May 31, 2022

This is subsumed by the recently merged fewer braces PR.

@odersky odersky closed this May 31, 2022
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.

Function type ascriptions discrepancy between parser and syntax.
1 participant