From de4b894541f6304b29c5714b032f415f567c2f3c Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Fri, 9 Dec 2011 15:56:48 +0200 Subject: proc arguments are in scope when the result type definition is being processed This is intended for type deduction depending on the arguments proc foo(x: type): type(expr(x)) --- compiler/seminst.nim | 3 ++- compiler/semstmts.nim | 6 ++++-- compiler/semtempl.nim | 3 ++- compiler/semtypes.nim | 6 +++++- tests/compile/ttypeselectors.nim | 3 +++ 5 files changed, 16 insertions(+), 5 deletions(-) diff --git a/compiler/seminst.nim b/compiler/seminst.nim index d7dbe623b..641cf9a89 100755 --- a/compiler/seminst.nim +++ b/compiler/seminst.nim @@ -135,7 +135,8 @@ proc generateInstance(c: PContext, fn: PSym, pt: TIdTable, if n.sons[paramsPos].kind != nkEmpty: removeDefaultParamValues(n.sons[ParamsPos]) semParamList(c, n.sons[ParamsPos], nil, result) - addParams(c, result.typ.n) + # XXX: obsoleted - happens in semParamList # + # addParams(c, result.typ.n) else: result.typ = newTypeS(tyProc, c) addSon(result.typ, nil) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 3766841f1..0136724fa 100755 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -616,7 +616,8 @@ proc semLambda(c: PContext, n: PNode): PNode = illFormedAst(n) # process parameters: if n.sons[paramsPos].kind != nkEmpty: semParamList(c, n.sons[ParamsPos], nil, s) - addParams(c, s.typ.n) + # XXX: obsoleted - happens in semParamList + # addParams(c, s.typ.n) ParamsTypeCheck(c, s.typ) else: s.typ = newTypeS(tyProc, c) @@ -665,7 +666,8 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind, n.sons[genericParamsPos] = gp # check for semantics again: semParamList(c, n.sons[ParamsPos], nil, s) - addParams(c, s.typ.n) + # XXX: obsoleted - happens in semParamList + # addParams(c, s.typ.n) else: s.typ = newTypeS(tyProc, c) addSon(s.typ, nil) diff --git a/compiler/semtempl.nim b/compiler/semtempl.nim index ee540c5d2..26216ab4c 100755 --- a/compiler/semtempl.nim +++ b/compiler/semtempl.nim @@ -185,7 +185,8 @@ proc semTemplateDef(c: PContext, n: PNode): PNode = # use ``stmt`` as implicit result type s.typ.sons[0] = newTypeS(tyStmt, c) s.typ.n.sons[0] = newNodeIT(nkType, n.info, s.typ.sons[0]) - addParams(c, s.typ.n) # resolve parameters: + # XXX: obsoleted - happens in semParamList # + # addParams(c, s.typ.n) # resolve parameters: var toBind = initIntSet() n.sons[bodyPos] = resolveTemplateParams(c, n.sons[bodyPos], false, toBind) if s.typ.sons[0].kind notin {tyStmt, tyTypeDesc}: diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index 9e78f98e3..78a95c56b 100755 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -569,6 +569,8 @@ proc semProcTypeNode(c: PContext, n, genericParams: PNode, LocalError(a.sons[j].info, errAttemptToRedefine, arg.name.s) addSon(result.n, newSymNode(arg)) addSon(result, typ) + addDecl(c, arg) + if n.sons[0].kind != nkEmpty: var r = paramType(c, n.sons[0], genericParams, cl) # turn explicit 'void' return type into 'nil' because the rest of the @@ -711,11 +713,13 @@ proc semTypeNode(c: PContext, n: PNode, prev: PType): PType = of nkDistinctTy: result = semDistinct(c, n, prev) of nkProcTy: checkSonsLen(n, 2) - result = semProcTypeNode(c, n.sons[0], nil, prev) + openScope(c.tab) + result = semProcTypeNode(c, n.sons[0], nil, prev) # dummy symbol for `pragma`: var s = newSymS(skProc, newIdentNode(getIdent("dummy"), n.info), c) s.typ = result pragma(c, s, n.sons[1], procTypePragmas) + closeScope(c.tab) of nkEnumTy: result = semEnum(c, n, prev) of nkType: result = n.typ of nkStmtListType: result = semStmtListType(c, n, prev) diff --git a/tests/compile/ttypeselectors.nim b/tests/compile/ttypeselectors.nim index 7d76cafef..9ed7e2008 100644 --- a/tests/compile/ttypeselectors.nim +++ b/tests/compile/ttypeselectors.nim @@ -35,3 +35,6 @@ var y*: type(t2(100)) = "test" proc t6*(x: type(t3(0))): type(t1(0)) = result = $x +proc t7*(x: int): type($x) = + result = "test" + -- cgit 1.4.1-2-gfad0