fixes #22123; Compiler bug with default initializer values and arrays (#22128)

This commit is contained in:
ringabout
2023-06-20 14:02:06 +08:00
committed by GitHub
parent 29a43124cf
commit f524d60fa1
3 changed files with 23 additions and 6 deletions

View File

@@ -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

View File

@@ -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)

View File

@@ -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()