@@ -178,6 +178,16 @@ class TreeChecker extends Phase with SymTransformer {
178178 res
179179 }
180180
181+ // used to check invariant of lambda encoding
182+ var nestingBlock : untpd.Block | Null = null
183+ private def withBlock [T ](block : untpd.Block )(op : => T ): T = {
184+ val outerBlock = nestingBlock
185+ nestingBlock = block
186+ val res = op
187+ nestingBlock = outerBlock
188+ res
189+ }
190+
181191 def assertDefined (tree : untpd.Tree )(implicit ctx : Context ): Unit =
182192 if (tree.symbol.maybeOwner.isTerm)
183193 assert(nowDefinedSyms contains tree.symbol, i " undefined symbol ${tree.symbol} at line " + tree.sourcePos.line)
@@ -407,8 +417,22 @@ class TreeChecker extends Phase with SymTransformer {
407417 }
408418 }
409419
420+ override def typedClosure (tree : untpd.Closure , pt : Type )(implicit ctx : Context ): Tree = {
421+ if (! ctx.phase.lambdaLifted) nestingBlock match {
422+ case Block ((meth : DefDef ) :: Nil , closure : Closure ) =>
423+ assert(meth.symbol == closure.meth.symbol, " closure.meth symbol not equal to method symbol. Block: " + nestingBlock.show)
424+
425+ case block : untpd.Block =>
426+ assert(false , " function literal are not properly formed as a block of DefDef and Closure. Found: " + tree.show + " Nesting block: " + block.show)
427+
428+ case null =>
429+ assert(false , " function literal are not properly formed as a block of DefDef and Closure. Found: " + tree.show + " Nesting block: null" )
430+ }
431+ super .typedClosure(tree, pt)
432+ }
433+
410434 override def typedBlock (tree : untpd.Block , pt : Type )(implicit ctx : Context ): Tree =
411- withDefinedSyms(tree.stats) { super .typedBlock(tree, pt) }
435+ withBlock(tree) { withDefinedSyms(tree.stats) { super .typedBlock(tree, pt) } }
412436
413437 override def typedInlined (tree : untpd.Inlined , pt : Type )(implicit ctx : Context ): Tree =
414438 withDefinedSyms(tree.bindings) { super .typedInlined(tree, pt) }
0 commit comments