fixes #22936; Generic inheritance matching gives type mismatch when object has members (#25836)

fixes #22936

This pull request improves the compiler's handling of generic type
constraints, specifically for subtypes of generics, and adds a test to
cover this behavior. The main changes are an enhancement to the type
relationship logic in the compiler and a new test case for generic
subtyping with `Future`.

### Compiler improvements for generic subtyping

* Updated `typeRel` in `compiler/sigmatch.nim` to allow generic
constraints (like `F: Future`) to accept not just direct instantiations
but also descendants of the generic family, ensuring more flexible and
correct overload resolution. Inheritance depth is now considered for
overload ranking, making deeper descendants slightly less preferred,
consistent with other inheritance-based matches.

### New test coverage

* Added a test in `tests/typerel/t8905.nim` to verify that generic
constraints correctly accept subtypes of `Future`, including a custom
`B[T, E] = ref object of Future[T]` type, and that overloads like
`take`, `takeMany`, and the macro `checkFutures` work as expected with
these types.
This commit is contained in:
ringabout
2026-06-08 15:12:00 +08:00
committed by GitHub
parent 9b80b2e868
commit 1d7510dff0
2 changed files with 43 additions and 0 deletions

View File

@@ -5,3 +5,31 @@ type
proc newFoo[T](): Foo[T] = Foo[T](newSeq[T]())
var x = newFoo[Bar[int]]()
# issue #22936
import std/macros
type
InternalFutureBase = object of RootObj
FutureBase = ref object of InternalFutureBase
Future[T] = ref object of FutureBase
internalValue: T
B[T, E] = ref object of Future[T]
proc take[F: Future](fut: F) = discard
proc takeMany[F: Future](futs: seq[F]) = discard
macro checkFutures[F: Future](futs: seq[F]): untyped =
newEmptyNode()
var future: B[void, void]
var futures: seq[B[void, void]]
take(future)
takeMany(futures)
checkFutures(futures)