Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2f211fb

Browse files
committed
test more ObjectTpeJava, support varargs
1 parent44713c2 commit2f211fb

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-
caseANNOTATEDtpt=> tpd.Annotated(readTpt(), readTerm()(ctx.addMode(ReadAnnotTopLevel)))
1192+
caseANNOTATEDtpt=> tpd.Annotated(readTpt(), readTerm()(ctx.addMode(ReadAnnotTopLevel)), isJava)
11931193
caseLAMBDAtpt=> tpd.LambdaTypeTree(readParams[NoCycle](TYPEPARAM).map(symFromNoCycle), readTpt())
11941194
caseMATCHtpt=> matchTypeIsUnsupported
11951195
caseTYPEBOUNDStpt=>

‎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
defAppliedTypeTree(tpt:Tree,args:List[Tree],isJava:Boolean)(implicitctx: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+
elseif (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-
defAnnotated(tpt:Tree,annot:Tree)(implicitctx:Context):Tree= {
173+
defAnnotated(tpt:Tree,annot:Tree,isJava:Boolean)(implicitctx: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+
packagetastytest
2+
3+
importlib.ObjectTpeJava
4+
importscala.annotation.nowarn
5+
6+
// keep in sync with ObjectTpeJava.java
7+
objectTestObjectTpeJavaextends scala.App {
8+
9+
valnewOTJ=newObjectTpeJava
10+
11+
valnewOTJInner=newObjectTpeJava.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+
valli=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+
packagelib;
4+
5+
publicclassObjectTpeJava {
6+
7+
publicstaticclassInner<T>extendsObject {
8+
publicTfield1;
9+
publicTgetter1() {returnfield1; }
10+
publicObjectfield2;
11+
publicObjectgetter2() {returnfield2; }
12+
13+
publicInner(Tparam1,Objectparam2) {
14+
this.field1 =param1;
15+
this.field2 =param2;
16+
}
17+
18+
publicvoidmeth1(Targ) {}
19+
public <UextendsT>voidmeth2(Uarg) {}
20+
}
21+
22+
// 1. At the top level:
23+
publicvoidmeth1(Objectarg) {}
24+
public <T>voidmeth2(Targ) {}// T implicitly extends Object
25+
26+
// 2. In a class type parameter:
27+
publicvoidmeth3(scala.collection.immutable.List<Object>arg) {}
28+
public <T>voidmeth4(scala.collection.immutable.List<T>arg) {}
29+
30+
// 3. As the type parameter of an array:
31+
publicvoidmeth5(Object[]arg) {}
32+
public <T>voidmeth6(T[]arg) {}
33+
34+
// 4. As the repeated argument of a varargs method:
35+
publicvoidmeth7(Object...args) {}
36+
public <T>voidmeth8(T...args) {}
37+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp