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

fix: crash when passing NimNode to static parameter #1449

Merged

Conversation

zerbina
Copy link
Collaborator

@zerbina zerbina commented Sep 5, 2024

Summary

  • fix a compiler crash when passing a NimNode to a static parameter
  • fix a compiler crash when evaluating a NimNode-returning constant
    expression

Details

There were two problems:

  • nkNimNodeLit wasn't handled in constant data expression by mirgen
  • NimNode values returned directly from a VM invocation weren't
    wrapped in nkNimNodeLit trees

For handling NimNode values in vm.regToNode, the existing
deserialization logic for NimNode values in vmcompilerserdes is
moved to a separate procedure, so that it can be used by regToNode.

The opcRepr implementation relied on regToNode always returning
an unwrapped PNode -- it is adjusted to manually handle NimNode
values.

A test covering both issues is added.

Fixes #1448.

The VM serialization logic already supports AST literals, but `mirgen`
didn't let them through. Now it does.
`NimNode` values returned directly from VM execution were neither
checked (for cycles), nor wrapped in a `nkNimNodeLit` tree, leaving
them in an invalid state.

The `vmcompilerserdes` handling for NimNodes is split into a separate
procedure, which is then used by both `vmcompilerserdes` and
`regToNode`.
The test covers both fixed issues, since the `newStmtList` call is
fully evaluated first before it is passed to the macro (where it is
then processed by `constDataToMir`).
The implementation relied on `regToNode` returning the the raw `PNode`
for `NimNode` values. It no longer does, so the `opcRepr` logic is
adjusted to manually handle the `NimNode` case.
@zerbina zerbina added bug Something isn't working compiler/backend Related to backend system of the compiler compiler/vm Embedded virtual machine labels Sep 5, 2024
@zerbina zerbina added this to the MIR phase milestone Sep 5, 2024
@@ -2490,6 +2490,8 @@ proc constDataToMir*(env: var MirEnv, n: PNode): MirTree =
bu.use toFloatLiteral(env, n)
of nkStrLiterals:
bu.use strLiteral(env, n.strVal, typ)
of nkNimNodeLit:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay, nkNimNodeLit is becoming more pervasive, this means fewer places where AST is ambiguously passed around.

newNodeIT(nkEmpty, info, formal),
PAstDiag(kind: adCyclicTree, cyclic: n))
else:
result = newTreeIT(nkNimNodeLit, info, formal): n
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't see this as an issue now, and probably ever, but it looks like we're going to treat nkNimNodeLit to mean either a literal/static AST value or dynamic AST value. Where the latter one is generated on the fly, at compile time.

@saem
Copy link
Collaborator

saem commented Sep 5, 2024

/merge

Copy link

github-actions bot commented Sep 5, 2024

Merge requested by: @saem

Contents after the first section break of the PR description has been removed and preserved below:


@chore-runner chore-runner bot added this pull request to the merge queue Sep 5, 2024
Merged via the queue into nim-works:devel with commit 3955af7 Sep 5, 2024
35 checks passed
@zerbina zerbina deleted the mirgen-fix-nim-node-value-bugs branch September 11, 2024 00:29
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/backend Related to backend system of the compiler compiler/vm Embedded virtual machine
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compiler crash on compile-time NimNode variable as a static NimNode param to macros.
2 participants