[Mono-bugs] [Bug 49904][Nor] New - Error CS0136 for compiling program -- scope issue
bugzilla-daemon at bugzilla.ximian.com
bugzilla-daemon at bugzilla.ximian.com
Wed Jan 10 04:08:52 EST 2007
Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.
Changed by cristian.adam at gmx.net.
http://bugzilla.ximian.com/show_bug.cgi?id=49904
--- shadow/49904 2007-01-10 04:08:52.000000000 -0500
+++ shadow/49904.tmp.24762 2007-01-10 04:08:52.000000000 -0500
@@ -0,0 +1,130 @@
+Bug#: 49904
+Product: Mono: Compilers
+Version: unspecified
+OS: unknown
+OS Details: debian gnu/linux woody 2.4.18
+Status: RESOLVED
+Resolution: NOTABUG
+Severity: Unknown
+Priority: Normal
+Component: C#
+AssignedTo: mono-bugs at ximian.com
+ReportedBy: baskmau at hotmail.com
+QAContact: mono-bugs at ximian.com
+TargetMilestone: ---
+URL:
+Cc:
+Summary: Error CS0136 for compiling program -- scope issue
+
+Description of Problem:
+
+Scopes seem to work differently in Java and C# per mcs and javac -- not
+sure if this is the case:
+
+Compiling C# program with 0.26 mcs gives:
+
+class Scope
+{
+ bool condition;
+
+ Scope()
+ {
+ condition = true;
+
+ if ( condition )
+ {
+ int inner = 0;
+ Console.WriteLine( " .. inside if block .. " );
+ }
+
+ int inner = 0;
+ Console.WriteLine( " .. top level scope .. " );
+ }
+
+ public static void Main()
+ {
+ Scope test = new Scope();
+ Console.WriteLine( " .. fini scope test .. " );
+ }
+}
+
+gives the following error message:
+
+Scope.cs(15) error CS0136: A local variable named `inner' cannot be
+declared in this scope since it would give a different meaning to `inner',
+which is already used in a `child' scope to denote something else
+Compilation failed: 1 error(s), 0 warnings
+
+Expected Results:
+Additional Information:
+
+The corresponding Java program compiles and runs:
+class Scope
+{
+ boolean condition;
+
+ Scope()
+ {
+ condition = true;
+
+ if ( condition )
+ {
+ int inner = 0;
+ System.out.println( " .. inside if block .. " );
+ }
+
+ int inner = 0;
+ System.out.println( " .. top level scope .. " );
+ }
+
+ public static void main( String[] args )
+ {
+ Scope test = new Scope();
+ System.out.println( " .. fini scope test .. " );
+ }
+}
+~/j2sdk142/bin/java Scope
+ .. inside if block ..
+ .. top level scope ..
+ .. fini scope test ..
+
+I thought scopes would work similarly in this case:
+
+------- Additional Comments From bmaurer at users.sf.net 2003-11-18 12:36 -------
+Please see the spec, section 3.7:
+
+"The scope of a local variable declared in a local-variable-
+declaration (Section 8.5.1) is the block in which the declaration
+occurs."
+
+http://msdn.microsoft.com/library/default.asp?url=/library/en-
+us/csspec/html/vclrfcsharpspec_3_9.asp
+
+------- Additional Comments From cristian.adam at gmx.net 2007-01-10 04:08 -------
+It's a bug!
+Compiler Error CS0136A local variable named 'var' cannot be declared
+in this scope because it would give a different meaning to 'var',
+which is already used in a 'parent or current' scope to denote
+something else
+A variable declaration hides another declaration that would otherwise
+be in scope. Rename the variable that is declared on the line that
+generated CS0136.
+The following sample generates CS0136:
+// CS0136.cs
+namespace x
+{
+ public class a
+ {
+ public static void Main()
+ {
+ int i = 0;
+ {
+ char i = 'a'; // CS0136, hides int i
+ }
+ i++;
+ }
+ }
+}
+
+This is not the case, inner was in a closed block (the if block) when
+the second inner appeared.
More information about the mono-bugs
mailing list