Skip to content
Merged
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
10 changes: 9 additions & 1 deletion compiler/concepts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ proc bindParam(c: PContext, m: var MatchCon; key, v: PType): bool {. discardable
# check previously bound value
if not matchType(c, old, value, m):
return false
elif key.hasElementType and key.elementType.kind != tyNone:
elif key.hasElementType and not key.elementType.isNil and key.elementType.kind != tyNone:
# check constaint
if matchType(c, unrollGenericParam(key), value, m) == false:
return false
Expand Down Expand Up @@ -358,6 +358,14 @@ proc matchType(c: PContext; fo, ao: PType; m: var MatchCon): bool =
if not matchType(c, f[i], ea[i], m):
result = false
break
elif f.kind == tyGenericInvocation:
# bind potential generic constraints into body
let body = f.base
for i in 1 ..< len(f):
bindParam(c,m,body[i-1], f[i])
result = matchType(c, body, a, m)
else: # tyGenericInst
result = matchType(c, f.last, a, m)
of tyOrdinal:
result = isOrdinalType(a, allowEnumWithHoles = false) or a.kind == tyGenericParam
of tyStatic:
Expand Down
39 changes: 39 additions & 0 deletions tests/concepts/tconceptsv2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,42 @@ proc len[T](t: DummyIndexable[T]): int =

let dummyIndexable = DummyIndexable(@[1, 2])
echoAll(dummyIndexable)

block:
type
C = concept
proc a(x: Self, i: int)
AObj[T] = object
x: T
ARef[T] = ref AObj[T]

proc a[T: int](x: ARef[T], i: int) =
discard

assert (ref AObj[int]) is C

block:
type
C = concept
proc a(x: Self, i: int)
AObj[T; B] = object
x: T
ARef[T; B] = ref AObj[T,B]

proc a[T: int, C: float](x: ARef[T, C], i: int) =
discard

assert (ref AObj[int, int]) isnot C
assert (ref AObj[int, float]) is C

block:
type
C = concept
proc a(x: Self, i: int)
AObj[T] = object
ARef[T] = ref AObj[T]

proc a(x: ARef, i: int) =
discard

assert (ref AObj[int]) is C