Skip to content

Commit

Permalink
fixes #23321; Error: internal error: openArrayLoc: ref array[0..0, in…
Browse files Browse the repository at this point in the history
…t] (#23548)

fixes #23321

In the function `mapType`, ptrs (tyPtr, tyVar, tyLent, tyRef)
are mapped into ctPtrToArray, the dereference of which is skipped
in the `genref`. We need to skip these ptrs in the function
`genOpenArraySlice`.
  • Loading branch information
ringabout committed Apr 29, 2024
1 parent 47594eb commit d09c3c0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
5 changes: 4 additions & 1 deletion compiler/ccgcalls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ proc genOpenArraySlice(p: BProc; q: PNode; formalType, destType: PType; prepareF
genBoundsCheck(p, a, b, c)
if prepareForMutation:
linefmt(p, cpsStmts, "#nimPrepareStrMutationV2($1);$n", [byRefLoc(p, a)])
let ty = skipTypes(a.t, abstractVar+{tyPtr})
# bug #23321: In the function mapType, ptrs (tyPtr, tyVar, tyLent, tyRef)
# are mapped into ctPtrToArray, the dereference of which is skipped
# in the `genref`. We need to skip these ptrs here
let ty = skipTypes(a.t, abstractVar+{tyPtr, tyRef})
let dest = getTypeDesc(p.module, destType)
let lengthExpr = "($1)-($2)+1" % [rdLoc(c), rdLoc(b)]
case ty.kind
Expand Down
35 changes: 35 additions & 0 deletions tests/openarray/topenarray.nim
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,41 @@ proc main =
doAssert testing(mySeq) == mySeq
doAssert testing(mySeq[2..^2]) == mySeq[2..^2]

block: # bug #23321
block:
proc foo(x: openArray[int]) =
doAssert x[0] == 0

var d = new array[1, int]
foo d[].toOpenArray(0, 0)

block:
proc foo(x: openArray[int]) =
doAssert x[0] == 0

proc task(x: var array[1, int]): var array[1, int] =
result = x
var d: array[1, int]
foo task(d).toOpenArray(0, 0)

block:
proc foo(x: openArray[int]) =
doAssert x[0] == 0

proc task(x: var array[1, int]): lent array[1, int] =
result = x
var d: array[1, int]
foo task(d).toOpenArray(0, 0)

block:
proc foo(x: openArray[int]) =
doAssert x[0] == 0

proc task(x: var array[1, int]): ptr array[1, int] =
result = addr x
var d: array[1, int]
foo task(d)[].toOpenArray(0, 0)


main()
static: main()

0 comments on commit d09c3c0

Please sign in to comment.