Hi there,
In tracking down a bug in my code, I've observed the following behavior I can't seem to be able to explain (Nim 2.x, refc only):
import std/sequtils
type
SomeType* = object
proc `=copy`*(dest: var SomeType, src: SomeType) =
discard
type
PeerId* = object
data: seq[int]
SwarmPeerCtx* = object
id*: PeerId
blocks*: SomeType
Swarm* = ref object of RootObj
peers*: seq[SwarmPeerCtx]
iterator peerIt(self: Swarm): SwarmPeerCtx =
for i in 0 .. high(self.peers):
yield self.peers[i]
proc getPeers*(self: Swarm): seq[SwarmPeerCtx] =
var peers = self.peerIt().toSeq
return peers
let
swarm = Swarm()
peerId = PeerId(data: @[1, 2, 3, 4])
swarm.peers.add(SwarmPeerCtx(id: peerId))
doAssert (swarm.peerIt().toSeq)[0].id.data == @[1, 2, 3, 4] # Passes
doAssert swarm.getPeers()[0].id.data == @[1, 2, 3, 4] # Fails, .data is empty
Removing the =copy proc fixes it. More weirdly, however, moving the type declaration for SomeType into the block with the other types; e.g.:
import std/sequtils
type
PeerId* = object
data: seq[int]
SomeType* = object
SwarmPeerCtx* = object
id*: PeerId
blocks*: SomeType
Swarm* = ref object of RootObj
peers*: seq[SwarmPeerCtx]
proc `=copy`*(dest: var SomeType, src: SomeType) =
discard
...
also fixes it. The problem only seems to affect refc, and it doesn't happen in older versions of NIm (tried 1.6.14, works fine). Am I doing anything silly here, or should I file this as a bug?
Do you think it'd be helpful if I file a bug for this?
Definitely.