diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index 953dcfa747..2159abecde 100755 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -97,6 +97,45 @@ proc copyCandidate(a: var TCandidate, b: TCandidate) = a.baseTypeMatch = b.baseTypeMatch copyIdTable(a.bindings, b.bindings) +proc sumGeneric(t: PType): int = + var t = t + while true: + case t.kind + of tyGenericInst, tyArray, tyRef, tyPtr, tyDistinct, tyArrayConstr, + tyOpenArray, tyVarargs, tySet, tyRange, tySequence, tyGenericBody: + t = t.lastSon + inc result + of tyVar: + # but do not make 'var T' more specific than 'T'! + t = t.sons[0] + of tyGenericInvokation, tyTuple: + result = ord(t.kind == tyGenericInvokation) + for i in 0 .. b.sumGeneric + + if a.len > 1 and b.len > 1: + let aa = a.sons[1].sumGeneric + let bb = b.sons[1].sumGeneric + var a = a + var b = b + + if aa < bb: swap(a, b) + # all must be better + for i in 2 .. 0: return false return false -proc `$`*[T: tuple](x: T): string = +proc `$`*[T: tuple|object](x: T): string = ## generic ``$`` operator for tuples that is lifted from the components ## of `x`. Example: ## diff --git a/tests/run/toverl3.nim b/tests/run/toverl3.nim new file mode 100755 index 0000000000..b3e0f2fa7c --- /dev/null +++ b/tests/run/toverl3.nim @@ -0,0 +1,20 @@ +discard """ + file: "toverl3.nim" + output: '''m1 +tup1''' +""" + +# Tests more specific generic match: + +proc m[T](x: T) = echo "m2" +proc m[T](x: var ref T) = echo "m1" + +proc tup[S, T](x: tuple[a: S, b: ref T]) = echo "tup1" +proc tup[S, T](x: tuple[a: S, b: T]) = echo "tup2" + +var + obj: ref int + tu: tuple[a: int, b: ref bool] + +m(obj) +tup(tu) diff --git a/tests/run/toverlop.nim b/tests/run/toverlop.nim deleted file mode 100755 index ce302345f7..0000000000 --- a/tests/run/toverlop.nim +++ /dev/null @@ -1,16 +0,0 @@ -discard """ - file: "toverlop.nim" - output: "3" -""" -# Test operator overloading - -proc `%` (a, b: int): int = - return a mod b - -var x, y: int -x = 15 -y = 6 -write(stdout, x % y) -#OUT 3 - -