Skip to content

Commit 1844c1d

Browse files
committed
Avoid creating dependent function types for closures
Without this step, anonymous functions can have dependent types which causes the parameter references to "leak out" to types in the environment in illegal ways. This caused a tasty failure for Typer before (not sure why the failure was not observed under the old hk scheme).
1 parent 3560e56 commit 1844c1d

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
10381038
val tparams1 = tparams mapconserve (typed(_).asInstanceOf[TypeDef])
10391039
val vparamss1 = vparamss nestedMapconserve (typed(_).asInstanceOf[ValDef])
10401040
if (sym is Implicit) checkImplicitParamsNotSingletons(vparamss1)
1041-
val tpt1 = checkSimpleKinded(typedType(tpt))
1041+
var tpt1 = checkSimpleKinded(typedType(tpt))
10421042

10431043
var rhsCtx = ctx
10441044
if (sym.isConstructor && !sym.isPrimaryConstructor && tparams1.nonEmpty) {
@@ -1050,6 +1050,12 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
10501050
rhsCtx.gadt.setBounds(tdef.symbol, TypeAlias(tparam.typeRef)))
10511051
}
10521052
val rhs1 = typedExpr(ddef.rhs, tpt1.tpe)(rhsCtx)
1053+
if (sym.isAnonymousFunction) {
1054+
// If we define an anonymous function, make sure the return type does not
1055+
// refer to parameters. This is necessary because closure types are
1056+
// function types so no dependencies on parameters are allowed.
1057+
tpt1 = tpt1.withType(avoid(tpt1.tpe, vparamss1.flatMap(_.map(_.symbol))))
1058+
}
10531059
assignType(cpy.DefDef(ddef)(name, tparams1, vparamss1, tpt1, rhs1), sym)
10541060
//todo: make sure dependent method types do not depend on implicits or by-name params
10551061
}

0 commit comments

Comments
 (0)