Scala compiler crash (?)


#1

Hello, I am unsure of where to post this so I figured I go straight to the source :wink:

Scala 2.12.8 and previous all produce same behavior. Here is the code:

object problem1 {

    def main(args:Array[String]) = {
		val l1 = List(("a",(1,2)),("a", (2,3)), ("a",(3,4)), ("b", (17,18)), ("b", (18,19)), ("c", (20,21)), ("d",(0,0)))
		val m1 = l1.
    foldLeft(Map().empty.asInstanceOf[String,List[(Int,Int)]])((y,x) => { if (!y.contains(x._1)) y ++ Map(x._1->List(x._2)) else y ++ Map(x._1 -> (y(x._1):+x._2))})

		println("m1=",m1)
	}
}

Here is the build.sbt:
scalaVersion := “2.12.8”

If I run “sbt compile”, the compiler throws up with a bunch of errors such as:
[error] (Compile / compileIncremental) java.lang.AssertionError: assertion failed:
[error] Context(testbed.scala) {
[error] owner = value m1
[error] tree = Apply:l1.foldLeft(Map().empty.asInstanceOf[String, List[scala.Tuple2[Int, In
[error] scope = 3 decls
[error] contextMode = MacrosEnabled TypeConstructorAllowed
[error] outer.owner = value m1
[error] }
[error] while compiling: /usr/home/maketo/dev/scala/testbed/problem1/src/main/scala/testbed.scala
[error] during phase: globalPhase=typer, enteringPhase=namer
[error] library version: version 2.12.8
[error] compiler version: version 2.12.8
[error] reconstructed args: -bootclasspath /usr/local/openjdk8/jre/lib/resources.jar:/usr/local/openjdk8/jre/lib/rt.jar:/usr/local/openjdk8/jre/lib/sunrsasign.jar:/usr/local/openjdk8/jre/lib/jsse.jar:/usr/local/openjdk8/jre/lib/jce.jar:/usr/local/openjdk8/jre/lib/charsets.jar:/usr/local/openjdk8/jre/lib/jfr.jar:/usr/local/openjdk8/jre/classes:/home/maketo/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.12.8.jar -classpath /usr/home/maketo/dev/scala/testbed/problem1/target/scala-2.12/classes
[error]
[error] last tree to typer: Ident(scala)
[error] tree position: line 6 of /usr/home/maketo/dev/scala/testbed/problem1/src/main/scala/testbed.scala
[error] tree tpe: scala.type
[error] symbol: final package scala
[error] symbol definition: final package scala (a ModuleSymbol)
[error] symbol package:
[error] symbol owners: package scala
[error] call site: method main in object problem1 in package
[error]
[error] == Source file context for tree position ==
[error]
[error] 3 def main(args:Array[String]) = {
[error] 4 val l1 = List((“a”,(1,2)),(“a”, (2,3)), (“a”,(3,4)), (“b”, (17,18)), (“b”, (18,19)), (“c”, (20,21)), (“d”,(0,0)))
[error] 5 val m1 = l1.
[error] 6 foldLeft(Map().empty.asInstanceOf[String,List[(Int,Int)]])((y,x) => { if (!y.contains(x._1)) y ++ Map(x._1->List(x._2)) else y ++ Map(x._1 -> (y(x._1):+x._2))})
[error] 7
[error] 8 println(“m1=”,m1)
[error] 9 }
[error] Total time: 4 s, completed Jan 3, 2019 11:45:30 AM

I should mention that the code runs happily in the scala REPL:
scala> val l1 = List((“a”,(1,2)),(“a”, (2,3)), (“a”,(3,4)), (“b”, (17,18)), (“b”, (18,19)), (“c”, (20,21)), (“d”,(0,0)))
l1: List[(String, (Int, Int))] = List((a,(1,2)), (a,(2,3)), (a,(3,4)), (b,(17,18)), (b,(18,19)), (c,(20,21)), (d,(0,0)))

scala> l1.foldLeft(Map().empty.asInstanceOf[Map[String,List[(Int,Int)]]])((y,x) => if (!y.contains(x._1)) y ++ Map(x._1->List(x._2)) else y ++ Map(x._1 -> (y(x._1):+x._2)))
res90: Map[String,List[(Int, Int)]] = Map(a -> List((1,2), (2,3), (3,4)), b -> List((17,18), (18,19)), c -> List((20,21)), d -> List((0,0)))

#2

Missing the surrounding Map[...] here :smile:

Also, I think you want Map.empty[String, List[(Int, Int)]] instead.

Edit: It still shouldn’t be a compiler crash, though… I’d expect wrong number of type arguments for asInstanceOf, should be 1. So it’s still an issue.


#3

Yes, thanks for taking the time to reply! :slight_smile:


#4

Compiler crashes should be reported on https://github.com/scala/bug/issues, not here.


#5

Sorry, this snippet from https://www.scala-lang.org/contribute/bug-reporting-guide.html made it sound it was OK to report here:

“In general, if you find yourself stuck on any of these steps, asking on Scala Contributors can be helpful:”

Thanks!


#6

any compiler crash does merit a ticket on https://github.com/scala/bug, but:

  • there’s a lot of open compiler crash tickets, you shouldn’t expect the likelihood of any particular one getting attention to be real high, especially if the crash is (like this one) on incorrect code
  • please do a search first to see whether’s a duplicate, there isn’t an army of operators standing by to do the searching for you :slight_smile:
  • please minimize the triggering code, the report isn’t useful unless minimized

in this case, I think Jeremy’s Map().asInstanceOf[String,List[(Int,Int)]] is minimal, at least, I was unable to minimize it any further.

Dotty does the right thing:

scala> Map().asInstanceOf[String,List[(Int,Int)]]                                                                      
1 |Map().asInstanceOf[String,List[(Int,Int)]]
  |^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |Too many type arguments for scala.Any.asInstanceOf[X0]
  |expected: [X0]
  |actual:   [String, List[Tuple2[Int, Int]]]