Skip to content

Commit 0964663

Browse files
committed
Add test cases for specialisation
1 parent cc653e4 commit 0964663

6 files changed

+53
-7
lines changed

tests/pos/specialization/multi_specialization.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
object multi_specialization {
22
def one[@specialized T](n: T): T = n
3-
def two[@specialized T, U](n: T, m: U): (T,U) = (n,m)
4-
def three[@specialized T, U, V](n: T, m: U, o: V): (T,U,V) = (n,m,o)
3+
def two[@specialized(Int, Double) T,@specialized(Double, Int) U](n: T, m: U): (T,U) = (n,m)
4+
def three[@specialized(Int, Double) T,@specialized(Double, Int) U, V](n: T, m: U, o: V): (T,U,V) = (n,m,o)
55

66
one(1)
77
two(1,2)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
object mutual_specialization {
22
class A[T] {
3-
def foo[@specialized U](b: U, n: Int): Unit = if (n > 0) bar(b, n-1)
4-
def bar[@specialized V](a: V, n: Int): Unit = if (n > 0) foo(a, n-1)
3+
def foo[@specialized(Double) U](b: U, n: Int): Unit = if (n > 0) bar(b, n-1)
4+
def bar[@specialized(Double) V](a: V, n: Int): Unit = if (n > 0) foo(a, n-1)
55
}
66
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
trait partial_specialization {
2+
def foo1stOutOf1[@specialized(Int, Char) T](t: T) = ???
3+
def foo1stOutOf2[@specialized(Int, Char) T, U](t: T, u: U): T = t
4+
def foo2ndOutOf2[T, @specialized(Int, Char) U](t: T, u: U) = ???
5+
def fooAllOutOf2[@specialized(Int, Char) T, @specialized(Int, Char) U](t: T, u: U) = ???
6+
def foo1st3rdOutOf3[@specialized(Int, Char) T, U, @specialized(Int, Char) V](t: T, u: U, v: V) = ???
7+
8+
def main(args: Array[String]) = {
9+
foo1stOutOf2(1, 2.0)
10+
foo1stOutOf2(1, 2.0)
11+
foo1st3rdOutOf3(1, 2, 'c')
12+
foo2ndOutOf2(1, 'c')
13+
fooAllOutOf2('a','b')
14+
fooAllOutOf2(1.0,1.0)
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
object subtype_specialization {
2+
3+
class Seq[+A]
4+
5+
case class FirstName[T](name: String) extends Seq[Char] {}
6+
7+
def foo[@specialized(Char) A](stuff: Seq[A]): Seq[A] = {
8+
stuff
9+
}
10+
11+
val s: Seq[FirstName] = foo[FirstName](new Seq[FirstName])
12+
13+
}

tests/run/method-specialization.check

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
10
22
82
33
3
4+
10
45
int
5-
double,int
6+
double,int
7+
int,class java.lang.Object

tests/run/method-specialization.scala

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ object Test extends dotty.runtime.LegacyApp {
44
def foo[@specialized U](u: U) = u
55
}
66
class Bar {
7-
def bar[@specialized U, V](u: U, v: V) = v
7+
def bar[@specialized U,@specialized V](u: U, v: V) = v
88
}
99
class Baz {
1010
def baz[@specialized(Int, Char) V](v: V): V = v
1111
}
12+
class Kung {
13+
def kung[@specialized U, V](u: U, v: V) = println(u.getClass)
14+
}
1215

1316
override def main(args: Array[String]): Unit = {
1417
/**
@@ -17,24 +20,36 @@ object Test extends dotty.runtime.LegacyApp {
1720
* 10
1821
* 82
1922
* 3
23+
* 10
2024
* int
2125
* double,int
22-
* int,double
26+
* int,class java.lang.Object
2327
*/
2428

2529
val a = new Foo
2630
val b = new Bar
2731
val c = new Baz
32+
val d = new Kung
2833
val foo_methods = a.getClass.getMethods
2934
val bar_methods = b.getClass.getMethods
3035
val baz_methods = c.getClass.getMethods
36+
val kung_methods = d.getClass.getMethods
3137
println(foo_methods.filter(_.toString.contains("foo")).length)
3238
println(bar_methods.filter(_.toString.contains("bar")).length)
3339
println(baz_methods.filter(_.toString.contains("baz")).length)
40+
println(kung_methods.filter(_.toString.contains("kung")).length)
3441

3542
val baz_int_param = baz_methods.filter(_.toString.contains("$mIc$sp")).head.getParameterTypes.mkString(",")
3643
val bar_int_double_params = bar_methods.filter(s => s.toString.contains("$mDIc$sp"))
44+
val kung_int_gen_params = kung_methods.filter(s => s.toString.contains("$mIc$sp"))
3745
println(baz_int_param)
3846
println(bar_int_double_params.head.getParameterTypes.mkString(","))
47+
println(kung_int_gen_params.head.getParameterTypes.mkString(","))
48+
49+
def genericKung[A](a: A) = d.kung(a, a)
50+
genericKung(1)
51+
52+
d.kung(1, 1)
53+
d.kung(1.0, 1.0)
3954
}
4055
}

0 commit comments

Comments
 (0)