Nim icon indicating copy to clipboard operation
Nim copied to clipboard

Option not playing nice with ref type

Open PMunch opened this issue 4 years ago • 7 comments

ARC generates bad C code for Option of a nested ref type.

Example

import options

type
  Person = ref object
    parent: Option[Person]

proc newPerson(parent: Option[Person]): Person =
  Person(parent: parent)

var person = newPerson(none(Person))

Current Output

/home/peter/.cache/nim/test_d/@mtest.nim.c: In function ‘newPerson__jROa2s0gVAb4xKj5JL9bVWw’:
/home/peter/.cache/nim/test_d/@mtest.nim.c:205:15: error: ‘tyObject_Option__41UD9alCHc2M9bOv9crGpxoLw’ has no member named ‘has’
  205 |  (*T1_).parent.has = colontmpD_.has;
      |               ^
/home/peter/.cache/nim/test_d/@mtest.nim.c:205:32: error: ‘tyObject_Option__41UD9alCHc2M9bOv9crGpxoLw’ has no member named ‘has’
  205 |  (*T1_).parent.has = colontmpD_.has;
      |                                ^

Possible Solution

Not sure what causes this, possibly related to https://github.com/nim-lang/Nim/issues/14387 and https://github.com/nim-lang/Nim/issues/9566, but this only happens for ARC. A workaround is to split the type:

import options

type
  Person = ref PersonObj
  PersonObj = object
    parent: Option[Person]

proc newPerson(parent: Option[Person]): Person =
  Person(parent: parent)

var person = newPerson(none(Person))

Additional Information

Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2021-01-19
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: 580b8f744b288e2069792ff4181743e4e588eced
active boot switches: -d:release

PMunch avatar Jan 19 '21 11:01 PMunch