Skip to content

Commit 88da5e8

Browse files
Gravefloringabout
andauthored
two small concept patches (#25076)
- slightly better typeclass logic (eg for bare `range`) - reverse matching now substitutes potential implementation for `Self` --------- Co-authored-by: ringabout <[email protected]>
1 parent d73f478 commit 88da5e8

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

compiler/concepts.nim

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,10 @@ proc matchType(c: PContext; fo, ao: PType; m: var MatchCon): bool =
271271
var
272272
a = ao
273273
f = fo
274-
274+
if a.isSelf:
275+
if m.magic in {mArrPut, mArrGet}:
276+
return false
277+
a = m.potentialImplementation
275278
if a.kind in bindableTypes:
276279
a = existingBinding(m, ao)
277280
if a == ao and a.kind == tyGenericParam and a.hasElementType and a.elementType.kind != tyNone:
@@ -337,8 +340,11 @@ proc matchType(c: PContext; fo, ao: PType; m: var MatchCon): bool =
337340
result = true
338341
else:
339342
let ak = a.skipTypes(ignorableForArgType - {f.kind})
340-
if ak.kind == f.kind and f.kidsLen == ak.kidsLen:
341-
result = matchKids(c, f, ak, m)
343+
if ak.kind == f.kind:
344+
if f.base.kind == tyNone:
345+
result = true
346+
elif f.kidsLen == ak.kidsLen:
347+
result = matchKids(c, f, ak, m)
342348
of tyGenericInvocation, tyGenericInst:
343349
result = false
344350
let ea = a.skipTypes(ignorableForArgType)

tests/concepts/tconceptsv2.nim

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,28 @@ block:
497497

498498
spring({One,Two})
499499

500+
block: # bare `range`
501+
type
502+
MyRange = 0..64
503+
MyConcept = concept
504+
proc a(x: typedesc[Self])
505+
506+
proc a(x: typedesc[range]) = discard
507+
proc spring(x: typedesc[MyConcept]) = discard
508+
spring(MyRange)
509+
510+
block:
511+
type
512+
A = object
513+
TestConcept =
514+
concept
515+
proc x(x: Self)
516+
517+
proc x(x: not object) =
518+
discard
519+
520+
assert A isnot TestConcept
521+
500522
# this code fails inside a block for some reason
501523
type Indexable[T] = concept
502524
proc `[]`(t: Self, i: int): T

0 commit comments

Comments
 (0)