diff --git a/compiler/backend/jsgen.nim b/compiler/backend/jsgen.nim index 1d5375dcfcb..90000480e9f 100644 --- a/compiler/backend/jsgen.nim +++ b/compiler/backend/jsgen.nim @@ -1673,13 +1673,12 @@ proc genVarInit(p: PProc, typ: PType, varName: string, storage: StorageFlags, s: Rope if n.kind == cnkEmpty: - let isIndirect = stfIndirect in storage - if not isIndirect and - typ.kind in {tyVar, tyPtr, tyLent, tyRef} and mapType(typ) == etyBaseIndex: + if mapType(typ) == etyBaseIndex and (stfBoxed notin storage): lineF(p, "var $1 = null;$n", [varName]) lineF(p, "var $1_Idx = 0;$n", [varName]) else: - lineF(p, "var $1 = $2;$n", [varName, createVar(p, typ, isIndirect)]) + let val = createVar(p, typ, stfIndirect in storage) + lineF(p, "var $1 = $2;$n", [varName, val]) else: gen(p, n, a) case mapType(typ) @@ -2293,10 +2292,7 @@ proc finishProc*(p: PProc): string = let loc {.cursor.} = p.locals[resultId] mname = loc.name - let returnAddress = not isIndirect(loc) and - loc.typ.kind in {tyVar, tyPtr, tyLent, tyRef} and - mapType(loc.typ) == etyBaseIndex - if returnAddress: + if mapType(loc.typ) == etyBaseIndex and (stfBoxed notin loc.storage): resultAsgn = p.indentLine(("var $# = null;$n") % [mname]) resultAsgn.add p.indentLine("var $#_Idx = 0;$n" % [mname]) else: @@ -2304,7 +2300,7 @@ proc finishProc*(p: PProc): string = resultAsgn = p.indentLine(("var $# = $#;$n") % [mname, resVar]) var a: TCompRes accessLoc(mname, loc, a) - if returnAddress: + if a.typ == etyBaseIndex: returnStmt = "return [$#, $#];$n" % [a.address, a.res] else: returnStmt = "return $#;$n" % [a.res] diff --git a/tests/js/tdefault_init_pointer_var.nim b/tests/js/tdefault_init_pointer_var.nim new file mode 100644 index 00000000000..743cef212aa --- /dev/null +++ b/tests/js/tdefault_init_pointer_var.nim @@ -0,0 +1,13 @@ +discard """ + description: ''' + Regression test for wrong code being generated for locals of `pointer` + type + ''' +""" + +proc test(): pointer = + # the result variable was affected too + var x: pointer + doAssert x == nil # this assertion failed + +doAssert test() == nil # this one too \ No newline at end of file