Skip to content

Commit 40d9443

Browse files
committed
fix #715 again
the regression was caused by the introduction of "generic" lambdas
1 parent e8a7366 commit 40d9443

File tree

6 files changed

+29
-4
lines changed

6 files changed

+29
-4
lines changed

compiler/msgs.nim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ type
106106
errThreadvarCannotInit, errWrongSymbolX, errIllegalCaptureX,
107107
errXCannotBeClosure, errXMustBeCompileTime,
108108
errCannotInferTypeOfTheLiteral,
109+
errCannotInferReturnType,
110+
errGenericLambdaNotAllowed,
109111
errUser,
110112
warnCannotOpenFile,
111113
warnOctalEscape, warnXIsNeverRead, warnXmightNotBeenInit,
@@ -355,6 +357,10 @@ const
355357
errXCannotBeClosure: "'$1' cannot have 'closure' calling convention",
356358
errXMustBeCompileTime: "'$1' can only be used in compile-time context",
357359
errCannotInferTypeOfTheLiteral: "cannot infer the type of the $1",
360+
errCannotInferReturnType: "cannot infer the return type of the proc",
361+
errGenericLambdaNotAllowed: "A nested proc can have generic parameters only when " &
362+
"it is used as an operand to another routine and the types " &
363+
"of the generic paramers can be infered from the expected signature.",
358364
errUser: "$1",
359365
warnCannotOpenFile: "cannot open \'$1\' [CannotOpenFile]",
360366
warnOctalEscape: "octal escape sequences do not exist; leading zero is ignored [OctalEscape]",

compiler/semdata.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ type
4242

4343
TExprFlag* = enum
4444
efLValue, efWantIterator, efInTypeof, efWantStmt, efDetermineType,
45-
efAllowDestructor, efWantValue
45+
efAllowDestructor, efWantValue, efOperand
4646
TExprFlags* = set[TExprFlag]
4747

4848
PContext* = ref TContext

compiler/semexprs.nim

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ proc semFieldAccess(c: PContext, n: PNode, flags: TExprFlags = {}): PNode
2121

2222
proc semOperand(c: PContext, n: PNode, flags: TExprFlags = {}): PNode =
2323
# same as 'semExprWithType' but doesn't check for proc vars
24-
result = semExpr(c, n, flags)
24+
result = semExpr(c, n, flags + {efOperand})
2525
if result.kind == nkEmpty:
2626
# do not produce another redundant error message:
2727
#raiseRecoverableError("")
@@ -1218,6 +1218,7 @@ proc semReturn(c: PContext, n: PNode): PNode =
12181218

12191219
proc semProcBody(c: PContext, n: PNode): PNode =
12201220
openScope(c)
1221+
12211222
result = semExpr(c, n)
12221223
if c.p.resultSym != nil and not isEmptyType(result.typ):
12231224
# transform ``expr`` to ``result = expr``, but not if the expr is already
@@ -1241,6 +1242,10 @@ proc semProcBody(c: PContext, n: PNode): PNode =
12411242
result = semAsgn(c, a)
12421243
else:
12431244
discardCheck(c, result)
1245+
1246+
if c.p.resultSym != nil and c.p.resultSym.typ.isMetaType:
1247+
localError(c.p.resultSym.info, errCannotInferReturnType)
1248+
12441249
closeScope(c)
12451250

12461251
proc semYieldVarResult(c: PContext, n: PNode, restype: PType) =

compiler/semstmts.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -944,13 +944,15 @@ proc semLambda(c: PContext, n: PNode, flags: TExprFlags): PNode =
944944
localError(n.sons[bodyPos].info, errImplOfXNotAllowed, s.name.s)
945945
#if efDetermineType notin flags:
946946
# XXX not good enough; see tnamedparamanonproc.nim
947-
if n.sons[genericParamsPos].kind == nkEmpty:
947+
if gp.len == 0 or (gp.len == 1 and tfRetType in gp[0].typ.flags):
948948
pushProcCon(c, s)
949949
addResult(c, s.typ.sons[0], n.info, skProc)
950950
let semBody = hloBody(c, semProcBody(c, n.sons[bodyPos]))
951951
n.sons[bodyPos] = transformBody(c.module, semBody, s)
952952
addResultNode(c, n)
953953
popProcCon(c)
954+
elif efOperand notin flags:
955+
localError(n.info, errGenericLambdaNotAllowed)
954956
sideEffectsCheck(c, s)
955957
else:
956958
localError(n.info, errImplOfXexpected, s.name.s)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
discard """
2+
msg: "nested proc can have generic parameters only when"
3+
line: 6
4+
"""
5+
6+
let x = proc (x, y): auto = x + y
7+

tests/generics/tgenericlambda.nim

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
discard """
2-
output: "10\n10\n1\n2\n3"
2+
output: "10\n10\n1\n2\n3\n15"
33
"""
44

55
proc test(x: proc (a, b: int): int) =
@@ -16,3 +16,8 @@ proc foreach[T](s: seq[T], body: proc(x: T)) =
1616
foreach(@[1,2,3]) do (x):
1717
echo x
1818

19+
proc foo =
20+
let x = proc (a, b: int): auto = a + b
21+
echo x(5, 10)
22+
23+
foo()

0 commit comments

Comments
 (0)