Skip to content

Commit 7bdfeb7

Browse files
authored
Fixed concept constraints for static types (#19391)
1 parent a93f6e7 commit 7bdfeb7

File tree

2 files changed

+71
-25
lines changed

2 files changed

+71
-25
lines changed

compiler/sigmatch.nim

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,15 @@ proc typeRel(c: var TCandidate, f, aOrig: PType,
16341634
bindConcreteTypeToUserTypeClass(matched, a)
16351635
if doBind: put(c, f, matched)
16361636
result = isGeneric
1637+
elif a.len > 0 and a.lastSon == f:
1638+
# Needed for checking `Y` == `Addable` in the following
1639+
#[
1640+
type
1641+
Addable = concept a, type A
1642+
a + a is A
1643+
MyType[T: Addable; Y: static T] = object
1644+
]#
1645+
result = isGeneric
16371646
else:
16381647
result = isNone
16391648

tests/generics/tstatic_constrained.nim

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,78 @@ discard """
22
cmd: "nim check --hints:off --warnings:off $file"
33
action: "reject"
44
nimout:'''
5-
tstatic_constrained.nim(41, 20) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(27, 3)]
5+
tstatic_constrained.nim(44, 22) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(30, 5)]
66
got: <typedesc[int], int literal(10)>
77
but expected: <T: float or string, Y>
8-
tstatic_constrained.nim(41, 20) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(27, 3)]
8+
tstatic_constrained.nim(44, 22) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(30, 5)]
99
got: <typedesc[int], int literal(10)>
1010
but expected: <T: float or string, Y>
11-
tstatic_constrained.nim(41, 29) Error: object constructor needs an object type [proxy]
12-
tstatic_constrained.nim(41, 29) Error: expression '' has no type (or is ambiguous)
13-
tstatic_constrained.nim(42, 20) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(27, 3)]
11+
tstatic_constrained.nim(44, 31) Error: object constructor needs an object type [proxy]
12+
tstatic_constrained.nim(44, 31) Error: expression '' has no type (or is ambiguous)
13+
tstatic_constrained.nim(45, 22) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(30, 5)]
1414
got: <typedesc[byte], uint8>
1515
but expected: <T: float or string, Y>
16-
tstatic_constrained.nim(42, 20) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(27, 3)]
16+
tstatic_constrained.nim(45, 22) Error: cannot instantiate MyOtherType [type declared in tstatic_constrained.nim(30, 5)]
1717
got: <typedesc[byte], uint8>
1818
but expected: <T: float or string, Y>
19-
tstatic_constrained.nim(42, 32) Error: object constructor needs an object type [proxy]
20-
tstatic_constrained.nim(42, 32) Error: expression '' has no type (or is ambiguous)
19+
tstatic_constrained.nim(45, 34) Error: object constructor needs an object type [proxy]
20+
tstatic_constrained.nim(45, 34) Error: expression '' has no type (or is ambiguous)
21+
tstatic_constrained.nim(77, 14) Error: cannot instantiate MyType [type declared in tstatic_constrained.nim(71, 5)]
22+
got: <typedesc[float], float64>
23+
but expected: <T: MyConstraint, Y>
2124
'''
2225
"""
26+
block:
27+
type
28+
MyType[T; X: static T] = object
29+
data: T
30+
MyOtherType[T: float or string, Y: static T] = object
2331

24-
type
25-
MyType[T; X: static T] = object
26-
data: T
27-
MyOtherType[T: float or string, Y: static T] = object
32+
func f[T,X](a: MyType[T,X]): MyType[T,X] =
33+
when T is string:
34+
MyType[T,X](data: a.data & X)
35+
else:
36+
MyType[T,X](data: a.data + X)
2837

29-
func f[T,X](a: MyType[T,X]): MyType[T,X] =
30-
when T is string:
31-
MyType[T,X](data: a.data & X)
32-
else:
33-
MyType[T,X](data: a.data + X)
38+
discard MyType[int, 2](data: 1)
39+
discard MyType[string, "Helelello"](data: "Hmmm")
40+
discard MyType[int, 2](data: 1).f()
41+
discard MyType[string, "Helelello"](data: "Hmmm").f()
42+
discard MyOtherType[float, 1.3]()
43+
discard MyOtherType[string, "Hello"]()
44+
discard MyOtherType[int, 10]()
45+
discard MyOtherType[byte, 10u8]()
3446

35-
discard MyType[int, 2](data: 1)
36-
discard MyType[string, "Helelello"](data: "Hmmm")
37-
discard MyType[int, 2](data: 1).f()
38-
discard MyType[string, "Helelello"](data: "Hmmm").f()
39-
discard MyOtherType[float, 1.3]()
40-
discard MyOtherType[string, "Hello"]()
41-
discard MyOtherType[int, 10]()
42-
discard MyOtherType[byte, 10u8]()
47+
block:
48+
type
49+
Moduloable = concept m, type M
50+
m mod m is M
51+
Addable = concept a, type A
52+
a + a is A
53+
Modulo[T: Moduloable; Mod: static T] = distinct T
54+
ModuloAdd[T: Moduloable or Addable; Mod: static T] = distinct T
55+
ModuAddable = Addable or Moduloable
56+
ModdAddClass[T: ModuAddable; Mod: static T] = distinct T
57+
58+
proc toMod[T](val: T, modVal: static T): Modulo[T, modVal] =
59+
mixin `mod`
60+
Modulo[T, modVal](val mod modVal)
61+
var
62+
a = 3231.toMod(10)
63+
b = 5483.toMod(10)
64+
discard ModuloAdd[int, 3](0)
65+
discard ModdAddClass[int, 3](0)
66+
67+
block:
68+
type
69+
MyConstraint = int or string
70+
MyOtherConstraint[T] = object
71+
MyType[T: MyConstraint; Y: static T] = object
72+
MyOtherType[T: MyOtherConstraint; Y: static T] = object
73+
74+
var
75+
a: MyType[int, 10]
76+
b: MyType[string, "hello"]
77+
c: MyType[float, 10d]
78+
d: MyOtherType[MyOtherConstraint[float],MyOtherConstraint[float]()]
79+
e: MyOtherType[MyOtherConstraint[int], MyOtherConstraint[int]()]

0 commit comments

Comments
 (0)