Bug 316107 (MONO66777) - [PATCH] wrong CS0642 warning
Summary: [PATCH] wrong CS0642 warning
Status: RESOLVED FIXED
Alias: MONO66777
Product: Mono: Compilers
Classification: Mono
Component: C# (show other bugs)
Version: unspecified
Hardware: Other Other
: P3 - Medium : Normal
Target Milestone: ---
Assignee: Miguel de Icaza
QA Contact: Mono Bugs
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-09-24 12:57 UTC by Marek Safar
Modified: 2007-09-15 21:24 UTC (History)
1 user (show)

See Also:
Found By: ---
Services Priority:
Business Priority:
Blocker: ---
Marketing QA Status: ---
IT Deployment: ---


Attachments
proposed fix (4.48 KB, patch)
2005-08-24 16:13 UTC, Thomas Wiest
Details | Diff
revised. The previous one didn't fix cs0642-2.cs (4.60 KB, patch)
2005-08-24 21:28 UTC, Thomas Wiest
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Wiest 2007-09-15 18:54:43 UTC


---- Reported by marek.safar@seznam.cz 2004-09-24 05:57:21 MST ----

Please fill in this template when reporting a bug, unless you know what you
are doing.
Description of Problem:


Steps to reproduce the problem:
1. compile with /warn:4

public class C
{
    public static void Main ()
    {
        int p = 0;
        while (p++ > 100);
        #if A
        #endif
    }
}

Actual Results:

5.cs(6) warning CS0642: Possible mistaken empty statement


Expected Results:

no warning


How often does this happen? 


Additional Information:



---- Additional Comments From miguel@ximian.com 2004-09-28 00:29:31 MST ----

Strange, this seems to be a genuine error.

Why should we not report this as an error?



---- Additional Comments From marek.safar@seznam.cz 2004-09-28 05:50:37 MST ----

Because it conforms to specification.

while-statement:
while   (   boolean-expression   )   embedded-statement

And ; is empty statement (sort of embedded-statement)

This warning should be reported only when after ';' follows { } block.
See errors/cs0642-2.cs




---- Additional Comments From marek.safar@seznam.cz 2004-11-23 03:45:33 MST ----

Miguel, any progress ?



---- Additional Comments From miguel@ximian.com 2004-11-23 13:20:51 MST ----

I am confused Marek.

The ';' after the while is a common error in software:

while (condition);
      statement ();

The situation arises when someone mistakenly leaves the trailing `;'
after the while condition, and *visually* the code looks fine, but
in reality `statement()' will be executed *always*.





---- Additional Comments From marek.safar@seznam.cz 2004-11-24 12:26:54 MST ----

Miguel, look at this

while (condition); { statement (); }

this one and only case where we should report warning for "while".
Simply ';' is most likely dirty in this code.

but we report this warning also for your code which is wrong.




---- Additional Comments From miguel@ximian.com 2004-11-24 14:57:58 MST ----

Hello Marek,

     Although technically:

     while (xxx) 
        ;

     Is valid code (and the indentation shows that the intent of the
while loop is on the xxxx itself) the problem is that it is common
to write code like this:

     while (xxxx);
         something;

     Which is what the warning catches.  This matches exactly the
behavior of C and C++ compilers that warn about an empty statement.





---- Additional Comments From marek.safar@seznam.cz 2004-11-25 07:31:19 MST ----

Hey Miguel,

It is easy. Your snippets are correct and the bug is that mcs reports
CS0642 warning for this code too. We should report warning for "while"
case only when someone wrote code like this

using System;

class X {
	static void Test (int arg)
	{
        while (--arg > 0);
            { Console.WriteLine ("foo"); }
	}
}




---- Additional Comments From atsushi@ximian.com 2005-08-24 09:13:25 MST ----

Created an attachment (id=166829)
proposed fix




---- Additional Comments From miguel@ximian.com 2005-08-24 13:18:31 MST ----

Ok, lets get this patch in.



---- Additional Comments From atsushi@ximian.com 2005-08-24 14:28:32 MST ----

Created an attachment (id=166830)
revised. The previous one didn't fix cs0642-2.cs




---- Additional Comments From atsushi@ximian.com 2005-08-24 14:30:41 MST ----

I made a tiny change and commiting the revised patch. Here is the
reason of the change (from statement.cs):

	// Some statements are wrapped by a Block. Since
	// others' internal could be changed, here I treat
	// them as possibly wrapped by Block equally.

Lock in cs0642-6.cs and While in cs0642-2.cs showed such "non-wrapped"
cases, while others are wrapped by a Block.




---- Additional Comments From atsushi@ximian.com 2005-08-24 14:43:38 MST ----

fixed as r48805.

Imported an attachment (id=166829)
Imported an attachment (id=166830)

Unknown operating system unknown. Setting to default OS "Other".