Skip to content

Incorrect code causes the parser to crash when trying to rewrite #22906

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
guofengzh opened this issue Apr 2, 2025 · 5 comments · May be fixed by #22918
Open

Incorrect code causes the parser to crash when trying to rewrite #22906

guofengzh opened this issue Apr 2, 2025 · 5 comments · May be fixed by #22918

Comments

@guofengzh
Copy link

guofengzh commented Apr 2, 2025

Compiler version

Scala 3.6.4/3.7.0-RC1

Minimized code

//> using options -rewrite -indent

def program: Int => Int =
    {`1`: Int  =>  5}

Output (click arrow to expand)

[error] java.lang.ArrayIndexOutOfBoundsException: Index -3 out of bounds for length 22
[error] dotty.tools.dotc.parsing.Parsers$Parser.testChar(Parsers.scala:740)
[error] dotty.tools.dotc.parsing.Parsers$Parser.testChars(Parsers.scala:745)
[error] dotty.tools.dotc.parsing.Parsers$Parser.bracesToIndented(Parsers.scala:863)
[error] dotty.tools.dotc.parsing.Parsers$Parser.inBracesOrIndented(Parsers.scala:648)
[error] dotty.tools.dotc.parsing.Parsers$Parser.inDefScopeBraces(Parsers.scala:652)
......
@guofengzh guofengzh added itype:bug itype:crash stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 2, 2025
@Gedochao
Copy link
Contributor

Gedochao commented Apr 3, 2025

On Scala 3.3.5/3.6.4/3.7.0-RC1, I'm getting:

-- Error: /Users/pchabelski/IdeaProjects/scala-cli-tests-3/scala-cli-tests-3/compiler-repro/repro.scala:1:37 
1 |def program: Int => Int = {`1`: Int  =>  5}
  |                                     ^
  |parentheses are required around the parameter of a lambda
  |This construct can be rewritten automatically under -rewrite -source 3.0-migration.
1 error found

when the parenthesis gets rewritten:

def program: Int => Int = {(`1`: Int)  =>  5}

the code seems to compile with no issue.

@guofengzh am I missing something? I can't seem to reproduce the parser crash.

@Gedochao Gedochao added stat:cannot reproduce area:parser and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 3, 2025
@som-snytt
Copy link
Contributor

som-snytt commented Apr 3, 2025

Ah looking up the stack

if in.rewriteToIndent then bracesToIndented(body, rewriteWithColon)

or

compiler/src/dotty/tools/dotc/parsing/Scanners.scala:    val rewriteToIndent = ctx.settings.indent.value && rewrite

so to reproduce

//> using options -rewrite -indent

scala-cli directives are magical but only if we use them.

You never know if trivial text changes will fool rewriting, since rewrite can be tricky.

//> using options -rewrite -indent

def program: Int => Int =
    {`1`: Int  =>  5}

@Gedochao
Copy link
Contributor

Gedochao commented Apr 3, 2025

@som-snytt thanks for nailing down the reproduction.
@guofengzh I adjusted the snippet in the original post.

@Gedochao Gedochao changed the title Incorrect code causes the compiler to crash Incorrect code causes the parser to crash when trying to rewrite Apr 3, 2025
@som-snytt
Copy link
Contributor

It reproduces as

scala-cli compile --server=false -S 3.7.0-RC1 tests/rewrites/i22906.scala

but not

sbt:scala3> testCompilation i22906

[info] Test dotty.tools.dotc.CompilationTests.rewrites started
[                                        ] completed (0/1, 0 failed, 0s)-- Error: out/rewrites/i22906/rewrites/i22906/i22906.scala:4:15 --------------------------------------------------------
4 |    {`1`: Int  =>  5}
  |               ^
  |               parentheses are required around the parameter of a lambda
  |               This construct can be rewritten automatically under -rewrite -source 3.0-migration.
[                                        ] completed (0/1, 0 failed, 1s)Compilation failed for: 'out/rewrites/i22906/rewrites/i22906/i22906.scala'
[=======================================>] completed (1/1, 1 failed, 1s)
[error] Test dotty.tools.dotc.CompilationTests.rewrites failed: java.lang.AssertionError: Rewrite test failed, took 1.341 sec
[error]     at dotty.tools.vulpix.ParallelTesting$CompilationTest.checkFail(ParallelTesting.scala:1283)
[error]     at dotty.tools.vulpix.ParallelTesting$CompilationTest.checkRewrites(ParallelTesting.scala:1260)
[error]     at dotty.tools.dotc.CompilationTests.rewrites(CompilationTests.scala:93)
[error]     at jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
[error]     at java.lang.reflect.Method.invoke(Method.java:580)
[error]     ...

I don't know if vulpix is folding in other options or if it progressed.

It also fails with whatever I published locally.

scala-cli compile --server=false -S 3.7.1-RC1-bin-SNAPSHOT tests/rewrites/i22906.scala

I forgot to try nightly. OK yes, fails on 3.nightly.

@som-snytt som-snytt self-assigned this Apr 5, 2025
@som-snytt som-snytt linked a pull request Apr 5, 2025 that will close this issue
@guofengzh
Copy link
Author

Thanks for your kind response!

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

Successfully merging a pull request may close this issue.

3 participants