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.22k stars 1.46k forks source link

Compiler SIGSEGV with generic term rewriting template #6411

Open ghost opened 6 years ago

ghost commented 6 years ago

This code:

template rewriteMinusOne{(a = a + 1)|(a += 1)}[T](a: T): untyped = 
  a -= T(1)

var a = 3

a = a + 1
echo a
a += 1
echo a

crashes the compiler. Traceback:

nim.nim(121)             nim
nim.nim(77)              handleCmdLine
main.nim(163)            mainCommand
main.nim(74)             commandCompileToC
modules.nim(240)         compileProject
modules.nim(180)         compileModule
passes.nim(217)          processModule
passes.nim(135)          processTopLevelStmt
sem.nim(550)             myProcess
sem.nim(531)             semStmtAndGenerateGenerics
hlo.nim(103)             hloStmt
hlo.nim(75)              hlo
hlo.nim(56)              applyPatterns
hlo.nim(27)              evalPattern
semexprs.nim(18)         semTemplateExpr
evaltempl.nim(157)       evalTemplate
evaltempl.nim(59)        evalTemplateAux
evaltempl.nim(59)        evalTemplateAux
evaltempl.nim(43)        evalTemplateAux
system.nim(2737)         sysFatal
Error: unhandled exception: index out of bounds [IndexError]

I know that this snippet is stupid and doesn't make any sense. However it works with a -= 1 instead of a -= T(1)

krux02 commented 4 years ago

The orror occurs in compiler/evaltempl.nim(44): index 1 not in 0 .. 0. @Araq, to fix it, I request some documentation for evalTemplateAux. What should this function do precisely? What are all the arguments? Also a better documentation for the fields in TemplCtx would be nice.

ghost commented 4 years ago

With latest devel it produces an error instead:

/home/dian/Projects/nimforum/kk.nim(6, 3) template/generic instantiation of `rewriteMinusOne` from here
/home/dian/Projects/nimforum/kk.nim(1, 54) Error: undeclared identifier: 'T'
narimiran commented 3 years ago

With latest devel it produces an error instead:

July 2020 update:

Error: unhandled exception: index 1 not in 0 .. 0 [IndexDefect]

which is the same as reported originally.

bung87 commented 1 year ago

in 1.6.8 Error: unhandled exception: index 1 not in 0 .. 0 [IndexDefect]

think this can be close ?

ringabout commented 1 year ago

think this can be close ?

No, it is still a crash.

bung87 commented 1 year ago

what's the expected, I dont think localError == crash