From aa1cdebdc206857902081b4862dad2c2990179e8 Mon Sep 17 00:00:00 2001 From: cooldome Date: Mon, 13 Aug 2018 15:02:20 +0200 Subject: [PATCH] Converters to take into account constraints. Fixes #7520 (#8593) --- compiler/sigmatch.nim | 4 ++++ .../converter/tconverter_with_constraint.nim | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 tests/converter/tconverter_with_constraint.nim diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index 5afa1b0312..c665aad7f2 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -1809,6 +1809,10 @@ proc userConvMatch(c: PContext, m: var TCandidate, f, a: PType, # see tests/tgenericconverter: let srca = typeRel(m, src, a) if srca notin {isEqual, isGeneric, isSubtype}: continue + + let constraint = c.converters[i].typ.n[1].sym.constraint + if not constraint.isNil and not matchNodeKinds(constraint, arg): + continue let destIsGeneric = containsGenericType(dest) if destIsGeneric: diff --git a/tests/converter/tconverter_with_constraint.nim b/tests/converter/tconverter_with_constraint.nim new file mode 100644 index 0000000000..7932644340 --- /dev/null +++ b/tests/converter/tconverter_with_constraint.nim @@ -0,0 +1,20 @@ + +discard """ + file: "tconverter_with_constraint.nim" + line: 20 + errormsg: "type mismatch: got " +""" + +type + MyType = distinct int + +converter to_mytype(m: int{lit}): MyType = + m.MyType + +proc myproc(m: MyType) = + echo m.int, ".MyType" + +myproc(1) # call by literal is ok + +var x: int = 12 +myproc(x) # should fail \ No newline at end of file