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

Segfault, when assigning quote do to compileTime var NimNode #21222

Open
yuanweixin opened this issue Jan 3, 2023 · 7 comments
Open

Segfault, when assigning quote do to compileTime var NimNode #21222

yuanweixin opened this issue Jan 3, 2023 · 7 comments

Comments

@yuanweixin
Copy link

Description

Code

import std/macros
var x {.compileTime.} : NimNode = 
  quote do:
    echo "xxx"

The context is that I am trying to build up a compile-time list, containing data and NimNode (representing codeblocks), intended to be passed into a macro. But it seems directly assigning from quote do crashes the compiler (no compile error or warnings whatsoever).

Nim Version

Nim Compiler Version 1.6.8 [Linux: amd64]
Compiled at 2022-10-05
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: c9f46ca
active boot switches: -d:release

Current Output

...........................................................SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Segmentation fault

Expected Output

I expected this to compile. After all, I am using quote do in a compile-time context.

Possible Solution

No response

Additional Information

No response

@shirleyquirk
Copy link
Contributor

workaround (fix tbh, genast is a superior replacement for quote do):
use genast

import std/[macros,genasts]
var x{.compiletime.} : NimNode =
  genast():
    echo "xxx"

@yuanweixin
Copy link
Author

thanks! was growing frustrated that I can't just quote things and pass them around. at least, this gets me past that segfault.

i suppose this is more of a user experience thing, but i find that putting genAst inside a tuple results in Error: named expression not allowed here, so have to resort to assigning the genAst to a var and then using that var in the tuple.

@juancarlospaco
Copy link
Collaborator

!nim c

import std/macros
var x {.compileTime.} : NimNode =
  quote do:
    echo "xxx"

@github-actions
Copy link
Contributor

@juancarlospaco (contributor)

devel :-1: FAIL

Output

Error: Command failed: nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings:off --lineTrace:off  --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Stats

  • Created 2023-07-11T19:11:46Z
  • Started 2023-07-11T19:12:17
  • Finished 2023-07-11T19:12:18
  • Duration 1 minute
  • Commands nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim

IR

AST

nnkStmtList.newTree(
  nnkImportStmt.newTree(
    nnkInfix.newTree(
      newIdentNode("/"),
      newIdentNode("std"),
      newIdentNode("macros")
    )
  ),
  nnkVarSection.newTree(
    nnkIdentDefs.newTree(
      nnkPragmaExpr.newTree(
        newIdentNode("x"),
        nnkPragma.newTree(
          newIdentNode("compileTime")
        )
      ),
      newIdentNode("NimNode"),
      nnkCall.newTree(
        newIdentNode("quote"),
        nnkStmtList.newTree(
          nnkCommand.newTree(
            newIdentNode("echo"),
            newLit("xxx")
          )
        )
      )
    )
  )
)
stable :-1: FAIL

Output

Error: Command failed: nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings:off --lineTrace:off  --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Stats

  • Created 2023-07-11T19:11:46Z
  • Started 2023-07-11T19:12:18
  • Finished 2023-07-11T19:12:19
  • Duration 1 minute
  • Commands nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim

IR

AST

nnkStmtList.newTree(
  nnkImportStmt.newTree(
    nnkInfix.newTree(
      newIdentNode("/"),
      newIdentNode("std"),
      newIdentNode("macros")
    )
  ),
  nnkVarSection.newTree(
    nnkIdentDefs.newTree(
      nnkPragmaExpr.newTree(
        newIdentNode("x"),
        nnkPragma.newTree(
          newIdentNode("compileTime")
        )
      ),
      newIdentNode("NimNode"),
      nnkCall.newTree(
        newIdentNode("quote"),
        nnkStmtList.newTree(
          nnkCommand.newTree(
            newIdentNode("echo"),
            newLit("xxx")
          )
        )
      )
    )
  )
)
1.6.0 :-1: FAIL

Output

Error: Command failed: nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings:off --lineTrace:off  --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Stats

  • Created 2023-07-11T19:11:46Z
  • Started 2023-07-11T19:12:23
  • Finished 2023-07-11T19:12:23
  • Duration 1 minute
  • Commands nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim

IR

AST

