Skip to content
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

Polymorphic capture functions can be exploited to hide captures #21347

Closed
Linyxus opened this issue Aug 7, 2024 · 1 comment · Fixed by #21309
Closed

Polymorphic capture functions can be exploited to hide captures #21347

Linyxus opened this issue Aug 7, 2024 · 1 comment · Fixed by #21309
Assignees
Labels
area:experimental:cc Capture checking related itype:bug
Milestone

Comments

@Linyxus
Copy link
Contributor

Linyxus commented Aug 7, 2024

Compiler version

The main branch

Minimized code

import language.experimental.captureChecking

def runOps[C^](ops: List[() ->{C^} Unit]): Unit =
  ops.foreach: op =>
    op()

def boom(f: () => Unit): () -> Unit =
  () => runOps(f :: Nil)

Output

It type-checks.

Expectation

It should not type-check. Accepting the boom function is clearly unsound: given any impure operations that could perform arbitrary effects, boom could hide its captures and pretend that this operation is pure.

The capture checker rejects the morally equivalent variant that uses only reach capabilities:

def runOpsAlt(ops: List[() => Unit]): Unit =
  ops.foreach: op =>
    op()

The compiler complains:

-- Error: issues/drop-caparg.scala:4:15 --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4 |  ops.foreach: op =>
  |               ^
  |               Local reach capability ops* leaks into capture scope of method runOpsAlt
5 |    op()

We should do something similar with capture polymorphism.

/cc @odersky

@Linyxus Linyxus added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Aug 7, 2024
@odersky
Copy link
Contributor

odersky commented Aug 7, 2024

Should be fixed in #21309.

@Gedochao Gedochao added area:experimental:cc Capture checking related and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Aug 8, 2024
@Gedochao Gedochao linked a pull request Aug 8, 2024 that will close this issue
@odersky odersky closed this as completed in 618bbc5 Aug 8, 2024
odersky added a commit to dotty-staging/dotty that referenced this issue Sep 13, 2024
This is necessary for types that contain possibly illegal @retains annotations
since those annotations are only removed before pickling for InferredTypes.

Fixes scala#21347
odersky added a commit to dotty-staging/dotty that referenced this issue Sep 13, 2024
This is necessary for types that contain possibly illegal @retains annotations
since those annotations are only removed before pickling for InferredTypes.

Fixes scala#21347
WojciechMazur pushed a commit that referenced this issue Sep 14, 2024
This is necessary for types that contain possibly illegal @retains annotations
since those annotations are only removed before pickling for InferredTypes.

Fixes #21347
@WojciechMazur WojciechMazur added this to the 3.6.0 milestone Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:experimental:cc Capture checking related itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants