Skip to content

Commit 5fb4662

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 5355568 commit 5fb4662

File tree

4 files changed

+50
-22
lines changed

4 files changed

+50
-22
lines changed

compiler/ccgexprs.nim

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3118,16 +3118,7 @@ proc expr(p: BProc, n: PNode, d: var TLoc) =
31183118
of nkObjConstr: genObjConstr(p, n, d)
31193119
of nkCast: genCast(p, n, d)
31203120
of nkHiddenStdConv, nkHiddenSubConv, nkConv: genConv(p, n, d)
3121-
of nkHiddenAddr:
3122-
if n[0].kind == nkDerefExpr:
3123-
# addr ( deref ( x )) --> x
3124-
var x = n[0][0]
3125-
if n.typ.skipTypes(abstractVar).kind != tyOpenArray:
3126-
x.typ = n.typ
3127-
expr(p, x, d)
3128-
return
3129-
genAddr(p, n, d)
3130-
of nkAddr: genAddr(p, n, d)
3121+
of nkAddr, nkHiddenAddr: genAddr(p, n, d)
31313122
of nkBracketExpr: genBracketExpr(p, n, d)
31323123
of nkDerefExpr, nkHiddenDeref: genDeref(p, n, d)
31333124
of nkDotExpr: genRecordField(p, n, d)

compiler/jsgen.nim

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,15 +1519,8 @@ proc genAddr(p: PProc, n: PNode, r: var TCompRes) =
15191519
else: internalError(p.config, n[0].info, "expr(nkBracketExpr, " & $kindOfIndexedExpr & ')')
15201520
of nkObjDownConv:
15211521
gen(p, n[0], r)
1522-
of nkHiddenDeref:
1522+
of nkHiddenDeref, nkDerefExpr:
15231523
gen(p, n[0], r)
1524-
of nkDerefExpr:
1525-
var x = n[0]
1526-
if n.kind == nkHiddenAddr:
1527-
x = n[0][0]
1528-
if n.typ.skipTypes(abstractVar).kind != tyOpenArray:
1529-
x.typ = n.typ
1530-
gen(p, x, r)
15311524
of nkHiddenAddr:
15321525
gen(p, n[0], r)
15331526
of nkConv:

compiler/transf.nim

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,17 @@ proc transformAddrDeref(c: PTransf, n: PNode, kinds: TNodeKinds, isAddr = false)
497497
elif n.typ.skipTypes(abstractInst).kind in {tyVar}:
498498
result.typ = toVar(result.typ, n.typ.skipTypes(abstractInst).kind, c.idgen)
499499
else:
500-
if n[0].kind in kinds:
500+
if n[0].kind in kinds and
501+
not (n[0][0].kind == nkSym and n[0][0].sym.kind == skForVar and
502+
n[0][0].typ.skipTypes(abstractVar).kind == tyTuple
503+
) and not (n[0][0].kind == nkSym and n[0][0].sym.kind == skParam and
504+
n.typ.kind == tyVar and
505+
n.typ.skipTypes(abstractVar).kind == tyOpenArray and
506+
n[0][0].typ.skipTypes(abstractVar).kind == tyString) and
507+
not (isAddr and n.typ.kind == tyVar and n[0][0].typ.kind == tyRef and
508+
n[0][0].kind == nkObjConstr)
509+
: # elimination is harmful to `for tuple unpack` because of newTupleAccess
510+
# it is also harmful to openArrayLoc (var openArray) for strings
501511
# addr ( deref ( x )) --> x
502512
result = n[0][0]
503513
if n.typ.skipTypes(abstractVar).kind != tyOpenArray:
@@ -1035,9 +1045,7 @@ proc transform(c: PTransf, n: PNode, noConstFold = false): PNode =
10351045
of nkBreakStmt: result = transformBreak(c, n)
10361046
of nkCallKinds:
10371047
result = transformCall(c, n)
1038-
of nkHiddenAddr:
1039-
result = transformAddrDeref(c, n, {nkHiddenDeref}, isAddr = true)
1040-
of nkAddr:
1048+
of nkAddr, nkHiddenAddr:
10411049
result = transformAddrDeref(c, n, {nkDerefExpr, nkHiddenDeref}, isAddr = true)
10421050
of nkDerefExpr:
10431051
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)