Dotty: The inferred AppliedTypes from Java are not checked

Created on 4 Nov 2019  路  3Comments  路  Source: lampepfl/dotty

We could construct AppliedTypes from Java whose arguments do not conform to type bound. When the type is used in Dotty, it is not checked.

minimized code

S.scala

class A

class D[T >: A](v: T) {
  def getV(): T = v // ensure T is correctly inferred
}

object S {
  // J.getDS() : D[String] is inferred but not checked
  val dv: String = J.getDS().getV()
}

J.java

public class J {
  // for java, D is D<T extends Object>
  public static D<String> getDS() {
    return new D<String>("DS");
  }
}

expectation

I expected to see a type error at J.getDS() in S.scala, since String does not conform to lower bound A. However, this test passed. I also test the same code in scalac 2.13, and it also compiled.

typer bug

Most helpful comment

You're right! D<T super A> isn't valid Java. I guess using Scala has rubbed off on my Java knowledge :smile:

All 3 comments

// for java, D is D<T extends Object>

Wait a minute, why isn't it D<T super A>? 馃
I know Java's generics aren't perfect but I would expect this case to work properly.

@TheElectronWill Thanks!

I don't think class D<T super A> is allowed in Java. There is no lower bound allowed in Type Parameters. The lower bound can only be used for widecard in Type Arguments of Parameterized Types, for example: Reference(T referent, ReferenceQueue<? super T> queue).

See: 9.1.2. Generic Interfaces and Type Parameters and 4.5.1. Type Arguments of Parameterized Types .

There are many types in Dotty which cannot be translated in Java, the lower bound is just one case. Another example is OrType: D[T <: A | B].

You're right! D<T super A> isn't valid Java. I guess using Scala has rubbed off on my Java knowledge :smile:

Was this page helpful?
0 / 5 - 0 ratings