Skip to content

Commit 7aed2e7

Browse files
committed
Adapt specialization tests and clean up
1 parent a48de98 commit 7aed2e7

File tree

6 files changed

+32
-14
lines changed

6 files changed

+32
-14
lines changed

src/dotty/tools/dotc/transform/TypeSpecializer.scala

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TypeSpecializer extends MiniPhaseTransform with InfoTransformer {
1919
import tpd._
2020
override def phaseName = "specialize"
2121

22-
final val maxTparamsToSpecialize = 2
22+
final var maxTparamsToSpecialize = 0
2323

2424
private final def specialisedTypeToSuffix(implicit ctx: Context) =
2525
Map(defn.ByteType -> "B",
@@ -56,7 +56,7 @@ class TypeSpecializer extends MiniPhaseTransform with InfoTransformer {
5656
private val newSymbolMap: mutable.HashMap[Symbol, mutable.HashMap[List[Type], Symbols.Symbol]] = mutable.HashMap.empty
5757

5858
def allowedToSpecialize(sym: Symbol, numOfTypes: Int)(implicit ctx: Context): Boolean = {
59-
numOfTypes <= maxTparamsToSpecialize &&
59+
(maxTparamsToSpecialize == 0 || numOfTypes <= maxTparamsToSpecialize) &&
6060
numOfTypes > 0 &&
6161
sym.name != nme.asInstanceOf_ &&
6262
sym.name != nme.isInstanceOf_ &&
@@ -179,14 +179,12 @@ class TypeSpecializer extends MiniPhaseTransform with InfoTransformer {
179179
polyDefDef(newSym.asTerm, { tparams => vparams => {
180180
val tmap: (Tree => Tree) = _ match {
181181
case Return(t, from) if from.symbol == tree.symbol => Return(t, ref(newSym))
182-
case t: TypeApply => {
182+
case t: TypeApply =>
183183
(origTParams zip instantiations(index)).foreach(x => genericToInstantiation.put(x._1, x._2))
184184
transformTypeApply(t)
185-
}
186-
case t: Apply => {
185+
case t: Apply =>
187186
(origTParams zip instantiations(index)).foreach(x => genericToInstantiation.put(x._1, x._2))
188187
transformApply(t)
189-
}
190188
case t => t
191189
}
192190

@@ -204,7 +202,7 @@ class TypeSpecializer extends MiniPhaseTransform with InfoTransformer {
204202
override def transform(tree1: Tree)(implicit ctx: Context) = super.transform(tree1) match {
205203
case t @ Apply(fun, args) =>
206204
assert(sameLength(args, fun.tpe.widen.firstParamTypes))
207-
val newArgs = (args zip fun.tpe.widen.firstParamTypes).map{case(t, tpe) => t.ensureConforms(tpe)}
205+
val newArgs = (args zip fun.tpe.widen.firstParamTypes).map{case(tr, tpe) => tr.ensureConforms(tpe)}
208206
if (sameTypes(args, newArgs)) {
209207
t
210208
} else tpd.Apply(fun, newArgs)

test/dotc/tests.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,11 @@ class tests extends CompilerTest {
203203
@Test def bounds_spec = compileFile(specialDir, "bounds_specialization")
204204
@Test def multi_spec = compileFile(specialDir, "multi_specialization")
205205
@Test def pos_this_specialization = compileFile(specialDir, "this_specialization")
206-
@Test def pos_spec_all = compileFiles(specialDir)
206+
@Test def specializable_spec = runFile(specialDir, "specializable_specialization")
207+
@Test def anyRef_spec = runFile(specialDir, "anyRef_specialization")
208+
@Test def recursive_spec = runFile(specialDir, "recursive_specialization")
207209

208210
@Test def run_spec = runFile(runDir, "method-specialization")
209-
211+
210212
//@Test def dotc_compilercommand = compileFile(dotcDir + "config/", "CompilerCommand")
211213
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object Test {
2+
def foo[@specialized(AnyRef) T](t: T): T = t
3+
}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
object mutual_specialization {
22
class A[T] {
3-
def foo[T](b: B[T], n: Int): Unit = if (n > 0) b.bar(this, n-1)
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)
45
}
5-
class B[T] {
6-
def bar[T](a: A[T], n: Int): Unit = if (n > 0) a.foo(this, n-1)
7-
}
8-
def foobar[@specialized(Int, Float, Double) T](n: T): Unit = new A[T].foo(new B[T], 5)
6+
def foobar[@specialized(Char) X](n: X): Unit = new A[X].foo(2, 5)
97
foobar(5)
108
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Spec {
2+
def plus[@specialized T](a: T, b:T)(ev: Numeric[T]): T = plus(b, a)(ev)
3+
}
4+
5+
class IntSpec extends Spec {
6+
val res = plus(1,2)(Numeric.IntIsIntegral)
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import Specializable._
2+
3+
object specializable {
4+
def foo[@specialized(Primitives) T](t: T): T = t
5+
def foo2[@specialized(Everything) T](t: T): T = t
6+
def foo3[@specialized(Bits32AndUp) T](t: T): T = t
7+
def foo4[@specialized(Integral) T](t: T): T = t
8+
def foo5[@specialized(AllNumeric) T](t: T): T = t
9+
def foo6[@specialized(BestOfBreed) T](t: T): T = t
10+
}

0 commit comments

Comments
 (0)