Skip to content
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

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

Closed
alexarchambault opened this issue Nov 16, 2022 · 4 comments · Fixed by #16354
Labels
area:transform itype:bug regression This worked in a previous version but doesn't anymore

Comments

@alexarchambault
Copy link
Contributor

alexarchambault commented Nov 16, 2022

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"
@alexarchambault alexarchambault added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Nov 16, 2022
@alexarchambault
Copy link
Contributor Author

alexarchambault commented Nov 16, 2022

Bisecting using nightlies, it seems the regression happened between 3.2.0-RC1-bin-20220415-8037f3b-NIGHTLY (no runtime crash) and 3.2.0-RC1-bin-20220416-53f5531-NIGHTLY (runtime crash).

@alexarchambault
Copy link
Contributor Author

Standalone reproduction:

scala-2-lib/Lib.scala:

//> using scala "2.13.10"
//> using publish.organization "local.debug"
//> using publish.name "scala-2-lib"
//> using publish.version "0.1.0"

package lib

class Value(val value: String)

class Lib(
  value: => Value,
  a: Int = 0,
  b: Int
)

scala-3-app/App.scala:

//> using scala "3.2.1"
//> using lib "local.debug:scala-2-lib_2.13:0.1.0"

package app

import lib.*

object App {
  def main(args: Array[String]): Unit =
    new Lib(Value("Foo"), b = 2) {}
}

Publish the Scala 2 library locally:

$ scala-cli publish local scala-2-lib

Run the Scala 3 app:

$ scala-cli scala-3-app
Compiling project (Scala 3.2.1, JVM)
Compiled project (Scala 3.2.1, JVM)
Exception in thread "main" java.lang.ClassCastException: class lib.Value cannot be cast to class scala.Function0 (lib.Value and scala.Function0 are in unnamed module of loader 'app')
	at app.App$$anon$1.<init>(App.scala:10)
	at app.App$.main(App.scala:10)
	at app.App.main(App.scala)

@bishabosha
Copy link
Member

could be #14919?

@jchyb jchyb added area:transform regression This worked in a previous version but doesn't anymore and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Nov 16, 2022
@odersky
Copy link
Contributor

odersky commented Nov 16, 2022

I tried this standalone without a publishing step (i.e. compile Lib, compile App) and it ran OK. So not sure what goes on here.

EDIT: Ah, sorry, I see now that lib needs to be compiled with Scala 2.13. Now I can repoduce.

odersky added a commit to dotty-staging/dotty that referenced this issue Nov 16, 2022
This needs follow up but I don't have the time.

 - figure out why we crash and what is the right fix
 - fix the test to compile lib with 2.13

Fixes scala#16531  (provisionally)
bishabosha added a commit that referenced this issue Nov 17, 2022
This needs follow up but I don't have the time.

 - figure out why we crash and what is the right fix
 - fix the test to compile lib with 2.13

Fixes #16351  (provisionally)
little-inferno pushed a commit to little-inferno/dotty that referenced this issue Jan 25, 2023
This needs follow up but I don't have the time.

 - figure out why we crash and what is the right fix
 - fix the test to compile lib with 2.13

Fixes scala#16531  (provisionally)
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
area:transform itype:bug regression This worked in a previous version but doesn't anymore
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants