From 5e17c884160dc79bc98fd4d71e29bcf6e2f9b492 Mon Sep 17 00:00:00 2001 From: metagn Date: Wed, 25 Jun 2025 16:42:26 +0300 Subject: [PATCH] fix generic converter subtype match regression (#25015) fixes #25014 `implicitConv` tries to instantiate the supertype to convert to, previously the bindings of `m` was shared with the bindings of the converter but now an isolated match `convMatch` holds the bindings, so `convMatch` is now used in the call to `implicitConv` instead of `m` so that its bindings are used when instantiating the supertype. (cherry picked from commit 97a6f42b5688307293c4f11484d9fcae15db9ee0) --- compiler/sigmatch.nim | 3 ++- tests/converter/tgenericsubtypeconverter.nim | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 tests/converter/tgenericsubtypeconverter.nim diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index 6324b157b1..5094e61aed 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -2328,7 +2328,8 @@ proc userConvMatch(c: PContext, m: var TCandidate, f, a: PType, # it is correct var param: PNode = nil if srca == isSubtype: - param = implicitConv(nkHiddenSubConv, src, copyTree(arg), m, c) + # convMatch used here to use its bindings to instantiate subtype: + param = implicitConv(nkHiddenSubConv, src, copyTree(arg), convMatch, c) elif src.kind in {tyVar}: # Analyse the converter return type. param = newNodeIT(nkHiddenAddr, arg.info, s.typ.firstParamType) diff --git a/tests/converter/tgenericsubtypeconverter.nim b/tests/converter/tgenericsubtypeconverter.nim new file mode 100644 index 0000000000..c364c5e127 --- /dev/null +++ b/tests/converter/tgenericsubtypeconverter.nim @@ -0,0 +1,14 @@ +# issue #25014 + +type + FooBase[T] {.inheritable.} = object + val: T + FooChild[T] = object of FooBase[T] + +converter toValue*[T](r: FooBase[T]): T = r.val + +proc foo(a: int) = discard +var f: FooChild[int] +foo(f) +proc fooGeneric[T](a: T) = discard +fooGeneric(f)