From 39ad3a0da69908ab937ea2156f06f4d3bbd0b2b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20D=C3=B6ring?= Date: Tue, 20 Nov 2018 13:12:47 +0100 Subject: [PATCH] fix #9726 (#9765) --- compiler/semstmts.nim | 5 +++-- tests/discard/tdiscardable.nim | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index adc94f24c7..1f2b9f0b35 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -42,10 +42,11 @@ proc semDiscard(c: PContext, n: PNode): PNode = if n.sons[0].kind != nkEmpty: n.sons[0] = semExprWithType(c, n.sons[0]) let sonType = n.sons[0].typ + let sonKind = n.sons[0].kind if isEmptyType(sonType) or sonType.kind == tyNone or n.sons[0].kind == nkTypeOfExpr: localError(c.config, n.info, errInvalidDiscard) - if sonType.kind == tyProc: - # tyProc is disallowed to prevent ``discard foo`` to pass, when ``discard foo()`` is meant. + if sonType.kind == tyProc and sonKind notin nkCallKinds: + # tyProc is disallowed to prevent ``discard foo`` to be valid, when ``discard foo()`` is meant. localError(c.config, n.info, "illegal discard proc, did you mean: " & $n[0] & "()") proc semBreakOrContinue(c: PContext, n: PNode): PNode = diff --git a/tests/discard/tdiscardable.nim b/tests/discard/tdiscardable.nim index f979b29c93..99144e3248 100644 --- a/tests/discard/tdiscardable.nim +++ b/tests/discard/tdiscardable.nim @@ -27,3 +27,11 @@ proc bar(b: int):int = echo foo(0) echo bar(0) + +# bug #9726 + +proc foo: (proc: int) = + proc bar: int = 1 + return bar + +discard foo()