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

When self-referenced optional field is defined inside a ref type then the option type behaves in a wrong way during an assignment #24133

Open
KarolBajkowski opened this issue Sep 18, 2024 · 2 comments

Comments

@KarolBajkowski
Copy link

KarolBajkowski commented Sep 18, 2024

Description

When the type contains a self-referenced optional field then during the variable assignment (value copy takes place I believe), the field 'isSome' has a wrong value. Below are the examples:

This works fine:

import options

type Foo = ref object
    name: string

var foo1: Option[Foo] = none(Foo)
var foo2: Option[Foo] = foo1
if foo2.isSome:
    echo "SHOULD NOT BE REACHABLE"
else:
    echo "OK"

It will print: "OK"

however, when I add 'prev' field, this program misbehaves:

import options

type Foo = ref object
    name: string
    prev: Option[Foo]    # <-- this has been added

var foo1: Option[Foo] = none(Foo)
var foo2: Option[Foo] = foo1
if foo2.isSome:
    echo "SHOULD NOT BE REACHABLE"
else:
    echo "OK"

It will print "SHOULD NOT BE REACHABLE" which is an unexpected result.

Nim Version

Nim Compiler Version 2.0.8 [Windows: amd64]
Compiled at 2024-07-03
Copyright (c) 2006-2023 by Andreas Rumpf

Current Output

No response

Expected Output

No response

Known Workarounds

import options

type 
    Foo = ref FooObj
    FooObj = object
        name: string
        abc: Option[Foo]

var foo1: Option[Foo] = none(Foo)
var foo2: Option[Foo] = foo1
if foo2.isSome:
    echo "SHOULD NOT BE REACHABLE"
else:
    echo "OK"

Additional Information

No response

@metagn
Copy link
Collaborator

metagn commented Sep 18, 2024

Related #16754

@KarolBajkowski
Copy link
Author

@metagn Actually it might be exactly the same. I refined my example. It looks like this one also misbehaves:

import options

type Foo = ref object
    name: string
    abc: Option[Foo]

var foo1: Option[Foo] = none(Foo)
var foo2: Option[Foo] = foo1
if foo2.isSome:
    echo "SHOULD NOT BE REACHABLE"
else:
    echo "OK"

It prints "SHOULD NOT BE REACHABLE" as well.

@KarolBajkowski KarolBajkowski changed the title When 'prev' field is defined inside a type then the option type behaves in a wrong way during an assignment When self referenced field is defined inside a ref type then the option type behaves in a wrong way during an assignment Sep 18, 2024
@KarolBajkowski KarolBajkowski changed the title When self referenced field is defined inside a ref type then the option type behaves in a wrong way during an assignment When self referenced optional field is defined inside a ref type then the option type behaves in a wrong way during an assignment Sep 18, 2024
@KarolBajkowski KarolBajkowski changed the title When self referenced optional field is defined inside a ref type then the option type behaves in a wrong way during an assignment When self-referenced optional field is defined inside a ref type then the option type behaves in a wrong way during an assignment Sep 18, 2024
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

2 participants