diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 6a3b401111e0..c2623f8f76ce 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -722,29 +722,46 @@ proc arrayConstrType(c: PContext, n: PNode): PType = proc semArrayConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType: PType = nil): PNode = result = newNodeI(nkBracket, n.info) - result.typ = newTypeS(tyArray, c) + # nkBracket nodes can also be produced by the VM as seq constant nodes + # in which case, we cannot produce a new array type for the node, + # as this might lose type info even when the node has array type + let constructType = n.typ.isNil var expectedElementType, expectedIndexType: PType = nil - if expectedType != nil: - let expected = expectedType.skipTypes(abstractRange-{tyDistinct}) + if constructType: + result.typ = newTypeS(tyArray, c) + if expectedType != nil: + let expected = expectedType.skipTypes(abstractRange-{tyDistinct}) + case expected.kind + of tyArray: + expectedIndexType = expected[0] + expectedElementType = expected[1] + of tyOpenArray: + expectedElementType = expected[0] + else: discard + rawAddSon(result.typ, nil) # index type + else: + result.typ = n.typ + let expected = n.typ.skipTypes(abstractRange) # include tyDistinct this time case expected.kind of tyArray: expectedIndexType = expected[0] expectedElementType = expected[1] - of tyOpenArray: + of tyOpenArray, tySequence: + # typed bracket expressions can also have seq type expectedElementType = expected[0] else: discard - rawAddSon(result.typ, nil) # index type var firstIndex, lastIndex: Int128 = Zero indexType = getSysType(c.graph, n.info, tyInt) lastValidIndex = lastOrd(c.config, indexType) if n.len == 0: - rawAddSon(result.typ, - if expectedElementType != nil and - typeAllowed(expectedElementType, skLet, c) == nil: - expectedElementType - else: - newTypeS(tyEmpty, c)) # needs an empty basetype! + if constructType: + rawAddSon(result.typ, + if expectedElementType != nil and + typeAllowed(expectedElementType, skLet, c) == nil: + expectedElementType + else: + newTypeS(tyEmpty, c)) # needs an empty basetype! lastIndex = toInt128(-1) else: var x = n[0] @@ -761,9 +778,13 @@ proc semArrayConstr(c: PContext, n: PNode, flags: TExprFlags; expectedType: PTyp x = x[1] let yy = semExprWithType(c, x, {efTypeAllowed}, expectedElementType) - var typ = yy.typ - if expectedElementType == nil: - expectedElementType = typ + var typ: PType + if constructType: + typ = yy.typ + if expectedElementType == nil: + expectedElementType = typ + else: + typ = expectedElementType result.add yy #var typ = skipTypes(result[0].typ, {tyGenericInst, tyVar, tyLent, tyOrdinal}) for i in 1..