From f6f21d1f3d6a14d52511a95287cb231ffa3e1679 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Fri, 16 Jun 2023 18:06:50 +0800 Subject: [PATCH] fixes #22049; fixes #22054; implicit conversion keeps varness (#22097) * fixes #22054; codegen for var tuples conv * rethink fixes * add test cases * templates only * fixes var tuples * keep varness no matter what * fixes typ.isNil * make it work for generics * restore isSubrange * add a test case as requested --- compiler/sigmatch.nim | 8 +++++++- tests/errmsgs/t22097.nim | 9 +++++++++ tests/tuples/ttuples_various.nim | 26 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/errmsgs/t22097.nim diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim index dc723c4f1d389..24b4779ef4846 100644 --- a/compiler/sigmatch.nim +++ b/compiler/sigmatch.nim @@ -1934,7 +1934,13 @@ proc implicitConv(kind: TNodeKind, f: PType, arg: PNode, m: TCandidate, else: result.typ = errorType(c) else: - result.typ = f.skipTypes({tySink, tyVar}) + result.typ = f.skipTypes({tySink}) + # keep varness + if arg.typ != nil and arg.typ.kind == tyVar: + result.typ = toVar(result.typ, tyVar, c.idgen) + else: + result.typ = result.typ.skipTypes({tyVar}) + if result.typ == nil: internalError(c.graph.config, arg.info, "implicitConv") result.add c.graph.emptyNode result.add arg diff --git a/tests/errmsgs/t22097.nim b/tests/errmsgs/t22097.nim new file mode 100644 index 0000000000000..b50db08a39719 --- /dev/null +++ b/tests/errmsgs/t22097.nim @@ -0,0 +1,9 @@ +discard """ + errormsg: "for a 'var' type a variable needs to be passed; but 'uint16(x)' is immutable" +""" + +proc toUInt16(x: var uint16) = + discard + +var x = uint8(1) +toUInt16 x \ No newline at end of file diff --git a/tests/tuples/ttuples_various.nim b/tests/tuples/ttuples_various.nim index dc060da1e24ea..97bc70bd286d1 100644 --- a/tests/tuples/ttuples_various.nim +++ b/tests/tuples/ttuples_various.nim @@ -171,3 +171,29 @@ block tuple_with_seq: echo s (s, 7) t = test(t.a) + +block: # bug #22049 + type A = object + field: tuple[a, b, c: seq[int]] + + func value(v: var A): var tuple[a, b, c: seq[int]] = + v.field + template get(v: A): tuple[a, b, c: seq[int]] = v.value + + var v = A(field: (@[1], @[2], @[3])) + var (a, b, c) = v.get() + + doAssert a == @[1] + doAssert b == @[2] + doAssert c == @[3] + +block: # bug #22054 + type A = object + field: tuple[a: int] + + func value(v: var A): var tuple[a: int] = + v.field + template get(v: A): tuple[a: int] = v.value + + var v = A(field: (a: 1314)) + doAssert get(v)[0] == 1314