Skip to content

Commit 8cbc5b3

Browse files
committed
ElimByName now handles casts to by-name-parameter TermRefs
A cast like expr.asInstanceOf[x.type] where x is a by-name parameter has to be mapped to expr.asInstanceOf[x.type].apply() See pos/t296.scala which would otherwise start failing when the pattern matcher is integrated.
1 parent 57c110d commit 8cbc5b3

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,29 @@ class ElimByName extends MiniPhaseTransform with InfoTransformer { thisTransform
7676
cpy.Apply(tree)(tree.fun, args1)
7777
}
7878

79-
private def becomesFunction(symd: SymDenotation)(implicit ctx: Context) =
79+
/** If denotation had an ExprType before, it now gets a function type */
80+
private def exprBecomesFunction(symd: SymDenotation)(implicit ctx: Context) =
8081
(symd is Param) || (symd is (ParamAccessor, butNot = Method))
8182

82-
override def transformIdent(tree: Ident)(implicit ctx: Context, info: TransformerInfo): Tree = {
83-
val origDenot = originalDenotation(tree)
84-
if (becomesFunction(origDenot) && (origDenot.info.isInstanceOf[ExprType]))
83+
/** Map `tree` to `tree.apply()` is `ftree` was of ExprType and becomes now a function */
84+
private def applyIfFunction(tree: Tree, ftree: Tree)(implicit ctx: Context) = {
85+
val origDenot = originalDenotation(ftree)
86+
if (exprBecomesFunction(origDenot) && (origDenot.info.isInstanceOf[ExprType]))
8587
tree.select(defn.Function0_apply).appliedToNone
8688
else tree
8789
}
8890

91+
override def transformIdent(tree: Ident)(implicit ctx: Context, info: TransformerInfo): Tree =
92+
applyIfFunction(tree, tree)
93+
94+
override def transformTypeApply(tree: TypeApply)(implicit ctx: Context, info: TransformerInfo): Tree = tree match {
95+
case TypeApply(Select(_, nme.asInstanceOf_), arg :: Nil) =>
96+
// tree might be of form e.asInstanceOf[x.type] where x becomes a function.
97+
// See pos/t296.scala
98+
applyIfFunction(tree, arg)
99+
case _ => tree
100+
}
101+
89102
def elimByNameParams(tp: Type)(implicit ctx: Context): Type = tp match {
90103
case tp: PolyType =>
91104
tp.derivedPolyType(tp.paramNames, tp.paramBounds, elimByNameParams(tp.resultType))
@@ -102,6 +115,6 @@ class ElimByName extends MiniPhaseTransform with InfoTransformer { thisTransform
102115
}
103116

104117
def transformInfo(tp: Type, sym: Symbol)(implicit ctx: Context): Type =
105-
if (becomesFunction(sym)) transformParamInfo(tp)
118+
if (exprBecomesFunction(sym)) transformParamInfo(tp)
106119
else elimByNameParams(tp)
107120
}

0 commit comments

Comments
 (0)