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

Leaving off Array ClassTag causes compiler stack overflow #6685

Closed
scabug opened this issue Nov 18, 2012 · 11 comments
Closed

Leaving off Array ClassTag causes compiler stack overflow #6685

scabug opened this issue Nov 18, 2012 · 11 comments

Comments

@scabug
Copy link

scabug commented Nov 18, 2012

The compiler runs into an error when a parameterized array is used without a ClassTag.
Adding the ClassTag fixes the problem.

@scabug
Copy link
Author

scabug commented Nov 18, 2012

Imported From: https://issues.scala-lang.org/browse/SI-6685?orig=1
Reporter: Rows Dower (rowsdower)
Affected Versions: 2.10.0-RC2
Other Milestones: 2.10.0
Attachments:

@scabug
Copy link
Author

scabug commented Nov 18, 2012

@xeno-by said (edited on Nov 18, 2012 6:12:37 PM UTC):
Sorry, can't reproduce:

scala> class ArrayUser[A /*: ClassTag*/](initSize:Int) {
     |   var it: Array[A] = _
     |   initIt(initSize)
     |   def initIt(initSize:Int) { it = new Array[A](initSize) }
     | }
<console>:29: error: cannot find class tag for element type A
         def initIt(initSize:Int) { it = new Array[A](initSize) }
                                         ^

Could you please try this in REPL?

@scabug
Copy link
Author

scabug commented Nov 18, 2012

Rows Dower (rowsdower) said:
It seems this is only a problem when running from SBT. Reinstalling SBT doesn't help.

@scabug
Copy link
Author

scabug commented Nov 18, 2012

@xeno-by said:
Have you tried removing ~/.sbt, ~/.ivy2 and ~/.m2?

@scabug
Copy link
Author

scabug commented Nov 18, 2012

Rows Dower (rowsdower) said (edited on Nov 18, 2012 9:44:38 PM UTC):
That didn't fix the problem, but it did produce a different, more detailed error message.

@scabug
Copy link
Author

scabug commented Nov 18, 2012

@retronym said (edited on Nov 18, 2012 10:08:08 PM UTC):
Tip: in the SBT shell, run last:compile to show the full command line (including classpath) and output of the previous scalac invocation and include this with the bug report.

@scabug
Copy link
Author

scabug commented Nov 18, 2012

Rows Dower (rowsdower) said:
Okay, here it is.

@scabug
Copy link
Author

scabug commented Nov 18, 2012

