Skip to content

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

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

Closed
smarter opened this issue Mar 16, 2017 · 0 comments

Comments

@smarter
Copy link
Member

smarter commented Mar 16, 2017

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.

smarter added a commit to dotty-staging/dotty that referenced this issue Mar 16, 2017
smarter added a commit to dotty-staging/dotty that referenced this issue Mar 20, 2017
felixmulder pushed a commit that referenced this issue Mar 20, 2017
@odersky odersky self-assigned this Jul 18, 2017
allanrenucci added a commit that referenced this issue Dec 29, 2017
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