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

Templates: Crash with gensym'ed proc & method call #20002

Closed
metagn opened this issue Jul 11, 2022 · 0 comments · Fixed by #20004
Closed

Templates: Crash with gensym'ed proc & method call #20002

metagn opened this issue Jul 11, 2022 · 0 comments · Fixed by #20004

Comments

@metagn
Copy link
Collaborator

metagn commented Jul 11, 2022

Example

proc bar(x: int): int = 10
template foo =
  proc bar(x: int): int {.gensym.} = x + 2
  echo bar(3)
block:
  foo()
proc bar(x: int): int = 10
template foo =
  proc bar(x: int): int {.gensym.} = x + 2
  echo 3.bar
block:
  foo()

Current Output

5
compiler\nim.nim(143) nim
compiler\nim.nim(98) handleCmdLine
compiler\main.nim(284) mainCommand
compiler\main.nim(254) compileToBackend
compiler\main.nim(110) commandCompileToC
compiler\modules.nim(181) compileProject
compiler\modules.nim(101) compileModule
compiler\passes.nim(184) processModule
compiler\passes.nim(76) processTopLevelStmt
compiler\sem.nim(653) myProcess
compiler\sem.nim(621) semStmtAndGenerateGenerics
compiler\semstmts.nim(2429) semStmt
compiler\semexprs.nim(1066) semExprNoType
compiler\semexprs.nim(3026) semExpr
compiler\semstmts.nim(2371) semStmtList
compiler\semexprs.nim(3025) semExpr
compiler\semexprs.nim(2602) semBlock
compiler\semexprs.nim(3026) semExpr
compiler\semstmts.nim(2371) semStmtList
compiler\semexprs.nim(2908) semExpr
compiler\semexprs.nim(1048) semDirectOp
compiler\semexprs.nim(931) afterCallActions
compiler\semexprs.nim(39) semTemplateExpr
compiler\sem.nim(430) semAfterMacroCall
compiler\semstmts.nim(2429) semStmt
compiler\semexprs.nim(1066) semExprNoType
compiler\semexprs.nim(3026) semExpr
compiler\semstmts.nim(2371) semStmtList
compiler\semexprs.nim(2922) semExpr
compiler\semexprs.nim(2397) semMagic
compiler\semexprs.nim(1047) semDirectOp
compiler\semexprs.nim(878) semOverloadedCallAnalyseEffects
compiler\semcall.nim(588) semOverloadedCall
compiler\semcall.nim(371) resolveOverloads
compiler\semcall.nim(95) pickBestCandidate
compiler\sigmatch.nim(2574) matches
compiler\sigmatch.nim(2507) matchesAux
compiler\sigmatch.nim(2303) prepareOperand
compiler\semexprs.nim(54) semOperand
compiler\semexprs.nim(2876) semExpr
compiler\semexprs.nim(1491) semFieldAccess
compiler\semexprs.nim(1407) builtinFieldAccess
compiler\lookups.nim(55) considerQuotedIdent
lib\system\fatal.nim(53) sysFatal
Error: unhandled exception: index out of bounds, the container is empty [IndexDefect]

Expected Output

5
10?

Possible Solution

Change s to a in this line:

if a.kind != skModule and (not isField or sfGenSym notin s.flags):

This makes the crashing code output 10. Did not test for other consequences.

Additional Information

$ nim -v
Nim Compiler Version 1.7.1 [Windows: amd64]
Compiled at 2022-07-11
Copyright (c) 2006-2022 by Andreas Rumpf

git hash: 8bfc396a4dfa722239818f399a119452a53fe07f
@metagn metagn changed the title Templates: Crash with gensym & method call Templates: Crash with gensym'ed proc & method call Jul 11, 2022
metagn added a commit to metagn/Nim that referenced this issue Jul 11, 2022
metagn added a commit to metagn/Nim that referenced this issue Jul 11, 2022
While there is nothing wrong with this fix,
this unlocks the hidden behavior of method calls not being able to call
gensym'ed procs in templates.
metagn added a commit to metagn/Nim that referenced this issue Jul 11, 2022
While this fix seems innocent,
this unlocks the hidden behavior of
method calls not being able to call
gensym'ed routines inside templates.
@metagn metagn mentioned this issue Jul 11, 2022
Araq pushed a commit that referenced this issue Jul 15, 2022
While this fix seems innocent,
this unlocks the hidden behavior of
method calls not being able to call
gensym'ed routines inside templates.
FedericoCeratto pushed a commit to FedericoCeratto/Nim that referenced this issue Jul 30, 2022
While this fix seems innocent,
this unlocks the hidden behavior of
method calls not being able to call
gensym'ed routines inside templates.
capocasa pushed a commit to capocasa/Nim that referenced this issue Mar 31, 2023
While this fix seems innocent,
this unlocks the hidden behavior of
method calls not being able to call
gensym'ed routines inside templates.
narimiran pushed a commit that referenced this issue Apr 24, 2023
While this fix seems innocent,
this unlocks the hidden behavior of
method calls not being able to call
gensym'ed routines inside templates.

(cherry picked from commit f35c9cf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant