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

static param in macro crashes nim and with no error shown (nim gags internalAssert errors!) #18278

Open
timotheecour opened this issue Jun 16, 2021 · 1 comment

Comments

@timotheecour
Copy link
Member

timotheecour commented Jun 16, 2021

there are multiple bugs involved here:

  • crash sholdn't happen
  • nim gags errors even fatal ones (errInternal) generated from internalAssert(c.config, c.prc.regInfo[dest].kind < slotSomeTemp) which calls internalErrorImpl ; the error is gagged because we're inside tryConstExpr but we not gag and crash at the same time

Example

after a painful reduction from CI failure in #18274 I obtained this:

when true: # D20210616T031729
  import macros

  # works without static
  proc `&`*(a1: static float, a2: string, a3: float) = discard

  macro fmt2*(): untyped =
    let res = genSym(nskVar, "bar")
    result = quote do:
      var `res` = "zook"
      `res`
  # this would work:
  # template fmt3(pattern: float): untyped = (var ret = "zok"; ret)
  proc main =
    var zook = fmt2() & "a2" & "a3"

Current Output

nim crashes, no error shown (even though it went through normal error handling path as shown in stacktrace, ie reached handleError):
stacktrace with in nim debug mode: https://gist.github.com/timotheecour/081237875166325b692a0fbc6af6f977

Expected Output

works

Example 2

ditto

looks like the problem happens when a static param is in same position as a var (temporary?) and sigmatch tries to match it via tryConstExpr and fails

when defined case9c:
  import macros
  proc add2(a, b: string): string =
    result = ""
    result.add a
    result.add b
  proc add2(a1: static float, a2: string, a3: float) = discard
  # proc add2(x0: float, a1: static float, a2: string, a3: float) = discard # would be no bug
  macro fmt2*(): untyped =
    let res = genSym(nskVar, "bar")
    result = quote do:
      var `res` = "zook"
      `res`
  proc main =
    var zook = add2(add2(fmt2(), "a2"), "a3")

Example 3

when defined case9e:
  proc add2(a, b: string): string = discard
  proc add2(a1: static float, a2: string, a3: float) = discard

  when defined case9e_1:
    import macros
    macro fmt2*(): untyped =
      let res = genSym(nskVar, "bar")
      result = quote do:
        var `res` = "zook"
        `res`

  when defined case9e_2:
    template fmt2(): untyped =
      var res = "zook"
      res

  proc main =
    const x = add2(fmt2(), "a2")

with case9e_1:
/Users/timothee/git_clone/nim/timn/tests/nim/all/t12411.nim(11, 1) template/generic instantiation from here
/Users/timothee/git_clone/nim/timn/tests/nim/all/t12411.nim(11, 1) template/generic instantiation from here
Error: internal error: /Users/timothee/git_clone/nim/Nim_devel/compiler/vmgen.nim(1665, 23)
=> same error but not gagged

with case9e_2:
Error: cannot evaluate at compile time: res`gensym0
var res = "zook"

Additional Information

1.5.1 e80d7ff
CI in #18274 failed because of this

after debugging, conf.m.errorOutputs is {}, which gags the errors in msgs.nim (eg styledMsgWriteln)

it has vibes of #18275 (and in fact i hit both bugs with that simpler change); both might be related

@timotheecour
Copy link
Member Author

timotheecour commented Jun 17, 2021

#18290 fixes the "don't gag fatal msgs part", and #18282 improves stacktraces, but the underlying bug remains

timotheecour added a commit to timotheecour/Nim that referenced this issue Jun 17, 2021
Araq pushed a commit that referenced this issue Jun 18, 2021
PMunch pushed a commit to PMunch/Nim that referenced this issue Mar 28, 2022
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

1 participant