summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorZahary Karadjov <zahary@gmail.com>2016-07-29 04:56:14 +0300
committerZahary Karadjov <zahary@gmail.com>2017-03-24 16:58:15 +0200
commit66e0f0e19cc47caa01aa898988eb7452d07e22cf (patch)
treefe1fadc292d04edc1e8240e1a0cbfc04d050343c
parent0d8a503e450b1d240fb5ab914f40aa30d7b6cd8b (diff)
downloadNim-66e0f0e19cc47caa01aa898988eb7452d07e22cf.tar.gz
concept type params inference working in basic examples
-rw-r--r--compiler/ast.nim1
-rw-r--r--compiler/semexprs.nim4
-rw-r--r--compiler/semtypes.nim3
-rw-r--r--compiler/sigmatch.nim38
-rw-r--r--tests/concepts/tstackconcept.nim49
5 files changed, 93 insertions, 2 deletions
diff --git a/compiler/ast.nim b/compiler/ast.nim
index 26305cf3b..4a25e0336 100644
--- a/compiler/ast.nim
+++ b/compiler/ast.nim
@@ -482,6 +482,7 @@ type
     tfHasStatic
     tfGenericTypeParam
     tfImplicitTypeParam
+    tfInferrableTypeClassTypeParam
     tfWildcard        # consider a proc like foo[T, I](x: Type[T, I])
                       # T and I here can bind to both typedesc and static types
                       # before this is determined, we'll consider them to be a
diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim
index 4d698dbfc..cb16bf406 100644
--- a/compiler/semexprs.nim
+++ b/compiler/semexprs.nim
@@ -318,7 +318,9 @@ proc isOpImpl(c: PContext, n: PNode): PNode =
     else:
       result = newIntNode(nkIntLit, 0)
   else:
-    var t2 = n[2].typ.skipTypes({tyTypeDesc})
+    var rhsOrigType = n[2].typ
+    discard inferTypeClassParam(c, t1, rhsOrigType)
+    var t2 = rhsOrigType.skipTypes({tyTypeDesc})
     maybeLiftType(t2, c, n.info)
     var m: TCandidate
     initCandidate(c, m, t2)
diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim
index e86b527d6..e3c3123f9 100644
--- a/compiler/semtypes.nim
+++ b/compiler/semtypes.nim
@@ -883,7 +883,8 @@ proc liftParamType(c: PContext, procKind: TSymKind, genericParams: PNode,
     for i in 1 .. <paramType.len:
       let lifted = liftingWalk(paramType.sons[i])
       if lifted != nil: paramType.sons[i] = lifted
-    when false:
+
+    if paramType.base.lastSon.kind == tyUserTypeClass:
       let expanded = instGenericContainer(c, info, paramType,
                                           allowMetaTypes = true)
       result = liftingWalk(expanded, true)
diff --git a/compiler/sigmatch.nim b/compiler/sigmatch.nim
index 164fd0999..b279e7d2d 100644
--- a/compiler/sigmatch.nim
+++ b/compiler/sigmatch.nim
@@ -585,6 +585,8 @@ proc matchUserTypeClass*(c: PContext, m: var TCandidate,
     dec c.inTypeClass
     closeScope(c)
 
+  var typeParams: seq[(PSym, PType)]
+
   if ff.kind == tyUserTypeClassInst:
     for i in 1 .. <(ff.len - 1):
       var
@@ -607,7 +609,12 @@ proc matchUserTypeClass*(c: PContext, m: var TCandidate,
         param = paramSym skType
         param.typ = makeTypeDesc(c, typ)
 
+      if typ.isMetaType:
+        param.typ.flags.incl tfInferrableTypeClassTypeParam
+
       addDecl(c, param)
+      typeParams.safeAdd((param, typ))
+
       #echo "A ", param.name.s, " ", typeToString(param.typ), " ", param.kind
 
   for param in body.n[0]:
@@ -630,6 +637,13 @@ proc matchUserTypeClass*(c: PContext, m: var TCandidate,
 
   var checkedBody = c.semTryExpr(c, body.n[3].copyTree)
   if checkedBody == nil: return isNone
+
+  # The inferrable type params have been identified during the semTryExpr above.
+  # We need to put them in the current sigmatch's binding table in order for them
+  # to be resolvable while matching the rest of the parameters
+  for p in typeParams:
+    put(m.bindings, p[0].typ, p[1])
+
   return isGeneric
 
 proc shouldSkipDistinct(rules: PNode, callIdent: PIdent): bool =
@@ -1355,6 +1369,27 @@ proc incMatches(m: var TCandidate; r: TTypeRelation; convMatch = 1) =
   of isEqual: inc(m.exactMatches)
   of isNone: discard
 
+proc skipToInferrableParam(tt: PType): PType =
+  var t = tt
+  while t != nil:
+    if tfInferrableTypeClassTypeParam in t.flags:
+      return t
+    if t.sonsLen > 0 and t.kind == tyTypeDesc:
+      t = t.base
+    else:
+      return nil
+
+  return nil
+
+proc inferTypeClassParam*(c: PContext, f, a: PType): bool =
+  if c.inTypeClass == 0: return false
+
+  var inferrableType = a.skipToInferrableParam
+  if inferrableType == nil: return false
+
+  inferrableType.assignType f
+  return true
+
 proc paramTypesMatchAux(m: var TCandidate, f, argType: PType,
                         argSemantized, argOrig: PNode): PNode =
   var
@@ -1363,6 +1398,9 @@ proc paramTypesMatchAux(m: var TCandidate, f, argType: PType,
     argType = argType
     c = m.c
 
+  if inferTypeClassParam(c, f, argType):
+    return argSemantized
+
   if tfHasStatic in fMaybeStatic.flags:
     # XXX: When implicit statics are the default
     # this will be done earlier - we just have to
diff --git a/tests/concepts/tstackconcept.nim b/tests/concepts/tstackconcept.nim
new file mode 100644
index 000000000..3993ca534
--- /dev/null
+++ b/tests/concepts/tstackconcept.nim
@@ -0,0 +1,49 @@
+discard """
+output: "20\n10"
+msg: '''
+INFERRED int
+'''
+"""
+
+import typetraits
+
+template reject(e: expr) =
+  static: assert(not compiles(e))
+
+type
+  ArrayStack = object
+    data: seq[int]
+
+proc push(s: var ArrayStack, item: int) =
+  s.data.add item
+
+proc pop(s: var ArrayStack): int =
+  return s.data.pop()
+
+type
+  Stack[T] = concept var s
+    s.push(T)
+    s.pop() is T
+
+proc genericAlgorithm[T](s: var Stack[T], y: T) =
+  static: echo "INFERRED ", T.name
+
+  s.push(y)
+  echo s.pop
+
+proc implicitGeneric(s: var Stack): auto =
+  # static: echo "IMPLICIT INFERRED ", s.T.name, " ", Stack.T.name
+
+  return s.pop()
+
+var s = ArrayStack(data: @[])
+
+s.push 10
+s.genericAlgorithm 20
+echo s.implicitGeneric
+
+reject s.genericAlgorithm "x"
+reject s.genericAlgorithm 1.0
+reject "str".implicitGeneric
+reject implicitGeneric(10)
+
1:25:12 +0100 Plugins: Added completer_remove' href='/danisanti/profani-tty/commit/src/plugins/autocompleters.c?id=bfdc3b88072e0231c026fa6ebadc942343a56e2c'>bfdc3b88 ^
71178b36 ^

bfdc3b88 ^


71178b36 ^

bfdc3b88 ^
71178b36 ^


bfdc3b88 ^

2f432a94 ^
71178b36 ^
2f432a94 ^
71178b36 ^

2f432a94 ^


71178b36 ^




2f432a94 ^


dcc2123e ^













6b830277 ^
41fe8c22 ^


71178b36 ^







6b830277 ^
71178b36 ^
dcc2123e ^
71178b36 ^



41fe8c22 ^
71178b36 ^


41fe8c22 ^
71178b36 ^
41fe8c22 ^
dcc2123e ^








6b830277 ^
dcc2123e ^














41fe8c22 ^





71178b36 ^











41fe8c22 ^
72f94036 ^
71178b36 ^
41fe8c22 ^



71178b36 ^
41fe8c22 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209