Skip to content

Commit 98403a0

Browse files
ringaboutnarimiran
authored andcommitted
fixes #18081; fixes #18079; fixes #18080; nested ref/deref'd types (#24335)
fixes #18081; fixes #18080 fixes #18079 reverts #20738 It is probably more reasonable to use the type node from `nkObjConstr` since it is barely changed unlike the external type, which is susceptible to code transformation e.g. `addr(deref objconstr)`. (cherry picked from commit aa90d00)
1 parent 4bdeddc commit 98403a0

File tree

4 files changed

+42
-22
lines changed

4 files changed

+42
-22
lines changed

compiler/ccgexprs.nim

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3074,16 +3074,7 @@ proc expr(p: BProc, n: PNode, d: var TLoc) =
30743074
of nkObjConstr: genObjConstr(p, n, d)
30753075
of nkCast: genCast(p, n, d)
30763076
of nkHiddenStdConv, nkHiddenSubConv, nkConv: genConv(p, n, d)
3077-
of nkHiddenAddr:
3078-
if n[0].kind == nkDerefExpr:
3079-
# addr ( deref ( x )) --> x
3080-
var x = n[0][0]
3081-
if n.typ.skipTypes(abstractVar).kind != tyOpenArray:
3082-
x.typ() = n.typ
3083-
expr(p, x, d)
3084-
return
3085-
genAddr(p, n, d)
3086-
of nkAddr: genAddr(p, n, d)
3077+
of nkAddr, nkHiddenAddr: genAddr(p, n, d)
30873078
of nkBracketExpr: genBracketExpr(p, n, d)
30883079
of nkDerefExpr, nkHiddenDeref: genDeref(p, n, d)
30893080
of nkDotExpr: genRecordField(p, n, d)

compiler/jsgen.nim

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,15 +1585,8 @@ proc genAddr(p: PProc, n: PNode, r: var TCompRes) =
15851585
else: internalError(p.config, n[0].info, "expr(nkBracketExpr, " & $kindOfIndexedExpr & ')')
15861586
of nkObjDownConv:
15871587
gen(p, n[0], r)
1588-
of nkHiddenDeref:
1588+
of nkHiddenDeref, nkDerefExpr:
15891589
gen(p, n[0], r)
1590-
of nkDerefExpr:
1591-
var x = n[0]
1592-
if n.kind == nkHiddenAddr:
1593-
x = n[0][0]
1594-
if n.typ.skipTypes(abstractVar).kind != tyOpenArray:
1595-
x.typ() = n.typ
1596-
gen(p, x, r)
15971590
of nkHiddenAddr:
15981591
gen(p, n[0], r)
15991592
of nkConv:

compiler/transf.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,9 @@ proc transformAddrDeref(c: PTransf, n: PNode, kinds: TNodeKinds, isAddr = false)
507507
) and not (n[0][0].kind == nkSym and n[0][0].sym.kind == skParam and
508508
n.typ.kind == tyVar and
509509
n.typ.skipTypes(abstractVar).kind == tyOpenArray and
510-
n[0][0].typ.skipTypes(abstractVar).kind == tyString)
510+
n[0][0].typ.skipTypes(abstractVar).kind == tyString) and
511+
not (isAddr and n.typ.kind == tyVar and n[0][0].typ.kind == tyRef and
512+
n[0][0].kind == nkObjConstr)
511513
: # elimination is harmful to `for tuple unpack` because of newTupleAccess
512514
# it is also harmful to openArrayLoc (var openArray) for strings
513515
# addr ( deref ( x )) --> x
@@ -1075,9 +1077,7 @@ proc transform(c: PTransf, n: PNode, noConstFold = false): PNode =
10751077
of nkBreakStmt: result = transformBreak(c, n)
10761078
of nkCallKinds:
10771079
result = transformCall(c, n)
1078-
of nkHiddenAddr:
1079-
result = transformAddrDeref(c, n, {nkHiddenDeref}, isAddr = true)
1080-
of nkAddr:
1080+
of nkAddr, nkHiddenAddr:
10811081
result = transformAddrDeref(c, n, {nkDerefExpr, nkHiddenDeref}, isAddr = true)
10821082
of nkDerefExpr:
10831083
result = transformAddrDeref(c, n, {nkAddr, nkHiddenAddr})

tests/ccgbugs/t13062.nim

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,39 @@ elif defined(gcRefc):
3131
doAssert x.repr == "[p = nil]"
3232
else: # fixme # bug #20081
3333
doAssert x.repr == "Pledge(p: nil)"
34+
35+
block:
36+
block: # bug #18081
37+
type
38+
Foo = object
39+
discard
40+
41+
Bar = object
42+
x: Foo
43+
44+
proc baz(state: var Bar) =
45+
state.x = Foo()
46+
47+
baz((ref Bar)(x: (new Foo)[])[])
48+
49+
block: # bug #18079
50+
type
51+
Foo = object
52+
discard
53+
54+
Bar = object
55+
x: Foo
56+
57+
proc baz(state: var Bar) = discard
58+
baz((ref Bar)(x: (new Foo)[])[])
59+
60+
block: # bug #18080
61+
type
62+
Foo = object
63+
discard
64+
65+
Bar = object
66+
x: Foo
67+
68+
proc baz(state: var Bar) = discard
69+
baz((ref Bar)(x: Foo())[])

0 commit comments

Comments
 (0)