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

weird ObjectAssignmentDefect runtime errors on based object class assignment #17602

Open
timotheecour opened this issue Apr 1, 2021 · 3 comments · May be fixed by #24071
Open

weird ObjectAssignmentDefect runtime errors on based object class assignment #17602

timotheecour opened this issue Apr 1, 2021 · 3 comments · May be fixed by #24071

Comments

@timotheecour
Copy link
Member

timotheecour commented Apr 1, 2021

Example 1

when true:
  type A = object of RootObj
    x: int
  type B = object of A
    y: int
  proc fn1(a: var A) =
    echo (a,) # bug: ObjectAssignmentDefect
  proc fn2(a: A) =
    echo a.x # ok
    echo $a # ok
    echo ($a,) # ok
    echo (a,) # BUG

  proc main =
    var b = B(x: 3, y:4)
    fn2(b) # crash in RT, no crash in VM
    # fn1(b) # ditto

  main()
  static: main()

Current Output

  • works in CT
  • works in js
  • works in gc:arc
  • crashes in RT with gc:refc
3
(x: 3)
("(x: 3)",)
((x: 3),)
Hint: 55209 lines; 0.497s; 75.797MiB peakmem; Debug build; proj: /Users/timothee/git_clone/nim/timn/tests/nim/all/t12117.nim; out: /Users/timothee/git_clone/nim/timn/build/nimcache/t12117 [SuccessX]
/Users/timothee/git_clone/nim/Nim_devel/compiler/main.nim(377, 15) compiler msg initiated here [MsgOrigin]
3
(x: 3)
("(x: 3)",)
/Users/timothee/git_clone/nim/timn/tests/nim/all/t12117.nim(66) t12117
/Users/timothee/git_clone/nim/timn/tests/nim/all/t12117.nim(63) main
/Users/timothee/git_clone/nim/timn/tests/nim/all/t12117.nim(59) fn2
/Users/timothee/git_clone/nim/Nim_devel/lib/system/assign.nim(143) genericAssign
/Users/timothee/git_clone/nim/Nim_devel/lib/system/assign.nim(129) genericAssignAux
/Users/timothee/git_clone/nim/Nim_devel/lib/system/fatal.nim(53) sysFatal
Error: unhandled exception: invalid object assignment [ObjectAssignmentDefect]

Expected Output

  • VM, RT, js should have same behavior
  • that behavior should be that it works
    (similar code works in C++)
  • even if it wasn't supposed to work, it should give CT error, not RT crash

Example 2

when true:
  type A = object of RootObj
    x: int
  type B = object of A
    y: int

  proc main =
    var b: B
    var a: A
    a = b # VM or js: no crash, RT: ObjectAssignmentDefect

  static: main()
  main()

same behavior as Example 1

Additional Information

1.5.1 fe9a37f

links

similar code works in C++

#include <iostream>
#include <stdio.h>

struct A{int x;};
struct B: public A{int y;};
void fn(A a){
  printf("%d\n", a.x);
}
int main (int argc, char *argv[]) {
  A a;
  B b;
  b.x = 3;
  b.y = 4;
  a = b;
  fn(a);
  fn(b);
  return 0;
}
@ringabout
Copy link
Member

ringabout commented Apr 1, 2021

You can use ref object.

@timotheecour
Copy link
Member Author

timotheecour commented Apr 1, 2021

it should work IMO (eg suppose i want stack allocation and don't want ref)

  • similar code (without pointer) works in C++
  • it works in js, CT, gc:arc

IMO it should work in gc:refc

@ringabout
Copy link
Member

ringabout commented Apr 1, 2021

Related:
#11592
https://forum.nim-lang.org/t/7733

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