nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.54k stars 1.47k forks source link

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

Open yuanweixin opened 1 year ago

yuanweixin commented 1 year ago

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: c9f46ca8c9eeca8b5f68591b1abe14b962f80a4c 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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

!nim c

import std/macros
var x {.compileTime.} : NimNode =
  quote do:
    echo "xxx"
github-actions[bot] commented 1 year ago

@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

```cpp ```

AST

```nim 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

```cpp ```

AST

```nim 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

```cpp ```

AST

```nim 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

```cpp ```

AST

```nim 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 :arrow_right: :bug:

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).

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

ringabout commented 7 months ago

Similar case: https://github.com/nim-lang/Nim/issues/22282#issuecomment-1636644896

import std/macros

proc f(): NimNode =
  quote do:
    123

var a = static f()
Luyten-Orion commented 1 month ago
import std/macros
var myNode {.compileTime.} = newStmtList()
macro test(n: static NimNode) = discard
myNode.test()

This is enough to make the compiler segfault, too.