Skip to content

Commit 6fde114

Browse files
DarkDimiusAlexSikia
authored andcommitted
Workaround scala#592 Insert casts all over the place in specialised methods.
The way this fix is structured ensures that if scala#592 ever gets fixed, no casts will be inserted.
1 parent 0a137f2 commit 6fde114

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import dotty.tools.dotc.transform.SymUtils._
3131
* gets two different denotations in the same period. Hence, if -Yno-double-bindings is
3232
* set, we would get a data race assertion error.
3333
*/
34-
final class TreeTypeMap(
34+
class TreeTypeMap(
3535
val typeMap: Type => Type = IdentityTypeMap,
3636
val treeMap: tpd.Tree => tpd.Tree = identity _,
3737
val oldOwners: List[Symbol] = Nil,
@@ -60,7 +60,7 @@ final class TreeTypeMap(
6060

6161
def mapType(tp: Type) =
6262
mapOwnerThis(typeMap(tp).substSym(substFrom, substTo))
63-
63+
6464
private def updateDecls(prevStats: List[Tree], newStats: List[Tree]): Unit =
6565
if (prevStats.isEmpty) assert(newStats.isEmpty)
6666
else {
@@ -75,7 +75,7 @@ final class TreeTypeMap(
7575
updateDecls(prevStats.tail, newStats.tail)
7676
}
7777

78-
override def transform(tree: tpd.Tree)(implicit ctx: Context): tpd.Tree = treeMap(tree) match {
78+
override final def transform(tree: tpd.Tree)(implicit ctx: Context): tpd.Tree = treeMap(tree) match {
7979
case impl @ Template(constr, parents, self, _) =>
8080
val tmap = withMappedSyms(localSyms(impl :: self :: Nil))
8181
cpy.Template(impl)(

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,27 @@ class TypeSpecializer extends MiniPhaseTransform with InfoTransformer {
175175
val tmap: (Tree => Tree) = _ match {
176176
case Return(t, from) if from.symbol == tree.symbol => Return(t, ref(newSym))
177177
case t: TypeApply => transformTypeApply(t)
178-
case t: Apply => transformApply(t)
178+
case t: Apply =>
179+
transformApply(t)
179180
case t => t
180181
}
182+
val tp = new TreeMap() {
183+
// needed to workaround https://github.com/lampepfl/dotty/issues/592
184+
override def transform(t: Tree)(implicit ctx: Context) = super.transform(t) match {
185+
case t @ Apply(fun, args) =>
186+
val newArgs = (args zip fun.tpe.firstParamTypes).map{case(t, tpe) => t.ensureConforms(tpe)}
187+
if (sameTypes(args, newArgs)) {
188+
t
189+
} else tpd.Apply(fun, newArgs)
190+
case t: ValDef =>
191+
cpy.ValDef(t)(rhs = t.rhs.ensureConforms(t.tpe.widen))
192+
case t: DefDef =>
193+
cpy.DefDef(t)(rhs = t.rhs.ensureConforms(t.tpe.finalResultType))
194+
case t => t
195+
}
196+
}
181197

182-
new TreeTypeMap(
198+
val typesReplaced = new TreeTypeMap(
183199
treeMap = tmap,
184200
typeMap = _
185201
.substDealias(origTParams, instantiations(index))
@@ -188,6 +204,9 @@ class TypeSpecializer extends MiniPhaseTransform with InfoTransformer {
188204
oldOwners = tree.symbol :: Nil,
189205
newOwners = newSym :: Nil
190206
).transform(tree.rhs)
207+
208+
val expectedTypeFixed = tp.transform(typesReplaced)
209+
expectedTypeFixed.ensureConforms(newSym.info.widen.finalResultType)
191210
}})
192211
}
193212
} 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)