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

Error: internal error: expr: var not init sevColor_994035 #8573

Closed
copygirl opened this issue Aug 8, 2018 · 4 comments
Closed

Error: internal error: expr: var not init sevColor_994035 #8573

copygirl opened this issue Aug 8, 2018 · 4 comments

Comments

@copygirl
Copy link

copygirl commented Aug 8, 2018

The following code produces the compiler error:

util/log.nim(24, 24) Error: internal error: expr: var not init sevColor_994035
import
  macros,
  strutils,
  terminal

type LogSeverity* = enum
  sevError = "Error"
  sevWarn  = "Warn"
  sevInfo  = "Info"
  sevDebug = "Debug"

macro log*(severity: static[LogSeverity], group: static[string], m: varargs[typed]): untyped =
  let sevStr   = align("[" & toUpperAscii($severity) & "] ", 8)
  let sevColor = case severity
    of sevError: fgRed
    of sevWarn:  fgYellow
    of sevInfo:  fgWhite
    of sevDebug: fgBlack
  
  let groupStr = "[" & $group & "] "
  
  result = quote do:
    setStyle({ styleBright })
    setForegroundColor(sevColor) # <==
    write(stdout, sevStr)
    
    setStyle({ styleDim })
    setForegroundColor(fgWhite)
    write(stdout, groupStr)
  
  let wl = newCall(bindSym"styledWriteLine", bindSym"stdout")
  for arg in m: wl.add(arg)
  result.add(wl)
@copygirl
Copy link
Author

copygirl commented Aug 8, 2018

I suppose it's just lacking a proper error message and I didn't understand the power of quote correctly. If I write the following, it compiles fine:

  result = quote do:
    setStyle({ styleBright })
    setForegroundColor(ForegroundColor(`sevColor`))
    write(stdout, `sevStr`)
    
    setStyle({ styleDim })
    setForegroundColor(fgWhite)
    write(stdout, `groupStr`)

@LemonBoy
Copy link
Contributor

LemonBoy commented Aug 8, 2018

See #8574

@copygirl
Copy link
Author

copygirl commented Aug 8, 2018

Though, I wonder... Wouldn't it be nice if this was how quote worked in the first place? Or is there a reason why we have to use the backticks?

@mratsim
Copy link
Collaborator

mratsim commented Aug 9, 2018

Yes, to show intentional interpolation. The compile-time scope shouldn't leak into the run-time scope.
For example, the result variable of the macro shouldn't leak in the result variable of a proc you are building like #7791.

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

4 participants