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

Regression involving templates instantiated within generics #2215

Closed
MasonMcGill opened this issue Feb 25, 2015 · 0 comments
Closed

Regression involving templates instantiated within generics #2215

MasonMcGill opened this issue Feb 25, 2015 · 0 comments

Comments

@MasonMcGill
Copy link

The following code compiles with compiler versions before commit 34b4e9f, and fails with later versions:

import macros

template forStatic(index: expr, slice: Slice[int], predicate: stmt):
                   stmt {.immediate.} =
  const a = slice.a
  const b = slice.b
  when a <= b:
    template iteration(i: int) =
      block:
        const index = i
        predicate
    template iterateStartingFrom(i: int): stmt =
      when i <= b:
        iteration i
        iterateStartingFrom i + 1
    iterateStartingFrom a

proc concreteProc(x: int) =
  forStatic i, 0..3:
    echo i

proc genericProc(x: any) =
  forStatic i, 0..3:
    echo i

concreteProc(7) # This works
genericProc(7)  # This doesn't compile

The error message:

test.nim(28, 11) Info: template/generic instantiation from here
test.nim(17, 4) Info: template/generic instantiation from here
test.nim(15, 8) Info: template/generic instantiation from here
test.nim(11, 22) Error: cannot evaluate at compile time: x

However, variations on this code can cause

  • Symbols that are not integers being converted to type int.
  • Internal errors in genRecordField.

Use-case/significance: All of my current projects use some version of the forStatic snippet for compile-time iteration. It's the most straightforward approach to manipulating elements of generic arrays/tuples without actually generating loops (which may be too expensive in some situations, e.g. transforming tensor indices).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants