Compiler crash #2730

Closed
andreaferretti opened this Issue May 15, 2015 · 5 comments

Projects

None yet

4 participants

@andreaferretti
Contributor

The following will make the compiler crash (that is, no error is produced, nor any output file):

type
  Foo[a: static[int]] = array[a, int]
  Bar[a, b: static[int]] = array[b, Foo[a div 2]]

echo "done"

As far as I can tell, the same happens whenever one tries to use arithmetic on static[int], viewed as a type

@dom96 dom96 added the Static[T] label May 15, 2015
@andreaferretti
Contributor

After some more experiments, it turns out that arithmetic on static types is sometimes valid. The original example makes the compiler crash. The following produces an error (but no crash)

type Vect[N: static[int], A] = array[N, A]

proc push[N: static[int], A](a: Vect[N, A], x: A): Vect[N + 1, A] =
  for n in 0 .. < N:
    result[n] = a[n]
  result[N] = x

The following is valid

type
  Foo[a: static[int]] = array[a, int]
  Bar[a, b: static[int]] = array[b div 2, Foo[a]]

as well as this one

type Vect[N: static[int], A] = array[N, A]
proc plusOne(n: int): int = n + 1

proc push[N: static[int], A](a: Vect[N, A], x: A): Vect[plusOne(N), A] =
  for n in 0 .. < N:
    result[n] = a[n]
  result[N] = x
@gmfawcett

As mentioned on the forum, this seems to be (at least partly) an error-reporting bug, not a codegen bug. Changing line 1600 in vmgen.nim, and replacing internalError(n.info, "cannot generate code for: " & s.name.s) with globalError(...), produces a readable compilation error message for the original test code:

Error: cannot instantiate Foo
got: (float)
but expected: (a)
    Bar[a, b: static[int]] = array[b, Foo[a/2]]
@andreaferretti
Contributor

With an updated version of the compiler, the error reporting seems to work. Unfortunately, the previous workarounds do not work anymore

@Araq
Member
Araq commented Jul 30, 2015

Unfortunately, the previous workarounds do not work anymore

Well you need to give me your previous workarounds as test cases then. We already have lots of tests for the static_t mess, but it's never good enough to prevent regressions, apparently.

@andreaferretti
Contributor

Sorry, my fault. I was refering to the examples posted in my second post above that I claimed were working. I tried them yesterday, and I must have got something wrong, because I was under the impression they did not work anymore, but they do. Sorry for the confusion

The following (apparently similar) example, though, fails to compile

type Vect[N: static[int], A] = array[N, A]

proc plusTwo(x: int): int = x + 2

type
  Foo[a: static[int]] = Vect[a, int]
  Bar[a, b: static[int]] = Vect[a, Foo[plusTwo(b)]]

It is also worth noting that the compiler does not crash anymore on the original example (though it fails to compile it), so the bug title is now misleading

@Araq Araq added Crash and removed Crash labels May 31, 2016
@Araq Araq added a commit that closed this issue Jul 12, 2016
@Araq Araq fixes #2730 8dd27a6
@Araq Araq closed this in 8dd27a6 Jul 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment