Skip to content

Class cast exception when creating a new 2.13-defined class accepting a by-name parameter #16351

Closed
@alexarchambault

Description

@alexarchambault

Compiler version

3.2.1 (3.2.0 also suffers from this, but not 3.1.3)

Minimized code

(Edit: fully standalone reproduction in comment below)

Can be reproduced with Scala CLI. In Test.scala:

//> using lib "com.lihaoyi:ammonite-cross-3.2.1-interp_2.13.10:2.5.5-17-df243e14"
//> using lib "com.lihaoyi:ammonite-compiler_3.2.1:2.5.5-17-df243e14"
//> using scala "3.2.1"

import ammonite.compiler.*
import ammonite.interp.*
import ammonite.runtime.*
import ammonite.util.*

object Test {
  def main(args: Array[String]): Unit = {
    val frame = Frame.createInitial()
    new Interpreter(
      CompilerBuilder,
      Parsers, // this is the by-name parameter that triggers the class cast exception
      Printer(System.out, System.err, System.out, _ => (), _ => (), _ => ()),
      Storage.InMemory(),
      os.pwd,
      Ref(Colors.Default),
      getFrame = () => frame,
      createFrame = () => frame,
      replCodeWrapper = DefaultCodeWrapper,
      scriptCodeWrapper = DefaultCodeWrapper,
      alreadyLoadedDependencies = Nil
    ) {} // '{}' required to get the crash
  }
}

I've not been able to reproduce that without using those Ammonite classes yet.

Output

Running it:

$ scala-cli Test.scala
Exception in thread "main" java.lang.ClassCastException: class ammonite.compiler.Parsers$ cannot be cast to class scala.Function0 (ammonite.compiler.Parsers$ and scala.Function0 are in unnamed module of loader 'app')
	at Test$$anon$1.<init>(Test.scala:16)
	at Test$.main(Test.scala:25)
	at Test.main(Test.scala)

Expectation

No crash at runtime

Note that running this with Scala 3.1.3 requires slightly different dependencies. The header needs to be changed to

//> using lib "com.lihaoyi:ammonite-cross-3.1.3-interp_2.13.7:2.5.5-17-df243e14"
//> using lib "com.lihaoyi:ammonite-compiler_3.1.3:2.5.5-17-df243e14"
//> using scala "3.1.3"

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions