Jump to page: 1 2
Thread overview
Missed scope guard statements
Mar 05, 2008
Steve Jackson
Mar 05, 2008
Robert Fraser
Mar 06, 2008
Simen Kjaeraas
Mar 06, 2008
Gide Nwawudu
Mar 06, 2008
Max Samukha
Mar 06, 2008
Janice Caron
Mar 06, 2008
Max Samukha
Mar 06, 2008
Max Samukha
Mar 06, 2008
Steve Jackson
Mar 07, 2008
Robert Fraser
Mar 07, 2008
Max Samukha
March 05, 2008
(This is a report of what I believe is a "new" bug in the D compiler.  I'm not sure whether this newsgroup is the appropriate place to report such things, but it is the best I have been able to find.)

While experimenting with scope guard statements, I came across some unexpected behavior.  This snippet:

{
 if( true ){ scope(exit) writef("hello"); }
 writefln(" world" );
}

Produces the output "hello world", as I expected.  However, if I remove the if statement's braces, like so:

{
 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.

I'm using the dmd compiler v2.010 on Linux.

March 05, 2008
Hi Steve,

The place to report issues is here: http://d.puremagic.com/issues/ . You need to register a bugzilla account, then click "New" on the bottom bar, then "D", and enter a world of happiness and bug reporting.

I would guess that in both cases, that should produce the output:

 world
hello

Since the writefln("hello") is within a scope-exit, so should only fire as part of the function exit condition.

Steve Jackson wrote:
> (This is a report of what I believe is a "new" bug in the D compiler.  I'm not sure whether this newsgroup is the appropriate place to report such things, but it is the best I have been able to find.)
> 
> While experimenting with scope guard statements, I came across some unexpected behavior.  This snippet:
> 
> {
>  if( true ){ scope(exit) writef("hello"); }
>  writefln(" world" );
> }
> 
> Produces the output "hello world", as I expected.  However, if I remove the if statement's braces, like so:
> 
> {
>  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.
> 
> I'm using the dmd compiler v2.010 on Linux.
March 06, 2008
On Wed, 05 Mar 2008 23:54:35 +0100, Robert Fraser <fraserofthenight@gmail.com> wrote:

> Hi Steve,
>
> The place to report issues is here: http://d.puremagic.com/issues/ . You need to register a bugzilla account, then click "New" on the bottom bar, then "D", and enter a world of happiness and bug reporting.
>
> I would guess that in both cases, that should produce the output:
>
>   world
> hello
>
> Since the writefln("hello") is within a scope-exit, so should only fire as part of the function exit condition.

Actually, "hello world" is indeed correct output, as the scope(exit) is executed when it exits the scope of the if{}.

The latter does indeed look like a bug, though.

-- Simen
March 06, 2008
On Wed, 05 Mar 2008 16:26:27 -0500, Steve Jackson <sjackso@cs.wisc.edu> wrote:

>(This is a report of what I believe is a "new" bug in the D compiler.  I'm not sure whether this newsgroup is the appropriate place to report such things, but it is the best I have been able to find.)
>
>While experimenting with scope guard statements, I came across some unexpected behavior.  This snippet:
>
>{
> if( true ){ scope(exit) writef("hello"); }
> writefln(" world" );
>}
>
>Produces the output "hello world", as I expected.  However, if I remove the if statement's braces, like so:
>
>{
> 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.
>
>I'm using the dmd compiler v2.010 on Linux.

Looks similar to this bug.

http://d.puremagic.com/issues/show_bug.cgi?id=1765

Gide
March 06, 2008
On Wed, 05 Mar 2008 16:26:27 -0500, Steve Jackson <sjackso@cs.wisc.edu> wrote:

>(This is a report of what I believe is a "new" bug in the D compiler.  I'm not sure whether this newsgroup is the appropriate place to report such things, but it is the best I have been able to find.)
>
>While experimenting with scope guard statements, I came across some unexpected behavior.  This snippet:
>
>{
> if( true ){ scope(exit) writef("hello"); }
> writefln(" world" );
>}
>
>Produces the output "hello world", as I expected.  However, if I remove the if statement's braces, like so:
>
>{
> 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.
>
>I'm using the dmd compiler v2.010 on Linux.

Looks like a bug but makes no sense in a real application anyway?

if( true ) scope(exit) writef("hello");
is semantically the same as simply
if( true ) writef("hello");

Do you have any use case for that?

March 06, 2008
On 06/03/2008, Max Samukha <samukha@voliacable.com.removethis> wrote:
>  if( true ) scope(exit) writef("hello");
>
> is semantically the same as simply
>  if( true ) writef("hello");

For that matter,

    if (true) anything

is the same as

    anything

But I assume "if (condition)" what what was really intended. It seems to me that one could instead write:

    scope(exit) { if (condition) writef("hello") }

That is, reverse the order of "scope" and "if".
March 06, 2008
On Thu, 6 Mar 2008 10:47:19 +0000, "Janice Caron" <caron800@googlemail.com> wrote:

>On 06/03/2008, Max Samukha <samukha@voliacable.com.removethis> wrote:
>>  if( true ) scope(exit) writef("hello");
>>
>> is semantically the same as simply
>>  if( true ) writef("hello");
>
>For that matter,
>
>    if (true) anything
>
>is the same as
>
>    anything
>

scope is redundant in your example because it is the only statement within if's scope. It executes a block of statements when the thread exits if's scope. you could as well omit it.

if (condition)
{
    scope(exit)
   {
		...
   }
}

or without braces (according to you, doesn't work as expected)

if (condition)
    scope(exit)
    {
		...
    }

should be equivalent to

if (condition)
{
	...
}

In case of static if, which doesn't create a new scope:

{
    static if (condition)
       scope (exit)
            writef("hello");

} // hello should be printed when control gets here (not tested)

>But I assume "if (condition)" what what was really intended. It seems to me that one could instead write:
>
>    scope(exit) { if (condition) writef("hello") }
>
>That is, reverse the order of "scope" and "if".

{
    bool condition = ...

    ...

    scope(exit)
    {
        if (condition) writef("hello");
    }

  ...
} // hello is printed at this point if condition is true
  // and the function didn't return or throw before
  // the 'scope'

March 06, 2008
On Thu, 06 Mar 2008 15:37:07 +0200, Max Samukha <samukha@voliacable.com.removethis> wrote:

>On Thu, 6 Mar 2008 10:47:19 +0000, "Janice Caron" <caron800@googlemail.com> wrote:
>
>>On 06/03/2008, Max Samukha <samukha@voliacable.com.removethis> wrote:
>>>  if( true ) scope(exit) writef("hello");
>>>
>>> is semantically the same as simply
>>>  if( true ) writef("hello");
>>
>>For that matter,
>>
>>    if (true) anything
>>
>>is the same as
>>
>>    anything
>>
>
>scope is redundant in your example because it is the only statement within if's scope. It executes a block of statements when the thread exits if's scope. you could as well omit it.
>
>if (condition)
>{
>    scope(exit)
>   {
>		...
>   }
>}
>
>or without braces (according to you, doesn't work as expected)
>
>if (condition)
>    scope(exit)
>    {
>		...
>    }
>
>should be equivalent to
>
>if (condition)
>{
>	...
>}
>
>In case of static if, which doesn't create a new scope:
>
>{
>    static if (condition)
>       scope (exit)
>            writef("hello");
>
>} // hello should be printed when control gets here (not tested)
>
>>But I assume "if (condition)" what what was really intended. It seems to me that one could instead write:
>>
>>    scope(exit) { if (condition) writef("hello") }
>>
>>That is, reverse the order of "scope" and "if".
>
>{
>    bool condition = ...
>
>    ...
>
>    scope(exit)
>    {
>        if (condition) writef("hello");
>    }
>
>  ...
>} // hello is printed at this point if condition is true
>  // and the function didn't return or throw before
>  // the 'scope'

Sorry, Janice, somehow my brain mistook you for Steve.
Please ignore

March 06, 2008
Max Samukha Wrote:
> Do you have any use case for that?

No.  I encountered this while experimenting with the behavior of scope(), not while building any particular application.  I agree that my example is a corner case.  But I thought it best to raise the issue in case it reflects some more subtle problem in the compiler.

I appreciate the prompt responses!
March 07, 2008
Max Samukha wrote:
> On Wed, 05 Mar 2008 16:26:27 -0500, Steve Jackson
> <sjackso@cs.wisc.edu> wrote:
> 
>> (This is a report of what I believe is a "new" bug in the D compiler.  I'm not sure whether this newsgroup is the appropriate place to report such things, but it is the best I have been able to find.)
>>
>> While experimenting with scope guard statements, I came across some unexpected behavior.  This snippet:
>>
>> {
>> if( true ){ scope(exit) writef("hello"); }
>> writefln(" world" );
>> }
>>
>> Produces the output "hello world", as I expected.  However, if I remove the if statement's braces, like so:
>>
>> {
>> 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.
>>
>> I'm using the dmd compiler v2.010 on Linux.
> 
> Looks like a bug but makes no sense in a real application anyway?
> 
> if( true ) scope(exit) writef("hello");
> is semantically the same as simply
> if( true ) writef("hello");
> 
> Do you have any use case for that?

Compile-time values could evaluate to true, and compile-time code generation could very easily create code that would seem silly to write directly. So it's a valid problem.
« First   ‹ Prev
1 2