digitalmars.D.bugs - Missed scope guard statements
- Steve Jackson (13/13) Mar 05 2008 (This is a report of what I believe is a "new" bug in the D compiler. I...
- Robert Fraser (10/29) Mar 05 2008 Hi Steve,
- Simen Kjaeraas (6/15) Mar 05 2008 Actually, "hello world" is indeed correct output, as the scope(exit) is ...
- Gide Nwawudu (5/18) Mar 06 2008 Looks similar to this bug.
- Max Samukha (8/21) Mar 06 2008 Looks like a bug but makes no sense in a real application anyway?
- Janice Caron (9/12) Mar 06 2008 For that matter,
- Max Samukha (42/55) Mar 06 2008 scope is redundant in your example because it is the only statement
- Max Samukha (5/67) Mar 06 2008 Sorry, Janice, somehow my brain mistook you for Steve.
- Steve Jackson (3/4) Mar 06 2008 No. I encountered this while experimenting with the behavior of scope()...
- Robert Fraser (4/34) Mar 06 2008 Compile-time values could evaluate to true, and compile-time code
- Max Samukha (20/24) Mar 07 2008 You are right, this is a problem.
(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.
Mar 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.
Mar 05 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
Mar 05 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
Mar 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?
Mar 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".
Mar 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: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)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 anythingBut 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'
Mar 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:Sorry, Janice, somehow my brain mistook you for Steve. Please ignoreOn 06/03/2008, Max Samukha <samukha voliacable.com.removethis> wrote: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)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 anythingBut 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'
Mar 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!
Mar 06 2008
Max Samukha wrote:On Wed, 05 Mar 2008 16:26:27 -0500, Steve Jackson <sjackso cs.wisc.edu> wrote: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.(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?
Mar 06 2008
Robert Fraser wrote: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.You are right, this is a problem. Interestingly, mixing in the ThenStatement works fine: ---- if (true) mixin ("scope(exit) Stdout(\"hello \").newline;"); // prints hello as expected. ---- I guess, this is because a string mixin creates a new scope, which is a bug (or a feature? The specification is not specific on that): ---- mixin ("scope(exit) Stdout(\"Exiting scope.\").newline;"); Stdout("After mixin").newline; ---- outputs: Exiting mixin scope. After mixin while I would expect the reverse.
Mar 07 2008