Skip to content

Commit a9ebb46

Browse files
committed
Workaround scala#592 Insert casts all over the place in specialised methods.
The way how this fix is structures, is that if 592 ever gets fixed, no casts will be inserted.
1 parent ae129da commit a9ebb46

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import dotty.tools.dotc.transform.SymUtils._
3333
* gets two different denotations in the same period. Hence, if -Yno-double-bindings is
3434
* set, we would get a data race assertion error.
3535
*/
36-
final class TreeTypeMap(
36+
class TreeTypeMap(
3737
val typeMap: Type => Type = IdentityTypeMap,
3838
val treeMap: tpd.Tree => tpd.Tree = identity _,
3939
val oldOwners: List[Symbol] = Nil,

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ 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 => transformApply(t)
179+
case t: Apply =>
180+
transformApply(t)
180181
case t => t
181182
}
182183

@@ -188,7 +189,21 @@ class TypeSpecializer extends MiniPhaseTransform with InfoTransformer {
188189
,
189190
oldOwners = tree.symbol :: Nil,
190191
newOwners = newSym :: Nil
191-
).transform(tree.rhs)
192+
) {
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+
val newArgs = (args zip fun.tpe.firstParamTypes).map{case(t, tpe) => t.ensureConforms(tpe)}
197+
if (sameTypes(args, newArgs)) {
198+
t
199+
} else tpd.Apply(fun, newArgs)
200+
case t: ValDef =>
201+
cpy.ValDef(t)(rhs = t.rhs.ensureConforms(t.tpe.widen))
202+
case t: DefDef =>
203+
cpy.DefDef(t)(rhs = t.rhs.ensureConforms(t.tpe.finalResultType))
204+
case t => t
205+
}
206+
}.transform(tree.rhs)
192207
}})
193208
}
194209
} else Nil
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
sealed abstract class Foo[@specialized +A] {
1+
trait Foo[@specialized +A] {
2+
// all those examples trigger bugs due to https://github.com/lampepfl/dotty/issues/592
23
def bop[@specialized B >: A]: Foo[B] = new Bar[B](this)
4+
def gwa[@specialized B >: A]: Foo[B] = this
5+
def gwd[@specialized B >: A]: Foo[B] = {
6+
val d: Foo[B] = this
7+
d
8+
}
39
//def bip[@specialized C >: A, @specialized D >: A]: Foo[D] = new Cho[D, C](new Bar[C](this))
410
}
511

612
case class Bar[@specialized a](tl: Foo[a]) extends Foo[a]
713

8-
//case class Cho[@specialized c, @specialized d](tl: Bar[d]) extends Foo[c]
14+
//case class Cho[@specialized c, @specialized d](tl: Bar[d]) extends Foo[c]

0 commit comments

Comments
 (0)