nnkStmtList.newTree(
  nnkImportStmt.newTree(
    nnkInfix.newTree(
      newIdentNode("/"),
      newIdentNode("std"),
      newIdentNode("macros")
    )
  ),
  nnkVarSection.newTree(
    nnkIdentDefs.newTree(
      nnkPragmaExpr.newTree(
        newIdentNode("x"),
        nnkPragma.newTree(
          newIdentNode("compileTime")
        )
      ),
      newIdentNode("NimNode"),
      nnkCall.newTree(
        newIdentNode("quote"),
        nnkStmtList.newTree(
          nnkCommand.newTree(
            newIdentNode("echo"),
            newLit("xxx")
          )
        )
      )
    )
  )
)
1.4.0 :-1: FAIL

Output

Error: Command failed: nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings:off --lineTrace:off  --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Stats

  • Created 2023-07-11T19:11:46Z
  • Started 2023-07-11T19:12:27
  • Finished 2023-07-11T19:12:28
  • Duration now
  • Commands nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim

IR

AST

nnkStmtList.newTree(
  nnkImportStmt.newTree(
    nnkInfix.newTree(
      newIdentNode("/"),
      newIdentNode("std"),
      newIdentNode("macros")
    )
  ),
  nnkVarSection.newTree(
    nnkIdentDefs.newTree(
      nnkPragmaExpr.newTree(
        newIdentNode("x"),
        nnkPragma.newTree(
          newIdentNode("compileTime")
        )
      ),
      newIdentNode("NimNode"),
      nnkCall.newTree(
        newIdentNode("quote"),
        nnkStmtList.newTree(
          nnkCommand.newTree(
            newIdentNode("echo"),
            newLit("xxx")
          )
        )
      )
    )
  )
)
1.2.0 :+1: OK

Output


Stats

  • Created 2023-07-11T19:11:46Z
  • Started 2023-07-11T19:12:43
  • Finished 2023-07-11T19:12:44
  • Duration 1 minute
  • Commands nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
1.0.0 :+1: OK

Output


Stats

  • Created 2023-07-11T19:11:46Z
  • Started 2023-07-11T19:12:57
  • Finished 2023-07-11T19:12:58
  • Duration 1 minute
  • Commands nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
0.20.2 :+1: OK

Output


Stats

  • Created 2023-07-11T19:11:46Z
  • Started 2023-07-11T19:13:08
  • Finished 2023-07-11T19:13:09
  • Duration 1 minute
  • Commands nim c --run -d:strip -d:ssl -d:nimDisableCertificateValidation --forceBuild:on --colors:off --threads:off --verbosity:0 --hints:off --warnings:off --lineTrace:off --nimcache:/home/runner/work/Nim/Nim --out:/home/runner/work/Nim/Nim/temp /home/runner/work/Nim/Nim/temp.nim
#2629d619a ➡️ 🐛

Diagnostics

Clyybber introduced a bug at 2020-07-29 16:17:20 +0200 on commit #2629d619a with message:

Fix forward declaration issues in template/macro context (#15091)

* Fix forward declaration issues in template/macro context

* Correct forward declaration resolving for overloads

* Remove old dead code

* WIP consistent gensym ids

* Minimize diff

* Remove obsoleted hack

* Add templInstCounter to give unique IDs to template instantiations

* Remove obsoleted code

* Eh, init in myOpen, not myProcess...

* Remove optNimV019

* Add testcase for #13484

The bug is in the files:

changelog.md
compiler/commands.nim
compiler/evaltempl.nim
compiler/lookups.nim
compiler/options.nim
compiler/procfind.nim
compiler/sem.nim
compiler/semdata.nim
compiler/semexprs.nim
compiler/semstmts.nim
compiler/semtempl.nim
compiler/types.nim
compiler/vm.nim
compiler/vmdef.nim
testament/important_packages.nim
tests/macros/tmacros_issues.nim
tests/template/tparams_gensymed.nim

The bug can be in the commits:

(Diagnostics sometimes off-by-one).

🤖 Bug found in 46 minutes bisecting 4387 commits at 95 commits per second.

@ringabout
Copy link
Member

Similar case: #22282 (comment)

import std/macros

proc f(): NimNode =
  quote do:
    123

var a = static f()

@Luyten-Orion
Copy link

import std/macros
var myNode {.compileTime.} = newStmtList()
macro test(n: static NimNode) = discard
myNode.test()

This is enough to make the compiler segfault, too.

@ringabout
Copy link
Member

related to #18367

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Maybe
Development

No branches or pull requests

5 participants