From 6128ef53c5bbc6c1de4e3cadcb70db580f74dbcd Mon Sep 17 00:00:00 2001 From: heterodoxic <122719743+heterodoxic@users.noreply.github.com> Date: Sat, 27 May 2023 06:54:41 +0200 Subject: [PATCH] fix #10964 by honoring pointer deref syntax if a reified openarray is used to get an array's length (#21925) * fix #10964 * add test --- compiler/ccgexprs.nim | 13 +++++++++++-- tests/ccgbugs/t10964.nim | 6 ++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 tests/ccgbugs/t10964.nim diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim index fe776e8d3023..f5033bdc3b2b 100644 --- a/compiler/ccgexprs.nim +++ b/compiler/ccgexprs.nim @@ -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)") diff --git a/tests/ccgbugs/t10964.nim b/tests/ccgbugs/t10964.nim new file mode 100644 index 000000000000..a331b16cdc84 --- /dev/null +++ b/tests/ccgbugs/t10964.nim @@ -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) \ No newline at end of file