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 #22389; fixes #19840; don't fold paths containing addr #23807

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions compiler/semfold.nim
Original file line number Diff line number Diff line change
Expand Up @@ -701,10 +701,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 = nil # don't fold paths containing nkAddr
n[0] = a
result = nil # don't fold paths containing nkAddr
of nkBracket, nkCurly:
result = copyNode(n)
for son in n.items:
Expand Down
6 changes: 5 additions & 1 deletion compiler/transf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type
contSyms, breakSyms: seq[PSym] # to transform 'continue' and 'break'
deferDetected, tooEarly: bool
isIntroducingNewLocalVars: bool # true if we are in `introducingNewLocalVars` (don't transform yields)
inAddr: bool
flags: TransformFlags
graph: ModuleGraph
idgen: IdGenerator
Expand Down Expand Up @@ -1056,7 +1057,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:
Expand Down Expand Up @@ -1136,7 +1140,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):
Expand Down
15 changes: 15 additions & 0 deletions tests/vm/tconsttable.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Loading