Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/sigmatch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,11 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
return isNone
if doBind: put(c, f, a)
return isGeneric
elif targetKind == tyVar:
# always matches.
# The argument is checked if it is lvalue or not outside of this proc.
if doBind: put(c, f, if aOrig.kind == tyVar: aOrig else: makeVarType(c.c, a))
return isGeneric
else:
return isNone
of tyUserTypeClassInst, tyUserTypeClass:
Expand Down Expand Up @@ -2863,7 +2868,7 @@ proc matchesAux(c: PContext, n, nOrig: PNode, m: var TCandidate, marker: var Int
else:
noMatch()

if formal.typ.kind in {tyVar}:
if formal.typ.kind in {tyVar} or (formal.typ.kind == tyBuiltInTypeClass and formal.typ[0].kind == tyVar):
let argConverter = if arg.kind == nkHiddenDeref: arg[0] else: arg
if argConverter.kind == nkHiddenCallConv:
if argConverter.typ.kind notin {tyVar}:
Expand Down
23 changes: 23 additions & 0 deletions tests/typerel/tvar_type_class_param.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# issue #25826

proc foo(x: var) = x = 123
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember the part of my spec that says "you can write var without any type at all"...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var type class is listed in Nim manual:
https://nim-lang.org/docs/manual.html#generics-type-classes
But it is not clear how it should work:
#9443 (comment)


block:
var a = 0
foo(a)
doAssert a == 123

proc bar(x: var int) =
foo(x)

block:
var a = 0
bar(a)
doAssert a == 123

proc baz(x: var int): var int = x

block:
var a = 0
foo(baz(a))
doAssert a == 123
10 changes: 10 additions & 0 deletions tests/typerel/tvar_type_class_param_fail.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
discard """
action: "reject"
errormsg: "type mismatch: got <int>"
"""
# issue #25826

proc foo(x: var) = x = 123

let a = 0
foo(a)
Loading