This snippet:
{
if( true ){ scope(exit) writef("hello"); }
writefln(" world" );
}
Produces the output "hello world", as expected. However, when the if statement's braces are removed...
{
if( true ) scope(exit) writef("hello");
writefln( " world" );
}
... then the first writef call is skipped entirely, and the output is " world". No errors or compiler warnings are generated.
Comment #1 by gide — 2008-03-09T11:54:45Z
This looks similar to an issue I had with foreach and scope, BUG 1765.
Comment #2 by clugdbug — 2009-09-16T07:19:26Z
Seems to be the same issue as bug 1087.
Comment #3 by clugdbug — 2009-09-16T11:48:04Z
This is one example of an endemic problem. There's a host of possible test cases which fail. Basically, OnScopeStatement works ONLY when it is in a CompoundStatement: it's the only time when scopecode() is called. Everywhere else, the only thing called is OnScopeStatement::semantic(), which does nothing.
I think that what should happen, is that since in these cases it's the only thing in its scope, OnScopeStatement::semantic should either (1) generate an error; or (2) run semantic on its statement (depending on the type of scopecode it is).
And labels (bugzilla 1087) should be treated specially, since they are not really statements themselves.
Here's a non-exhaustive collection of test cases, each of which generate two asm instructions so that the disassembly is very easy to understand. In each case, an 'int 3' instruction should appear, but none are present.
This blocks deterministic destruction.
(BTW in each of these cases, in D2, if you replace "scope(exit) asm{int3}" with "A a;" where A is a struct with a destructor, the compiler segfaults).
---------------------
void bug1087a() { // Bugzilla 1087
dummylabel:
scope(exit)
asm { int 3; }
}
void bug1087b() { // Bugzilla 1894
if (true)
scope(exit)
asm { int 3; }
}
void bug1087c() {
if (false){} else
scope(exit)
asm { int 3; }
}
void bug1087d() {
while(true)
scope(exit)
asm { int 3; }
}
void bug1087e() {
do scope(exit)
asm { int 3; }
while(false)
}
void bug1087f() {
foreach(x; 1..2) scope(exit) asm {int 3; }
}
void bug1087g() {
scope(exit) scope(exit) asm {int 3; }
}
Comment #4 by clugdbug — 2009-09-16T11:53:18Z
And it applies equally to D1 (obviously, a couple of the test cases don't apply).
Comment #5 by clugdbug — 2010-05-22T14:21:41Z
An interim patch:
To patch the worst instance of this ( if (xxx) scope(exit) yyy;
creating a error which recommends scope(exit) if (xxx) yyy; ),
add IsScopeGuardStatement() to Statement and OnScopeStatement, and then
add the following lines to statement.c 2300 (IfStatement::semantic())
// If we can short-circuit evaluate the if statement, don't do the
// semantic analysis of the skipped code.
// This feature allows a limited form of conditional compilation.
condition = condition->optimize(WANTflags);
+ if (ifbody->IsScopeGuardStatement() ) {
+ OnScopeStatement *onsc = ifbody->IsScopeGuardStatement();
+ IfStatement *iff = new IfStatement(loc, arg, condition, onsc->statement, elsebody);
+ error("Use of ScopeGuardStatement in otherwise empty scope. Did you mean\n%s",
+ (new OnScopeStatement(onsc->loc, onsc->tok, iff))->toChars());
+ }
Comment #6 by bugzilla — 2010-05-28T21:38:56Z
*** Issue 1087 has been marked as a duplicate of this issue. ***