bugfix: 'indexOf' for tuple fields works
This commit is contained in:
parent
5ac5bedc66
commit
78b27ed7fa
3 changed files with 30 additions and 7 deletions
|
|
@ -490,7 +490,7 @@ proc getArrayConstr(m: PSym, n: PNode): PNode =
|
||||||
|
|
||||||
proc foldArrayAccess(m: PSym, n: PNode): PNode =
|
proc foldArrayAccess(m: PSym, n: PNode): PNode =
|
||||||
var x = getConstExpr(m, n.sons[0])
|
var x = getConstExpr(m, n.sons[0])
|
||||||
if x == nil: return
|
if x == nil or x.typ.skipTypes({tyGenericInst}).kind == tyTypeDesc: return
|
||||||
|
|
||||||
var y = getConstExpr(m, n.sons[1])
|
var y = getConstExpr(m, n.sons[1])
|
||||||
if y == nil: return
|
if y == nil: return
|
||||||
|
|
@ -542,6 +542,11 @@ proc foldConStrStr(m: PSym, n: PNode): PNode =
|
||||||
if a == nil: return nil
|
if a == nil: return nil
|
||||||
result.strVal.add(getStrOrChar(a))
|
result.strVal.add(getStrOrChar(a))
|
||||||
|
|
||||||
|
proc newSymNodeTypeDesc*(s: PSym; info: TLineInfo): PNode =
|
||||||
|
result = newSymNode(s, info)
|
||||||
|
result.typ = newType(tyTypeDesc, s.owner)
|
||||||
|
result.typ.addSonSkipIntLit(s.typ)
|
||||||
|
|
||||||
proc getConstExpr(m: PSym, n: PNode): PNode =
|
proc getConstExpr(m: PSym, n: PNode): PNode =
|
||||||
result = nil
|
result = nil
|
||||||
case n.kind
|
case n.kind
|
||||||
|
|
@ -569,6 +574,8 @@ proc getConstExpr(m: PSym, n: PNode): PNode =
|
||||||
if sfFakeConst notin s.flags: result = copyTree(s.ast)
|
if sfFakeConst notin s.flags: result = copyTree(s.ast)
|
||||||
elif s.kind in {skProc, skMethod}: # BUGFIX
|
elif s.kind in {skProc, skMethod}: # BUGFIX
|
||||||
result = n
|
result = n
|
||||||
|
elif s.kind in {skType, skGenericParam}:
|
||||||
|
result = newSymNodeTypeDesc(s, n.info)
|
||||||
of nkCharLit..nkNilLit:
|
of nkCharLit..nkNilLit:
|
||||||
result = copyNode(n)
|
result = copyNode(n)
|
||||||
of nkIfExpr:
|
of nkIfExpr:
|
||||||
|
|
|
||||||
|
|
@ -31,11 +31,6 @@ proc getIdentNode(n: PNode): PNode =
|
||||||
illFormedAst(n)
|
illFormedAst(n)
|
||||||
result = n
|
result = n
|
||||||
|
|
||||||
proc newSymNodeTypeDesc(s: PSym; info: TLineInfo): PNode =
|
|
||||||
result = newSymNode(s, info)
|
|
||||||
result.typ = newType(tyTypeDesc, s.owner)
|
|
||||||
result.typ.addSonSkipIntLit(s.typ)
|
|
||||||
|
|
||||||
proc semGenericStmt(c: PContext, n: PNode, flags: TSemGenericFlags,
|
proc semGenericStmt(c: PContext, n: PNode, flags: TSemGenericFlags,
|
||||||
ctx: var TIntSet): PNode
|
ctx: var TIntSet): PNode
|
||||||
proc semGenericStmtScope(c: PContext, n: PNode,
|
proc semGenericStmtScope(c: PContext, n: PNode,
|
||||||
|
|
|
||||||
21
tests/run/tfieldindex.nim
Normal file
21
tests/run/tfieldindex.nim
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
discard """
|
||||||
|
output: "1"
|
||||||
|
"""
|
||||||
|
|
||||||
|
type
|
||||||
|
TMyTuple = tuple[a, b: int]
|
||||||
|
|
||||||
|
proc indexOf*(t: typedesc, name: string): int {.compiletime.} =
|
||||||
|
## takes a tuple and looks for the field by name.
|
||||||
|
## returs index of that field.
|
||||||
|
var
|
||||||
|
d: t
|
||||||
|
i = 0
|
||||||
|
for n, x in fieldPairs(d):
|
||||||
|
if n == name: return i
|
||||||
|
i.inc
|
||||||
|
raise newException(EInvalidValue, "No field " & name & " in type " &
|
||||||
|
astToStr(t))
|
||||||
|
|
||||||
|
echo TMyTuple.indexOf("b")
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue