diff --git a/compiler/semfold.nim b/compiler/semfold.nim index d3b7fbf243..4cf7d823e0 100644 --- a/compiler/semfold.nim +++ b/compiler/semfold.nim @@ -688,10 +688,7 @@ proc getConstExpr(m: PSym, n: PNode; idgen: IdGenerator; g: ModuleGraph): PNode except DivByZeroDefect: localError(g.config, n.info, "division by zero") of nkAddr: - var a = getConstExpr(m, n[0], idgen, g) - if a != nil: - result = n - n[0] = a + result = nil # don't fold paths containing nkAddr of nkBracket, nkCurly: result = copyNode(n) for son in n.items: diff --git a/compiler/transf.nim b/compiler/transf.nim index 7be41787be..6dec8c434e 100644 --- a/compiler/transf.nim +++ b/compiler/transf.nim @@ -53,6 +53,7 @@ type isIntroducingNewLocalVars: bool # true if we are in `introducingNewLocalVars` (don't transform yields) contSyms, breakSyms: seq[PSym] # to transform 'continue' and 'break' deferDetected, tooEarly: bool + inAddr: bool graph: ModuleGraph idgen: IdGenerator @@ -1038,7 +1039,10 @@ proc transform(c: PTransf, n: PNode): PNode = of nkHiddenAddr: result = transformAddrDeref(c, n, {nkHiddenDeref}) of nkAddr: + let oldInAddr = c.inAddr + c.inAddr = true result = transformAddrDeref(c, n, {nkDerefExpr, nkHiddenDeref}) + c.inAddr = oldInAddr of nkDerefExpr: result = transformAddrDeref(c, n, {nkAddr, nkHiddenAddr}) of nkHiddenDeref: @@ -1118,7 +1122,7 @@ proc transform(c: PTransf, n: PNode): PNode = let exprIsPointerCast = n.kind in {nkCast, nkConv, nkHiddenStdConv} and n.typ != nil and n.typ.kind == tyPointer - if not exprIsPointerCast: + if not exprIsPointerCast and not c.inAddr: var cnst = getConstExpr(c.module, result, c.idgen, c.graph) # we inline constants if they are not complex constants: if cnst != nil and not dontInlineConstant(n, cnst): diff --git a/tests/vm/tconsttable.nim b/tests/vm/tconsttable.nim index 64a74a59d5..152a33cba6 100644 --- a/tests/vm/tconsttable.nim +++ b/tests/vm/tconsttable.nim @@ -17,3 +17,18 @@ x = "ah" echo foo[x] x = "possible." echo foo[x] + +block: # bug #19840 + const testBytes = [byte 0xD8, 0x08, 0xDF, 0x45, 0x00, 0x3D, 0x00, 0x52, 0x00, 0x61] + var tempStr = "__________________" + + tempStr.prepareMutation + copyMem(addr tempStr[0], addr testBytes[0], testBytes.len) + +block: # bug #22389 + func foo(): ptr UncheckedArray[byte] = + const bar = [77.byte] + cast[ptr UncheckedArray[byte]](addr bar[0]) + + doAssert foo()[0] == 77 +