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

Variables in {.compileTime.} cannot be captured if the capturing closure is nested twice #1425

Closed
alaviss opened this issue Aug 21, 2024 · 1 comment · Fixed by #1427
Closed
Assignees
Labels
bug Something isn't working compiler/sem Related to semantic-analysis system of the compiler

Comments

@alaviss
Copy link
Contributor

alaviss commented Aug 21, 2024

Example

proc foo() {.compileTime.} =
  var x = 0

  proc bar() =
    proc foobar() =
      x = 1

    foobar()

  bar()

static: foo()

Actual Output

test.nim(6, 7) Error: 'x' is inaccesible because run-time values cannot be captured in a compile-time context

Additional Information

  • If foobar body is inlined into bar, no error will occur.

  • This is found while fixing npeg.

@alaviss alaviss added bug Something isn't working compiler/sem Related to semantic-analysis system of the compiler labels Aug 21, 2024
@zerbina zerbina self-assigned this Aug 21, 2024
@zerbina
Copy link
Collaborator

zerbina commented Aug 21, 2024

The example code not working is deliberate, although for no good nor principled reason -- I'll remove the arbitrary restriction.

github-merge-queue bot pushed a commit that referenced this issue Aug 21, 2024
## Summary

Procedures useable in both compile- and run-time contexts can now close
over compile-time-only locals even if nested in another procedure
usable in both contexts.

Fixes #1425.

## Details

Given the nested procedures `a: b: c:`, where `c` closes over a local
of `a`, `c` effectively captures from `b` and `b` captures from `a`.

A procedure usable in both a compile- and run-time context may capture
from a compile-time-only procedure, so the aforementioned should work
when `a` is a compile-time-only procedure and the others are not, since
`c` can legally borrow from `b` and `b` from `a`, but it was disallowed
in practice.

The arbitrary restriction is now lifted, and the code implementing the
test restructured to be easier to understand.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compiler/sem Related to semantic-analysis system of the compiler
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants