mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-30 01:44:37 +00:00
fix #9872: setLen now works properly at CT [backport]
(cherry picked from commit f838b1e6c1)
This commit is contained in:
@@ -413,26 +413,12 @@ proc recSetFlagIsRef(arg: PNode) =
|
||||
arg.sons[i].recSetFlagIsRef
|
||||
|
||||
proc setLenSeq(c: PCtx; node: PNode; newLen: int; info: TLineInfo) =
|
||||
# FIXME: this doesn't attempt to solve incomplete
|
||||
# support of tyPtr, tyRef in VM.
|
||||
let typ = node.typ.skipTypes(abstractInst+{tyRange}-{tyTypeDesc})
|
||||
let typeEntry = typ.sons[0].skipTypes(abstractInst+{tyRange}-{tyTypeDesc})
|
||||
let typeKind = case typeEntry.kind
|
||||
of tyUInt..tyUInt64: nkUIntLit
|
||||
of tyRange, tyEnum, tyBool, tyChar, tyInt..tyInt64: nkIntLit
|
||||
of tyFloat..tyFloat128: nkFloatLit
|
||||
of tyString: nkStrLit
|
||||
of tyObject: nkObjConstr
|
||||
of tySequence: nkNilLit
|
||||
of tyProc, tyTuple: nkTupleConstr
|
||||
else: nkEmpty
|
||||
|
||||
let oldLen = node.len
|
||||
setLen(node.sons, newLen)
|
||||
if oldLen < newLen:
|
||||
# TODO: This is still not correct for tyPtr, tyRef default value
|
||||
for i in oldLen ..< newLen:
|
||||
node.sons[i] = newNodeI(typeKind, info)
|
||||
node.sons[i] = getNullValue(typ.sons[0], info, c.config)
|
||||
|
||||
const
|
||||
errIndexOutOfBounds = "index out of bounds"
|
||||
@@ -458,7 +444,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
|
||||
#if c.traceActive:
|
||||
when traceCode:
|
||||
echo "PC ", pc, " ", c.code[pc].opcode, " ra ", ra, " rb ", instr.regB, " rc ", instr.regC
|
||||
# message(c.config, c.debug[pc], warnUser, "Trace")
|
||||
# message(c.config, c.debug[pc], warnUser, "Trace")
|
||||
|
||||
case instr.opcode
|
||||
of opcEof: return regs[ra]
|
||||
|
||||
30
tests/vm/tsetlen.nim
Normal file
30
tests/vm/tsetlen.nim
Normal file
@@ -0,0 +1,30 @@
|
||||
type Foo = object
|
||||
index: int
|
||||
|
||||
block:
|
||||
proc fun[T]() =
|
||||
var foo: T
|
||||
var n = 10
|
||||
|
||||
var foos: seq[T]
|
||||
foos.setLen n
|
||||
|
||||
n.inc
|
||||
foos.setLen n
|
||||
|
||||
for i in 0 ..< n:
|
||||
let temp = foos[i]
|
||||
when T is object:
|
||||
doAssert temp.index == 0
|
||||
when T is ref object:
|
||||
doAssert temp == nil
|
||||
doAssert temp == foo
|
||||
|
||||
static:
|
||||
fun[Foo]()
|
||||
fun[int]()
|
||||
fun[float]()
|
||||
fun[string]()
|
||||
fun[(int, string)]()
|
||||
fun[ref Foo]()
|
||||
fun[seq[int]]()
|
||||
Reference in New Issue
Block a user