breaking change: symbol lookups in generics follows spec more closely; fixes #2664
This commit is contained in:
parent
2e4b59f3d3
commit
c733b3181e
4 changed files with 88 additions and 65 deletions
|
|
@ -39,9 +39,9 @@ type
|
||||||
proc semGenericStmt(c: PContext, n: PNode,
|
proc semGenericStmt(c: PContext, n: PNode,
|
||||||
flags: TSemGenericFlags, ctx: var GenericCtx): PNode
|
flags: TSemGenericFlags, ctx: var GenericCtx): PNode
|
||||||
|
|
||||||
proc semGenericStmtScope(c: PContext, n: PNode,
|
proc semGenericStmtScope(c: PContext, n: PNode,
|
||||||
flags: TSemGenericFlags,
|
flags: TSemGenericFlags,
|
||||||
ctx: var GenericCtx): PNode =
|
ctx: var GenericCtx): PNode =
|
||||||
openScope(c)
|
openScope(c)
|
||||||
result = semGenericStmt(c, n, flags, ctx)
|
result = semGenericStmt(c, n, flags, ctx)
|
||||||
closeScope(c)
|
closeScope(c)
|
||||||
|
|
@ -73,7 +73,7 @@ proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym,
|
||||||
result = semGenericStmt(c, result, {}, ctx)
|
result = semGenericStmt(c, result, {}, ctx)
|
||||||
else:
|
else:
|
||||||
result = symChoice(c, n, s, scOpen)
|
result = symChoice(c, n, s, scOpen)
|
||||||
of skGenericParam:
|
of skGenericParam:
|
||||||
if s.typ != nil and s.typ.kind == tyStatic:
|
if s.typ != nil and s.typ.kind == tyStatic:
|
||||||
if s.typ.n != nil:
|
if s.typ.n != nil:
|
||||||
result = s.typ.n
|
result = s.typ.n
|
||||||
|
|
@ -85,18 +85,18 @@ proc semGenericStmtSymbol(c: PContext, n: PNode, s: PSym,
|
||||||
of skParam:
|
of skParam:
|
||||||
result = n
|
result = n
|
||||||
styleCheckUse(n.info, s)
|
styleCheckUse(n.info, s)
|
||||||
of skType:
|
of skType:
|
||||||
if (s.typ != nil) and
|
if (s.typ != nil) and
|
||||||
(s.typ.flags * {tfGenericTypeParam, tfImplicitTypeParam} == {}):
|
(s.typ.flags * {tfGenericTypeParam, tfImplicitTypeParam} == {}):
|
||||||
result = newSymNodeTypeDesc(s, n.info)
|
result = newSymNodeTypeDesc(s, n.info)
|
||||||
else:
|
else:
|
||||||
result = n
|
result = n
|
||||||
styleCheckUse(n.info, s)
|
styleCheckUse(n.info, s)
|
||||||
else:
|
else:
|
||||||
result = newSymNode(s, n.info)
|
result = newSymNode(s, n.info)
|
||||||
styleCheckUse(n.info, s)
|
styleCheckUse(n.info, s)
|
||||||
|
|
||||||
proc lookup(c: PContext, n: PNode, flags: TSemGenericFlags,
|
proc lookup(c: PContext, n: PNode, flags: TSemGenericFlags,
|
||||||
ctx: var GenericCtx): PNode =
|
ctx: var GenericCtx): PNode =
|
||||||
result = n
|
result = n
|
||||||
let ident = considerQuotedIdent(n)
|
let ident = considerQuotedIdent(n)
|
||||||
|
|
@ -118,13 +118,13 @@ proc newDot(n, b: PNode): PNode =
|
||||||
result.add(n.sons[0])
|
result.add(n.sons[0])
|
||||||
result.add(b)
|
result.add(b)
|
||||||
|
|
||||||
proc fuzzyLookup(c: PContext, n: PNode, flags: TSemGenericFlags,
|
proc fuzzyLookup(c: PContext, n: PNode, flags: TSemGenericFlags,
|
||||||
ctx: var GenericCtx; isMacro: var bool): PNode =
|
ctx: var GenericCtx; isMacro: var bool): PNode =
|
||||||
assert n.kind == nkDotExpr
|
assert n.kind == nkDotExpr
|
||||||
semIdeForTemplateOrGenericCheck(n, ctx.cursorInBody)
|
semIdeForTemplateOrGenericCheck(n, ctx.cursorInBody)
|
||||||
|
|
||||||
let luf = if withinMixin notin flags: {checkUndeclared} else: {}
|
let luf = if withinMixin notin flags: {checkUndeclared} else: {}
|
||||||
|
|
||||||
var s = qualifiedLookUp(c, n, luf)
|
var s = qualifiedLookUp(c, n, luf)
|
||||||
if s != nil:
|
if s != nil:
|
||||||
result = semGenericStmtSymbol(c, n, s, ctx)
|
result = semGenericStmtSymbol(c, n, s, ctx)
|
||||||
|
|
@ -141,18 +141,20 @@ proc fuzzyLookup(c: PContext, n: PNode, flags: TSemGenericFlags,
|
||||||
elif s.name.id in ctx.toMixin:
|
elif s.name.id in ctx.toMixin:
|
||||||
result = newDot(result, symChoice(c, n, s, scForceOpen))
|
result = newDot(result, symChoice(c, n, s, scForceOpen))
|
||||||
else:
|
else:
|
||||||
let sym = semGenericStmtSymbol(c, n, s, ctx)
|
let syms = semGenericStmtSymbol(c, n, s, ctx)
|
||||||
if sym.kind == nkSym:
|
if syms.kind == nkSym:
|
||||||
result = newDot(result, symChoice(c, n, s, scForceOpen))
|
let choice = symChoice(c, n, s, scForceOpen)
|
||||||
|
choice.kind = nkClosedSymChoice
|
||||||
|
result = newDot(result, choice)
|
||||||
else:
|
else:
|
||||||
result = newDot(result, sym)
|
result = newDot(result, syms)
|
||||||
|
|
||||||
proc addTempDecl(c: PContext; n: PNode; kind: TSymKind) =
|
proc addTempDecl(c: PContext; n: PNode; kind: TSymKind) =
|
||||||
let s = newSymS(skUnknown, getIdentNode(n), c)
|
let s = newSymS(skUnknown, getIdentNode(n), c)
|
||||||
addPrelimDecl(c, s)
|
addPrelimDecl(c, s)
|
||||||
styleCheckDef(n.info, s, kind)
|
styleCheckDef(n.info, s, kind)
|
||||||
|
|
||||||
proc semGenericStmt(c: PContext, n: PNode,
|
proc semGenericStmt(c: PContext, n: PNode,
|
||||||
flags: TSemGenericFlags, ctx: var GenericCtx): PNode =
|
flags: TSemGenericFlags, ctx: var GenericCtx): PNode =
|
||||||
result = n
|
result = n
|
||||||
#if gCmd == cmdIdeTools: suggestStmt(c, n)
|
#if gCmd == cmdIdeTools: suggestStmt(c, n)
|
||||||
|
|
@ -181,16 +183,16 @@ proc semGenericStmt(c: PContext, n: PNode,
|
||||||
result = semGenericStmt(c, n.sons[0], flags+{withinBind}, ctx)
|
result = semGenericStmt(c, n.sons[0], flags+{withinBind}, ctx)
|
||||||
of nkMixinStmt:
|
of nkMixinStmt:
|
||||||
result = semMixinStmt(c, n, ctx.toMixin)
|
result = semMixinStmt(c, n, ctx.toMixin)
|
||||||
of nkCall, nkHiddenCallConv, nkInfix, nkPrefix, nkCommand, nkCallStrLit:
|
of nkCall, nkHiddenCallConv, nkInfix, nkPrefix, nkCommand, nkCallStrLit:
|
||||||
# check if it is an expression macro:
|
# check if it is an expression macro:
|
||||||
checkMinSonsLen(n, 1)
|
checkMinSonsLen(n, 1)
|
||||||
let fn = n.sons[0]
|
let fn = n.sons[0]
|
||||||
var s = qualifiedLookUp(c, fn, {})
|
var s = qualifiedLookUp(c, fn, {})
|
||||||
if s == nil and withinMixin notin flags and
|
if s == nil and withinMixin notin flags and
|
||||||
fn.kind in {nkIdent, nkAccQuoted} and
|
fn.kind in {nkIdent, nkAccQuoted} and
|
||||||
considerQuotedIdent(fn).id notin ctx.toMixin:
|
considerQuotedIdent(fn).id notin ctx.toMixin:
|
||||||
localError(n.info, errUndeclaredIdentifier, fn.renderTree)
|
localError(n.info, errUndeclaredIdentifier, fn.renderTree)
|
||||||
|
|
||||||
var first = 0
|
var first = 0
|
||||||
var mixinContext = false
|
var mixinContext = false
|
||||||
if s != nil:
|
if s != nil:
|
||||||
|
|
@ -220,7 +222,7 @@ proc semGenericStmt(c: PContext, n: PNode,
|
||||||
# we need to put the ``c`` in ``t(c)`` in a mixin context to prevent
|
# we need to put the ``c`` in ``t(c)`` in a mixin context to prevent
|
||||||
# the famous "undeclared identifier: it" bug:
|
# the famous "undeclared identifier: it" bug:
|
||||||
mixinContext = true
|
mixinContext = true
|
||||||
of skUnknown, skParam:
|
of skUnknown, skParam:
|
||||||
# Leave it as an identifier.
|
# Leave it as an identifier.
|
||||||
discard
|
discard
|
||||||
of skProc, skMethod, skIterators, skConverter:
|
of skProc, skMethod, skIterators, skConverter:
|
||||||
|
|
@ -230,9 +232,9 @@ proc semGenericStmt(c: PContext, n: PNode,
|
||||||
result.sons[0] = newSymNodeTypeDesc(s, fn.info)
|
result.sons[0] = newSymNodeTypeDesc(s, fn.info)
|
||||||
styleCheckUse(fn.info, s)
|
styleCheckUse(fn.info, s)
|
||||||
first = 1
|
first = 1
|
||||||
of skType:
|
of skType:
|
||||||
# bad hack for generics:
|
# bad hack for generics:
|
||||||
if (s.typ != nil) and (s.typ.kind != tyGenericParam):
|
if (s.typ != nil) and (s.typ.kind != tyGenericParam):
|
||||||
result.sons[0] = newSymNodeTypeDesc(s, fn.info)
|
result.sons[0] = newSymNodeTypeDesc(s, fn.info)
|
||||||
styleCheckUse(fn.info, s)
|
styleCheckUse(fn.info, s)
|
||||||
first = 1
|
first = 1
|
||||||
|
|
@ -244,34 +246,34 @@ proc semGenericStmt(c: PContext, n: PNode,
|
||||||
result.sons[0] = fuzzyLookup(c, fn, flags, ctx, mixinContext)
|
result.sons[0] = fuzzyLookup(c, fn, flags, ctx, mixinContext)
|
||||||
first = 1
|
first = 1
|
||||||
# Consider 'when declared(globalsSlot): ThreadVarSetValue(globalsSlot, ...)'
|
# Consider 'when declared(globalsSlot): ThreadVarSetValue(globalsSlot, ...)'
|
||||||
# in threads.nim: the subtle preprocessing here binds 'globalsSlot' which
|
# in threads.nim: the subtle preprocessing here binds 'globalsSlot' which
|
||||||
# is not exported and yet the generic 'threadProcWrapper' works correctly.
|
# is not exported and yet the generic 'threadProcWrapper' works correctly.
|
||||||
let flags = if mixinContext: flags+{withinMixin} else: flags
|
let flags = if mixinContext: flags+{withinMixin} else: flags
|
||||||
for i in countup(first, sonsLen(result) - 1):
|
for i in countup(first, sonsLen(result) - 1):
|
||||||
result.sons[i] = semGenericStmt(c, result.sons[i], flags, ctx)
|
result.sons[i] = semGenericStmt(c, result.sons[i], flags, ctx)
|
||||||
of nkIfStmt:
|
of nkIfStmt:
|
||||||
for i in countup(0, sonsLen(n)-1):
|
for i in countup(0, sonsLen(n)-1):
|
||||||
n.sons[i] = semGenericStmtScope(c, n.sons[i], flags, ctx)
|
n.sons[i] = semGenericStmtScope(c, n.sons[i], flags, ctx)
|
||||||
of nkWhenStmt:
|
of nkWhenStmt:
|
||||||
for i in countup(0, sonsLen(n)-1):
|
for i in countup(0, sonsLen(n)-1):
|
||||||
n.sons[i] = semGenericStmt(c, n.sons[i], flags+{withinMixin}, ctx)
|
n.sons[i] = semGenericStmt(c, n.sons[i], flags+{withinMixin}, ctx)
|
||||||
of nkWhileStmt:
|
of nkWhileStmt:
|
||||||
openScope(c)
|
openScope(c)
|
||||||
for i in countup(0, sonsLen(n)-1):
|
for i in countup(0, sonsLen(n)-1):
|
||||||
n.sons[i] = semGenericStmt(c, n.sons[i], flags, ctx)
|
n.sons[i] = semGenericStmt(c, n.sons[i], flags, ctx)
|
||||||
closeScope(c)
|
closeScope(c)
|
||||||
of nkCaseStmt:
|
of nkCaseStmt:
|
||||||
openScope(c)
|
openScope(c)
|
||||||
n.sons[0] = semGenericStmt(c, n.sons[0], flags, ctx)
|
n.sons[0] = semGenericStmt(c, n.sons[0], flags, ctx)
|
||||||
for i in countup(1, sonsLen(n)-1):
|
for i in countup(1, sonsLen(n)-1):
|
||||||
var a = n.sons[i]
|
var a = n.sons[i]
|
||||||
checkMinSonsLen(a, 1)
|
checkMinSonsLen(a, 1)
|
||||||
var L = sonsLen(a)
|
var L = sonsLen(a)
|
||||||
for j in countup(0, L-2):
|
for j in countup(0, L-2):
|
||||||
a.sons[j] = semGenericStmt(c, a.sons[j], flags, ctx)
|
a.sons[j] = semGenericStmt(c, a.sons[j], flags, ctx)
|
||||||
a.sons[L - 1] = semGenericStmtScope(c, a.sons[L-1], flags, ctx)
|
a.sons[L - 1] = semGenericStmtScope(c, a.sons[L-1], flags, ctx)
|
||||||
closeScope(c)
|
closeScope(c)
|
||||||
of nkForStmt, nkParForStmt:
|
of nkForStmt, nkParForStmt:
|
||||||
var L = sonsLen(n)
|
var L = sonsLen(n)
|
||||||
openScope(c)
|
openScope(c)
|
||||||
n.sons[L - 2] = semGenericStmt(c, n.sons[L-2], flags, ctx)
|
n.sons[L - 2] = semGenericStmt(c, n.sons[L-2], flags, ctx)
|
||||||
|
|
@ -279,27 +281,27 @@ proc semGenericStmt(c: PContext, n: PNode,
|
||||||
addTempDecl(c, n.sons[i], skForVar)
|
addTempDecl(c, n.sons[i], skForVar)
|
||||||
n.sons[L - 1] = semGenericStmt(c, n.sons[L-1], flags, ctx)
|
n.sons[L - 1] = semGenericStmt(c, n.sons[L-1], flags, ctx)
|
||||||
closeScope(c)
|
closeScope(c)
|
||||||
of nkBlockStmt, nkBlockExpr, nkBlockType:
|
of nkBlockStmt, nkBlockExpr, nkBlockType:
|
||||||
checkSonsLen(n, 2)
|
checkSonsLen(n, 2)
|
||||||
openScope(c)
|
openScope(c)
|
||||||
if n.sons[0].kind != nkEmpty:
|
if n.sons[0].kind != nkEmpty:
|
||||||
addTempDecl(c, n.sons[0], skLabel)
|
addTempDecl(c, n.sons[0], skLabel)
|
||||||
n.sons[1] = semGenericStmt(c, n.sons[1], flags, ctx)
|
n.sons[1] = semGenericStmt(c, n.sons[1], flags, ctx)
|
||||||
closeScope(c)
|
closeScope(c)
|
||||||
of nkTryStmt:
|
of nkTryStmt:
|
||||||
checkMinSonsLen(n, 2)
|
checkMinSonsLen(n, 2)
|
||||||
n.sons[0] = semGenericStmtScope(c, n.sons[0], flags, ctx)
|
n.sons[0] = semGenericStmtScope(c, n.sons[0], flags, ctx)
|
||||||
for i in countup(1, sonsLen(n)-1):
|
for i in countup(1, sonsLen(n)-1):
|
||||||
var a = n.sons[i]
|
var a = n.sons[i]
|
||||||
checkMinSonsLen(a, 1)
|
checkMinSonsLen(a, 1)
|
||||||
var L = sonsLen(a)
|
var L = sonsLen(a)
|
||||||
for j in countup(0, L-2):
|
for j in countup(0, L-2):
|
||||||
a.sons[j] = semGenericStmt(c, a.sons[j], flags+{withinTypeDesc}, ctx)
|
a.sons[j] = semGenericStmt(c, a.sons[j], flags+{withinTypeDesc}, ctx)
|
||||||
a.sons[L-1] = semGenericStmtScope(c, a.sons[L-1], flags, ctx)
|
a.sons[L-1] = semGenericStmtScope(c, a.sons[L-1], flags, ctx)
|
||||||
of nkVarSection, nkLetSection:
|
of nkVarSection, nkLetSection:
|
||||||
for i in countup(0, sonsLen(n) - 1):
|
for i in countup(0, sonsLen(n) - 1):
|
||||||
var a = n.sons[i]
|
var a = n.sons[i]
|
||||||
if a.kind == nkCommentStmt: continue
|
if a.kind == nkCommentStmt: continue
|
||||||
if (a.kind != nkIdentDefs) and (a.kind != nkVarTuple): illFormedAst(a)
|
if (a.kind != nkIdentDefs) and (a.kind != nkVarTuple): illFormedAst(a)
|
||||||
checkMinSonsLen(a, 3)
|
checkMinSonsLen(a, 3)
|
||||||
var L = sonsLen(a)
|
var L = sonsLen(a)
|
||||||
|
|
@ -307,49 +309,49 @@ proc semGenericStmt(c: PContext, n: PNode,
|
||||||
a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags, ctx)
|
a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags, ctx)
|
||||||
for j in countup(0, L-3):
|
for j in countup(0, L-3):
|
||||||
addTempDecl(c, getIdentNode(a.sons[j]), skVar)
|
addTempDecl(c, getIdentNode(a.sons[j]), skVar)
|
||||||
of nkGenericParams:
|
of nkGenericParams:
|
||||||
for i in countup(0, sonsLen(n) - 1):
|
for i in countup(0, sonsLen(n) - 1):
|
||||||
var a = n.sons[i]
|
var a = n.sons[i]
|
||||||
if (a.kind != nkIdentDefs): illFormedAst(a)
|
if (a.kind != nkIdentDefs): illFormedAst(a)
|
||||||
checkMinSonsLen(a, 3)
|
checkMinSonsLen(a, 3)
|
||||||
var L = sonsLen(a)
|
var L = sonsLen(a)
|
||||||
a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc}, ctx)
|
a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc}, ctx)
|
||||||
# do not perform symbol lookup for default expressions
|
# do not perform symbol lookup for default expressions
|
||||||
for j in countup(0, L-3):
|
for j in countup(0, L-3):
|
||||||
addTempDecl(c, getIdentNode(a.sons[j]), skType)
|
addTempDecl(c, getIdentNode(a.sons[j]), skType)
|
||||||
of nkConstSection:
|
of nkConstSection:
|
||||||
for i in countup(0, sonsLen(n) - 1):
|
for i in countup(0, sonsLen(n) - 1):
|
||||||
var a = n.sons[i]
|
var a = n.sons[i]
|
||||||
if a.kind == nkCommentStmt: continue
|
if a.kind == nkCommentStmt: continue
|
||||||
if (a.kind != nkConstDef): illFormedAst(a)
|
if (a.kind != nkConstDef): illFormedAst(a)
|
||||||
checkSonsLen(a, 3)
|
checkSonsLen(a, 3)
|
||||||
addTempDecl(c, getIdentNode(a.sons[0]), skConst)
|
addTempDecl(c, getIdentNode(a.sons[0]), skConst)
|
||||||
a.sons[1] = semGenericStmt(c, a.sons[1], flags+{withinTypeDesc}, ctx)
|
a.sons[1] = semGenericStmt(c, a.sons[1], flags+{withinTypeDesc}, ctx)
|
||||||
a.sons[2] = semGenericStmt(c, a.sons[2], flags, ctx)
|
a.sons[2] = semGenericStmt(c, a.sons[2], flags, ctx)
|
||||||
of nkTypeSection:
|
of nkTypeSection:
|
||||||
for i in countup(0, sonsLen(n) - 1):
|
for i in countup(0, sonsLen(n) - 1):
|
||||||
var a = n.sons[i]
|
var a = n.sons[i]
|
||||||
if a.kind == nkCommentStmt: continue
|
if a.kind == nkCommentStmt: continue
|
||||||
if (a.kind != nkTypeDef): illFormedAst(a)
|
if (a.kind != nkTypeDef): illFormedAst(a)
|
||||||
checkSonsLen(a, 3)
|
checkSonsLen(a, 3)
|
||||||
addTempDecl(c, getIdentNode(a.sons[0]), skType)
|
addTempDecl(c, getIdentNode(a.sons[0]), skType)
|
||||||
for i in countup(0, sonsLen(n) - 1):
|
for i in countup(0, sonsLen(n) - 1):
|
||||||
var a = n.sons[i]
|
var a = n.sons[i]
|
||||||
if a.kind == nkCommentStmt: continue
|
if a.kind == nkCommentStmt: continue
|
||||||
if (a.kind != nkTypeDef): illFormedAst(a)
|
if (a.kind != nkTypeDef): illFormedAst(a)
|
||||||
checkSonsLen(a, 3)
|
checkSonsLen(a, 3)
|
||||||
if a.sons[1].kind != nkEmpty:
|
if a.sons[1].kind != nkEmpty:
|
||||||
openScope(c)
|
openScope(c)
|
||||||
a.sons[1] = semGenericStmt(c, a.sons[1], flags, ctx)
|
a.sons[1] = semGenericStmt(c, a.sons[1], flags, ctx)
|
||||||
a.sons[2] = semGenericStmt(c, a.sons[2], flags+{withinTypeDesc}, ctx)
|
a.sons[2] = semGenericStmt(c, a.sons[2], flags+{withinTypeDesc}, ctx)
|
||||||
closeScope(c)
|
closeScope(c)
|
||||||
else:
|
else:
|
||||||
a.sons[2] = semGenericStmt(c, a.sons[2], flags+{withinTypeDesc}, ctx)
|
a.sons[2] = semGenericStmt(c, a.sons[2], flags+{withinTypeDesc}, ctx)
|
||||||
of nkEnumTy:
|
of nkEnumTy:
|
||||||
if n.sonsLen > 0:
|
if n.sonsLen > 0:
|
||||||
if n.sons[0].kind != nkEmpty:
|
if n.sons[0].kind != nkEmpty:
|
||||||
n.sons[0] = semGenericStmt(c, n.sons[0], flags+{withinTypeDesc}, ctx)
|
n.sons[0] = semGenericStmt(c, n.sons[0], flags+{withinTypeDesc}, ctx)
|
||||||
for i in countup(1, sonsLen(n) - 1):
|
for i in countup(1, sonsLen(n) - 1):
|
||||||
var a: PNode
|
var a: PNode
|
||||||
case n.sons[i].kind
|
case n.sons[i].kind
|
||||||
of nkEnumFieldDef: a = n.sons[i].sons[0]
|
of nkEnumFieldDef: a = n.sons[i].sons[0]
|
||||||
|
|
@ -360,26 +362,26 @@ proc semGenericStmt(c: PContext, n: PNode,
|
||||||
discard
|
discard
|
||||||
of nkFormalParams:
|
of nkFormalParams:
|
||||||
checkMinSonsLen(n, 1)
|
checkMinSonsLen(n, 1)
|
||||||
if n.sons[0].kind != nkEmpty:
|
if n.sons[0].kind != nkEmpty:
|
||||||
n.sons[0] = semGenericStmt(c, n.sons[0], flags+{withinTypeDesc}, ctx)
|
n.sons[0] = semGenericStmt(c, n.sons[0], flags+{withinTypeDesc}, ctx)
|
||||||
for i in countup(1, sonsLen(n) - 1):
|
for i in countup(1, sonsLen(n) - 1):
|
||||||
var a = n.sons[i]
|
var a = n.sons[i]
|
||||||
if (a.kind != nkIdentDefs): illFormedAst(a)
|
if (a.kind != nkIdentDefs): illFormedAst(a)
|
||||||
checkMinSonsLen(a, 3)
|
checkMinSonsLen(a, 3)
|
||||||
var L = sonsLen(a)
|
var L = sonsLen(a)
|
||||||
a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc}, ctx)
|
a.sons[L-2] = semGenericStmt(c, a.sons[L-2], flags+{withinTypeDesc}, ctx)
|
||||||
a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags, ctx)
|
a.sons[L-1] = semGenericStmt(c, a.sons[L-1], flags, ctx)
|
||||||
for j in countup(0, L-3):
|
for j in countup(0, L-3):
|
||||||
addTempDecl(c, getIdentNode(a.sons[j]), skParam)
|
addTempDecl(c, getIdentNode(a.sons[j]), skParam)
|
||||||
of nkProcDef, nkMethodDef, nkConverterDef, nkMacroDef, nkTemplateDef,
|
of nkProcDef, nkMethodDef, nkConverterDef, nkMacroDef, nkTemplateDef,
|
||||||
nkIteratorDef, nkLambdaKinds:
|
nkIteratorDef, nkLambdaKinds:
|
||||||
checkSonsLen(n, bodyPos + 1)
|
checkSonsLen(n, bodyPos + 1)
|
||||||
if n.sons[namePos].kind != nkEmpty:
|
if n.sons[namePos].kind != nkEmpty:
|
||||||
addTempDecl(c, getIdentNode(n.sons[0]), skProc)
|
addTempDecl(c, getIdentNode(n.sons[0]), skProc)
|
||||||
openScope(c)
|
openScope(c)
|
||||||
n.sons[genericParamsPos] = semGenericStmt(c, n.sons[genericParamsPos],
|
n.sons[genericParamsPos] = semGenericStmt(c, n.sons[genericParamsPos],
|
||||||
flags, ctx)
|
flags, ctx)
|
||||||
if n.sons[paramsPos].kind != nkEmpty:
|
if n.sons[paramsPos].kind != nkEmpty:
|
||||||
if n.sons[paramsPos].sons[0].kind != nkEmpty:
|
if n.sons[paramsPos].sons[0].kind != nkEmpty:
|
||||||
addPrelimDecl(c, newSym(skUnknown, getIdent("result"), nil, n.info))
|
addPrelimDecl(c, newSym(skUnknown, getIdent("result"), nil, n.info))
|
||||||
n.sons[paramsPos] = semGenericStmt(c, n.sons[paramsPos], flags, ctx)
|
n.sons[paramsPos] = semGenericStmt(c, n.sons[paramsPos], flags, ctx)
|
||||||
|
|
@ -394,7 +396,7 @@ proc semGenericStmt(c: PContext, n: PNode,
|
||||||
checkMinSonsLen(n, 2)
|
checkMinSonsLen(n, 2)
|
||||||
result.sons[1] = semGenericStmt(c, n.sons[1], flags, ctx)
|
result.sons[1] = semGenericStmt(c, n.sons[1], flags, ctx)
|
||||||
else:
|
else:
|
||||||
for i in countup(0, sonsLen(n) - 1):
|
for i in countup(0, sonsLen(n) - 1):
|
||||||
result.sons[i] = semGenericStmt(c, n.sons[i], flags, ctx)
|
result.sons[i] = semGenericStmt(c, n.sons[i], flags, ctx)
|
||||||
|
|
||||||
proc semGenericStmt(c: PContext, n: PNode): PNode =
|
proc semGenericStmt(c: PContext, n: PNode): PNode =
|
||||||
|
|
|
||||||
|
|
@ -226,6 +226,10 @@ proc `$`*[A, B](t: Table[A, B]): string =
|
||||||
## The `$` operator for hash tables.
|
## The `$` operator for hash tables.
|
||||||
dollarImpl()
|
dollarImpl()
|
||||||
|
|
||||||
|
proc hasKey*[A, B](t: TableRef[A, B], key: A): bool =
|
||||||
|
## returns true iff `key` is in the table `t`.
|
||||||
|
result = t[].hasKey(key)
|
||||||
|
|
||||||
template equalsImpl() =
|
template equalsImpl() =
|
||||||
if s.counter == t.counter:
|
if s.counter == t.counter:
|
||||||
# different insertion orders mean different 'data' seqs, so we have
|
# different insertion orders mean different 'data' seqs, so we have
|
||||||
|
|
@ -293,10 +297,6 @@ proc hasKeyOrPut*[A, B](t: var TableRef[A, B], key: A, val: B): bool =
|
||||||
## returns true iff `key` is in the table, otherwise inserts `value`.
|
## returns true iff `key` is in the table, otherwise inserts `value`.
|
||||||
t[].hasKeyOrPut(key, val)
|
t[].hasKeyOrPut(key, val)
|
||||||
|
|
||||||
proc hasKey*[A, B](t: TableRef[A, B], key: A): bool =
|
|
||||||
## returns true iff `key` is in the table `t`.
|
|
||||||
result = t[].hasKey(key)
|
|
||||||
|
|
||||||
proc contains*[A, B](t: TableRef[A, B], key: A): bool =
|
proc contains*[A, B](t: TableRef[A, B], key: A): bool =
|
||||||
## alias of `hasKey` for use with the `in` operator.
|
## alias of `hasKey` for use with the `in` operator.
|
||||||
return hasKey[A, B](t, key)
|
return hasKey[A, B](t, key)
|
||||||
|
|
|
||||||
10
tests/generics/mclosed_sym.nim
Normal file
10
tests/generics/mclosed_sym.nim
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
|
||||||
|
type R* = object
|
||||||
|
|
||||||
|
type Data*[T] = object
|
||||||
|
d*: T
|
||||||
|
|
||||||
|
proc same(r:R, d:int) = echo "TEST2"
|
||||||
|
|
||||||
|
proc doIt*(d:Data, r:R) =
|
||||||
|
r.same(1) # Expecting this to invoke the local `same()` method
|
||||||
11
tests/generics/tclosed_sym.nim
Normal file
11
tests/generics/tclosed_sym.nim
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
discard """
|
||||||
|
output: "TEST2"
|
||||||
|
"""
|
||||||
|
|
||||||
|
# bug #2664
|
||||||
|
|
||||||
|
import mclosed_sym
|
||||||
|
|
||||||
|
proc same(r:R, d:int) = echo "TEST1"
|
||||||
|
|
||||||
|
doIt(Data[int](d:123), R())
|
||||||
Loading…
Add table
Add a link
Reference in a new issue