flyx / NimYAML

YAML implementation for Nim
https://nimyaml.org
Other
191 stars 36 forks source link

type mismatch in construction of sparse object #101

Closed quantimnot closed 1 year ago

quantimnot commented 3 years ago

I ran into this bug. I think it may be a bug in the compiler, but I wanted to check with you first. Have you seen this before? Any clue?

It exist in many compiler versions back to 0.14.0.

fType[1].kind == nnkSym returns nnkBracketExpr in:

proc getOptionInner(fType: NimNode): NimNode {.compileTime.} =
  if fType.kind == nnkBracketExpr and len(fType) == 2 and
      fType[1].kind == nnkSym:
    return newIdentNode($fType[1])
  else: return nil

Simplified Example:

import yaml, streams

when not defined skipBad:
  # this line is what is triggering it
  # it can be in another module and not exported, but still trigger the problem
  func `==`(a: Option[seq[string]], b: seq[string]): bool = discard

type
  StrSeq = seq[string]
  B {.sparse.} = object
    c: Option[StrSeq]
    d: string

var b: B

yaml.load(newStringStream("d: e"), b)

Current Output:

... yaml/serialization.nim(655, 29) Error: type mismatch: got <NimNode>
but expected one of:
proc none(T: typedesc): Option[T]
  first type mismatch at position: 1
  required type for T: typedesc
  but expression 'nil' is of type: NimNode
proc none[T](): Option[T]
  first type mismatch at position: 1
  extra argument given

expression: none(nil)
quantimnot commented 3 years ago

It's a compiler bug:

  proc hgjhgjhgjhg(a: Option[seq[string]], b: seq[string]): bool = discard
  type
    StrSeq = seq[string]
    B = object
      c: Option[StrSeq]
    D = Option[StrSeq]

  macro a(t: typedesc) =
    doAssert t.getType.getTypeInst[1].kind == nnkSym

  macro b(t: typedesc) =
    doAssert t.getType[1].getType[2].children.toSeq[0].getTypeInst[1].kind == nnkSym

  a D
  b B
flyx commented 3 years ago

Even if this is a compiler bug, it affects NimYAML and therefore the issue should be kept open – there could exist a workaround that can be implemented in NimYAML.

Can you link an issue of the Nim compiler describing this bug?

quantimnot commented 3 years ago

Nim issue: https://github.com/nim-lang/Nim/issues/19042

theAkito commented 1 year ago

Suddenly getting this error, even though all code worked before with the version I had used. Now, no version of NimYAML works anymore, because I always encounter this issue. Tried several commits from the past 3 months, I always end up at this error. A few commits cause additional issues, but these come before this one, so in the end, this is the biggest culprit.

How can I avoid this, without forking this project or tampering with the source code in the Nimble folder?

Ideally, I just want to nimble install this & have it work, just as before.

Error started happening, after I added another {.sparse.} object, but this does not make sense, because I already had another sparse object & it worked fine!

theAkito commented 1 year ago

@flyx

You ARE truly amazing! ❤️ 😍

THANK YOU

flyx commented 1 year ago

Thanks @theAkito, your comment on the Nim issue made me realize this can be fixed in NimYAML.

The type alias problem was simply a symptom of the real problem, which was that NimYAML wasn't able to process generic type instances in Option[…]. I pushed a fix and did some testing, the code shown in this issue now works. Can you confirm this fixes your problem as well (via pulling yaml@#head)? If so I will release a bugfix version.

theAkito commented 1 year ago

Can you confirm this fixes your problem as well (via pulling yaml@#head)? If so I will release a bugfix version.

Of course, will do, still today, but unfortunately not right now. Will return here ASAP!!

theAkito commented 1 year ago

@flyx

Works beautifully!

Thank you very much! ❤️

THANK YOU