fixes #3329
This commit is contained in:
parent
432884861e
commit
36f68e1650
7 changed files with 65 additions and 17 deletions
|
|
@ -99,7 +99,10 @@ proc getUniqueType*(key: PType): PType =
|
||||||
gCanonicalTypes[k] = key
|
gCanonicalTypes[k] = key
|
||||||
result = key
|
result = key
|
||||||
of tyTypeDesc, tyTypeClasses, tyGenericParam, tyFromExpr, tyFieldAccessor:
|
of tyTypeDesc, tyTypeClasses, tyGenericParam, tyFromExpr, tyFieldAccessor:
|
||||||
internalError("getUniqueType")
|
if key.sym != nil:
|
||||||
|
internalError(key.sym.info, "metatype not eliminated")
|
||||||
|
else:
|
||||||
|
internalError("metatype not eliminated")
|
||||||
of tyDistinct:
|
of tyDistinct:
|
||||||
if key.deepCopy != nil: result = key
|
if key.deepCopy != nil: result = key
|
||||||
else: result = getUniqueType(lastSon(key))
|
else: result = getUniqueType(lastSon(key))
|
||||||
|
|
|
||||||
|
|
@ -234,7 +234,7 @@ proc semGenericStmt(c: PContext, n: PNode,
|
||||||
discard
|
discard
|
||||||
of skProc, skMethod, skIterators, skConverter, skModule:
|
of skProc, skMethod, skIterators, skConverter, skModule:
|
||||||
result.sons[0] = symChoice(c, fn, s, scOption)
|
result.sons[0] = symChoice(c, fn, s, scOption)
|
||||||
# do check of 's.magic==mRoof' here because it might be some
|
# do not check of 's.magic==mRoof' here because it might be some
|
||||||
# other '^' but after overload resolution the proper one:
|
# other '^' but after overload resolution the proper one:
|
||||||
if ctx.bracketExpr != nil and n.len == 2 and s.name.s == "^":
|
if ctx.bracketExpr != nil and n.len == 2 and s.name.s == "^":
|
||||||
result.add ctx.bracketExpr
|
result.add ctx.bracketExpr
|
||||||
|
|
|
||||||
|
|
@ -164,7 +164,7 @@ proc instantiateProcType(c: PContext, pt: TIdTable,
|
||||||
addDecl(c, prc)
|
addDecl(c, prc)
|
||||||
|
|
||||||
pushInfoContext(info)
|
pushInfoContext(info)
|
||||||
var cl = initTypeVars(c, pt, info)
|
var cl = initTypeVars(c, pt, info, nil)
|
||||||
var result = instCopyType(cl, prc.typ)
|
var result = instCopyType(cl, prc.typ)
|
||||||
let originalParams = result.n
|
let originalParams = result.n
|
||||||
result.n = originalParams.shallowCopy
|
result.n = originalParams.shallowCopy
|
||||||
|
|
|
||||||
|
|
@ -957,27 +957,35 @@ proc semDo(c: PContext, n: PNode, flags: TExprFlags): PNode =
|
||||||
proc semInferredLambda(c: PContext, pt: TIdTable, n: PNode): PNode =
|
proc semInferredLambda(c: PContext, pt: TIdTable, n: PNode): PNode =
|
||||||
var n = n
|
var n = n
|
||||||
|
|
||||||
n = replaceTypesInBody(c, pt, n)
|
let original = n.sons[namePos].sym
|
||||||
|
let s = copySym(original, false)
|
||||||
|
incl(s.flags, sfFromGeneric)
|
||||||
|
|
||||||
|
n = replaceTypesInBody(c, pt, n, original)
|
||||||
result = n
|
result = n
|
||||||
|
s.ast = result
|
||||||
|
n.sons[namePos].sym = s
|
||||||
n.sons[genericParamsPos] = emptyNode
|
n.sons[genericParamsPos] = emptyNode
|
||||||
n.sons[paramsPos] = n.typ.n
|
let params = n.typ.n
|
||||||
|
n.sons[paramsPos] = params
|
||||||
|
s.typ = n.typ
|
||||||
|
for i in 1..<params.len:
|
||||||
|
if params[i].typ.kind in {tyTypeDesc, tyGenericParam,
|
||||||
|
tyFromExpr, tyFieldAccessor}+tyTypeClasses:
|
||||||
|
localError(params[i].info, "cannot infer type of parameter: " &
|
||||||
|
params[i].sym.name.s)
|
||||||
openScope(c)
|
openScope(c)
|
||||||
var s = n.sons[namePos].sym
|
|
||||||
pushOwner(s)
|
pushOwner(s)
|
||||||
addParams(c, n.typ.n, skProc)
|
addParams(c, params, skProc)
|
||||||
pushProcCon(c, s)
|
pushProcCon(c, s)
|
||||||
addResult(c, n.typ.sons[0], n.info, skProc)
|
addResult(c, n.typ.sons[0], n.info, skProc)
|
||||||
addResultNode(c, n)
|
addResultNode(c, n)
|
||||||
let semBody = hloBody(c, semProcBody(c, n.sons[bodyPos]))
|
let semBody = hloBody(c, semProcBody(c, n.sons[bodyPos]))
|
||||||
n.sons[bodyPos] = transformBody(c.module, semBody, n.sons[namePos].sym)
|
n.sons[bodyPos] = transformBody(c.module, semBody, s)
|
||||||
popProcCon(c)
|
popProcCon(c)
|
||||||
popOwner()
|
popOwner()
|
||||||
closeScope(c)
|
closeScope(c)
|
||||||
|
|
||||||
s.ast = result
|
|
||||||
|
|
||||||
# alternative variant (not quite working):
|
# alternative variant (not quite working):
|
||||||
# var prc = arg[0].sym
|
# var prc = arg[0].sym
|
||||||
# let inferred = c.semGenerateInstance(c, prc, m.bindings, arg.info)
|
# let inferred = c.semGenerateInstance(c, prc, m.bindings, arg.info)
|
||||||
|
|
|
||||||
|
|
@ -90,6 +90,7 @@ type
|
||||||
allowMetaTypes*: bool # allow types such as seq[Number]
|
allowMetaTypes*: bool # allow types such as seq[Number]
|
||||||
# i.e. the result contains unresolved generics
|
# i.e. the result contains unresolved generics
|
||||||
skipTypedesc*: bool # wether we should skip typeDescs
|
skipTypedesc*: bool # wether we should skip typeDescs
|
||||||
|
owner*: PSym # where this instantiation comes from
|
||||||
recursionLimit: int
|
recursionLimit: int
|
||||||
|
|
||||||
proc replaceTypeVarsTAux(cl: var TReplTypeVars, t: PType): PType
|
proc replaceTypeVarsTAux(cl: var TReplTypeVars, t: PType): PType
|
||||||
|
|
@ -208,6 +209,9 @@ proc replaceTypeVarsN(cl: var TReplTypeVars, n: PNode): PNode =
|
||||||
|
|
||||||
proc replaceTypeVarsS(cl: var TReplTypeVars, s: PSym): PSym =
|
proc replaceTypeVarsS(cl: var TReplTypeVars, s: PSym): PSym =
|
||||||
if s == nil: return nil
|
if s == nil: return nil
|
||||||
|
# symbol is not our business:
|
||||||
|
if cl.owner != nil and s.owner != cl.owner:
|
||||||
|
return s
|
||||||
result = PSym(idTableGet(cl.symMap, s))
|
result = PSym(idTableGet(cl.symMap, s))
|
||||||
if result == nil:
|
if result == nil:
|
||||||
result = copySym(s, false)
|
result = copySym(s, false)
|
||||||
|
|
@ -477,22 +481,33 @@ proc replaceTypeVarsTAux(cl: var TReplTypeVars, t: PType): PType =
|
||||||
|
|
||||||
else: discard
|
else: discard
|
||||||
|
|
||||||
proc initTypeVars*(p: PContext, pt: TIdTable, info: TLineInfo): TReplTypeVars =
|
proc initTypeVars*(p: PContext, pt: TIdTable, info: TLineInfo;
|
||||||
|
owner: PSym): TReplTypeVars =
|
||||||
initIdTable(result.symMap)
|
initIdTable(result.symMap)
|
||||||
copyIdTable(result.typeMap, pt)
|
copyIdTable(result.typeMap, pt)
|
||||||
initIdTable(result.localCache)
|
initIdTable(result.localCache)
|
||||||
result.info = info
|
result.info = info
|
||||||
result.c = p
|
result.c = p
|
||||||
|
result.owner = owner
|
||||||
|
|
||||||
proc replaceTypesInBody*(p: PContext, pt: TIdTable, n: PNode): PNode =
|
proc replaceTypesInBody*(p: PContext, pt: TIdTable, n: PNode;
|
||||||
var cl = initTypeVars(p, pt, n.info)
|
owner: PSym): PNode =
|
||||||
|
var cl = initTypeVars(p, pt, n.info, owner)
|
||||||
|
pushInfoContext(n.info)
|
||||||
|
result = replaceTypeVarsN(cl, n)
|
||||||
|
popInfoContext()
|
||||||
|
|
||||||
|
proc replaceTypesForLambda*(p: PContext, pt: TIdTable, n: PNode;
|
||||||
|
original, new: PSym): PNode =
|
||||||
|
var cl = initTypeVars(p, pt, n.info, original)
|
||||||
|
idTablePut(cl.symMap, original, new)
|
||||||
pushInfoContext(n.info)
|
pushInfoContext(n.info)
|
||||||
result = replaceTypeVarsN(cl, n)
|
result = replaceTypeVarsN(cl, n)
|
||||||
popInfoContext()
|
popInfoContext()
|
||||||
|
|
||||||
proc generateTypeInstance*(p: PContext, pt: TIdTable, info: TLineInfo,
|
proc generateTypeInstance*(p: PContext, pt: TIdTable, info: TLineInfo,
|
||||||
t: PType): PType =
|
t: PType): PType =
|
||||||
var cl = initTypeVars(p, pt, info)
|
var cl = initTypeVars(p, pt, info, nil)
|
||||||
pushInfoContext(info)
|
pushInfoContext(info)
|
||||||
result = replaceTypeVarsT(cl, t)
|
result = replaceTypeVarsT(cl, t)
|
||||||
popInfoContext()
|
popInfoContext()
|
||||||
|
|
|
||||||
|
|
@ -590,7 +590,7 @@ proc tryResolvingStaticExpr(c: var TCandidate, n: PNode): PNode =
|
||||||
# Here, N-1 will be initially nkStaticExpr that can be evaluated only after
|
# Here, N-1 will be initially nkStaticExpr that can be evaluated only after
|
||||||
# N is bound to a concrete value during the matching of the first param.
|
# N is bound to a concrete value during the matching of the first param.
|
||||||
# This proc is used to evaluate such static expressions.
|
# This proc is used to evaluate such static expressions.
|
||||||
let instantiated = replaceTypesInBody(c.c, c.bindings, n)
|
let instantiated = replaceTypesInBody(c.c, c.bindings, n, nil)
|
||||||
result = c.c.semExpr(c.c, instantiated)
|
result = c.c.semExpr(c.c, instantiated)
|
||||||
|
|
||||||
proc typeRel(c: var TCandidate, f, aOrig: PType, doBind = true): TTypeRelation =
|
proc typeRel(c: var TCandidate, f, aOrig: PType, doBind = true): TTypeRelation =
|
||||||
|
|
|
||||||
22
tests/typerel/t2plus.nim
Normal file
22
tests/typerel/t2plus.nim
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
discard """
|
||||||
|
output: "2.0"
|
||||||
|
"""
|
||||||
|
|
||||||
|
{.warning[TypelessParam]: off.}
|
||||||
|
|
||||||
|
import future
|
||||||
|
|
||||||
|
# bug #3329
|
||||||
|
|
||||||
|
proc foldRight[T,U](lst: seq[T], v: U, f: (T, U) -> U): U =
|
||||||
|
result = v
|
||||||
|
for x in lst:
|
||||||
|
result = f(x, result)
|
||||||
|
|
||||||
|
proc mean[T: SomeNumber](xs: seq[T]): T =
|
||||||
|
xs.foldRight(0.T, (xBAZ: auto, yBAZ: auto) => xBAZ + yBAZ) / T(xs.len)
|
||||||
|
|
||||||
|
when isMainModule:
|
||||||
|
let x = mean(@[1.float, 2, 3])
|
||||||
|
echo x
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue