Skip to content

Unfriendly error: Unsafe instantation of type => List[(java.util.List[_] @UnsafeNonvariant)#java$util$List$$E] #2112

@smarter

Description

@smarter

While trying to compile dottydoc I ran across this error, here's a minimization:

import scala.collection.JavaConverters._

object Test {
  def test(x: Any): Unit = {
    x.asInstanceOf[java.util.List[_]].asScala.toList
  }
}
-- Error: jlist.scala ------------------------------------------------------
5 |    x.asInstanceOf[java.util.List[_]].asScala.toList
  |    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |    unsafe instantiation of type => 
  |      scala.collection.immutable.List[
  |        java.util.List'[_] @UnsafeNonvariant#java$util$List$$E
  |      ]
  |    (?1.toList)where:    List  is a class in package immutable
  |              List' is a trait in package util

The error disappear if the code is rewritten as:

import scala.collection.JavaConverters._

object Test {
  def test(x: Any): Unit = {
    val y = x.asInstanceOf[java.util.List[_]].asScala
    y.toList
  }
}

@odersky Could you explain why this happens exactly? Is this something that we can fix? Otherwise, we should try to give a better error message to the user, this one does not help you fix the problem. Ideally, the compiler would tell you to do the rewriting above.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions