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.55k stars 1.47k forks source link

using quote inside static block causes "Illegal storage access" error #18367

Open hamidb80 opened 3 years ago

hamidb80 commented 3 years ago

Example

static:
  discard quote:
    a and b

Current Output

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

Expected Output

compiles successfully

Additional Information

code on nim playground this code compiles successfully until nim version 1.2.12 the breaking change starts from nim 1.4.0 [same for version 1.5.1 (devel at this time)]

$ nim -v
Nim Compiler Version 1.5.1 [Windows: amd64]
Compiled at 2021-06-20
Copyright (c) 2006-2021 by Andreas Rumpf

active boot switches: -d:release
timotheecour commented 3 years ago

@hamidb80 did you forget import macros in your snippet?

when defined case4:
  import macros
  macro fn =
    let ret = quote: a
  fn()

when defined case5:
  import macros
  proc fn =
    let ret = quote: a
  static: fn()

-d:case4 works and is what should be used -d:case5 gives SIGSEGV:

/Users/timothee/git_clone/nim/Nim_prs/compiler/nim.nim(128) nim
/Users/timothee/git_clone/nim/Nim_prs/compiler/nim.nim(87) handleCmdLine
/Users/timothee/git_clone/nim/Nim_prs/compiler/main.nim(275) mainCommand
/Users/timothee/git_clone/nim/Nim_prs/compiler/main.nim(245) compileToBackend
/Users/timothee/git_clone/nim/Nim_prs/compiler/main.nim(101) commandCompileToC
/Users/timothee/git_clone/nim/Nim_prs/compiler/modules.nim(177) compileProject
/Users/timothee/git_clone/nim/Nim_prs/compiler/modules.nim(97) compileModule
/Users/timothee/git_clone/nim/Nim_prs/compiler/passes.nim(180) processModule
/Users/timothee/git_clone/nim/Nim_prs/compiler/passes.nim(73) processTopLevelStmt
/Users/timothee/git_clone/nim/Nim_prs/compiler/sem.nim(636) myProcess
/Users/timothee/git_clone/nim/Nim_prs/compiler/sem.nim(604) semStmtAndGenerateGenerics
/Users/timothee/git_clone/nim/Nim_prs/compiler/semstmts.nim(2315) semStmt
/Users/timothee/git_clone/nim/Nim_prs/compiler/semexprs.nim(1057) semExprNoType
/Users/timothee/git_clone/nim/Nim_prs/compiler/semexprs.nim(2942) semExpr /Users/timothee/git_clone/nim/timn/tests/nim/all/t12476.nim(5, 1) nkStmtList
/Users/timothee/git_clone/nim/Nim_prs/compiler/semstmts.nim(2257) semStmtList
/Users/timothee/git_clone/nim/Nim_prs/compiler/semexprs.nim(2869) semExpr /Users/timothee/git_clone/nim/timn/tests/nim/all/t12476.nim(30, 1) nkWhenStmt
/Users/timothee/git_clone/nim/Nim_prs/compiler/semexprs.nim(2361) semWhen
/Users/timothee/git_clone/nim/Nim_prs/compiler/semexprs.nim(2942) semExpr /Users/timothee/git_clone/nim/timn/tests/nim/all/t12476.nim(31, 3) nkStmtList
/Users/timothee/git_clone/nim/Nim_prs/compiler/semstmts.nim(2257) semStmtList
/Users/timothee/git_clone/nim/Nim_prs/compiler/semexprs.nim(2994) semExpr /Users/timothee/git_clone/nim/timn/tests/nim/all/t12476.nim(34, 3) nkStaticStmt
/Users/timothee/git_clone/nim/Nim_prs/compiler/semstmts.nim(2214) semStaticStmt
/Users/timothee/git_clone/nim/Nim_prs/compiler/vm.nim(2223) evalStaticStmt
/Users/timothee/git_clone/nim/Nim_prs/compiler/vm.nim(2212) evalConstExprAux
/Users/timothee/git_clone/nim/Nim_prs/compiler/vm.nim(1274) rawExecute
/Users/timothee/git_clone/nim/Nim_prs/compiler/evaltempl.nim(190) evalTemplate
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
hamidb80 commented 3 years ago

did you forget import macros in your snippet?

no, here's a part of issue template for bug report:

-d:case4 works and is what should be used -d:case5 gives SIGSEGV:

you mean I am allowed to use quote only inside a macro ? to be honest I couldn't find much information about static block in Nim manual.

but this code does work until Nim 1.2.12.

timotheecour commented 3 years ago

no, here's part of issue template for bug report:

but without import macros, I get: Error: undeclared identifier: 'quote' (as it should, sinec it's defined in macros). So import macros is needed here; ditto with 1.2

you mean I am allowed to use quote only inside a macro ?

there is a bug (it shouldn't SIGSEGV), I'm just telling you what you should use instead, regardless of that bug.

further investigation:

when true:
  import macros
  proc fn =
    let n = quote: a
  macro baz = fn()
  # baz() # ok
  static: fn() # SIGSEGV (unless you comment out baz())

marking as low priority because normal use wouldn't exhibit this bug