Skip to content

Commit

Permalink
fixes #18081; fixes #18079; fixes #18080; nested ref/deref'd types (#…
Browse files Browse the repository at this point in the history
…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)`.
  • Loading branch information
ringabout authored Oct 25, 2024
1 parent 2af602a commit aa90d00
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
11 changes: 1 addition & 10 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3144,16 +3144,7 @@ proc expr(p: BProc, n: PNode, d: var TLoc) =
of nkObjConstr: genObjConstr(p, n, d)
of nkCast: genCast(p, n, d)
of nkHiddenStdConv, nkHiddenSubConv, nkConv: genConv(p, n, d)
of nkHiddenAddr:
if n[0].kind == nkDerefExpr:
# addr ( deref ( x )) --> x
var x = n[0][0]
if n.typ.skipTypes(abstractVar).kind != tyOpenArray:
x.typ() = n.typ
expr(p, x, d)
return
genAddr(p, n, d)
of nkAddr: genAddr(p, n, d)
of nkAddr, nkHiddenAddr: genAddr(p, n, d)
of nkBracketExpr: genBracketExpr(p, n, d)
of nkDerefExpr, nkHiddenDeref: genDeref(p, n, d)
of nkDotExpr: genRecordField(p, n, d)
Expand Down
9 changes: 1 addition & 8 deletions compiler/jsgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1585,15 +1585,8 @@ proc genAddr(p: PProc, n: PNode, r: var TCompRes) =
else: internalError(p.config, n[0].info, "expr(nkBracketExpr, " & $kindOfIndexedExpr & ')')
of nkObjDownConv:
gen(p, n[0], r)
of nkHiddenDeref:
of nkHiddenDeref, nkDerefExpr:
gen(p, n[0], r)
of nkDerefExpr:
var x = n[0]
if n.kind == nkHiddenAddr:
x = n[0][0]
if n.typ.skipTypes(abstractVar).kind != tyOpenArray:
x.typ() = n.typ
gen(p, x, r)
of nkHiddenAddr:
gen(p, n[0], r)
of nkConv:
Expand Down
8 changes: 4 additions & 4 deletions compiler/transf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,9 @@ proc transformAddrDeref(c: PTransf, n: PNode, kinds: TNodeKinds, isAddr = false)
) and not (n[0][0].kind == nkSym and n[0][0].sym.kind == skParam and
n.typ.kind == tyVar and
n.typ.skipTypes(abstractVar).kind == tyOpenArray and
n[0][0].typ.skipTypes(abstractVar).kind == tyString)
n[0][0].typ.skipTypes(abstractVar).kind == tyString) and
not (isAddr and n.typ.kind == tyVar and n[0][0].typ.kind == tyRef and
n[0][0].kind == nkObjConstr)
: # elimination is harmful to `for tuple unpack` because of newTupleAccess
# it is also harmful to openArrayLoc (var openArray) for strings
# addr ( deref ( x )) --> x
Expand Down Expand Up @@ -1075,9 +1077,7 @@ proc transform(c: PTransf, n: PNode, noConstFold = false): PNode =
of nkBreakStmt: result = transformBreak(c, n)
of nkCallKinds:
result = transformCall(c, n)
of nkHiddenAddr:
result = transformAddrDeref(c, n, {nkHiddenDeref}, isAddr = true)
of nkAddr:
of nkAddr, nkHiddenAddr:
result = transformAddrDeref(c, n, {nkDerefExpr, nkHiddenDeref}, isAddr = true)
of nkDerefExpr:
result = transformAddrDeref(c, n, {nkAddr, nkHiddenAddr})
Expand Down
36 changes: 36 additions & 0 deletions tests/ccgbugs/t13062.nim
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,39 @@ elif defined(gcRefc):
doAssert x.repr == "[p = nil]"
else: # fixme # bug #20081
doAssert x.repr == "Pledge(p: nil)"

block:
block: # bug #18081
type
Foo = object
discard

Bar = object
x: Foo

proc baz(state: var Bar) =
state.x = Foo()

baz((ref Bar)(x: (new Foo)[])[])

block: # bug #18079
type
Foo = object
discard

Bar = object
x: Foo

proc baz(state: var Bar) = discard
baz((ref Bar)(x: (new Foo)[])[])

block: # bug #18080
type
Foo = object
discard

Bar = object
x: Foo

proc baz(state: var Bar) = discard
baz((ref Bar)(x: Foo())[])

0 comments on commit aa90d00

Please sign in to comment.