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

constructor causes SIGBUS if a destroy is defined #14601

Closed
timotheecour opened this issue Jun 8, 2020 · 2 comments · Fixed by #15677
Closed

constructor causes SIGBUS if a destroy is defined #14601

timotheecour opened this issue Jun 8, 2020 · 2 comments · Fixed by #15677

Comments

@timotheecour
Copy link
Member

constructor causes SIGBUS if a destroy is defined

Example 1

when true: # D20200607T202043
  type Foo = object
    x: int
    x2: array[10, int]

  type Vec = object
    vals: seq[Foo]

  proc `=destroy`*(a: var Foo) {.inline.} =
    discard

  proc initFoo(x: int): Foo = Foo(x: x)

  proc add2(v: var Vec, a: Foo) = # ditto with `a: sink Foo`
    v.vals.add a

  proc main()=
    var a: Vec
    var b = Foo(x: 10)
    a.add2 b # ok
    a.vals.add Foo(x: 10) # ok
    a.add2 initFoo(x = 10) # ok
    a.add2 Foo(x: 10) # bug
  main()

Current Output

/Users/timothee/git_clone/nim/timn/tests/nim/all/t10914.nim(95) t10914
/Users/timothee/git_clone/nim/timn/tests/nim/all/t10914.nim(94) main
/Users/timothee/git_clone/nim/timn/tests/nim/all/t10914.nim(86) add2
SIGBUS: Illegal storage access. (Attempt to read from nil?)

Expected Output

works

Example 2

here's another example without seq, directly setting a field in add2:

when true: # gitissue
  type Foo = object
    x: int
    x2: array[10, int]

  type Vec = object
    val: Foo

  proc `=destroy`*(a: var Foo) {.inline.} =
    discard

  proc initFoo(x: int): Foo = Foo(x: x)

  proc add2(v: var Vec, a: Foo) = # ditto with `a: sink Foo`
    v.val = a

  proc main()=
    var a: Vec
    # var a = default(Vec) # ditto
    # var a = Vec() # ditto
    var b = Foo(x: 10)
    a.add2 b # ok
    a.val = Foo(x: 10) # ok
    a.add2 initFoo(x = 10) # ok
    a.add2 Foo(x: 10) # bug
  main()

Additional Information

@ghost
Copy link

ghost commented Oct 22, 2020

@timotheecour both of these work for me on latest devel with both refc/arc, can you confirm?

Clyybber added a commit to Clyybber/Nim that referenced this issue Oct 22, 2020
Clyybber added a commit that referenced this issue Oct 22, 2020
@timotheecour
Copy link
Member Author

indeed, works, thanks! (and I rechecked that 52841db was still causing the above bug, so not sure what fixed it, but ok!)

narimiran pushed a commit that referenced this issue Nov 16, 2020
(cherry picked from commit 7435d91)
mildred pushed a commit to mildred/Nim that referenced this issue Jan 11, 2021
irdassis pushed a commit to irdassis/Nim that referenced this issue Mar 16, 2021
ardek66 pushed a commit to ardek66/Nim that referenced this issue Mar 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant