www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - bug? for(int i=0;i<1;) vs while(true)

reply ddos <oggs gmx.at> writes:
http://pastebin.com/fknwgjtz

i tried to call fibers in a loop forever, to multiplex some 
networking client worker fibers and a listener fiber
it seems to work correctly with  for(int i=0;i<1;)

with while(true) i get:

C:\dev\server_client>dub
Building server_client ~master configuration "application", build 
type debug.
Compiling using dmd...
source\app.d(72): Warning: statement is not reachable
FAIL 
.dub\build\application-debug-windows-x86-dmd_2068-32A80D3C074EAD350DDE74DB2
61C6BB5\ server_client executable
Error executing command run:
dmd failed with exit code 1.
Sep 17 2015
next sibling parent ddos <oggs gmx.at> writes:
using DMD32 D Compiler v2.068.0 on windows x64
Sep 17 2015
prev sibling next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 17 September 2015 at 19:32:16 UTC, ddos wrote:
 source\app.d(72): Warning: statement is not reachable
What's there? Anything after an endless loop is potentially unreachable and dub treats warnings as errors. With the for loop, the compiler can't be as sure that it is endless because plain for loops often have their variable changed inside and the compiler just isn't smart enough to actually check for that too.
Sep 17 2015
parent ddos <oggs gmx.at> writes:
On Thursday, 17 September 2015 at 19:35:05 UTC, Adam D. Ruppe 
wrote:
 What's there? Anything after an endless loop is potentially 
 unreachable and dub treats warnings as errors.
i see, thx
Sep 17 2015
prev sibling parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Thu, Sep 17, 2015 at 07:32:13PM +0000, ddos via Digitalmars-d-learn wrote:
 http://pastebin.com/fknwgjtz
 
 i tried to call fibers in a loop forever, to multiplex some networking
 client worker fibers and a listener fiber
 it seems to work correctly with  for(int i=0;i<1;)
 
 with while(true) i get:
 
 C:\dev\server_client>dub
 Building server_client ~master configuration "application", build type
 debug.
 Compiling using dmd...
 source\app.d(72): Warning: statement is not reachable
 FAIL
 .dub\build\application-debug-windows-x86-dmd_2068-32A80D3C074EAD350DDE74DB2
 61C6BB5\ server_client executable
 Error executing command run:
 dmd failed with exit code 1.
Maybe just write: for (;;) { ... } instead? You can read `(;;)` as "ever". :-P Also, could you post a (possibly reduced) code example that can be compiled? It's kinda hard to figure out what's wrong when the code in the paste is incomplete. T -- Любишь кататься - люби и саночки возить.
Sep 17 2015
parent reply ddos <oggs gmx.at> writes:
On Thursday, 17 September 2015 at 19:43:02 UTC, H. S. Teoh wrote:
 On Thu, Sep 17, 2015 at 07:32:13PM +0000, ddos via 
 Digitalmars-d-learn wrote:
 http://pastebin.com/fknwgjtz
 
 i tried to call fibers in a loop forever, to multiplex some 
 networking
 client worker fibers and a listener fiber
 it seems to work correctly with  for(int i=0;i<1;)
 
 with while(true) i get:
 
 C:\dev\server_client>dub
 Building server_client ~master configuration "application", 
 build type
 debug.
 Compiling using dmd...
 source\app.d(72): Warning: statement is not reachable
 FAIL
 .dub\build\application-debug-windows-x86-dmd_2068-32A80D3C074EAD350DDE74DB2
 61C6BB5\ server_client executable
 Error executing command run:
 dmd failed with exit code 1.
Maybe just write: for (;;) { ... } instead? You can read `(;;)` as "ever". :-P Also, could you post a (possibly reduced) code example that can be compiled? It's kinda hard to figure out what's wrong when the code in the paste is incomplete. T
yeah i tried for(;;) and it generates the same warning :) sure, here is the full example, it's not too long anyways ( the example doesn't make much sense tho because socket.accept is blocking :P ) http://pastebin.com/9K0wRRD6 ps: pastebin needs D support :-D
Sep 17 2015
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 17 September 2015 at 19:47:15 UTC, ddos wrote:
 yeah i tried for(;;) and it generates the same warning :)
 sure, here is the full example, it's not too long anyways
 ( the example doesn't make much sense tho because socket.accept 
 is blocking :P )
 http://pastebin.com/9K0wRRD6
Yeah, it is the return 0 at the bottom. You can actually just remove that and make main return void instead. (That's perfectly legal in D, different than C)
Sep 17 2015
parent reply ddos <oggs gmx.at> writes:
thank you :) works now
Sep 18 2015
parent Kagamin <spam here.lot> writes:
This compiles with enabled warnings:
---
int f()
{
	while(true){}
	assert(false);
}
---
Sep 18 2015
prev sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
On 09/17/2015 09:47 PM, ddos wrote:
 yeah i tried for(;;) and it generates the same warning :)
 sure, here is the full example, it's not too long anyways
 ( the example doesn't make much sense tho because socket.accept is
 blocking :P )
 http://pastebin.com/9K0wRRD6

 ps: pastebin needs D support :-D
Remove the return statement from main and the warning will go away. (Note: unless you want to influence the return code, you can actually declare main as returning void, then you don't need it even if the end of the function is reachable.)
Sep 17 2015