Skip to content

Commit

Permalink
fixes regression #23280; Operations on inline toOpenArray len return …
Browse files Browse the repository at this point in the history
…a wrong result (#23285)

fixes #23280

(cherry picked from commit 4b67ccc)
  • Loading branch information
ringabout authored and narimiran committed Feb 7, 2024
1 parent 5f6ff1f commit 57658b6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ proc binaryArithOverflow(p: BProc, e: PNode, d: var TLoc, m: TMagic) =
if t.kind == tyInt64: prc64[m] else: prc[m])
putIntoDest(p, d, e, "($#)($#)" % [getTypeDesc(p.module, e.typ), res])
else:
let res = "($1)($2 $3 $4)" % [getTypeDesc(p.module, e.typ), rdLoc(a), rope(opr[m]), rdLoc(b)]
let res = "($1)(($2) $3 ($4))" % [getTypeDesc(p.module, e.typ), rdLoc(a), rope(opr[m]), rdLoc(b)]
putIntoDest(p, d, e, res)

proc unaryArithOverflow(p: BProc, e: PNode, d: var TLoc, m: TMagic) =
Expand Down Expand Up @@ -1881,9 +1881,9 @@ proc genArrayLen(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
if optBoundsCheck in p.options:
genBoundsCheck(p, m, b, c)
if op == mHigh:
putIntoDest(p, d, e, ropecg(p.module, "($2)-($1)", [rdLoc(b), rdLoc(c)]))
putIntoDest(p, d, e, ropecg(p.module, "(($2)-($1))", [rdLoc(b), rdLoc(c)]))
else:
putIntoDest(p, d, e, ropecg(p.module, "($2)-($1)+1", [rdLoc(b), rdLoc(c)]))
putIntoDest(p, d, e, ropecg(p.module, "(($2)-($1)+1)", [rdLoc(b), rdLoc(c)]))
else:
if not reifiedOpenArray(a):
if op == mHigh: unaryExpr(p, e, d, "($1Len_0-1)")
Expand Down
10 changes: 10 additions & 0 deletions tests/ccgbugs/tcgbug.nim
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,13 @@ proc bug19613 =
doAssert x.bid.root.data[0] == 42

bug19613()

proc foo = # bug #23280
let foo = @[1,2,3,4,5,6]
doAssert toOpenArray(foo, 0, 5).len == 6
doAssert toOpenArray(foo, 0, 5).len mod 6 == 0 # this should output 0
doAssert toOpenArray(foo, 0, 5).max mod 6 == 0
let L = toOpenArray(foo, 0, 5).len
doAssert L mod 6 == 0

foo()

0 comments on commit 57658b6

Please sign in to comment.