Compiler becomes unusable after first compile with specific syntactically incorrect code

I’m trying to upgrade my app called Kojo (GitHub - litan/kojo: The Kojo Learning Environment) from Scala 2.13.6 to 2.13.16, and am running into this problem.
The problem is that – if the first piece of code that is given to the compiler to compile is something like the following:

class Hello3 { def pic = 20 def makePic(s: Int = { pic } } 

Then the compiler gets messed up and is unable to do any further compilations (it seems to lose track of predefined symbol names).

Here’s my attempt to reproduce the problem with a minimal example:

I bealive the Global instance should not be shared between the compiler invocations. The gist example works correctly if we create a fresh Global instance inside compileCode function.

1 Like

Hmm.
Some additional thoughts/inputs:

  • A shared Global instance has worked fine for me for many years with Scala 2.13.6 (and earlier Scala versions before that). I am seeing this problem here with Scala 2.13.16
  • If I do a “good” compile before the “deadly” compile, then everything is good, and the version 2.13.16 compiler continues to chug along happily. It’s only when the “deadly” compile is the first compile that the compiler gets hosed.
  • The idea with using the same/shared Global instance has been to avoid re-creating an object that is costly to create (if the sharing/reuse is permitted, of course; and it seems to have been, back in the day).

Is there some documentation somewhere on the recommended way to use the compiler programatically?

Hi Lalit, this is a bug indeed, you can create a ticket.

Compilation usually doesn’t re-use a Global instance, e.g., sbt will always create a new one. But we still support this in principle, it’s used heavily in the presentation compiler (metals).

We also re-use a global instance in our JUnit tests, everything extending BytecodeTesting (which is not only bytecode tests these days).

2 Likes

Hi Lukas, sounds good, I will go ahead and create a ticket as suggested.
And thanks for the info related to the usage of Global.

ticket: Compiler becomes unusable after first compile with specific syntactically incorrect code · Issue #13115 · scala/bug · GitHub
fix: Load package objects when failing early by som-snytt · Pull Request #11103 · scala/scala · GitHub

1 Like