Skip to content

Commit 144c8ea

Browse files
committed
Alternative scheme to handle branching
The new scheme alignes If, Match, Try to the scheme used in SeqLiteral. This favors unboxed over boxed types. It is also simpler than the scheme in the previous commit.
1 parent 3a74f7c commit 144c8ea

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

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

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -252,28 +252,6 @@ object Erasure extends TypeTestsCasts{
252252
if (tree.typeOpt.isRef(defn.UnitClass)) tree.withType(tree.typeOpt)
253253
else super.typedLiteral(tree)
254254

255-
override def typedIf(tree: untpd.If, pt: Type)(implicit ctx: Context): If = {
256-
val tree1 = super.typedIf(tree, pt)
257-
if (pt.isValueType) tree1
258-
else cpy.If(tree1)(thenp = adapt(tree1.thenp, tree1.tpe), elsep = adapt(tree1.elsep, tree1.tpe))
259-
}
260-
261-
override def typedMatch(tree: untpd.Match, pt: Type)(implicit ctx: Context): Match = {
262-
val tree1 = super.typedMatch(tree, pt).asInstanceOf[Match]
263-
if (pt.isValueType) tree1
264-
else cpy.Match(tree1)(tree1.selector, tree1.cases.map(adaptCase(_, tree1.tpe)))
265-
}
266-
267-
override def typedTry(tree: untpd.Try, pt: Type)(implicit ctx: Context): Try = {
268-
val tree1 = super.typedTry(tree, pt)
269-
if (pt.isValueType) tree1
270-
else cpy.Try(tree1)(expr = adapt(tree1.expr, tree1.tpe), cases = tree1.cases.map(adaptCase(_, tree1.tpe)))
271-
}
272-
273-
private def adaptCase(cdef: CaseDef, pt: Type)(implicit ctx: Context): CaseDef =
274-
cpy.CaseDef(cdef)(body = adapt(cdef.body, pt))
275-
276-
277255
/** Type check select nodes, applying the following rewritings exhaustively
278256
* on selections `e.m`, where `OT` is the type of the owner of `m` and `ET`
279257
* is the erased type of the selection's original qualifier expression.
@@ -409,9 +387,24 @@ object Erasure extends TypeTestsCasts{
409387
}
410388
}
411389

390+
// The following four methods take as the proto-type the erasure of the pre-existing type,
391+
// if the original proto-type is not a value type.
392+
// This makes all branches be adapted to the correct type.
412393
override def typedSeqLiteral(tree: untpd.SeqLiteral, pt: Type)(implicit ctx: Context) =
413394
super.typedSeqLiteral(tree, erasure(tree.typeOpt))
414-
// proto type of typed seq literal is original type; this makes elements be adapted to correct type.
395+
// proto type of typed seq literal is original type;
396+
397+
override def typedIf(tree: untpd.If, pt: Type)(implicit ctx: Context) =
398+
super.typedIf(tree, adaptProto(tree, pt))
399+
400+
override def typedMatch(tree: untpd.Match, pt: Type)(implicit ctx: Context) =
401+
super.typedMatch(tree, adaptProto(tree, pt))
402+
403+
override def typedTry(tree: untpd.Try, pt: Type)(implicit ctx: Context) =
404+
super.typedTry(tree, adaptProto(tree, pt))
405+
406+
private def adaptProto(tree: untpd.Tree, pt: Type)(implicit ctx: Context) =
407+
if (pt.isValueType) pt else erasure(tree.typeOpt)
415408

416409
override def typedValDef(vdef: untpd.ValDef, sym: Symbol)(implicit ctx: Context): ValDef =
417410
super.typedValDef(untpd.cpy.ValDef(vdef)(

0 commit comments

Comments
 (0)