parent
1c31de361d
commit
b1fe1690c4
2 changed files with 38 additions and 0 deletions
|
|
@ -731,7 +731,21 @@ proc isCppRef(p: BProc; typ: PType): bool {.inline.} =
|
||||||
skipTypes(typ, abstractInstOwned).kind in {tyVar} and
|
skipTypes(typ, abstractInstOwned).kind in {tyVar} and
|
||||||
tfVarIsPtr notin skipTypes(typ, abstractInstOwned).flags
|
tfVarIsPtr notin skipTypes(typ, abstractInstOwned).flags
|
||||||
|
|
||||||
|
proc derefBlock(p: BProc, e: PNode, d: var TLoc) =
|
||||||
|
# We transform (block: x)[] to (block: x[])
|
||||||
|
let e0 = e[0]
|
||||||
|
var n = shallowCopy(e0)
|
||||||
|
for i in 0 ..< e0.len - 1:
|
||||||
|
n[i] = e0[i]
|
||||||
|
n[e0.len-1] = newTreeIT(nkHiddenDeref, e.info, e.typ, e0[e0.len-1])
|
||||||
|
expr p, n, d
|
||||||
|
|
||||||
proc genDeref(p: BProc, e: PNode, d: var TLoc) =
|
proc genDeref(p: BProc, e: PNode, d: var TLoc) =
|
||||||
|
if e.kind == nkHiddenDeref and e[0].kind in {nkBlockExpr, nkBlockStmt}:
|
||||||
|
# bug #20107. Watch out to not deref the pointer too late.
|
||||||
|
derefBlock(p, e, d)
|
||||||
|
return
|
||||||
|
|
||||||
let mt = mapType(p.config, e[0].typ, mapTypeChooser(e[0]))
|
let mt = mapType(p.config, e[0].typ, mapTypeChooser(e[0]))
|
||||||
if mt in {ctArray, ctPtrToArray} and lfEnforceDeref notin d.flags:
|
if mt in {ctArray, ctPtrToArray} and lfEnforceDeref notin d.flags:
|
||||||
# XXX the amount of hacks for C's arrays is incredible, maybe we should
|
# XXX the amount of hacks for C's arrays is incredible, maybe we should
|
||||||
|
|
|
||||||
24
tests/ccgbugs/tderefblock.nim
Normal file
24
tests/ccgbugs/tderefblock.nim
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
discard """
|
||||||
|
cmd: "nim c -d:release -d:danger $file"
|
||||||
|
output: "42"
|
||||||
|
"""
|
||||||
|
|
||||||
|
# bug #20107
|
||||||
|
|
||||||
|
type Foo = object
|
||||||
|
a, b, c, d: uint64
|
||||||
|
|
||||||
|
proc c(i: uint64): Foo =
|
||||||
|
Foo(a: i, b: i, c: i, d: i)
|
||||||
|
|
||||||
|
func x(f: Foo): lent Foo {.inline.} =
|
||||||
|
f
|
||||||
|
|
||||||
|
proc m() =
|
||||||
|
let f = block:
|
||||||
|
let i = c(42)
|
||||||
|
x(i)
|
||||||
|
|
||||||
|
echo $f.a
|
||||||
|
|
||||||
|
m()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue