473,320 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

compiler warnings for unconditional recursive calls

Why doesn't the compiler give me any warning if it encounters code like
the following:

void foo()
{
// some code here
// foo() ist not nested in a pre-condition loop, some if- or
else- // block and no return statement appears before it.
foo();
}

The compiler should recognize something like that, shouldn't it?
Jun 7 '07 #1
5 1605
cody <de********@gmx.dewrote:
Why doesn't the compiler give me any warning if it encounters code like
the following:

void foo()
{
// some code here
// foo() ist not nested in a pre-condition loop, some if- or
else- // block and no return statement appears before it.
foo();
}

The compiler should recognize something like that, shouldn't it?
Well, there's nothing in the C# specification to say that it should.
Your criteria wouldn't be enough anyway - what about performing *any*
operation which could result in an exception (beyond running out of
memory or stack)?

I was slightly surprised to see the other day that this is valid too:

public Test()
: this("hello")
{
}

public Test(string x)
: this()
{
}

I believe that's forbidden in Java (I should check it, but I'm too
tired) but that recursive unconditional calls aren't.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 7 '07 #2
Hey Jon, you are lightning fast as always :)
comments inline!

Jon Skeet [C# MVP] wrote:
cody <de********@gmx.dewrote:
>Why doesn't the compiler give me any warning if it encounters code like
the following:

void foo()
{
// some code here
// foo() ist not nested in a pre-condition loop, some if- or
else- // block and no return statement appears before it.
foo();
}

The compiler should recognize something like that, shouldn't it?

Well, there's nothing in the C# specification to say that it should.
Your criteria wouldn't be enough anyway - what about performing *any*
operation which could result in an exception (beyond running out of
memory or stack)?
You are right, an exception could terminate the thing, but I wouldn't
consider it good style, so a warning wouldn't do any harm (if somebody
really wants he can disable this warning for this specific method).

I was slightly surprised to see the other day that this is valid too:

public Test()
: this("hello")
{
}

public Test(string x)
: this()
{
}

I believe that's forbidden in Java (I should check it, but I'm too
tired) but that recursive unconditional calls aren't.
This is very funny. Why is it allowed? This way your class is better
protected from instantiation then by declaring its ctors private :)

Even throwing an Exception from within the ctors doesn't help here
because that code is never reached..
Even if the throw() would be executed before the constructor chaining
then the Exception would also prevent instantiation.

So theoretically the Compiler should warn us:
- That the class can never be instantiated
- The be have unconditional recursion
- That we have unreachable code (when constructors are not empty)
But in reality: Not a single warning.

So you can do nothing with it, besides calling non-void static methods
in the this() call:

public Test()
: this(StaticFunction("hello"))
{
}

Very interesting. If Anders Heijlson and his team thought about this
case while developing the Language?
Jun 7 '07 #3
cody <de********@gmx.dewrote:
Well, there's nothing in the C# specification to say that it should.
Your criteria wouldn't be enough anyway - what about performing *any*
operation which could result in an exception (beyond running out of
memory or stack)?

You are right, an exception could terminate the thing, but I wouldn't
consider it good style, so a warning wouldn't do any harm (if somebody
really wants he can disable this warning for this specific method).
Hmm... I don't like that kind of warning. I very rarely need to do
anything other than fix my code in order to avoid warnings.

The larger issue, IMO, is the extra specification required in order to
determine what the compiler should do. Just as with the case of
definite assignment, there are bound to be quite a lot of corner cases
to consider...

<constructor example>
This is very funny. Why is it allowed? This way your class is better
protected from instantiation then by declaring its ctors private :)

Even throwing an Exception from within the ctors doesn't help here
because that code is never reached..
Even if the throw() would be executed before the constructor chaining
then the Exception would also prevent instantiation.
Indeed. That one's very nasty.
So theoretically the Compiler should warn us:
- That the class can never be instantiated
- The be have unconditional recursion
- That we have unreachable code (when constructors are not empty)
But in reality: Not a single warning.

So you can do nothing with it, besides calling non-void static methods
in the this() call:

public Test()
: this(StaticFunction("hello"))
{
}

Very interesting. If Anders Heijlson and his team thought about this
case while developing the Language?
Not sure - I'll try to find out though. Fortunately it's such a
fundamental problem that if your unit tests *ever* call any of the
cyclical constructors, you're bound to find it very quickly. And yes,
that's exactly how I found mine :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 8 '07 #4
On Jun 7, 3:52 pm, cody <deutron...@gmx.dewrote:
Why doesn't the compiler give me any warning if it encounters code like
the following:

void foo()
{
// some code here
// foo() ist not nested in a pre-condition loop, some if- or
else- // block and no return statement appears before it.
foo();

}

The compiler should recognize something like that, shouldn't it?
Why? It's perfectly cromulant code. Useless, but not in any way
syntactically or grammatically incorrect.

Jun 8 '07 #5

"cody" <de********@gmx.dewrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Why doesn't the compiler give me any warning if it encounters code like
the following:

void foo()
{
// some code here
// foo() ist not nested in a pre-condition loop, some if- or else-
// block and no return statement appears before it.
foo();
}

The compiler should recognize something like that, shouldn't it?
Why? The general case of determining whether code terminates is inhaously
hard (the "halting problem"), and the few specific cases a compiler might be
able to detect are such a timy minority it doesn't seem worth it. If you
really do need to be sure that a method terminates consider implementing a
loop variant of some sort (they're already in Spec#:
http://channel9.msdn.com/wiki/defaul....LoopVariants). If you
really /really/ need to be sure that a method terminates, you probably
shouldn't be using C# but rather something formal like SPARK.
(http://www.praxis-his.com/sparkada/).
Jun 9 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Mark Dufour | last post by:
After nine months of hard work, I am proud to introduce my baby to the world: an experimental Python-to-C++ compiler. It can convert many Python programs into optimized C++ code, without any user...
8
by: Charlie Zender | last post by:
Hi, First, this may be a GCC or Linux-specific problem, I'm not sure. I am unable to compile a large body of code with extremely pedantic compile time checks activate, so that warnings cause...
5
by: Sean Wolfe | last post by:
I have a request for the c# compiler one that is an obvious oversight in my opinion. I'm not sure if this is already being implemented in Whidebey, bu i would hope to find it in there, or in the...
5
by: rawCoder | last post by:
Hi All, In Visual Basic .NET , your function definition might requirre you to return a value but (accidently/intentionally) you dont put any 'return value' in the function. In this case VB...
9
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script...
0
by: erik.erikson | last post by:
I am getting a compiler error that I can't well explain or even understand the origin of (though I boiled it down close...). Below is a bare-bones example. What I am doing is defining the...
3
by: Rene | last post by:
Hello to all! For a long time I have been "fighting" a problem compiling an OpenGL program which uses GLUT. First I have put a question in a Watcom group (I want to use this compiler) to which I...
3
by: gil | last post by:
Hi, I'm trying to find the best way to work with compiler warnings. I'd like to remove *all* warnings from the code, and playing around with the warning level, I've noticed that compiling with...
51
by: jacob navia | last post by:
After a user asked for clarification about some warnings issued by lcc-win, I have updated the compiler to reflect this discussion. 1) The buggy warning about long l; printf("%li", l * 10L);...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.