@@ -1534,7 +1534,23 @@ class Namer { typer: Typer =>
15341534 // This case applies if the closure result type contains uninstantiated
15351535 // type variables. In this case, constrain the closure result from below
15361536 // by the parameter-capture-avoiding type of the body.
1537- typedAheadExpr(mdef.rhs, tpt.tpe).tpe
1537+ val rhsType = typedAheadExpr(mdef.rhs, tpt.tpe).tpe
1538+
1539+ // The following part is important since otherwise we might instantiate
1540+ // the closure result type with a plain functon type that refers
1541+ // to local parameters. An example where this happens in `dependent-closures.scala`
1542+ // If the code after `val rhsType` is commented out, this file fails pickling tests.
1543+ // AVOIDANCE TODO: Follow up why this happens, and whether there
1544+ // are better ways to achieve this. It would be good if we could get rid of this code.
1545+ // It seems at least partially redundant with the nesting level checking on TypeVar
1546+ // instantiation.
1547+ if ! Config .checkLevels then
1548+ val hygienicType = TypeOps .avoid(rhsType, termParamss.flatten)
1549+ if (! hygienicType.isValueType || ! (hygienicType <:< tpt.tpe))
1550+ report.error(i " return type ${tpt.tpe} of lambda cannot be made hygienic; \n " +
1551+ i " it is not a supertype of the hygienic type $hygienicType" , mdef.srcPos)
1552+ // println(i"lifting $rhsType over $termParamss -> $hygienicType = ${tpt.tpe}")
1553+ // println(TypeComparer.explained { implicit ctx => hygienicType <:< tpt.tpe })
15381554 case _ =>
15391555 }
15401556 WildcardType
0 commit comments