Nim icon indicating copy to clipboard operation
Nim copied to clipboard

Generic overload with `ref object of RootObj` type param not called; regression in Nim 2.0.16/version-2-0

Open nitely opened this issue 3 months ago • 1 comments

Nim Version

Nim Compiler Version 2.0.17 [Linux: amd64] Compiled at 2025-09-12 Copyright (c) 2006-2023 by Andreas Rumpf

active boot switches: -d:release

Description

The overload won't work when ref object of RootObj is the first parameter, otherwise it works.

type Obj = ref object of RootObj

proc foo(s: Obj, x: SomeFloat) =
  echo "SomeFloat"
template foo(s: Obj, x: auto) =
  echo "auto"

foo(Obj(), 1.23)

proc bar[T: SomeFloat](s: Obj, x: T) =
  echo "SomeFloat"
proc bar[T](s: Obj, x: T) =
  echo "any"

bar(Obj(), 1.23)

# the following works

proc baz[T: SomeFloat](x: T, s: Obj) =
  echo "SomeFloat"
proc baz[T](x: T, s: Obj) =
  echo "any"

baz(1.23, Obj())

type Obj2 = ref object

proc quz[T: SomeFloat](s: Obj2, x: T) =
  echo "SomeFloat"
proc quz[T](s: Obj2, x: T) =
  echo "any"

quz(Obj2(), 1.23)

Current Output

auto
any
SomeFloat
SomeFloat

Expected Output

SomeFloat
SomeFloat
SomeFloat
SomeFloat

Known Workarounds

No response

Additional Information

works as expected in Nim 2.0.8, 2.2.4 and devel the regression is present in 2.0.10 to 2.0.16 and version-2-0 branch (commit edeac84e476fe77edfdf693d69d2bf9a912a5e05)

nitely avatar Sep 12 '25 19:09 nitely

Another overload regression in 2.0.16:

type FBase {.inheritable, pure.} = object
type FA = object of FBase
type Obj[T] = object

proc foo[U: SomeFloat](s: Obj[FA], x: U) =
  echo "SomeFloat"
proc foo[U](s: Obj[FA], x: U) =
  echo "auto"

foo(Obj[FA](), 1.23)
# outputs: auto
# expected: SomeFloat

proc bar(s: Obj[FA], x: SomeFloat) =
  echo "SomeFloat"
proc bar(s: Obj[FA], x: auto) =
  echo "auto"

bar(Obj[FA](), 1.23)

proc baz[T: Obj[FA], U: SomeFloat](s: T, x: U) =
  echo "SomeFloat"
proc baz[T: Obj[FA], U](s: T, x: U) =
  echo "auto"

baz(Obj[FA](), 1.23)

nitely avatar Sep 29 '25 16:09 nitely