Skip to content
Prev Previous commit
use pattern matching and closureDef extractor
  • Loading branch information
Linyxus committed Oct 16, 2022
commit a25677eac79a1f88bce5f495b4e442eddfebcd75
21 changes: 6 additions & 15 deletions compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala
Original file line number Diff line number Diff line change
Expand Up @@ -507,19 +507,6 @@ class CheckCaptures extends Recheck, SymTransformer:
recheckFinish(result, arg, pt)
*/

extension (tree: Tree)
private def isFunctionLiteral(using Context): Boolean = tree match
case Block((defTree @ DefDef(_, _, _, _)) :: Nil, Closure(_, meth, _)) =>
val defSym = defTree.symbol
val methSym = meth.symbol
defSym.eq(methSym)
case _ =>
false

private def isRefTree: Boolean = tree match
case _: RefTree => true
case _ => false

/** If expected type `pt` is boxed and the tree is a function or a reference,
* don't propagate free variables.
* Otherwise, if the result type is boxed, simulate an unboxing by
Expand All @@ -528,8 +515,12 @@ class CheckCaptures extends Recheck, SymTransformer:
override def recheck(tree: Tree, pt: Type = WildcardType)(using Context): Type =
if tree.isTerm && pt.isBoxedCapturing then
val saved = curEnv
if tree.isRefTree || tree.isFunctionLiteral then
curEnv = Env(curEnv.owner, nestedInOwner = false, CaptureSet.Var(), isBoxed = true, curEnv)

tree match
case _: RefTree | closureDef(_) =>
curEnv = Env(curEnv.owner, nestedInOwner = false, CaptureSet.Var(), isBoxed = true, curEnv)
case _ =>

try super.recheck(tree, pt)
finally curEnv = saved
else
Expand Down