From 4f946cb44e4fe60451b8ce4d15868ef0bce6949a Mon Sep 17 00:00:00 2001 From: Araq Date: Wed, 5 Mar 2014 08:30:05 +0100 Subject: [PATCH] fixes #942 --- compiler/semstmts.nim | 2 +- tests/discard/tdiscardable.nim | 16 ++++++++++++++++ tests/exprs/texprstmt.nim | 2 +- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index c8d9e353a9..b2b3ceb6d0 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -1320,7 +1320,7 @@ proc semStmtList(c: PContext, n: PNode, flags: TExprFlags): PNode = if n.sons[i].typ == enforceVoidContext or usesResult(n.sons[i]): voidContext = true n.typ = enforceVoidContext - if i == last and efWantValue in flags: + if i == last and (length == 1 or efWantValue in flags): n.typ = n.sons[i].typ if not isEmptyType(n.typ): n.kind = nkStmtListExpr elif i != last or voidContext or c.inTypeClass > 0: diff --git a/tests/discard/tdiscardable.nim b/tests/discard/tdiscardable.nim index c0551ba2f8..a806ccdce6 100644 --- a/tests/discard/tdiscardable.nim +++ b/tests/discard/tdiscardable.nim @@ -11,3 +11,19 @@ proc q[T](x, y: T): T {.discardable.} = p(8, 2) q[float](0.8, 0.2) +# bug #942 + +template maybeMod(x: Tinteger, module:Natural):expr = + if module > 0: x mod module + else: x + +proc foo(b: int):int = + var x = 1 + result = x.maybeMod(b) # Works fine + +proc bar(b: int):int = + result = 1 + result = result.maybeMod(b) # Error: value returned by statement has to be discarded + +echo foo(0) +echo bar(0) diff --git a/tests/exprs/texprstmt.nim b/tests/exprs/texprstmt.nim index b32394d8d1..355da24073 100644 --- a/tests/exprs/texprstmt.nim +++ b/tests/exprs/texprstmt.nim @@ -1,6 +1,6 @@ discard """ line: 10 - errormsg: "value returned by statement has to be discarded" + errormsg: "value of type 'string' has to be discarded" """ # bug #578