Skip to content

Commit

Permalink
fixes #20048; fixes #15746; don't sink object fields if it's of opena…
Browse files Browse the repository at this point in the history
…rray type (#23608)

fixes #20048
fixes #15746
  • Loading branch information
ringabout authored Jun 15, 2024
1 parent de1f718 commit ae4b47c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion compiler/injectdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,8 @@ proc p(n: PNode; c: var Con; s: var Scope; mode: ProcessMode; tmpFlags = {sfSing
for i in 1..<n.len:
if n[i].kind == nkExprColonExpr:
let field = lookupFieldAgain(t, n[i][0].sym)
if field != nil and sfCursor in field.flags:
if field != nil and (sfCursor in field.flags or field.typ.kind in {tyOpenArray, tyVarargs}):
# don't sink fields with openarray types
result[i][1] = p(n[i][1], c, s, normal)
else:
result[i][1] = p(n[i][1], c, s, m)
Expand Down
10 changes: 10 additions & 0 deletions tests/views/tviews1.nim
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,13 @@ proc bug22597 = # bug #22597
doAssert i == 1

bug22597()

block: # bug #20048
type
Test = object
tokens: openArray[string]

func init(Self: typedesc[Test], tokens: openArray[string]): Self = Self(tokens: tokens)

let data = Test.init(["123"])
doAssert @(data.tokens) == @["123"]
12 changes: 12 additions & 0 deletions tests/views/tviews2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ block: # bug #16671

f()

block: # bug #15746
type
Reader = object
data: openArray[char]
current: int

proc initReader(data: openArray[char], offset = 0): Reader =
result = Reader(data: data, current: offset)

let s = "\x01\x00\x00\x00"
doAssert initReader(s).data[0].int == 1

block:
proc foo(x: openArray[char]) =
discard x
Expand Down

0 comments on commit ae4b47c

Please sign in to comment.