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

fixes #18081; fixes #18079; fixes #18080; nested ref/deref'd types #24335

Merged
merged 12 commits into from
Oct 25, 2024
Merged
11 changes: 1 addition & 10 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3074,16 +3074,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 @@ -1069,9 +1071,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())[])