@harrah said:
This is straightforward to reproduce by putting the compiler in a class loader below the application class loader.

  1. Compile C.scala below against Scala 2.9.2. (The different version helps to differentiate the classpaths, but I haven't tested whether it is absolutely necessary.)
  2. Run with Scala 2.9.2 on the application classpath, provide a classpath with Scala 2.10 as the first argument, and provide the source in this bug report as the second argument. For example, assuming C.scala was compiled to the classes directory:
java -classpath classes:2.9.2/scala-compiler-2.9.2.jar:2.9.2/scala-library-2.9.2.jar C 2.10/scala-compiler-2.10.0-RC2.jar:2.10/scala-library-2.10.0-RC2.jar:2.10/scala-reflect-2.10.0-RC2.jar ArrayUser.scala
// C.scala
import java.net.{URL, URLClassLoader}
import java.io.File

object C {
   final val OutputDirectory = "out"
   final val MainClass = "scala.tools.nsc.Main"
   final val MainMethod = "main"

   def main(args: Array[String]) {
      val scalaClasspath = args(0)
      val sources = args.drop(1)

      val scp = toClasspathArray(scalaClasspath)
      mkdir(OutputDirectory)

      val cl = new URLClassLoader(scp, null)
      val module = getModule(MainClass, cl)
      val method = module.getClass.getMethod(MainMethod, classOf[Array[String]])
      method.invoke(module, scalacArgs(scalaClasspath, sources.toList).toArray)
   }

   def scalacArgs(cp: String, sources: List[String]): List[String] =
      "-cp" :: cp :: "-d" :: OutputDirectory :: sources

   def mkdir(name: String): Unit = (new File(name)).mkdir()

   def toClasspathArray(cp: String): Array[URL] = 
      cp.split(File.pathSeparator).map(s => (new File(s)).toURL).toArray

   def getModule(className: String, loader: ClassLoader): Any =  {
      val obj = Class.forName(className + "$", true, loader)
      val singletonField = obj.getField("MODULE$")
      singletonField.get(null)
   }
}
// ArrayUser.scala
import scala.reflect.ClassTag

class ArrayUser[A /*: ClassTag*/](initSize:Int) {
  var it: Array[A] = _

  initIt(initSize)

  def initIt(initSize:Int) { it = new Array[A](initSize) }
}

The resulting stack trace looks like:

Caused by: java.lang.StackOverflowError
	at java.lang.ClassLoader.findBootstrapClass(Native Method)
	at java.lang.ClassLoader.findBootstrapClass0(ClassLoader.java:900)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:316)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
	at scala.tools.nsc.typechecker.ContextErrors$TyperContextErrors$TyperErrorGen$.liftedTree1$1(ContextErrors.scala:729)
	at scala.tools.nsc.typechecker.ContextErrors$TyperContextErrors$TyperErrorGen$.MacroGeneratedException(ContextErrors.scala:711)
	at scala.tools.nsc.typechecker.Macros$class.liftedTree1$1(Macros.scala:791)
	at scala.tools.nsc.typechecker.Macros$class.scala$tools$nsc$typechecker$Macros$$macroExpandWithRuntime(Macros.scala:768)
	at scala.tools.nsc.typechecker.Macros$$anonfun$scala$tools$nsc$typechecker$Macros$$macroExpand1$1.apply(Macros.scala:741)
	at scala.tools.nsc.typechecker.Macros$$anonfun$scala$tools$nsc$typechecker$Macros$$macroExpand1$1.apply(Macros.scala:732)
	at scala.tools.nsc.Global.withInfoLevel(Global.scala:189)
	at scala.tools.nsc.typechecker.Macros$class.scala$tools$nsc$typechecker$Macros$$macroExpand1(Macros.scala:732)
	at scala.tools.nsc.typechecker.Macros$class.macroExpand(Macros.scala:697)
	at scala.tools.nsc.Global$$anon$1.macroExpand(Global.scala:490)
	at scala.tools.nsc.typechecker.Typers$Typer.adapt(Typers.scala:1117)
	at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5467)
	at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5509)
	at scala.tools.nsc.typechecker.Typers$Typer.typedPos(Typers.scala:5514)
	at scala.tools.nsc.typechecker.Implicits$ImplicitSearch.success$1(Implicits.scala:1175)
	at scala.tools.nsc.typechecker.Implicits$ImplicitSearch.tagOfType(Implicits.scala:1210)
	at scala.tools.nsc.typechecker.Implicits$ImplicitSearch.materializeImplicit(Implicits.scala:1347)
	at scala.tools.nsc.typechecker.Implicits$ImplicitSearch.bestImplicit(Implicits.scala:1391)
	at scala.tools.nsc.typechecker.Implicits$class.inferImplicit(Implicits.scala:82)
	at scala.tools.nsc.Global$$anon$1.inferImplicit(Global.scala:490)
	at scala.tools.nsc.typechecker.Tags$Tag$$anonfun$resolveTag$1$$anonfun$apply$1.apply(Tags.scala:15)
	at scala.tools.nsc.typechecker.Tags$Tag$$anonfun$resolveTag$1$$anonfun$apply$1.apply(Tags.scala:23)
	at scala.tools.nsc.typechecker.Contexts$Context.withMacrosEnabled(Contexts.scala:233)
	at scala.tools.nsc.typechecker.Tags$Tag$$anonfun$resolveTag$1.wrapper$1(Tags.scala:14)
	at scala.tools.nsc.typechecker.Tags$Tag$$anonfun$resolveTag$1.apply(Tags.scala:15)
	at scala.tools.nsc.typechecker.Tags$Tag$$anonfun$resolveTag$1.apply(Tags.scala:13)
	at scala.reflect.internal.SymbolTable.atPhase(SymbolTable.scala:201)
	at scala.reflect.internal.SymbolTable.beforePhase(SymbolTable.scala:209)
	at scala.tools.nsc.typechecker.Tags$Tag$class.resolveTag(Tags.scala:13)
	at scala.tools.nsc.typechecker.Tags$Tag$class.resolveClassTag(Tags.scala:43)
	at scala.tools.nsc.typechecker.Typers$Typer.resolveClassTag(Typers.scala:96)
	at scala.tools.nsc.typechecker.Typers$Typer.typedApply$1(Typers.scala:4520)
	at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5381)
	at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5458)
	at scala.tools.nsc.typechecker.Typers$Typer.typedApply$1(Typers.scala:4524)
	at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5381)
	at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5458)
	at scala.tools.nsc.typechecker.Typers$Typer.typedApply$1(Typers.scala:4524)

@scabug
Copy link
Author

scabug commented Nov 29, 2012

@paulp said:
It's times like these when I pat myself on the back for all those years I spent at the university.

scala/scala#1684

@scabug
Copy link
Author

scabug commented Nov 30, 2012

@xeno-by said:
Reproduced the bug

@scabug
Copy link
Author

scabug commented Nov 30, 2012

@xeno-by said:
Fixed in scala/scala#1685

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