Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 05, 2008 Missed scope guard statements | ||||
---|---|---|---|---|
| ||||
(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 Re: Missed scope guard statements | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steve Jackson | 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 Re: Missed scope guard statements | ||||
---|---|---|---|---|
| ||||
Posted in reply to Robert Fraser | 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 Re: Missed scope guard statements | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steve Jackson | 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 Re: Missed scope guard statements | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steve Jackson | 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 Re: Missed scope guard statements | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samukha | 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 Re: Missed scope guard statements | ||||
---|---|---|---|---|
| ||||
Posted in reply to Janice Caron | 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 Re: Missed scope guard statements | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samukha | 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 Re: Missed scope guard statements | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samukha | 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 Re: Missed scope guard statements | ||||
---|---|---|---|---|
| ||||
Posted in reply to Max Samukha | 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.
|
Copyright © 1999-2021 by the D Language Foundation