3574 – post-condition in void main() is not evaluated if there is no return statement

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3574 - post-condition in void main() is not evaluated if there is no return statement
Summary: post-condition in void main() is not evaluated if there is no return statement
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: pull, wrong-code
Depends on:
Blocks:
Reported: 2009年12月04日 05:52 UTC by Koroskin Denis
Modified: 2015年06月09日 05:10 UTC (History)
3 users (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Koroskin Denis 2009年12月04日 05:52:43 UTC
import std.stdio;
void main()
out
{
 writeln("out");
}
body
{
 //return;
}
"out" is only printed if return is explicit.
Comment 1 Don 2010年01月27日 02:47:38 UTC
Applies to D1 (even ancient versions) as well as D2.
Comment 2 Don 2010年01月28日 00:29:30 UTC
This is a very obscure bug; it's of interest for educational purposes only.
Only void main() is affected, and it's because the return 0; needs to be added AFTER the invariant is processed, not before.
This patch (against DMD2, svn 356) just moves the return 0; insertion slightly later in FuncDeclaration::semantic3().
Index: func.c
===================================================================
--- func.c	(revision 356)
+++ func.c	(working copy)
@@ -1287,16 +1287,8 @@
 
 		int offend = blockexit & BEfallthru;
 #endif
-		if (type->nextOf()->ty == Tvoid)
+		if (type->nextOf()->ty != Tvoid)
 		{
-		 if (offend && isMain())
-		 {	// Add a return 0; statement
-			Statement *s = new ReturnStatement(0, new IntegerExp(0));
-			fbody = new CompoundStatement(0, fbody, s);
-		 }
-		}
-		else
-		{
 		 if (offend)
 		 { Expression *e;
 #if DMDV1
@@ -1462,8 +1454,17 @@
 		 }
 		 ReturnStatement *s = new ReturnStatement(0, e);
 		 a->push(s);
-		}
+		} 
 	 }
+#if DMDV2
+	 int blockexit = fbody ? fbody->blockExit() : BEfallthru;
+	 int offend = blockexit & BEfallthru;
+#endif
+	 if (offend && isMain() && type->nextOf()->ty == Tvoid)
+	 { // For void main(), add a 'return 0' statement
+		ReturnStatement *s = new ReturnStatement(0, new IntegerExp(0));
+		a->push(s);
+	 }
 
 	 fbody = new CompoundStatement(0, a);
 #if DMDV2
Comment 3 Don 2010年01月28日 14:00:19 UTC
Oops, there's something wrong with this patch. It can interfere badly with foreach for some reason.
Comment 4 Kenji Hara 2012年06月06日 07:21:09 UTC
https://github.com/D-Programming-Language/dmd/pull/986 
Comment 5 github-bugzilla 2012年06月11日 14:33:53 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dmd
https://github.com/D-Programming-Language/dmd/commit/2b05220ce3c10602748037189cfd6f3123faafd7
fix Issue 3574 - post-condition in void main() is not evaluated if there is no return statement
https://github.com/D-Programming-Language/dmd/commit/48d389b430b16b61d5f101485deb004a50b3a700
Merge pull request #986 from 9rnsr/fix3574
Issue 3574 - post-condition in void main() is not evaluated if there is no return statement


AltStyle によって変換されたページ (->オリジナル) /