fusion icon indicating copy to clipboard operation
fusion copied to clipboard

Compiler crashes when else match NimNode

Open levovix0 opened this issue 4 years ago • 3 comments

import macros, fusion/matching

{.experimental: "caseStmtMacros".}

case newLit 0
else: discard

current output

Nim/compiler/nim.nim(138) nim
Nim/compiler/nim.nim(94) handleCmdLine
Nim/compiler/main.nim(275) mainCommand
Nim/compiler/main.nim(245) compileToBackend
Nim/compiler/main.nim(101) commandCompileToC
Nim/compiler/modules.nim(178) compileProject
Nim/compiler/modules.nim(98) compileModule
Nim/compiler/passes.nim(180) processModule
Nim/compiler/passes.nim(73) processTopLevelStmt
Nim/compiler/sem.nim(653) myProcess
Nim/compiler/sem.nim(639) semStmtAndGenerateGenerics
Nim/compiler/sempass2.nim(1514) trackStmt
Nim/compiler/sempass2.nim(1089) track
Nim/compiler/sempass2.nim(735) trackBlock
Nim/compiler/sempass2.nim(1088) track
Nim/compiler/sempass2.nim(707) trackIf
Nim/lib/system/fatal.nim(53) sysFatal
Error: unhandled exception: index 1 not in 0 .. 0 [IndexDefect]

(stack trace from nim compiler, compiled without -d:release)

expected output compiles and do nothing

levovix0 avatar Oct 10 '21 10:10 levovix0

Compiling in static section gives expected results (no action/crash at compile-time). In general NimNode cannot be used at runtime, so I suspect it is a compiler bug instead, and has nothing to do with pattern matching itself (except it being able to trigger sempass failure. Just using newLit(0).kind fails with "kind can only be used in compile-time context")

import macros, fusion/matching

{.experimental: "caseStmtMacros".}

static:
  case newLit(0):
    else: 
      discard

^ Simply compiles with no error, outputs "executed else"

haxscramper avatar Oct 10 '21 11:10 haxscramper

but in macros it gives same error

import macros, fusion/matching

{.experimental: "caseStmtMacros".}

macro m =
  case newLit 0
  else: discard

m()

levovix0 avatar Oct 10 '21 11:10 levovix0

Fixed in #94 along with your example added to test suite.

haxscramper avatar Oct 10 '21 11:10 haxscramper