Skip to content

Commit

Permalink
fixes nim-lang#14003 (nim-lang#14006) [backport:1.2]
Browse files Browse the repository at this point in the history
Co-authored-by: cooldome <[email protected]>
  • Loading branch information
cooldome and cooldome authored Apr 17, 2020
1 parent d3b0132 commit f10689d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
18 changes: 12 additions & 6 deletions compiler/ccgcalls.nim
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ proc fixupCall(p: BProc, le, ri: PNode, d: var TLoc,

proc genBoundsCheck(p: BProc; arr, a, b: TLoc)

proc openArrayLoc(p: BProc, n: PNode): Rope =
proc openArrayLoc(p: BProc, formalType: PType, n: PNode): Rope =
var a: TLoc

var q = skipConv(n)
Expand Down Expand Up @@ -149,8 +149,11 @@ proc openArrayLoc(p: BProc, n: PNode): Rope =
of tyOpenArray, tyVarargs, tyUncheckedArray, tyCString:
result = "($4*)($1)+($2), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c), dest]
of tyString, tySequence:
if skipTypes(n.typ, abstractInst).kind == tyVar and
not compileToCpp(p.module):
let atyp = skipTypes(a.t, abstractInst)
if formalType.skipTypes(abstractInst).kind == tyVar and atyp.kind == tyString and
optSeqDestructors in p.config.globalOptions:
linefmt(p, cpsStmts, "#nimPrepareStrMutationV2($1);$n", [byRefLoc(p, a)])
if atyp.kind == tyVar and not compileToCpp(p.module):
result = "($5*)(*$1)$4+($2), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c), dataField(p), dest]
else:
result = "($5*)$1$4+($2), ($3)-($2)+1" % [rdLoc(a), rdLoc(b), rdLoc(c), dataField(p), dest]
Expand All @@ -162,8 +165,11 @@ proc openArrayLoc(p: BProc, n: PNode): Rope =
of tyOpenArray, tyVarargs:
result = "$1, $1Len_0" % [rdLoc(a)]
of tyString, tySequence:
if skipTypes(n.typ, abstractInst).kind == tyVar and
not compileToCpp(p.module):
let ntyp = skipTypes(n.typ, abstractInst)
if formalType.skipTypes(abstractInst).kind == tyVar and ntyp.kind == tyString and
optSeqDestructors in p.config.globalOptions:
linefmt(p, cpsStmts, "#nimPrepareStrMutationV2($1);$n", [byRefLoc(p, a)])
if ntyp.kind == tyVar and not compileToCpp(p.module):
var t: TLoc
t.r = "(*$1)" % [a.rdLoc]
result = "(*$1)$3, $2" % [a.rdLoc, lenExpr(p, t), dataField(p)]
Expand Down Expand Up @@ -194,7 +200,7 @@ proc genArg(p: BProc, n: PNode, param: PSym; call: PNode): Rope =
result = genArgStringToCString(p, n)
elif skipTypes(param.typ, abstractVar).kind in {tyOpenArray, tyVarargs}:
var n = if n.kind != nkHiddenAddr: n else: n[0]
result = openArrayLoc(p, n)
result = openArrayLoc(p, param.typ, n)
elif ccgIntroducedPtr(p.config, param, call[0].typ[0]):
initLocExpr(p, n, a)
result = addrLoc(p.config, a)
Expand Down
17 changes: 17 additions & 0 deletions tests/arc/tarcmisc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,20 @@ proc `=destroy`(x: var AObj) =
echo "closed"

var x = B(io: newStringStream("thestream"))


#------------------------------------------------------------------------------
# issue #14003

proc cryptCTR*(nonce: var openArray[char]) =
nonce[1] = 'A'

proc main() =
var nonce1 = "0123456701234567"
cryptCTR(nonce1)
doAssert(nonce1 == "0A23456701234567")
var nonce2 = "01234567"
cryptCTR(nonce2.toOpenArray(0, nonce2.len-1))
doAssert(nonce2 == "0A234567")

main()

0 comments on commit f10689d

Please sign in to comment.