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

Compiler bug when combining object variants and case switch #19833

Open
AurelienSaussay opened this issue May 26, 2022 · 2 comments · May be fixed by #23516
Open

Compiler bug when combining object variants and case switch #19833

AurelienSaussay opened this issue May 26, 2022 · 2 comments · May be fixed by #23516
Labels
refc refc issues

Comments

@AurelienSaussay
Copy link

AurelienSaussay commented May 26, 2022

I am using a custom AST for a mathematical modeling application. This led me to a compiler bug involving object variants and case switch.

The bug can be reproduced from the following minimal example below. The C code output from this example doesn't compile. Interestingly, it does compile if I remove if n.lag < 0: s.append(&"({$n.lag})")

Example

import strformat

type
  NodeKind* = enum
    nkBinary,
    nkVariable,
    nkExpression
  CompiledNode* = ref CompileNodeObj
  CompileNodeObj = object
    case kind*: NodeKind
    of nkExpression:
      node*: CompiledNode
    of nkBinary:
      op*: string
      lhs*: CompiledNode
      rhs*: CompiledNode
    of nkVariable:
      root*: string
      subscripts*: seq[string]
      lag*: int
      
proc append(s: var string, s2: string): var string {.discardable.} = s.add(s2); return s

proc tmpl*(s: var string, n: CompiledNode): var string {.discardable.} =
  case n.kind:
  of nkBinary: s.tmpl(n.lhs).append(&" {n.op} ").tmpl(n.rhs)
  of nkExpression: s.tmpl(n.node)
  of nkVariable: 
    s.append(n.root)
    if n.lag < 0: s.append(&"({$n.lag})")
  return s

var n = CompiledNode(kind: nkBinary, op: "+", lhs: CompiledNode(kind: nkVariable, root: "a"), rhs: CompiledNode(kind: nkVariable, root: "b"))
var s: string
echo s.tmpl(n)

Current Output

@mminimal.nim.c:495:30: error: use of undeclared identifier 'T4_'; did you mean 'T6_'?
                unsureAsgnRef((void**) (&(*T4_)), copyString((*T6_)));
                                           ^~~
                                           T6_
@mminimal.nim.c:490:19: note: 'T6_' declared here
                NimStringDesc** T6_;
                                ^
1 error generated.

Additional information

Inspecting mminimal.nim.c reveals that the C code that fails to compile was emitted by the corresponding Nim line of nkExpression: s.tmpl(n.node).

$ nim -v
Nim Compiler Version 1.6.4 [MacOSX: amd64]
(also fails on WIndows 10: x86-64)
@metagn
Copy link
Collaborator

metagn commented May 29, 2022

Works when using discard on the tmpl call in nkExpression.

@shirleyquirk
Copy link
Contributor

shirleyquirk commented Dec 9, 2023

works with --mm:orc, so in 2.0 and devel. still doesn't work with --mm:refc

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

Successfully merging a pull request may close this issue.

4 participants