Skip to content

-rewrite -indent breaks for-expressions with blocks for further processing via -rewrite -new-syntax #12503

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
nemoo opened this issue May 17, 2021 · 3 comments

Comments

@nemoo
Copy link

nemoo commented May 17, 2021

Compiler version

3.0.0

Minimized code before the rewrite

object RewriteTest {
  def main(args: Array[String]): Unit = {
    for { i <-  List(1,2,3)}
      {
        val a = 2
        println(s"$a")
      }
  }
}

Output after compiling with options -rewrite -indent

object RewriteTest:
  def main(args: Array[String]): Unit =
    for { i <-  List(1,2,3)}
        val a = 2
        println(s"$a")

When I try to rewrite further by using compile -rewrite -new-syntax , the compiler produces this error:

[error] -- [E018] Syntax Error: C:\gitrepo\scala3\src\main\scala\RewriteTest.scala:6:28 
[error] 6 |    for { i <-  List(1,2,3)}
[error]   |                            ^
[error]   |                            expression expected but val found
[error] -- [E006] Not Found Error: C:\gitrepo\scala3\src\main\scala\RewriteTest.scala:8:19 
[error] 8 |        println(s"$a")
[error]   |                   ^
[error]   |                   Not found: a

I am able to successfully call compile -rewrite -new-syntax when I first manually add the 'do' keyword:

  for { i <-  List(1,2,3)} do
        val a = 2
        println(s"$a")

The final compilation produces the expected transformed code:

    for  i <-  List(1,2,3) do
        val a = 2
        println(s"$a")

Expectation

The expectation is that the -rewrite -indent option honors code blocks correctly in conjunction with -rewrite -new-syntax.

@smarter
Copy link
Member

smarter commented May 17, 2021

From https://dotty.epfl.ch/docs/reference/other-new-features/indentation.html:

The -indent option only works on new-style syntax. So to go from old-style syntax to new-style indented code one has to invoke the compiler twice, first with options -rewrite -new-syntax, then again with options -rewrite -indent

So you need to do it in the opposite order.

@smarter smarter closed this as completed May 17, 2021
@nemoo
Copy link
Author

nemoo commented May 17, 2021

ok, then it is a documentation bug. I followed the instructions of the migration guide:
https://scalacenter.github.io/scala-3-migration-guide/docs/tooling/scala-3-syntax-rewrites.html

Instead we have to rewrite syntax one step at a time.

Let's start by moving to SIB syntax. We compile the code with options -indent -rewrite and the result looks as follows:

After this first rewrite, we can have the compiler rewrite the control structures to the new syntax by specifying the -new-syntax -rewrite. This results in the following version:

@smarter
Copy link
Member

smarter commented May 17, 2021

Indeed, please open an issue in https://github.com/scalacenter/scala-3-migration-guide/issues

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

No branches or pull requests

2 participants