Skip to content

Commit 5abd21d

Browse files
authored
fixes #25123; fixes #11862; Case object from compileTime proc unable to be passed as static param (#25224)
fixes #25123; fixes #11862 follow up #24442 ref #24441 > To fix this, fields from inactive branches are now detected in semmacrosanity.annotateType (called in fixupTypeAfterEval) and marked to prevent the codegen of their assignments. In #24441 these fields were excluded from the resulting node, but this causes issues when the node is directly supposed to go back into the VM, for example as const values. I don't know if this is the only case where this happens, so I wasn't sure about how to keep that implementation working. Object variants fields coming from inactive branches from VM are now flagged `nfPreventCg`. We can ignore them, as done by the C backends.
1 parent f009ea6 commit 5abd21d

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

compiler/semobjconstr.nim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ proc locateFieldInInitExpr(c: PContext, field: PSym, initExpr: PNode): PNode =
6868
let assignment = initExpr[i]
6969
if assignment.kind != nkExprColonExpr:
7070
invalidObjConstr(c, assignment)
71+
elif nfPreventCg in assignment.flags:
72+
# this is an object constructor node generated by the VM and
73+
# this field is in an inactive case branch, just ignore it
74+
discard
7175
elif fieldId == considerQuotedIdent(c, assignment[0]).id:
7276
return assignment
7377

@@ -515,6 +519,10 @@ proc semObjConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType: PType
515519
invalidObjConstr(c, field)
516520
hasError = true
517521
continue
522+
elif nfPreventCg in field.flags:
523+
# this is an object constructor node generated by the VM and
524+
# this field is in an inactive case branch, just ignore it
525+
continue
518526
let id = considerQuotedIdent(c, field[0])
519527
# This node was not processed. There are two possible reasons:
520528
# 1) It was shadowed by a field with the same name on the left
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import std/macros
2+
3+
# bug #11862
4+
type
5+
Kind = enum kOne, kTwo
6+
7+
Thing = object
8+
case kind: Kind
9+
of kOne:
10+
v1: int
11+
of kTwo:
12+
v2: int
13+
14+
macro magic(): untyped =
15+
var b = Thing(kind: kOne, v1: 3)
16+
quote do:
17+
`b`
18+
19+
const c = magic()
20+
21+
# bug #25123
22+
type V = object
23+
case a: bool
24+
of false: discard
25+
of true: t: int
26+
27+
proc s(): V {.compileTime.} = discard
28+
proc h(_: V) = discard
29+
30+
proc e(m: static[V]) = h(m)
31+
template j(m: static[V]) = h(m)
32+
macro r(m: static[V]) = h(m)
33+
34+
e(s())
35+
j(s())
36+
r(s())
37+
38+
s().e()
39+
s().j()
40+
s().r()

0 commit comments

Comments
 (0)