Skip to content

Commit

Permalink
fix #10964 by honoring pointer deref syntax if a reified openarray is…
Browse files Browse the repository at this point in the history
… used to get an array's length (#21925)

* fix #10964

* add test
  • Loading branch information
heterodoxic authored May 27, 2023
1 parent 09f36f5 commit 6128ef5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
13 changes: 11 additions & 2 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1882,8 +1882,17 @@ proc genArrayLen(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
if op == mHigh: unaryExpr(p, e, d, "($1Len_0-1)")
else: unaryExpr(p, e, d, "$1Len_0")
else:
if op == mHigh: unaryExpr(p, e, d, "($1.Field1-1)")
else: unaryExpr(p, e, d, "$1.Field1")
let isDeref = a.kind in {nkHiddenDeref, nkDerefExpr}
if op == mHigh:
if isDeref:
unaryExpr(p, e, d, "($1->Field1-1)")
else:
unaryExpr(p, e, d, "($1.Field1-1)")
else:
if isDeref:
unaryExpr(p, e, d, "$1->Field1")
else:
unaryExpr(p, e, d, "$1.Field1")
of tyCstring:
if op == mHigh: unaryExpr(p, e, d, "($1 ? (#nimCStrLen($1)-1) : -1)")
else: unaryExpr(p, e, d, "($1 ? #nimCStrLen($1) : 0)")
Expand Down
6 changes: 6 additions & 0 deletions tests/ccgbugs/t10964.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
func test*(input: var openArray[int32], start: int = 0, fin: int = input.len - 1) =
discard

var someSeq = @[1'i32]

test(someSeq)

0 comments on commit 6128ef5

Please sign in to comment.