@@ -1672,7 +1672,23 @@ class Namer { typer: Typer =>
16721672 // This case applies if the closure result type contains uninstantiated
16731673 // type variables. In this case, constrain the closure result from below
16741674 // by the parameter-capture-avoiding type of the body.
1675- typedAheadExpr(mdef.rhs, tpt.tpe).tpe
1675+ val rhsType = typedAheadExpr(mdef.rhs, tpt.tpe).tpe
1676+
1677+ // The following part is important since otherwise we might instantiate
1678+ // the closure result type with a plain functon type that refers
1679+ // to local parameters. An example where this happens in `dependent-closures.scala`
1680+ // If the code after `val rhsType` is commented out, this file fails pickling tests.
1681+ // AVOIDANCE TODO: Follow up why this happens, and whether there
1682+ // are better ways to achieve this. It would be good if we could get rid of this code.
1683+ // It seems at least partially redundant with the nesting level checking on TypeVar
1684+ // instantiation.
1685+ if ! Config .checkLevels then
1686+ val hygienicType = TypeOps .avoid(rhsType, termParamss.flatten)
1687+ if (! hygienicType.isValueType || ! (hygienicType <:< tpt.tpe))
1688+ report.error(i " return type ${tpt.tpe} of lambda cannot be made hygienic; \n " +
1689+ i " it is not a supertype of the hygienic type $hygienicType" , mdef.srcPos)
1690+ // println(i"lifting $rhsType over $termParamss -> $hygienicType = ${tpt.tpe}")
1691+ // println(TypeComparer.explained { implicit ctx => hygienicType <:< tpt.tpe })
16761692 case _ =>
16771693 }
16781694 WildcardType
0 commit comments