Fixes discard bug in cpp codegen (#9747) [backport]

Fixes discard bug in cpp codegen
This commit is contained in:
cooldome 2018-11-19 07:44:49 +00:00 • committed by Andreas Rumpf
commit 9b9f5dee0b
2 changed files with 13 additions and 10 deletions

View file

@ -2441,15 +2441,8 @@ proc expr(p: BProc, n: PNode, d: var TLoc) =
if ex.kind != nkEmpty: if ex.kind != nkEmpty:
genLineDir(p, n) genLineDir(p, n)
var a: TLoc var a: TLoc
if ex.kind in nkCallKinds and (ex[0].kind != nkSym or initLocExprSingleUse(p, ex, a)
ex[0].sym.magic == mNone): line(p, cpsStmts, "(void)(" & a.r & ");\L")
# bug #6037: do not assign to a temp in C++ mode:
incl a.flags, lfSingleUse
genCall(p, ex, a)
if lfSingleUse notin a.flags:
line(p, cpsStmts, a.r & ";\L")
else:
initLocExpr(p, ex, a)
of nkAsmStmt: genAsmStmt(p, n) of nkAsmStmt: genAsmStmt(p, n)
of nkTryStmt: of nkTryStmt:
if p.module.compileToCpp and optNoCppExceptions notin p.config.globalOptions: if p.module.compileToCpp and optNoCppExceptions notin p.config.globalOptions:

View file

@ -1,6 +1,6 @@
discard """ discard """
cmd: "nim cpp $file" cmd: "nim cpp $file"
output: "" output: '''{"vas": "kas", "123": "123"}'''
targets: "cpp" targets: "cpp"
""" """
@ -9,3 +9,13 @@ block: #5979
var p: pointer = cast[pointer](a) var p: pointer = cast[pointer](a)
var c = cast[char](p) var c = cast[char](p)
doAssert(c == 'a') doAssert(c == 'a')
#----------------------------------------------------
# bug #9739
import tables
var t = initTable[string, string]()
discard t.hasKeyOrPut("123", "123")
discard t.mgetOrPut("vas", "kas")
echo t