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

Invalid C code generated when creating an Option out of deref'd value, which contains a reference to its own type, gotten from an Option which is None at compile time. #14387

Open
kayabaNerve opened this issue May 18, 2020 · 3 comments

Comments

@kayabaNerve
Copy link
Collaborator

kayabaNerve commented May 18, 2020

As the title says, a very specific case involving Options causes Nim to generate invalid C.

Example

import options

type Nested = ref object
  child: Option[Nested]

let x = none(Nested)
discard some(Nested()[])

Current Output

~/.cache/nim/test_codegen_d/stdlib_options.nim.c: In function ‘some__9ahAc9c9c9bfjwxun9c9c2ibgAFg’:
~/.cache/nim/test_codegen_d/stdlib_options.nim.c:324:21: error: ‘tyObject_Option__LdGjTDNxAxkpdsbs9c9aMtGQ {aka struct tyObject_Option__LdGjTDNxAxkpdsbs9c9aMtGQ}’ has no member named ‘has’
  (*Result).val.child.has = val.child.has;
                     ^
~/.cache/nim/test_codegen_d/stdlib_options.nim.c:324:37: error: ‘tyObject_Option__LdGjTDNxAxkpdsbs9c9aMtGQ {aka struct tyObject_Option__LdGjTDNxAxkpdsbs9c9aMtGQ}’ has no member named ‘has’
  (*Result).val.child.has = val.child.has;
                                     ^
Error: execution of an external compiler program 'gcc -c  -w -pthread   -I'~/.choosenim/toolchains/nim-#devel/lib' -o ~/.cache/nim/test_codegen_d/stdlib_options.nim.c.o ~/.cache/nim/test_codegen_d/stdlib_options.nim.c' failed with exit code: 1

Expected Output

A runtime error that you can't get a value from None.

Additional Information

  • Inlining any of this causes valid code to be generated.
  • Using a non-circular type also causes valid code to be generated.
  • Using some instead of none also causes valid code to be generated.

Tested on 1.2.0 and the latest devel.

@kayabaNerve
Copy link
Collaborator Author

kayabaNerve commented May 18, 2020

Initially uploaded the wrong code; fixed.

@kayabaNerve kayabaNerve changed the title Invalid C code generated when creating an Option out of deref'd value gotten from an Option which is None at compile time. Invalid C code generated when creating an Option out of deref'd value, which contains a reference to its own type, gotten from an Option which is None at compile time. May 18, 2020
@ghost
Copy link

ghost commented Oct 19, 2020

Still an issue

@shirleyquirk
Copy link
Contributor

related to #12695
when reducing that bug, which also involves a nested type:

type
  Foo[T] = object
    when T is ptr:
      value:T
    else:
      notvalue:T
proc access[A](self:A) = discard
proc store[T](self: var Foo[ptr T]) = access(self.value)

type
  MyType = ptr object
    next: Foo[MyType]

var f:Foo[MyType]

f.store()

i get

CC: bug1.nim
/home/bwsq/.cache/nim/bug1_d/@mbug1.nim.c: In function ‘store__bug49_29’:
/home/bwsq/.cache/nim/bug1_d/@mbug1.nim.c:82:34: error: ‘tyObject_Foo__D9a9
crXqyX59c9a5NYkXwC5XKw’ has no member named ‘value’; did you mean ‘notvalue
’?
   82 |         access__bug49_35((*self).value);
      |                                  ^~~~~
      |                                  notvalue

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