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

template-generating templates are not exported #13828

Open
mfiano opened this issue Apr 1, 2020 · 4 comments · May be fixed by #23120
Open

template-generating templates are not exported #13828

mfiano opened this issue Apr 1, 2020 · 4 comments · May be fixed by #23120

Comments

@mfiano
Copy link

mfiano commented Apr 1, 2020

Example

import macros

type
  Storage[N: static[int]] = array[N, float32]
  Quat* = object
    data: Storage[4]
  
proc `[]`(q: Quat, index: int): float32 = q.data[index]
proc `[]=`(q: var Quat, index: int, value: float32) = q.data[index] = value

template genAccessor(t, a, i): untyped =
  template a*(q: t): float32 {.inject.} = q[i]
  template `a=`*(q: var t, value: float32) {.inject.} = q[i] = value

genAccessor Quat, w, 0
genAccessor Quat, x, 1
genAccessor Quat, y, 2
expandMacros:
  genAccessor Quat, z, 3

var a = Quat()
a.data = [1f,2,3,4]
a.x = 42.0
echo a

Current Output

Compiler output from expandMacros:

template z(q`gensym141121: Quat): float32 {.inject.} =
  q`gensym141121[3]

template z=(q`gensym141122: var Quat; value`gensym141123: float32) {.inject.} =
  []=(q`gensym141122, 3, value`gensym141123)

The generated templates are not usable outside of the current module. Making them procs instead preserves the export token.

Expected Output

template z*(q`gensym141121: Quat): float32 {.inject.} =
  q`gensym141121[3]

template `z=`*(q`gensym141122: var Quat; value`gensym141123: float32) {.inject.} =
  []=(q`gensym141122, 3, value`gensym141123)

Possible Solution

?

Additional Information

$ nim -v
Nim Compiler Version 1.0.6 [Linux: amd64]
Compiled at 2020-02-08
Copyright (c) 2006-2019 by Andreas Rumpf
@Varriount
Copy link
Contributor

Note that this bug can be worked around with the (undocumented) dirty pragma.

@Araq
Copy link
Member

Araq commented Apr 1, 2020

It's documented here https://nim-lang.org/docs/manual.html#templates-hygiene-in-templates

I opened the manual, searched for "dirty" and found it immediately. Maybe the next time you can do the same, thanks.

@Varriount
Copy link
Contributor

Varriount commented Apr 2, 2020

I'm aware of the mention in the manual. Unfortunately that's it though - it's not listed in the pragmas section, not does it have even a code block.

@Araq
Copy link
Member

Araq commented Apr 2, 2020

That only means that the docs are lacking, not that it's "undocumented".

metagn added a commit to metagn/Nim that referenced this issue Jun 23, 2024
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.

3 participants