NimYAML icon indicating copy to clipboard operation
NimYAML copied to clipboard

type mismatch in construction of sparse object

Open quantimnot opened this issue 2 years ago • 3 comments

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 avatar Oct 23 '21 20:10 quantimnot

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

quantimnot avatar Oct 23 '21 21:10 quantimnot

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?

flyx avatar Oct 24 '21 10:10 flyx

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

quantimnot avatar Oct 24 '21 12:10 quantimnot

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 avatar Nov 05 '23 02:11 theAkito

@flyx

You ARE truly amazing! ❤️ 😍

THANK YOU

theAkito avatar Nov 05 '23 14:11 theAkito

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.

flyx avatar Nov 05 '23 14:11 flyx

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 avatar Nov 05 '23 14:11 theAkito

@flyx

Works beautifully!

Thank you very much! ❤️

THANK YOU

theAkito avatar Nov 05 '23 18:11 theAkito