Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value lost when assigning variable to itself #19613

Closed
arnetheduck opened this issue Mar 16, 2022 · 5 comments
Closed

Value lost when assigning variable to itself #19613

arnetheduck opened this issue Mar 16, 2022 · 5 comments

Comments

@arnetheduck
Copy link
Contributor

arnetheduck commented Mar 16, 2022

type
  Eth2Digest = object
    data: array[42, byte]

  BlockId* = object
    root*: Eth2Digest

  BlockSlotId* = object
    bid*: BlockId
    slot*: uint64

func init*(T: type BlockSlotId, bid: BlockId, slot: uint64): T =
  debugecho "init ", bid, " ", slot
  BlockSlotId(bid: bid, slot: slot)

var x: BlockSlotId
x.bid.root.data[0] = 42

x =
  if x.slot > 0:
    BlockSlotId.init(x.bid, x.slot)
  else:
    BlockSlotId.init(x.bid, x.slot)
doAssert x.bid.root.data[0] == 42
init (root: (data: [42, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])) 0
/usercode/in.nim(25)     in
/playground/nim/lib/system/assertions.nim(38) failedAssertImpl
/playground/nim/lib/system/assertions.nim(28) raiseAssert
/playground/nim/lib/system/fatal.nim(53) sysFatal
Error: unhandled exception: /usercode/in.nim(25, 10) `x.bid.root.data[0] == 42`  [AssertionDefect]

nim 1.2.16, 1.6.4

@ynfle
Copy link
Contributor

ynfle commented Mar 16, 2022

With the following changes everything works as expected https://play.nim-lang.org/#ix=3Syp

@@ -1,6 +1,6 @@
 type
   Eth2Digest = object
-    data: array[42, byte]
+    data: seq[byte]
 
   BlockId* = object
     root*: Eth2Digest
@@ -14,7 +14,7 @@ func init*(T: type BlockSlotId, bid: BlockId, slot: uint64): T =
   BlockSlotId(bid: bid, slot: slot)
 
 var x: BlockSlotId
-x.bid.root.data[0] = 42
+x.bid.root.data.add 42
 
 x =
   if x.slot > 0:

I wonder if it's related to #16017

@Menduist
Copy link
Contributor

Menduist commented Mar 16, 2022

May also be related to #18342
EDIT: works fine without the if, or with orc, so same symptoms

@ringabout
Copy link
Member

fixed by #19618

@arnetheduck
Copy link
Contributor Author

fixed by #19618

can we get a 1.2/1.6 backport? this is a significant issue.

@ringabout
Copy link
Member

can we get a 1.2/1.6 backport? this is a significant issue.

Fyi, it has been backported to 1.6: ff81975

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants