nim-lang / nimsuggest

idetools for the nim language
MIT License
42 stars 9 forks source link

CacheTable key already exists error #107

Open zedeus opened 5 years ago

zedeus commented 5 years ago

Using a macrocache.CacheTable results in a key already exists error. Example:

import jester

routes:
  get "/":
    resp "Hello"
chk skUnknown       Error   /tmp/jest.nim   3   0   "template/generic instantiation from here"  0
chk skUnknown       Error   /home/zed/.nimble/pkgs/jester-0.4.1/jester.nim  1133    15  "key already exists: match" 0

Minimal example:

import macros, macrocache

const cache = CacheTable"routes"

proc routesEx(name: string, body: NimNode) =
  cache[name] = body.copyNimTree

macro routes(body: untyped): untyped =
  routesEx("match", body)

routes:
  test
chk skUnknown       Error   /tmp/error.nim  11  0   "template/generic instantiation from here"  0
chk skUnknown       Error   /tmp/error.nim  6   7   "key already exists: match" 0

It's not related to the NimNode argument, this throws same error:

import macros, macrocache

const cache = CacheTable"routes"

proc routesEx(name: string) =
  cache[name] = newNimNode(nnkEmpty)

macro routes(body: untyped): untyped =
  routesEx("match")

routes:
  test

When not called from inside a macro, the error isn't triggered:

import macros, macrocache

const cache = CacheTable"routes"

proc routesEx(name: string) =
  cache[name] = newNimNode(nnkEmpty)

routesEx("match")
ghost commented 4 years ago

Still an issue:

~/P/stuff ❯❯❯ nimsuggest --stdin --debug  b.nim
Hint: used config file '/home/dian/Things/Nim/config/nim.cfg' [Conf]
Hint: used config file '/home/dian/Things/Nim/config/config.nims' [Conf]
Hint: used config file '/home/dian/nim.cfg' [Conf]
......usage: sug|con|def|use|dus|chk|mod|highlight|outline|known|project file.nim[;dirtyfile.nim]:line:col
type 'quit' to quit
type 'debug' to toggle debug mode on/off
type 'terse' to toggle terse mode on/off
> chk b.nim
.chk    skUnknown               Hint    ???     0       -1      "b [Processing]"        0
chk     skUnknown               Hint    /home/dian/Projects/stuff/b.nim 11      0       "template/generic instantiation from here"      0
chk     skUnknown               Error   /home/dian/Projects/stuff/b.nim 6       7       "key already exists: match"     0
saem commented 3 years ago

If the macro is storing information across runs, then it'll run into issues. I suspect that this might extend beyond nimsuggest and also be an issue for any incremental complication or caching compilation. If there is cross run state it might be necessary that such macros need to account for being called repeatedly or be compile lifecycle aware as they're no longer idempotent.

Araq commented 3 years ago

If the macro is storing information across runs, then it'll run into issues.

Exactly. Which is why we created the "macrocache" API.