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

Compiler crash when accessing constant in nested template #5235

Closed
euantorano opened this issue Jan 17, 2017 · 3 comments

Comments

Projects
None yet
4 participants
@euantorano
Copy link
Contributor

commented Jan 17, 2017

The following code causes the compiler to crash with SIGSEGV: Illegal storage access. (Attempt to read from nil?):

template outer(body: untyped) =
  template test(val: string) =
    const SomeConst: string = val
    echo SomeConst

  body

outer:
  test("Hello, World!")

The output is:

nim c test.nim
Hint: used config file '/Users/euan/nim/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: test [Processing]
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

If I remove the line echo SomeConst from within the test template, the code compiles fine (though the SomeConst is not declared and cannot be accessed in the outer template).

@stisa

This comment has been minimized.

Copy link
Contributor

commented Jan 18, 2017

This compiles:

template outer(body: untyped) =
  template test(val: string) {.dirty}  =
    const SomeConst: string = val
    echo SomeConst
  body

outer:
  test("Hello, World!")

Note the {.dirty} pragma in the inner template.

Nim templates by default don't allow accessing identifiers outside the template that defines them ( see http://nim-lang.org/docs/manual.html#templates-hygiene-in-templates ).

As I understand it, body in the outer template tries to access SomeConst, which is only defined inside test and so it fails.

An alternative if you only need SomeConst in the outer scope would be to inject it:

template outer(body: untyped) =
  template test(val: string)  =
    const SomeConst {.inject} : string = val
    echo SomeConst
  body
@euantorano

This comment has been minimized.

Copy link
Contributor Author

commented Jan 18, 2017

Ah, thanks @stisa. It probably shouldn't crash the compiler though and should show an error instead of a SIGSEV.

@Araq Araq added the Crash label Jan 18, 2017

@genotrance

This comment has been minimized.

Copy link
Contributor

commented May 7, 2018

Still crashes with #head

-------- SNIPPET --------
template outer(body: untyped) =
  template test(val: string) =
    const SomeConst: string = val
    echo SomeConst

  body

outer:
  test("Hello, World!")
-------------------------

-------- OUTPUT --------
Hint: used config file 'c:\users\gt\Desktop\DL\programming\nimdevel\config\nim.cfg' [Conf]
Hint: system [Processing]
Hint: temp [Processing]
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
------------------------

-------- NIMTEMP --------
Hint: used config file 'c:\users\gt\Desktop\DL\programming\nimdevel\config\nim.cfg' [Conf]
Hint: system [Processing]
Hint: temp [Processing]
Traceback (most recent call last)
nim.nim(113)             nim
nim.nim(77)              handleCmdLine
main.nim(173)            mainCommand
main.nim(77)             commandCompileToC
modules.nim(137)         compileProject
modules.nim(77)          compileModule
passes.nim(236)          processModule
passes.nim(125)          processTopLevelStmt
sem.nim(577)             myProcess
sem.nim(545)             semStmtAndGenerateGenerics
semstmts.nim(1862)       semStmt
semexprs.nim(803)        semExprNoType
semexprs.nim(2296)       semExpr
semexprs.nim(787)        semDirectOp
semexprs.nim(684)        afterCallActions
semexprs.nim(19)         semTemplateExpr
sem.nim(398)             semAfterMacroCall
semstmts.nim(1862)       semStmt
semexprs.nim(803)        semExprNoType
semexprs.nim(2404)       semExpr
semstmts.nim(1803)       semStmtList
semexprs.nim(2404)       semExpr
semstmts.nim(1803)       semStmtList
semexprs.nim(2296)       semExpr
semexprs.nim(787)        semDirectOp
semexprs.nim(684)        afterCallActions
semexprs.nim(19)         semTemplateExpr
sem.nim(398)             semAfterMacroCall
semstmts.nim(1862)       semStmt
semexprs.nim(803)        semExprNoType
semexprs.nim(2404)       semExpr
semstmts.nim(1803)       semStmtList
semexprs.nim(2311)       semExpr
semexprs.nim(1941)       semMagic
semexprs.nim(786)        semDirectOp
semexprs.nim(639)        semOverloadedCallAnalyseEffects
semcall.nim(433)         semOverloadedCall
semcall.nim(257)         resolveOverloads
semcall.nim(93)          pickBestCandidate
sigmatch.nim(2317)       matches
sigmatch.nim(2272)       matchesAux
sigmatch.nim(2106)       prepareOperand
semexprs.nim(26)         semOperand
semexprs.nim(2224)       semExpr
semexprs.nim(942)        semSym
ast.nim(1399)            skipTypes
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
-------------------------

-------- VERSION --------
Nim Compiler Version 0.18.1 [Windows: i386]
Compiled at 2018-05-06 23:11:03
Copyright (c) 2006-2018 by Andreas Rumpf

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

@narimiran narimiran closed this in 4f989b9 May 29, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.