🌐 AI搜索 & 代理 主页
Skip to content

Commit 2f211fb

Browse files
committed
test more ObjectTpeJava, support varargs
1 parent 44713c2 commit 2f211fb

File tree

4 files changed

+85
-4
lines changed

4 files changed

+85
-4
lines changed

src/compiler/scala/tools/nsc/tasty/TreeUnpickler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ class TreeUnpickler[Tasty <: TastyUniverse](
11891189
// wrong number of arguments in some scenarios reading F-bounded
11901190
// types. This came up in #137 of collection strawman.
11911191
tpd.AppliedTypeTree(readTpt(), until(end)(readTpt()), isJava)
1192-
case ANNOTATEDtpt => tpd.Annotated(readTpt(), readTerm()(ctx.addMode(ReadAnnotTopLevel)))
1192+
case ANNOTATEDtpt => tpd.Annotated(readTpt(), readTerm()(ctx.addMode(ReadAnnotTopLevel)), isJava)
11931193
case LAMBDAtpt => tpd.LambdaTypeTree(readParams[NoCycle](TYPEPARAM).map(symFromNoCycle), readTpt())
11941194
case MATCHtpt => matchTypeIsUnsupported
11951195
case TYPEBOUNDStpt =>

src/compiler/scala/tools/nsc/tasty/bridge/TreeOps.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,21 @@ trait TreeOps { self: TastyUniverse =>
161161
def AppliedTypeTree(tpt: Tree, args: List[Tree], isJava: Boolean)(implicit ctx: Context): Tree = {
162162
if (tpt.tpe === AndTpe) {
163163
u.CompoundTypeTree(u.Template(args, u.noSelfType, Nil)).setType(u.intersectionType(args.map(_.tpe)))
164-
} else {
164+
}
165+
else if (isJava && u.definitions.isScalaRepeatedParamType(tpt.tpe)) {
166+
u.AppliedTypeTree(tpt, args).setType(u.definitions.javaRepeatedType(args.head.tpe))
167+
}
168+
else {
165169
u.AppliedTypeTree(tpt, args).setType(defn.AppliedType(tpt.tpe, args.map(_.tpe), isJava))
166170
}
167171
}
168172

169-
def Annotated(tpt: Tree, annot: Tree)(implicit ctx: Context): Tree = {
173+
def Annotated(tpt: Tree, annot: Tree, isJava: Boolean)(implicit ctx: Context): Tree = {
170174
if (annot.tpe.typeSymbol === defn.RepeatedAnnot
171175
&& tpt.tpe.typeSymbol.isSubClass(u.definitions.SeqClass)
172176
&& tpt.tpe.typeArgs.length == 1) {
173-
tpd.TypeTree(u.definitions.scalaRepeatedType(tpt.tpe.typeArgs.head))
177+
if (isJava) tpd.TypeTree(u.definitions.javaRepeatedType(tpt.tpe.typeArgs.head))
178+
else tpd.TypeTree(u.definitions.scalaRepeatedType(tpt.tpe.typeArgs.head))
174179
}
175180
else {
176181
u.Annotated(annot, tpt).setType(defn.AnnotatedType(tpt.tpe, annot))
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package tastytest
2+
3+
import lib.ObjectTpeJava
4+
import scala.annotation.nowarn
5+
6+
// keep in sync with ObjectTpeJava.java
7+
object TestObjectTpeJava extends scala.App {
8+
9+
val newOTJ = new ObjectTpeJava
10+
11+
val newOTJInner = new ObjectTpeJava.Inner[Int](23, true)
12+
13+
newOTJ.meth1(1) // OK
14+
newOTJ.meth2(1) // OK
15+
newOTJ.meth3(List[Int](1)) // OK
16+
newOTJ.meth4(List[Int](1)) // OK
17+
newOTJ.meth5(Array[Object]("abc")) // OK
18+
newOTJ.meth6(Array[String]("abc")) // Ok
19+
// newOTJ.meth5(Array[Int](1)) // error: Array[Int] is not a subtype of Array[Object]
20+
// newOTJ.meth6(Array[Int](1)) // error: Array[Int] is not a subtype of Array[T & Object]
21+
newOTJ.meth7(1) // OK (creates a reference array)
22+
newOTJ.meth8(1) // OK (creates a primitive array and copies it into a reference array at Erasure)
23+
val li = Array[Int](1)
24+
newOTJ.meth7(li: _*) // OK (will copy the array at Erasure)
25+
newOTJ.meth8(li: _*) // OK (will copy the array at Erasure)
26+
27+
newOTJInner.meth1(1) // OK
28+
newOTJInner.meth2(1) // OK
29+
30+
// assert((newOTJInner.field1: Int) == 23) // OK
31+
// newOTJInner.field1 = 31 // OK
32+
// assert((newOTJInner.getter1: Int) == 31) // OK
33+
assert((newOTJInner.getter1: Int) == 23) // OK
34+
// assert(newOTJInner.field2 == true) // OK
35+
// newOTJInner.field2 = false // OK
36+
// assert(newOTJInner.getter2 == false) // OK
37+
assert(newOTJInner.getter2 == true) // OK
38+
}
39+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// this test ensures that Object can accept Any from Scala
2+
// see Definitions.ObjectTpeJava
3+
package lib;
4+
5+
public class ObjectTpeJava {
6+
7+
public static class Inner<T> extends Object {
8+
public T field1;
9+
public T getter1() { return field1; }
10+
public Object field2;
11+
public Object getter2() { return field2; }
12+
13+
public Inner(T param1, Object param2) {
14+
this.field1 = param1;
15+
this.field2 = param2;
16+
}
17+
18+
public void meth1(T arg) {}
19+
public <U extends T> void meth2(U arg) {}
20+
}
21+
22+
// 1. At the top level:
23+
public void meth1(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 <T> void meth4(scala.collection.immutable.List<T> arg) {}
29+
30+
// 3. As the type parameter of an array:
31+
public void meth5(Object[] arg) {}
32+
public <T> void meth6(T[] arg) {}
33+
34+
// 4. As the repeated argument of a varargs method:
35+
public void meth7(Object... args) {}
36+
public <T> void meth8(T... args) {}
37+
}

0 commit comments

Comments
 (0)