Skip to content

Generic overload is incorrectly matched #25433

@ire4ever1190

Description

@ire4ever1190

Nim Version

Nim Compiler Version 2.3.1 [Linux: amd64]
Compiled at 2026-01-10
Copyright (c) 2006-2026 by Andreas Rumpf

git hash: 01eedd9
active boot switches: -d:release

Description

Reproducible case is this

import std/options

type
  Combinator*[T] = proc (): Option[T] {.closure.}
    ## Parser that optionally returns data

proc fin*(): Option[bool] = some(true)

# This is the overload that should be matched
proc `*`*[T](left: Combinator[T], right: Combinator[bool]): Combinator[bool] =
  return proc (): Option[bool] =
    echo right().get()

# Instead this one is selected
proc `*`*[T](left: Combinator[T], right: Combinator[seq[T]]): Combinator[seq[T]] =
  return proc (): Option[seq[T]] =
    echo right().get()

proc foo(): Option[string] = discard

# The type must be Combinator[string], if its the normal proc type then the type checking
# correctly fails
# let bar = foo
let bar: Combinator[string] = foo

let t = bar * fin
assert type(t()) is Option[bool], "Wrong overload selected"
echo t()

For some reason it is selecting the second overload instead of the first which caused a nil access error since it thought right was returning a string. Behavior only happens when I haven't explicitly set the type to be Combinator[string], making the type be proc (): Option[string] makes the code work correctly

Current Output

/home/user/Documents/projects/nort/test.nim(26) test
/home/user/.choosenim/toolchains/nim-hashdevel/lib/std/assertions.nim(45) failedAssertImpl
/home/user/.choosenim/toolchains/nim-hashdevel/lib/std/assertions.nim(40) raiseAssert
/home/user/.choosenim/toolchains/nim-hashdevel/lib/system/fatal.nim(53) sysFatal
Error: unhandled exception: /home/user/Documents/projects/nort/test.nim(26, 1) `type(t()) is Option[bool]` Wrong overload selected [AssertionDefect]
Error: execution of an external program failed: '/home/user/.cache/nim/test_d/test_951333475FBBAD2DA50919906415803BCCFC2541'

# When I remove the assertion
Traceback (most recent call last)
/home/user/Documents/projects/nort/test.nim(27) test
/home/user/Documents/projects/nort/test.nim(17) :anonymous
/home/user/.choosenim/toolchains/nim-hashdevel/lib/system/dollars.nim(133) $
/home/user/.choosenim/toolchains/nim-hashdevel/lib/system/dollars.nim(116) collectionToString
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Error: execution of an external program failed: '/home/user/.cache/nim/test_d/test_951333475FBBAD2DA50919906415803BCCFC2541'

Expected Output

true
none(bool)

Known Workarounds

No response

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions