nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.55k stars 1.47k forks source link

generic ref object typeRel problem #5621

Closed jangko closed 7 years ago

jangko commented 7 years ago
type
  Fruit[T] = ref object of RootObj
    color: T

  Apple[T, U] = ref object of Fruit[T]

proc getColor[T](v: Fruit[T]): T =
  v.color

proc newApple[T, U](x: T; y: U): Apple[T, U] =
  new(result)
  result.color = x

var e = newApple(13, "AX")
let x = getColor(e)

the error message:

bug7.nim(15, 17) Error: type mismatch: got (Apple[system.int, system.string])
but expected one of:
proc getColor[T](v: Fruit[T]): T

At first glance this one look similar with #5615, indeed this one was discovered during #5615 investigation. Although related, but they are distinct bug or to be precise they are cascading bugs.

5615 manifested itself if there are overloaded procs, while this one no overloaded proc present(getColor). They also produce different error message.

one shocking fact I found is, if the type definition changed slightly by removing the 'ref', the bug is gone. What shocked me is the internal representation of the instantiated type of tyobject vs tyref tyobject differs greatly, really unexpected. And the ref version appears to missing their tyGenericParam part, that's why this and #5615 occur.

type
  Fruit[T] = object of RootObj
    color: T

  Apple[T, U] = object of Fruit[T]

5615 appears as a regression after #5573 bugfix. This one already there undetected for a long time. Nim ver 0.15.0 already exibit this behaviour.

I believe this one must be fixed first before #5615, then fixing #5615 will be easy. But fixing this one is not easy, because the root of the problem lies deep somewhere, and perhaps already cancerous

vegansk commented 7 years ago

I guess that my library is affected by this bug too. Without commit 568c954062c203383be0073126b2e7090721364f it works fine.

tests/fp/test_option.nim(3, 7) template/generic instantiation from here
tests/fp/test_option.nim(5, 8) template/generic instantiation from here
tests/fp/test_option.nim(32, 13) Error: type mismatch: got (int literal(1), typedesc[Option])
but expected one of: 
proc point[A](v: A; t: typedesc[Option[A]]): Option[A]

stack trace: (most recent call last)
Nim/lib/system/nimscript.nim(294)
config.nims(46)          testTask
Nim/lib/system/nimscript.nim(206) exec
Nim/lib/system/nimscript.nim(206, 7) Error: unhandled exception: FAILED: nim test_int