@@ -252,6 +252,36 @@ object Erasure extends TypeTestsCasts{
252
252
if (tree.typeOpt.isRef(defn.UnitClass )) tree.withType(tree.typeOpt)
253
253
else super .typedLiteral(tree)
254
254
255
+ // The following methods, typedIf, typedMatch and typedTry need to compensate
256
+ // for the fact that after erasure, a subtype may notbe compatible with the
257
+ // type of a lub. Hence we might need to readapt branches to the common type.
258
+ // Note that the typing of SeqLiteral faces a similar problem but solves it
259
+ // differently: It adapts all elements to the erasure of the type that existed
260
+ // before. TODO: It looks like a good idea to harmonize this.
261
+ //
262
+ override def typedIf (tree : untpd.If , pt : Type )(implicit ctx : Context ): If = {
263
+ val tree1 = super .typedIf(tree, pt)
264
+ println(i " typed if $tree1 with $pt = ${tree1.tpe}" )
265
+ if (pt.isValueType) tree1
266
+ else cpy.If (tree1)(thenp = adapt(tree1.thenp, tree1.tpe), elsep = adapt(tree1.elsep, tree1.tpe))
267
+ }
268
+
269
+ override def typedMatch (tree : untpd.Match , pt : Type )(implicit ctx : Context ): Match = {
270
+ val tree1 = super .typedMatch(tree, pt).asInstanceOf [Match ]
271
+ if (pt.isValueType) tree1
272
+ else cpy.Match (tree1)(tree1.selector, tree1.cases.map(adaptCase(_, tree1.tpe)))
273
+ }
274
+
275
+ override def typedTry (tree : untpd.Try , pt : Type )(implicit ctx : Context ): Try = {
276
+ val tree1 = super .typedTry(tree, pt)
277
+ if (pt.isValueType) tree1
278
+ else cpy.Try (tree1)(expr = adapt(tree1.expr, tree1.tpe), cases = tree1.cases.map(adaptCase(_, tree1.tpe)))
279
+ }
280
+
281
+ private def adaptCase (cdef : CaseDef , pt : Type )(implicit ctx : Context ): CaseDef =
282
+ cpy.CaseDef (cdef)(body = adapt(cdef.body, pt))
283
+
284
+
255
285
/** Type check select nodes, applying the following rewritings exhaustively
256
286
* on selections `e.m`, where `OT` is the type of the owner of `m` and `ET`
257
287
* is the erased type of the selection's original qualifier expression.
0 commit comments