Skip to content

Commit ec2a0c1

Browse files
committed
test all combinations of java.lang.Object from java
1 parent 3f71bd5 commit ec2a0c1

File tree

12 files changed

+231
-0
lines changed

12 files changed

+231
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// this test ensures that Object can accept Any from Scala
2+
// see Definitions.FromJavaObjectSymbol
3+
package a;
4+
5+
public class A {
6+
7+
public static class Inner<T> extends Object {
8+
public Inner() {}
9+
10+
public void meth1(T arg) {}
11+
public <U extends T> void meth2(U arg) {}
12+
}
13+
14+
public static class Inner_sel<T> extends java.lang.Object {
15+
public Inner_sel() {}
16+
17+
public void meth1(T arg) {}
18+
public <U extends T> void meth2(U arg) {}
19+
}
20+
21+
// 1. At the top level:
22+
public void meth1(Object arg) {}
23+
public void meth1_sel(java.lang.Object arg) {}
24+
public <T> void meth2(T arg) {} // T implicitly extends Object
25+
26+
// 2. In a class type parameter:
27+
public void meth3(scala.collection.immutable.List<Object> arg) {}
28+
public void meth3_sel(scala.collection.immutable.List<java.lang.Object> arg) {}
29+
public <T> void meth4(scala.collection.immutable.List<T> arg) {}
30+
31+
// 3. As the type parameter of an array:
32+
public void meth5(Object[] arg) {}
33+
public void meth5_sel(java.lang.Object[] arg) {}
34+
public <T> void meth6(T[] arg) {}
35+
36+
// 4. As the repeated argument of a varargs method:
37+
public void meth7(Object... args) {}
38+
public void meth7_sel(java.lang.Object... args) {}
39+
public <T> void meth8(T... args) {}
40+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// this test ensures that Object can accept Any from Scala
2+
// see Definitions.FromJavaObjectSymbol
3+
package a;
4+
5+
import java.lang.Object;
6+
7+
// same signatures that reference Object explicitly from A.java, but with the java.lang.Object import
8+
public class AImport {
9+
10+
public static class Inner<T> extends Object {
11+
public Inner() {}
12+
13+
public void meth1(T arg) {}
14+
}
15+
16+
// 1. At the top level:
17+
public void meth1(Object arg) {}
18+
19+
// 2. In a class type parameter:
20+
public void meth3(scala.collection.immutable.List<Object> arg) {}
21+
22+
// 3. As the type parameter of an array:
23+
public void meth5(Object[] arg) {}
24+
25+
// 4. As the repeated argument of a varargs method:
26+
public void meth7(Object... args) {}
27+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// THIS FILE EXISTS SO THAT `A.java` WILL BE COMPILED BY SCALAC
2+
package a

sbt-test/pipelining/Yjava-tasty-fromjavaobject/b-alt/.keep

Whitespace-only changes.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package b
2+
3+
import a.A
4+
5+
// keep in sync with Bexplicit.scala
6+
object B {
7+
8+
val newA = new A
9+
10+
val newAInner = new A.Inner[Int]()
11+
val newAInner_sel = new A.Inner_sel[Int]()
12+
13+
@main
14+
def test = {
15+
newA.meth1(1) // OK
16+
newA.meth1_sel(1) // OK
17+
newA.meth2(1) // OK
18+
newA.meth3(List[Int](1)) // OK
19+
newA.meth3_sel(List[Int](1)) // OK
20+
newA.meth4(List[Int](1)) // OK
21+
newA.meth5(Array[Object]("abc")) // OK
22+
newA.meth5_sel(Array[Object]("abc")) // OK
23+
newA.meth6(Array[String]("abc")) // Ok
24+
// newA.meth5(Array[Int](1)) // error: Array[Int] is not a subtype of Array[Object]
25+
// newA.meth6(Array[Int](1)) // error: Array[Int] is not a subtype of Array[T & Object]
26+
newA.meth7(1) // OK (creates a reference array)
27+
newA.meth7_sel(1) // OK (creates a reference array)
28+
newA.meth8(1) // OK (creates a primitive array and copies it into a reference array at Erasure)
29+
val ai = Array[Int](1)
30+
newA.meth7(ai: _*) // OK (will copy the array at Erasure)
31+
newA.meth7_sel(ai: _*) // OK (will copy the array at Erasure)
32+
newA.meth8(ai: _*) // OK (will copy the array at Erasure)
33+
34+
newAInner.meth1(1) // OK
35+
newAInner.meth2(1) // OK
36+
newAInner_sel.meth1(1) // OK
37+
newAInner_sel.meth2(1) // OK
38+
39+
BImport.testImport() // OK
40+
}
41+
}
42+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package b
2+
3+
import a.AImport
4+
5+
object BImport {
6+
7+
val newA = new AImport
8+
9+
val newAInner = new AImport.Inner[Int]()
10+
11+
def testImport() = {
12+
newA.meth1(1) // OK
13+
newA.meth3(List[Int](1)) // OK
14+
newA.meth5(Array[Object]("abc")) // OK
15+
// newA.meth5(Array[Int](1)) // error: Array[Int] is not a subtype of Array[Object]
16+
newA.meth7(1) // OK (creates a reference array)
17+
val ai = Array[Int](1)
18+
newA.meth7(ai: _*) // OK (will copy the array at Erasure)
19+
20+
newAInner.meth1(1) // OK
21+
}
22+
}
23+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
lazy val a = project.in(file("a"))
2+
.settings(
3+
compileOrder := CompileOrder.Mixed, // ensure we send java sources to Scala compiler
4+
scalacOptions += "-Yjava-tasty", // enable pickling of java signatures
5+
scalacOptions ++= Seq("-Yjava-tasty-output", ((ThisBuild / baseDirectory).value / "a-enum-java-tasty.jar").toString),
6+
scalacOptions += "-Ycheck:all",
7+
Compile / classDirectory := ((ThisBuild / baseDirectory).value / "a-enum-classes"), // send classfiles to a different directory
8+
)
9+
10+
11+
lazy val b = project.in(file("b"))
12+
.settings(
13+
Compile / unmanagedClasspath := Seq(Attributed.blank((ThisBuild / baseDirectory).value / "a-enum-java-tasty.jar")),
14+
scalacOptions += "-Ycheck:all",
15+
)
16+
.settings(
17+
fork := true, // we have to fork the JVM if we actually want to run the code with correct failure semantics
18+
Runtime / unmanagedClasspath += Attributed.blank((ThisBuild / baseDirectory).value / "a-enum-classes"), // make sure the java classes are visible at runtime
19+
)
20+
21+
// same as b, but adds the real classes to the classpath instead of the tasty jar
22+
lazy val bAlt = project.in(file("b-alt"))
23+
.settings(
24+
Compile / sources := (b / Compile / sources).value,
25+
Compile / unmanagedClasspath := Seq(Attributed.blank((ThisBuild / baseDirectory).value / "a-enum-classes")),
26+
scalacOptions += "-Ycheck:all",
27+
)
28+
.settings(
29+
fork := true, // we have to fork the JVM if we actually want to run the code with correct failure semantics
30+
Runtime / unmanagedClasspath += Attributed.blank((ThisBuild / baseDirectory).value / "a-enum-classes"), // make sure the java classes are visible at runtime
31+
)
32+
33+
// negative compilation tests
34+
lazy val c = project.in(file("c"))
35+
.settings(
36+
Compile / unmanagedClasspath := Seq(Attributed.blank((ThisBuild / baseDirectory).value / "a-enum-java-tasty.jar")),
37+
scalacOptions += "-Ycheck:all",
38+
)
39+
40+
// same as c, but adds the real classes to the classpath instead of the tasty jar
41+
lazy val cAlt = project.in(file("c-alt"))
42+
.settings(
43+
Compile / sources := (c / Compile / sources).value,
44+
Compile / unmanagedClasspath := Seq(Attributed.blank((ThisBuild / baseDirectory).value / "a-enum-classes")),
45+
scalacOptions += "-Ycheck:all",
46+
)

sbt-test/pipelining/Yjava-tasty-fromjavaobject/c-alt/.keep

Whitespace-only changes.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package c
2+
3+
import a.A
4+
5+
object C {
6+
7+
val newA = new A
8+
9+
@main
10+
def test = {
11+
newA.meth5(Array[Int](1)) // error: Array[Int] is not a subtype of Array[Object]
12+
newA.meth5_sel(Array[Int](1)) // error: Array[Int] is not a subtype of Array[Object]
13+
newA.meth6(Array[Int](1)) // error: Array[Int] is not a subtype of Array[T & Object]
14+
}
15+
}
16+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package c
2+
3+
import a.AImport
4+
5+
object CImport {
6+
7+
val newA = new AImport
8+
9+
@main
10+
def test = {
11+
newA.meth5(Array[Int](1)) // error: Array[Int] is not a subtype of Array[Object]
12+
}
13+
}
14+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import sbt._
2+
import Keys._
3+
4+
object DottyInjectedPlugin extends AutoPlugin {
5+
override def requires = plugins.JvmPlugin
6+
override def trigger = allRequirements
7+
8+
override val projectSettings = Seq(
9+
scalaVersion := sys.props("plugin.scalaVersion"),
10+
scalacOptions += "-source:3.0-migration"
11+
)
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
> a/compile
2+
# test depending on a java compiled enum through TASTy
3+
> b/run
4+
# double check against the real java classes
5+
> bAlt/run
6+
# check that java Array T is Array T & Object
7+
-> c/compile
8+
# double check against the real java classes
9+
-> cAlt/compile

0 commit comments

Comments
 (0)