Skip to content

Create boxed environments only for references and function values #16136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala
Original file line number Diff line number Diff line change
Expand Up @@ -507,14 +507,20 @@ class CheckCaptures extends Recheck, SymTransformer:
recheckFinish(result, arg, pt)
*/

/** If expected type `pt` is boxed, don't propagate free variables.
/** 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
* adding all references in the boxed capture set to the current environment.
*/
override def recheck(tree: Tree, pt: Type = WildcardType)(using Context): Type =
if tree.isTerm && pt.isBoxedCapturing then
val saved = curEnv
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
46 changes: 46 additions & 0 deletions tests/neg-custom-args/captures/i16114.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
trait Cap { def use(): Int; def close(): Unit }
def mkCap(): {*} Cap = ???

def expect[T](x: T): x.type = x

def withCap[T](op: ({*} Cap) => T): T = {
val cap: {*} Cap = mkCap()
val result = op(cap)
cap.close()
result
}

def main(fs: {*} Cap): Unit = {
def badOp(io: {*} Cap): {} Unit -> Unit = {
val op1: {io} Unit -> Unit = (x: Unit) => // error // limitation
expect[{*} Cap] {
io.use()
fs
}

val op2: {fs} Unit -> Unit = (x: Unit) => // error // limitation
expect[{*} Cap] {
fs.use()
io
}

val op3: {io} Unit -> Unit = (x: Unit) => // ok
expect[{*} Cap] {
io.use()
io
}

val op4: {} Unit -> Unit = (x: Unit) => // ok
expect[{*} Cap](io)

val op: {} Unit -> Unit = (x: Unit) => // error
expect[{*} Cap] {
io.use()
io
}
op
}

val leaked: {} Unit -> Unit = withCap(badOp)
leaked(())
}
2 changes: 1 addition & 1 deletion tests/pos-custom-args/captures/boxmap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def box[T <: Top](x: T): Box[T] =
def map[A <: Top, B <: Top](b: Box[A])(f: A => B): Box[B] =
b[Box[B]]((x: A) => box(f(x)))

def lazymap[A <: Top, B <: Top](b: Box[A])(f: A => B): (() -> Box[B]) =
def lazymap[A <: Top, B <: Top](b: Box[A])(f: A => B): {f} (() -> Box[B]) =
() => b[Box[B]]((x: A) => box(f(x)))

def test[A <: Top, B <: Top] =
Expand Down