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

Overloading generic proc on typedesc causes C compilation to fail #2637

Closed
bluenote10 opened this issue May 2, 2015 · 2 comments
Closed

Comments

@bluenote10
Copy link
Contributor

I guess the following problem is caused by overloading a generic proc on typedesc:

type
  OptionKind = enum
    None,
    Some

  Option*[T] = object
    case kind: OptionKind
    of None:
      discard
    of Some:
      value*: T

proc none*[T](): Option[T] =
  Option[T](kind: None)

proc none*(T: typedesc): Option[T] = none[T]()


proc test(): Option[int] =
  int.none

echo test()

This results in the following error regarding the C compilation:

/tmp/tmp.9fZIF9AH68/stdlib_system.c: In function ‘HEX24_90091’:
/tmp/tmp.9fZIF9AH68/stdlib_system.c:11193:22: error: expected identifier before ‘)’ token
   if (!(((2 &(1<<((x.)&7)))!=0))) raiseFieldError(((NimStringDesc*) &TMP145));

Version: devel c82cc7c

@jyapayne
Copy link
Contributor

jyapayne commented May 3, 2015

Looks like this has something to do with echoing the result (not sure how you would echo a discard statement).

If you put a $ proc with instructions on how to print the Option object, it compiles and runs fine.

type
  OptionKind = enum
    None,
    Some

  Option*[T] = object
    case kind: OptionKind
    of None:
      discard
    of Some:
      value*: T

proc `$`[T](opt: Option[T]): string =
    return $opt.kind

proc none*[T](): Option[T] =
  Option[T](kind: None)

proc none*(T: typedesc): Option[T] = none[T]()


proc test(): Option[int] =
  int.none

echo test()

@bluenote10
Copy link
Contributor Author

That's interesting. In my actual implementation I do have a $ proc, and (as far as I remember) I did not echo an Option, but I still get the error. So it looks like the error can somehow come back. I do not even have to call the none(typedesc) proc, just having it defined causes a problem. What is also interesting is that the error is not "in function" none but always in a function that is close by. For instance, I have this:

proc some*[T](value: T): Option[T] =
  Option[T](kind: Some, value: value)

proc none*[T](): Option[T] =
  Option[T](kind: None)

proc none*(T: typedesc): Option[T] = none[T]()

And get the error for the "some" function:

.../nimcache/option.c: In function ‘some_765109’:
.../nimcache/option.c:656:24: error: expected identifier before ‘)’ token
  if (!(((2 &(1<<((LOC1.)&7)))!=0))) raiseFieldError(((NimStringDesc*) &TMP687));

Another surprise: If I reverse the order of the some and none functions, it works! Update: No, error came back, depending on use on client side...

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

3 participants