Skip to content

fix handing of the java type T[] #533

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 May 4, 2015 · 2 comments
Closed

fix handing of the java type T[] #533

smarter opened this issue May 4, 2015 · 2 comments
Labels

Comments

@smarter
Copy link
Member

smarter commented May 4, 2015

JA.java:

class JA {
  public static <T> T get(T[] arr) {
    return arr[0];
  }
}

Compat.scala:

object Compat {
  def main(args: Array[String]): Unit = {
    val x = new Array[Int](1)
    x(0) = 10
    println(JA.get(x))
  }
}

This should output an error since T[] is erased to Object[] but it compiles fine, for comparison, scalac will complain with:

Compat.scala:5: error: type mismatch;
 found   : Array[Int]
 required: Array[? with Object]
Note: Int >: ? with Object, but class Array is invariant in type T.
You may wish to investigate a wildcard type such as `_ >: ? with Object`. (SLS 3.2.10)
    println(JA.get(x))
                   ^
one error found
@smarter
Copy link
Member Author

smarter commented May 4, 2015

Note that if you replace JA.get(x) by JA.get[Int](x), the error message of scalac becomes rather funny:

Compat.scala:5: error: type mismatch;
 found   : Array[Int]
 required: Array[Int]
Note: Int >: Int, but class Array is invariant in type T.
You may wish to investigate a wildcard type such as `_ >: Int`. (SLS 3.2.10)
    println(JA.get[Int](x))
                        ^
one error found

@DarkDimius
Copy link
Contributor

Assigned LowPriority as this does not seem to stop us from bootstrap.

odersky added a commit to dotty-staging/dotty that referenced this issue Jan 26, 2018
…parameters

Introduce a new test isAbstractOrParamType to capture this. Sicne we did not do this
before, we missed out on the `Array[T] --> Array[T & Object]` transform for genereic
Java arrays.

The fix also needs an opposite fix to "undo" the parameter encoding again in arrayToRepeated.
odersky added a commit that referenced this issue Jan 30, 2018
Fix #533: Some isAbstractType tests should succeed for parameters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants