From 36b7870c982358f097333b0c12e005317ad6233c Mon Sep 17 00:00:00 2001 From: metagn Date: Sat, 15 Feb 2025 09:12:30 +0300 Subject: [PATCH 1/4] match generic invocations to generic body types --- compiler/sigmatch.nim | 17 +++++++++++++++++ tests/overload/tgenericbodymatch.nim | 13 +++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/overload/tgenericbodymatch.nim diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index 950ebe5196d66..885c85868a133 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -1745,6 +1745,23 @@ proc typeRel(c: var TCandidate, f, aOrig: PType, elif x.kind == tyGenericInst and concpt.kind == tyConcept: result = if concepts.conceptMatch(c.c, concpt, x, c.bindings, f): isGeneric else: isNone + elif x.kind == tyGenericBody: + # pretend we are matching against invocation type + let base = x[0] + if base == f[0]: + for i in 1..= base.len-1: return + # least specific type that the parameter satisfies: + let constraint = + if base[i].genericParamHasConstraints: + base[i].genericConstraint + else: + # same as in liftParamType + newTypeS(tyAnything, c.c) + # match against constraint + let tr = typeRel(c, f[i], constraint, {trDontBind}) + if tr <= isSubtype: return + result = isGeneric else: let genericBody = f[0] var askip = skippedNone diff --git a/tests/overload/tgenericbodymatch.nim b/tests/overload/tgenericbodymatch.nim new file mode 100644 index 0000000000000..2dd3e79c10997 --- /dev/null +++ b/tests/overload/tgenericbodymatch.nim @@ -0,0 +1,13 @@ +# issue #24688 + +type + R[T, E] = object + A = object + B = object + +# Remove this overload to make it work +func err[T](_: type R[T, void]): R[T, void] = discard + +func err(_: type R): R[A, B] = discard + +discard R.err() From 74bb960212c0830462ece1bed05d097fffc2783f Mon Sep 17 00:00:00 2001 From: metagn Date: Sat, 15 Feb 2025 09:14:19 +0300 Subject: [PATCH 2/4] do not match bodies to invocations at all --- compiler/sigmatch.nim | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index 885c85868a133..bface2398ba3b 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -1745,23 +1745,9 @@ proc typeRel(c: var TCandidate, f, aOrig: PType, elif x.kind == tyGenericInst and concpt.kind == tyConcept: result = if concepts.conceptMatch(c.c, concpt, x, c.bindings, f): isGeneric else: isNone - elif x.kind == tyGenericBody: - # pretend we are matching against invocation type - let base = x[0] - if base == f[0]: - for i in 1..= base.len-1: return - # least specific type that the parameter satisfies: - let constraint = - if base[i].genericParamHasConstraints: - base[i].genericConstraint - else: - # same as in liftParamType - newTypeS(tyAnything, c.c) - # match against constraint - let tr = typeRel(c, f[i], constraint, {trDontBind}) - if tr <= isSubtype: return - result = isGeneric + elif x.kind == tyGenericBody and f[0] == x: + # not specific enough + result = isNone else: let genericBody = f[0] var askip = skippedNone From 6ce4e70f0efbfadcaadbd02b48490adfd03290b8 Mon Sep 17 00:00:00 2001 From: metagn Date: Sat, 15 Feb 2025 10:21:55 +0300 Subject: [PATCH 3/4] update #5632 test --- tests/generics/tgenerics_issues.nim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/generics/tgenerics_issues.nim b/tests/generics/tgenerics_issues.nim index 3068a22f25be2..2cda8d3b518ad 100644 --- a/tests/generics/tgenerics_issues.nim +++ b/tests/generics/tgenerics_issues.nim @@ -741,7 +741,8 @@ block t1684: block t5632: type Option[T] = object - proc point[A](v: A, t: typedesc[Option[A]]): Option[A] = + # original issue test uses Option[A] instead + proc point[A](v: A, t: typedesc[Option]): Option[A] = discard discard point(1, Option) From 04984ceab23ff4178a8b469473d6933f2449b1de Mon Sep 17 00:00:00 2001 From: metagn Date: Sat, 29 Mar 2025 03:21:53 +0300 Subject: [PATCH 4/4] test arraymancer patch --- testament/important_packages.nim | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/testament/important_packages.nim b/testament/important_packages.nim index 2471a2d113186..ed5344fe27f00 100644 --- a/testament/important_packages.nim +++ b/testament/important_packages.nim @@ -35,7 +35,9 @@ proc pkg(name: string; cmd = "nimble test -l"; url = "", useHead = true, allowFa pkg "alea" pkg "argparse" -pkg "arraymancer", "nimble install -y; nimble uninstall -i -y nimcuda; nimble install nimcuda@0.2.1; nim c tests/tests_cpu.nim" +pkg "arraymancer", "nimble install -y; nimble uninstall -i -y nimcuda; nimble install nimcuda@0.2.1; nim c tests/tests_cpu.nim", + # remove when https://github.com/mratsim/Arraymancer/pull/674 is merged + url = "https://github.com/metagn/Arraymancer" pkg "ast_pattern_matching", "nim c -r tests/test1.nim" pkg "asyncftpclient", "nimble compileExample" pkg "asyncthreadpool", "nimble test --mm:refc"