-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Assignment overloading: infinite recursion even with system.=
#13024
Comments
And a slightly different case, adding a =sink recreates the problem even with a main proc type
Foo = object
Bar = object
foo: Foo
proc `=sink`*(dst: var Foo, src: Foo) {.inline.}
proc `=`*(dest: var Foo, src: Foo) =
echo "hello World!"
system.`=`(dest, src)
proc `=sink`*(dst: var Foo, src: Foo) {.inline.} =
system.`=sink`(dst, src)
proc initFoo*(): Foo =
discard
proc initBar*(foo: sink Foo): Bar =
echo "hi"
result.foo = foo
echo "ho"
proc main =
let x = initFoo()
echo "ha"
let y = initBar(x)
main() The forward declaration is a workaround for #13025 |
Ah it was not local variables but the sink annotation that solved the issue here, this recurses infinitely: type
Foo = object
Bar = object
foo: Foo
proc `=`*(dest: var Foo, src: Foo) =
echo "hello World!"
system.`=`(dest, src)
proc initFoo*(): Foo =
discard
proc initBar*(foo: Foo): Bar =
echo "hi"
result.foo = foo
echo "ho"
proc main =
let x = initFoo()
echo "ha"
let y = initBar(x)
main() |
=
=
Hi @mratsim, Possibly you wanted: `=`(ParentType(a), ParentType(b)) |
It can be closed as won't fix then. However, I expect others will encounter this issue as in the past this was the recommended pattern to overload I was trying to do custom atomic ref counting. So after discussing with @Araq what I wanted to do is: type
Foo = object
p: FooImpl
FooImp = ptr object
proc `=`(dst: var Foo, src: Foo) =
# refcounting goes here
system.`=`(dst.p, src.p) # Notice that I don't copy the dst/src but the fields to avoid recursion |
only the |
(deleting duplicate comment) |
The following will lead to infinite Hello World being printed
Having local variables instead works (cc @Clyybber)
The text was updated successfully, but these errors were encountered: