Skip to content

Commit

Permalink
fixes #22049; fixes #22054; implicit conversion keeps varness (#22097)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ringabout authored Jun 16, 2023
1 parent 0750210 commit 77beb15
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
8 changes: 7 additions & 1 deletion compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1932,7 +1932,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
Expand Down
9 changes: 9 additions & 0 deletions tests/errmsgs/t22097.nim
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions tests/tuples/ttuples_various.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 77beb15

Please sign in to comment.