From 0b7e22635ed1996cdc9e5922dab38e913fc11884 Mon Sep 17 00:00:00 2001 From: Sam <3115209+saemideluxe@users.noreply.github.com> Date: Sun, 10 Nov 2024 23:16:07 +0700 Subject: [PATCH] Fixes #24369 (#24370) Hope this fixes #24369, happy for any feedback on the PR. (cherry picked from commit 1fddb61b3ba4fd34e2254298cc734f3fe67130b9) --- compiler/sem.nim | 5 +++-- lib/system.nim | 11 ++++++----- tests/system/tcopyprotected_arrayinit.nim | 10 ++++++++++ 3 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 tests/system/tcopyprotected_arrayinit.nim diff --git a/compiler/sem.nim b/compiler/sem.nim index cb0e5590ce..a42719a753 100644 --- a/compiler/sem.nim +++ b/compiler/sem.nim @@ -671,8 +671,9 @@ proc defaultNodeField(c: PContext, a: PNode, aTyp: PType, checkDefault: bool): P if child != nil: let node = newNode(nkIntLit) node.intVal = toInt64(lengthOrd(c.graph.config, aTypSkip)) - result = semExpr(c, newTree(nkCall, newSymNode(getSysSym(c.graph, a.info, "arrayWith"), a.info), - semExprWithType(c, child), + let typeNode = newNode(nkType) + typeNode.typ() = makeTypeDesc(c, aTypSkip[1]) + result = semExpr(c, newTree(nkCall, newTree(nkBracketExpr, newSymNode(getSysSym(c.graph, a.info, "arrayWithDefault"), a.info), typeNode), node )) result.typ() = aTyp diff --git a/lib/system.nim b/lib/system.nim index 523e73e095..f4a7005c68 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -2966,8 +2966,9 @@ when notJSnotNims and not defined(nimSeqsV2): proc arrayWith*[T](y: T, size: static int): array[size, T] {.raises: [].} = ## Creates a new array filled with `y`. for i in 0..size-1: - when nimvm: - result[i] = y - else: - # TODO: fixme it should be `=dup` - result[i] = y + result[i] = y + +proc arrayWithDefault*[T](size: static int): array[size, T] {.raises: [].} = + ## Creates a new array filled with `default(T)`. + for i in 0..size-1: + result[i] = default(T) diff --git a/tests/system/tcopyprotected_arrayinit.nim b/tests/system/tcopyprotected_arrayinit.nim new file mode 100644 index 0000000000..6caec7c5d3 --- /dev/null +++ b/tests/system/tcopyprotected_arrayinit.nim @@ -0,0 +1,10 @@ +type + Bar = object + b: int = 1 + + Foo = object + f: array[1, Bar] + +proc `=copy`(dest: var Bar, source: Bar) {.error.} + +discard Foo()