diff --git a/compiler/sem.nim b/compiler/sem.nim index ca2c6bc005..f92853d9e3 100644 --- a/compiler/sem.nim +++ b/compiler/sem.nim @@ -639,10 +639,10 @@ proc defaultNodeField(c: PContext, a: PNode, aTyp: PType): PNode = if child != nil: let node = newNode(nkIntLit) node.intVal = toInt64(lengthOrd(c.graph.config, aTypSkip)) - result = semExpr(c, newTree(nkCall, newSymNode(getCompilerProc(c.graph, "nimArrayWith"), a.info), - semExprWithType(c, child), - node - )) + result = semExpr(c, newTree(nkCall, newSymNode(getSysSym(c.graph, a.info, "arrayWith"), a.info), + semExprWithType(c, child), + node + )) result.typ = aTyp elif aTypSkip.kind == tyTuple: var hasDefault = false diff --git a/lib/system.nim b/lib/system.nim index 949d717b0d..2290ff6f6a 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -2801,7 +2801,10 @@ when notJSnotNims and not defined(nimSeqsV2): assert y == "abcgh" discard -proc nimArrayWith[T](y: T, size: static int): array[size, T] {.compilerRtl, raises: [].} = +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: - result[i] = y + when nimvm: + result[i] = y + else: + result[i] = `=dup`(y) diff --git a/tests/objects/tobject_default_value.nim b/tests/objects/tobject_default_value.nim index b571965ea1..2d86dce115 100644 --- a/tests/objects/tobject_default_value.nim +++ b/tests/objects/tobject_default_value.nim @@ -688,6 +688,20 @@ template main {.dirty.} = else: testAssignResult() + block: # bug #22123 + type Thing = object + x: float32 = 1 + + type ThingWithArray = object + arr: array[256, float32] + n: float32 = 1 + + type Container = ref object + thing: array[5, Thing] + thing_with_array: array[5, ThingWithArray] + + var foo = new Container + doAssert int(foo.thing[0].x) == 1 static: main() main()