enforce the covariance rules for user-defined generic types

This commit is contained in:
Zahary Karadjov 2017-05-13 14:38:07 +03:00
commit 0aede22e87
5 changed files with 105 additions and 17 deletions

View file

@ -270,8 +270,8 @@ type
sfDiscardable, # returned value may be discarded implicitly
sfOverriden, # proc is overriden
sfGenSym # symbol is 'gensym'ed; do not add to symbol table
sfCovariant # covariant generic param mimicing seq/array type
sfStrongCovariant # covariant generic param mimicing ptr type
sfCovariant # covariant generic param mimicing a ptr type
sfWeakCovariant # covariant generic param mimicing a seq/array type
sfContravariant # contravariant generic param
TSymFlags* = set[TSymFlag]
@ -1009,15 +1009,17 @@ proc add*(father, son: PNode) =
if isNil(father.sons): father.sons = @[]
add(father.sons, son)
proc `[]`*(n: PNode, i: int): PNode {.inline.} =
result = n.sons[i]
type Indexable = PNode | PType
template `[]`*(n: Indexable, i: int): Indexable =
n.sons[i]
template `-|`*(b, s: untyped): untyped =
(if b >= 0: b else: s.len + b)
# son access operators with support for negative indices
template `{}`*(n: PNode, i: int): untyped = n[i -| n]
template `{}=`*(n: PNode, i: int, s: PNode) =
template `{}`*(n: Indexable, i: int): untyped = n[i -| n]
template `{}=`*(n: Indexable, i: int, s: Indexable) =
n.sons[i -| n] = s
when defined(useNodeIds):