Skip to content

Commit afd9322

Browse files
committed
Add casts, and debug implementation
An issue occurs when trying to specialize certain methods when relying on typer only - this is described by scala#592 , and occured in test `this_specialization`. # with '#' will be ignored, and an empty message aborts the commit.
1 parent 1d138f2 commit afd9322

File tree

8 files changed

+55
-57
lines changed

8 files changed

+55
-57
lines changed

src/dotty/tools/dotc/ast/Trees.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ object Trees {
109109
*/
110110
val uniqueId = {
111111
nextId += 1
112-
if (nextId == 72)
113-
println("asdsad")
112+
/*if (nextId == 246) {
113+
println()
114+
}*/
114115
//assert(nextId != 214, this)
115116
nextId
116117
}

src/dotty/tools/dotc/core/Symbols.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,9 @@ object Symbols {
344344

345345
type ThisName <: Name
346346
private[this] var _id: Int = nextId
347-
if (_id == 16651) {
348-
println("plop")
349-
}
347+
/*if (_id == 29792) {
348+
println()
349+
}*/
350350
//assert(_id != 30214)
351351

352352
/** The unique id of this symbol */

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

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package dotty.tools.dotc.transform
22

33
import dotty.tools.dotc.ast.{tpd, TreeTypeMap}
44
import dotty.tools.dotc.ast.Trees._
5-
import dotty.tools.dotc.core.Annotations.Annotation
65
import dotty.tools.dotc.core.Contexts.Context
76
import dotty.tools.dotc.core.Decorators.StringDecorator
87
import dotty.tools.dotc.core.DenotTransformers.InfoTransformer
@@ -13,6 +12,7 @@ import dotty.tools.dotc.core.Types._
1312
import dotty.tools.dotc.transform.TreeTransforms.{TransformerInfo, MiniPhaseTransform}
1413
import scala.collection.mutable
1514
import dotty.tools.dotc.core.StdNames.nme
15+
import dotty.tools._
1616

1717
class TypeSpecializer extends MiniPhaseTransform with InfoTransformer {
1818

@@ -176,36 +176,34 @@ class TypeSpecializer extends MiniPhaseTransform with InfoTransformer {
176176
val tmap: (Tree => Tree) = _ match {
177177
case Return(t, from) if from.symbol == tree.symbol => Return(t, ref(newSym))
178178
case t: TypeApply => transformTypeApply(t)
179-
case t: Apply =>
180-
transformApply(t)
179+
case t: Apply => transformApply(t)
181180
case t => t
182181
}
183-
val tp = new TreeMap() {
184-
// needed to workaround https://github.com/lampepfl/dotty/issues/592
185-
override def transform(t: Tree)(implicit ctx: Context) = super.transform(t) match {
186-
case t @ Apply(fun, args) =>
187-
val newArgs = (args zip fun.tpe.firstParamTypes).map{case(t, tpe) => t.ensureConforms(tpe)}
188-
if (sameTypes(args, newArgs)) {
189-
t
190-
} else tpd.Apply(fun, newArgs)
191-
case t: ValDef =>
192-
cpy.ValDef(t)(rhs = t.rhs.ensureConforms(t.tpe.widen))
193-
case t: DefDef =>
194-
cpy.DefDef(t)(rhs = t.rhs.ensureConforms(t.tpe.finalResultType))
195-
case t => t
196-
}
197-
}
198182

199183
val typesReplaced = new TreeTypeMap(
200184
treeMap = tmap,
201185
typeMap = _
202186
.substDealias(origTParams, instantiations(index))
203-
.subst(origVParams, vparams.flatten.map(_.tpe))
204-
,
187+
.subst(origVParams, vparams.flatten.map(_.tpe)),
205188
oldOwners = tree.symbol :: Nil,
206189
newOwners = newSym :: Nil
207190
).transform(tree.rhs)
208191

192+
val tp = new TreeMap() {
193+
// needed to workaround https://github.com/lampepfl/dotty/issues/592
194+
override def transform(t: Tree)(implicit ctx: Context) = super.transform(t) match {
195+
case t @ Apply(fun, args) =>
196+
assert(sameLength(args, fun.tpe.widen.firstParamTypes))
197+
val newArgs = (args zip fun.tpe.widen.firstParamTypes).map{case(t, tpe) => t.ensureConforms(tpe)}
198+
if (sameTypes(args, newArgs)) {
199+
t
200+
} else tpd.Apply(fun, newArgs)
201+
case t: ValDef =>
202+
cpy.ValDef(t)(rhs = if(t.rhs.isEmpty) EmptyTree else t.rhs.ensureConforms(t.tpt.tpe))
203+
case t: DefDef =>
204+
cpy.DefDef(t)(rhs = if(t.rhs.isEmpty) EmptyTree else t.rhs.ensureConforms(t.tpt.tpe))
205+
case t => t
206+
}}
209207
val expectedTypeFixed = tp.transform(typesReplaced)
210208
expectedTypeFixed.ensureConforms(newSym.info.widen.finalResultType)
211209
}})
@@ -228,23 +226,13 @@ class TypeSpecializer extends MiniPhaseTransform with InfoTransformer {
228226
val Apply(fun, args) = tree
229227
fun match {
230228
case fun: TypeApply => {
231-
println(
232-
s"""
233-
|args -> ${args}
234-
235-
|f.fun -> ${fun.fun.tree}
236-
""".stripMargin)
237-
238229
val newFun = rewireTree(fun)
239230
if (fun ne newFun) {
240-
val b = (args zip newFun.tpe.firstParamTypes)
241-
val a = b.map{
231+
val as = (args zip newFun.tpe.widen.firstParamTypes).map{
242232
case (arg, tpe) =>
243233
arg.ensureConforms(tpe)
244234
}
245-
Apply(newFun,a)
246-
/* zip (instantiations zip paramTypes)).map{
247-
case (argType, (specType, castType)) => argType.ensureConforms(specType)})*/
235+
Apply(newFun,as)
248236
} else tree
249237
}
250238
case _ => tree

test/dotc/tests.scala

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,19 +207,18 @@ class tests extends CompilerTest {
207207
208208
val javaDir = "./tests/pos/java-interop/"
209209
@Test def java_all = compileFiles(javaDir, twice)
210-
211210
*/
212-
//@Test def specialization = compileFile(specialDir, "specialization")
213-
//@Test def mutual_spec = compileFile(specialDir, "mutual_specialization", List("-Xprint:all"))
214-
//@Test def return_spec = compileFile(specialDir, "return_specialization")
215-
// @Test def nothing_spec = compileFile(specialDir, "nothing_specialization")
216-
// @Test def method_in_class_spec = compileFile(specialDir, "method_in_class_specialization")
217-
// @Test def method_in_method_spec = compileFile(specialDir, "method_in_method_specialization")
218-
// @Test def pos_type_check = compileFile(specialDir, "type_test")
219-
// @Test def bounds_spec = compileFile(specialDir, "bounds_specialization")
220-
// @Test def multi_spec = compileFile(specialDir, "multi_specialization")
221-
// @Test def pos_spec_all = compileFiles(specialDir)
222-
@Test def pos_this_specialization = compileFile(specialDir, "this_specialization", List("-Xprint:specialize"))
211+
@Test def specialization = compileFile(specialDir, "specialization", List("-Xprint:specialize"))
212+
@Test def mutual_spec = compileFile(specialDir, "mutual_specialization", List("-Xprint:specialize"))
213+
@Test def return_spec = compileFile(specialDir, "return_specialization", List("-Xprint:specialize"))
214+
@Test def nothing_spec = compileFile(specialDir, "nothing_specialization", List("-Xprint:specialize"))
215+
@Test def method_in_class_spec = compileFile(specialDir, "method_in_class_specialization", List("-Xprint:specialize"))
216+
@Test def method_in_method_spec = compileFile(specialDir, "method_in_method_specialization", List("-Xprint:specialize"))
217+
@Test def pos_type_check = compileFile(specialDir, "type_test", List("-Xprint:specialize"))
218+
@Test def bounds_spec = compileFile(specialDir, "bounds_specialization", List("-Xprint:specialize"))
219+
@Test def multi_spec = compileFile(specialDir, "multi_specialization", List("-Xprint:specialize"))
220+
@Test def pos_this_specialization = compileFile(specialDir, "this_specialization", List("-Xprint:specialize"))
221+
@Test def pos_spec_all = compileFiles(specialDir)
223222

224223
//@Test def mini_method = compileFiles(miniMethodDir)//, List("-Xprint:all"))
225224
//@Test def mini_more = compileFiles(miniMoreDir)//, List("-Xprint:all"))

tests/pos/specialization/method_in_method_specialization.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,10 @@ object method_in_method_specialization {
1010

1111
outer(2)
1212
outer('d')
13+
14+
def outer2[@specialized(Int) O](o: O): Int = {
15+
def inner2[@specialized(Int) I] (i: I) = 1
16+
inner2(42)
17+
}
18+
outer2(1)
1319
}

tests/pos/specialization/multi_specialization.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ object multi_specialization {
22
def one[@specialized T](n: T): T = n
33
def two[@specialized T, U](n: T, m: U): (T,U) = (n,m)
44
def three[@specialized T, U, V](n: T, m: U, o: V): (T,U,V) = (n,m,o)
5+
6+
one(1)
7+
two(1,2)
8+
two('a', null)
59
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
object nothing_specialization {
2-
def ret_nothing[@specialized T] = {
2+
def ret_nothing[@specialized(Char) T] = {
33
//val a: List[T] = List[Nothing]()
4-
def apply[@specialized X](xs : X*) : List[X] = List(xs:_*)
5-
def apply6[@specialized X](xs : Nothing*) : List[Nothing] = List(xs: _*)
4+
def apply[@specialized(Char) X](xs : X*) : List[X] = List(xs:_*)
5+
def apply6[@specialized(Char) X](xs : Nothing*) : List[Nothing] = List(xs: _*)
6+
def apply2[@specialized(Long) U] = 1.asInstanceOf[U]
67
}
78
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
class specialization {
1+
trait specialization {
22
def printer1[@specialized(Int, Long) T](a: T) = {
33
println(a.toString)
44
}
5-
65
def printer2[@specialized(Int, Long) T, U](a: T, b: U) = {
76
println(a.toString + b.toString)
87
}
9-
def print(a: Int) = {
10-
printer1(a)
8+
def print(i: Int) = {
9+
printer1(i)
1110
println(" ---- ")
12-
printer2(a,a)
11+
printer2(i,i)
1312
}
1413
print(9)
1514
}

0 commit comments

Comments
 (0)