Skip to content

Commit

Permalink
fix nim-lang#15405. deepcopy arc (nim-lang#15410)
Browse files Browse the repository at this point in the history
* fix nim-lang#15405
* fix tests
* deepcopy for ARC has to be enabled via --deepcopy:on

Co-authored-by: Araq <rumpf_a@web.de>
  • Loading branch information
2 people authored and mildred committed Jan 11, 2021
1 parent dacea4f commit a8f4252
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 5 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ proc mydiv(a, b): int {.raises: [].} =
performance this keyword can enable.

- `items` no longer compiles with enum with holes as its behavior was error prone, see #14004
- `system.deepcopy` has to be enabled explicitly for `--gc:arc` and `--gc:orc` via
`--deepcopy:on`.


- Added `critbits.toCritBitTree`, similar to `tables.toTable`, creates a new `CritBitTree` with given arguments.

Expand Down
4 changes: 4 additions & 0 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2394,6 +2394,10 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
let n = semparallel.liftParallel(p.module.g.graph, p.module.module, e)
expr(p, n, d)
of mDeepCopy:
if p.config.selectedGC in {gcArc, gcOrc} and optEnableDeepCopy notin p.config.globalOptions:
localError(p.config, e.info,
"for --gc:arc|orc 'deepcopy' support has to be enabled with --deepcopy:on")

var a, b: TLoc
let x = if e[1].kind in {nkAddr, nkHiddenAddr}: e[1][0] else: e[1]
initLocExpr(p, x, a)
Expand Down
7 changes: 5 additions & 2 deletions compiler/ccgtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,9 @@ proc genTypeInfoV2Impl(m: BModule, t, origType: PType, name: Rope; info: TLineIn
name, destroyImpl, getTypeDesc(m, t), typeName,
traceImpl, disposeImpl])

if t.kind == tyObject and t.len > 0 and t[0] != nil and optEnableDeepCopy in m.config.globalOptions:
discard genTypeInfoV1(m, t, info)

proc genTypeInfoV2(m: BModule, t: PType; info: TLineInfo): Rope =
let origType = t
var t = skipTypes(origType, irrelevantForBackend + tyUserTypeClasses)
Expand Down Expand Up @@ -1454,12 +1457,12 @@ proc genTypeInfoV1(m: BModule, t: PType; info: TLineInfo): Rope =
genTupleInfo(m, x, x, result, info)
of tySequence:
genTypeInfoAux(m, t, t, result, info)
if m.config.selectedGC >= gcMarkAndSweep:
if m.config.selectedGC in {gcMarkAndSweep, gcRefc, gcV2, gcGo}:
let markerProc = genTraverseProc(m, origType, sig)
m.s[cfsTypeInit3].addf("$1.marker = $2;$n", [tiNameForHcr(m, result), markerProc])
of tyRef:
genTypeInfoAux(m, t, t, result, info)
if m.config.selectedGC >= gcMarkAndSweep:
if m.config.selectedGC in {gcMarkAndSweep, gcRefc, gcV2, gcGo}:
let markerProc = genTraverseProc(m, origType, sig)
m.s[cfsTypeInit3].addf("$1.marker = $2;$n", [tiNameForHcr(m, result), markerProc])
of tyPtr, tyRange, tyUncheckedArray: genTypeInfoAux(m, t, t, result, info)
Expand Down
3 changes: 2 additions & 1 deletion compiler/commands.nim
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,8 @@ proc processSwitch*(switch, arg: string, pass: TCmdLinePass, info: TLineInfo;
of "sourcemap":
conf.globalOptions.incl optSourcemap
conf.options.incl optLineDir
# processOnOffSwitchG(conf, {optSourcemap, opt}, arg, pass, info)
of "deepcopy":
processOnOffSwitchG(conf, {optEnableDeepCopy}, arg, pass, info)
of "": # comes from "-" in for example: `nim c -r -` (gets stripped from -)
handleStdinInput(conf)
else:
Expand Down
1 change: 1 addition & 0 deletions compiler/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ type # please make sure we have under 32 options
optNimV1Emulation # emulate Nim v1.0
optSourcemap
optProfileVM # enable VM profiler
optEnableDeepCopy # ORC specific: enable 'deepcopy' for all types.

TGlobalOptions* = set[TGlobalOption]

Expand Down
1 change: 1 addition & 0 deletions doc/advopt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,4 @@ Advanced options:
--profileVM:on|off enable compile time VM profiler
--sinkInference:on|off en-/disable sink parameter inference (default: on)
--panics:on|off turn panics into process terminations (default: off)
--deepcopy:on|off enable 'system.deepCopy' for ``--gc:arc|orc``
3 changes: 3 additions & 0 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2895,6 +2895,9 @@ when hasAlloc and notJSnotNims:
##
## This is also used by the code generator
## for the implementation of ``spawn``.
##
## For ``--gc:arc`` or ``--gc:orc`` deepcopy support has to be enabled
## via ``--deepcopy:on``.
discard

proc deepCopy*[T](y: T): T =
Expand Down
33 changes: 32 additions & 1 deletion tests/arc/tarcmisc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ closed
destroying variable: 20
destroying variable: 10
'''
cmd: "nim c --gc:arc $file"
cmd: "nim c --gc:arc --deepcopy:on $file"
"""

proc takeSink(x: sink string): bool = true
Expand Down Expand Up @@ -347,3 +347,34 @@ var data = {

# For ARC listVal is empty for some reason
doAssert data["examples"]["values"].listVal[0].strVal == "test"




###############################################################################
# bug #15405
import parsexml
const test_xml_str = "<A><B>value</B></A>"
var stream = newStringStream(test_xml_str)
var xml: XmlParser
open(xml, stream, "test")
var xml2 = deepCopy(xml)

proc text_parser(xml: var XmlParser) =
var test_passed = false
while true:
xml.next()
case xml.kind
of xmlElementStart:
if xml.elementName == "B":
xml.next()
if xml.kind == xmlCharData and xml.charData == "value":
test_passed = true

of xmlEof: break
else: discard
xml.close()
doAssert(test_passed)

text_parser(xml)
text_parser(xml2)
2 changes: 1 addition & 1 deletion tests/arc/tdeepcopy.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
discard """
cmd: "nim c --gc:arc $file"
cmd: "nim c --gc:arc --deepcopy:on $file"
output: '''13 abc
13 abc
13 abc
Expand Down

0 comments on commit a8f4252

Please sign in to comment.