From 497e1e04bb5f83272213cba1a0a44a416b939249 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Fri, 22 May 2026 23:41:21 +0800 Subject: [PATCH] fix: improve generic subtype handling in typeRel function --- compiler/sigmatch.nim | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index 994a2a6bc2..0d6b0b9ab2 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -1754,22 +1754,21 @@ proc typeRel(c: var TCandidate, f, aOrig: PType, considerPreviousT: if a == f or a.kind == tyGenericInst and a.skipGenericAlias[0] == f: bindingRet isGeneric - var depth = -1 - # Generic constraints like `F: Future` reach sigmatch as a `tyGenericBody`. - # Accept descendants of the generic family here as well, not just direct - # `Future[...]` instantiations. - if isGenericSubtype(c, a, f, depth, f): - if depth > 0: + let ff = last(f) + if ff != nil: + result = typeRel(c, ff, a, flags) + if result == isNone and a.kind == tyGenericInst: + var depth = -1 + # Generic constraints like `F: Future` can miss in `last(f)` when the + # actual type inherits from a concrete generic instantiation. + if isGenericSubtype(c, a, f, depth, f) and depth > 0: var askip = skippedNone let aobj = a.skipToObject(askip) if aobj != nil and tfFinal notin aobj.flags: # Keep overload ranking consistent with other inheritance-based # matches: deeper descendants are slightly worse candidates. inc c.inheritancePenalty, depth + int(c.inheritancePenalty < 0) - bindingRet(if depth == 0: isGeneric else: isSubtype) - let ff = last(f) - if ff != nil: - result = typeRel(c, ff, a, flags) + result = isGeneric of tyGenericInvocation: var x = a.skipGenericAlias if x.kind == tyGenericParam and x.len > 0: