In mcs:
authorRaja R Harinath <harinath@hurrynot.org>
Tue, 29 Mar 2005 12:27:05 +0000 (12:27 -0000)
committerRaja R Harinath <harinath@hurrynot.org>
Tue, 29 Mar 2005 12:27:05 +0000 (12:27 -0000)
Fix #47991.  Remove a TODO.
* statement.cs (Block.Toplevel): Make into a field.
(Block.Parameters): Move into ToplevelBlock.
(Block.known_variables): Rename from child_variable_names.
(Block.Block): Remove variants that take Parameters.  Initialize
'Toplevel' with the immediately surrounding toplevel block.
(Block.AddKnownVariable): Rename from AddChildVariableName.  Add a
LocalInfo parameter.
(Block.GetKnownVariableInfo): New.
(Block.IsVariableNameUsedInChildBlock): Update.
(Block.IsVariableNameUsedInBlock): New.  Checks if a name is used in
the block, even though it may not be in scope.
(Block.AddVariable): Remove Parameters parameter.  Use
Toplevel.Parameters instead.
(Block.AddConstant): Remove Parameters parameter.
(Block.GetParameterReference): Update to use Toplevel.Parameters.
(Block.IsParamaterReference): Likewise.
(Block.IsLocalParameter): Likewise.  Simplify a lot.
(ToplevelBlock.Parameters): New.  Moved from Block.
(ToplevelBlock.ToplevelBlock): Update to changes.  Always
initialize Parameters to a non-null value.
* cs-parser.jay: Update to changes.
* ecore.cs (SimpleName.SimpleNameResolve): Emit cs0136 error for
simple names that mean different things in the same block.  Use
Block.IsVariableNameUsedInBlock.

In errors:
* cs0136-3.cs, cs0136-4.cs: New tests from #47991.

svn path=/trunk/mcs/; revision=42330

mcs/errors/ChangeLog
mcs/errors/cs0136-3.cs [new file with mode: 0644]
mcs/errors/cs0136-4.cs [new file with mode: 0644]
mcs/errors/errors.txt
mcs/errors/gmcs-expect-no-error
mcs/mcs/ChangeLog
mcs/mcs/cs-parser.jay
mcs/mcs/ecore.cs
mcs/mcs/statement.cs

index c38d6c3eb06867998bba5e08c77f26bd06da9095..6d753ae21fad4f3b3618260fa5e92249f66fe691 100644 (file)
@@ -1,3 +1,7 @@
+2005-03-29  Raja R Harinath  <rharinath@novell.com>
+
+       * cs0136-3.cs, cs0136-4.cs: New tests from #47991.
+
 2005-03-18  Raja R Harinath  <rharinath@novell.com>
 
        * Makefile (DISTFILES): Don't distribute TestRunner.exe.
diff --git a/mcs/errors/cs0136-3.cs b/mcs/errors/cs0136-3.cs
new file mode 100644 (file)
index 0000000..3fa3b0a
--- /dev/null
@@ -0,0 +1,11 @@
+// cs0136: 'y' has a different meaning later in the block
+// Line: 8
+
+class X
+{
+   static void y () { }
+   static void Main () {
+     y ();
+     int y = 5;
+   }
+}
diff --git a/mcs/errors/cs0136-4.cs b/mcs/errors/cs0136-4.cs
new file mode 100644 (file)
index 0000000..4821938
--- /dev/null
@@ -0,0 +1,11 @@
+// cs0136: 'y' has a different meaning later in the block
+// Line: 8
+
+class X
+{
+       static int y;
+       static void Main () {
+               y = 10;
+               int y = 5;
+       }
+}
index 2fc96244fd890fe68dcd593dcb04e1258694d6f0..73be6768a8b13d6887025b8223619e9dbab8d586 100644 (file)
@@ -81,6 +81,11 @@ numbers, as they look nicer on the debugging output.
 
 -27    Attribute resolved to two different types
 
+-28    (warning) Somehow there's a function marked override, and
+       doesn't override anything.  Warn that this method will be
+       ignored during overload resolution.  (marked override == in IL,
+       it has a 'virtual', but no 'newslot')
+
 ----------
 
 Errors that we have allocated that will have corresponding errors in
index 0578a849a2b0244a8363624121df3db67218e5d9..6b7888369f8cb84d15327b1b51d6662ad6738059 100644 (file)
@@ -12,6 +12,8 @@ cs0035.cs
 cs0108-3.cs
 cs0121-3.cs
 cs0121-4.cs
+cs0136-3.cs
+cs0136-4.cs
 cs0157-5.cs
 cs0161-2.cs # new in GMCS
 cs0164.cs
index 7136c4c39b4a0a9ddb85ca635444ea1815402d37..4aa7c109f0669727af8b2a1d127653acd066054f 100644 (file)
@@ -1,3 +1,31 @@
+2005-03-29  Raja R Harinath  <rharinath@novell.com>
+
+       Fix #47991.  Remove a TODO.
+       * statement.cs (Block.Toplevel): Make into a field.
+       (Block.Parameters): Move into ToplevelBlock.
+       (Block.known_variables): Rename from child_variable_names.
+       (Block.Block): Remove variants that take Parameters.  Initialize
+       'Toplevel' with the immediately surrounding toplevel block.
+       (Block.AddKnownVariable): Rename from AddChildVariableName.  Add a
+       LocalInfo parameter.
+       (Block.GetKnownVariableInfo): New.
+       (Block.IsVariableNameUsedInChildBlock): Update.
+       (Block.IsVariableNameUsedInBlock): New.  Checks if a name is used in
+       the block, even though it may not be in scope.
+       (Block.AddVariable): Remove Parameters parameter.  Use
+       Toplevel.Parameters instead.
+       (Block.AddConstant): Remove Parameters parameter.
+       (Block.GetParameterReference): Update to use Toplevel.Parameters.
+       (Block.IsParamaterReference): Likewise.
+       (Block.IsLocalParameter): Likewise.  Simplify a lot.
+       (ToplevelBlock.Parameters): New.  Moved from Block.
+       (ToplevelBlock.ToplevelBlock): Update to changes.  Always
+       initialize Parameters to a non-null value.
+       * cs-parser.jay: Update to changes.
+       * ecore.cs (SimpleName.SimpleNameResolve): Emit cs0136 error for
+       simple names that mean different things in the same block.  Use
+       Block.IsVariableNameUsedInBlock.
+
 2005-03-28  Raja R Harinath  <rharinath@novell.com>
 
        * typemanager.cs (TypeHandle.BaseType): Make into an IMemberContainer.
index e8fb49807afa480de3cdfb62e7501b60dfdf9ec1..daa6947b0a042c51fc77f4bad786add985dab133 100644 (file)
@@ -3515,8 +3515,7 @@ block
                        current_block = new ToplevelBlock ((ToplevelBlock) top_current_block, current_local_parameters, lexer.Location);
                        top_current_block = current_block;
                } else {
-               current_block = new Block (current_block, current_local_parameters,
-                                          lexer.Location, Location.Null);
+               current_block = new Block (current_block, lexer.Location, Location.Null);
                }
          } 
          opt_statement_list CLOSE_BRACE 
@@ -3928,8 +3927,7 @@ for_statement
 
                                LocalInfo vi;
 
-                               vi = current_block.AddVariable (
-                                       type, decl.identifier, current_local_parameters, decl.Location);
+                               vi = current_block.AddVariable (type, decl.identifier, decl.Location);
                                if (vi == null)
                                        continue;
 
@@ -4039,7 +4037,7 @@ foreach_statement
                Location l = lexer.Location;
                LocalInfo vi;
 
-               vi = foreach_block.AddVariable ((Expression) $3, (string) $4, current_local_parameters, l);
+               vi = foreach_block.AddVariable ((Expression) $3, (string) $4, l);
                if (vi != null) {
                        vi.ReadOnly = true;
 
@@ -4341,7 +4339,7 @@ fixed_statement
                        Pair p = (Pair) list [i];
                        LocalInfo v;
 
-                       v = current_block.AddVariable (type, (string) p.First,current_local_parameters, l);
+                       v = current_block.AddVariable (type, (string) p.First, l);
                        if (v == null)
                                continue;
 
@@ -4426,9 +4424,7 @@ using_statement
 
                        foreach (VariableDeclaration decl in var_declarators){
 
-                               LocalInfo vi    = current_block.AddVariable (
-                                       type, decl.identifier, 
-                                       current_local_parameters, decl.Location);
+                               LocalInfo vi = current_block.AddVariable (type, decl.identifier, decl.Location);
                                if (vi == null)
                                        continue;
                                vi.ReadOnly = true;
@@ -4668,7 +4664,7 @@ Block declare_local_variables (Expression type, ArrayList variable_declarators,
 
        foreach (VariableDeclaration decl in variable_declarators){
 
-               if (implicit_block.AddVariable (type, decl.identifier, current_local_parameters, decl.Location) != null) {
+               if (implicit_block.AddVariable (type, decl.identifier, decl.Location) != null) {
                        if (decl.expression_or_array_initializer != null){
                                if (inits == null)
                                        inits = new ArrayList (4);
@@ -4714,8 +4710,7 @@ Block declare_local_constants (Expression type, ArrayList declarators)
                implicit_block = current_block;
 
        foreach (VariableDeclaration decl in declarators){
-               implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer,
-                                                 current_local_parameters, decl.Location);
+               implicit_block.AddConstant (type, decl.identifier, (Expression) decl.expression_or_array_initializer, decl.Location);
        }
        
        return implicit_block;
index 39c9bbcf8762e7839654cf681a36c0f361896b79..e9844856f0ac53c114966957d52991ee0a60e2eb 100644 (file)
@@ -2079,6 +2079,15 @@ namespace Mono.CSharp {
                                                      "'{0}' has a different meaning in a child block", Name);
                                        return null;
                                }
+
+                               if (!(e is LocalVariableReference) && current_block.IsVariableNameUsedInBlock (Name)) {
+                                       // Catch some false positives, e.g., when a local variable resolves to a constant.
+                                       LocalInfo vi = current_block.GetLocalInfo (Name);
+                                       if (vi == null) {
+                                               Report.Error (136, Location, "'{0}' has a different meaning later in the block", Name);
+                                               return null;
+                                       }
+                               }
                        }
 
                        return e;
index 421abe5cd86231b63abb0fc18fb0016c21c5669e..16c0b5b08fc6808eb6db915b32f5f94b4eb0939d 100644 (file)
@@ -1170,6 +1170,8 @@ namespace Mono.CSharp {
                public readonly Location  StartLocation;
                public Location EndLocation = Location.Null;
 
+               public readonly ToplevelBlock Toplevel;
+
                [Flags]
                public enum Flags {
                        Implicit  = 1,
@@ -1248,11 +1250,6 @@ namespace Mono.CSharp {
                //
                // Keeps track of constants
                Hashtable constants;
-
-               //
-               // The parameters for the block, this is only needed on the toplevel block really
-               // TODO: move `parameters' into ToplevelBlock
-               Parameters parameters;
                
                //
                // If this is a switch section, the enclosing switch block.
@@ -1271,42 +1268,33 @@ namespace Mono.CSharp {
                        : this (parent, flags, Location.Null, Location.Null)
                { }
 
-               public Block (Block parent, Flags flags, Parameters parameters)
-                       : this (parent, flags, parameters, Location.Null, Location.Null)
-               { }
-
                public Block (Block parent, Location start, Location end)
                        : this (parent, (Flags) 0, start, end)
                { }
 
-               public Block (Block parent, Parameters parameters, Location start, Location end)
-                       : this (parent, (Flags) 0, parameters, start, end)
-               { }
-
                public Block (Block parent, Flags flags, Location start, Location end)
-                       : this (parent, flags, Parameters.EmptyReadOnlyParameters, start, end)
-               { }
-
-               public Block (Block parent, Flags flags, Parameters parameters,
-                             Location start, Location end)
                {
                        if (parent != null)
                                parent.AddChild (this);
                        
                        this.Parent = parent;
                        this.flags = flags;
-                       this.parameters = parameters;
                        this.StartLocation = start;
                        this.EndLocation = end;
                        this.loc = start;
                        this_id = id++;
                        statements = new ArrayList ();
 
+                       if ((flags & Flags.IsToplevel) != 0)
+                               Toplevel = (ToplevelBlock) this;
+                       else
+                               Toplevel = parent.Toplevel;
+
                        if (parent != null && Implicit) {
-                               if (parent.child_variable_names == null)
-                                       parent.child_variable_names = new Hashtable();
+                               if (parent.known_variables == null)
+                                       parent.known_variables = new Hashtable ();
                                // share with parent
-                               child_variable_names = parent.child_variable_names;
+                               known_variables = parent.known_variables;
                        }
                                
                }
@@ -1453,19 +1441,26 @@ namespace Mono.CSharp {
                        }
                }
 
-               Hashtable child_variable_names;
+               Hashtable known_variables;
 
                // <summary>
-               //   Marks a variable with name @name as being used in a child block.
+               //   Marks a variable with name @name as being used in this or a child block.
                //   If a variable name has been used in a child block, it's illegal to
                //   declare a variable with the same name in the current block.
                // </summary>
-               public void AddChildVariableName (string name)
+               void AddKnownVariable (string name, LocalInfo info)
                {
-                       if (child_variable_names == null)
-                               child_variable_names = new Hashtable ();
+                       if (known_variables == null)
+                               known_variables = new Hashtable ();
+
+                       known_variables [name] = info;
+               }
 
-                       child_variable_names [name] = null;
+               public LocalInfo GetKnownVariableInfo (string name)
+               {
+                       if (known_variables == null || !known_variables.Contains (name))
+                               return null;
+                       return (LocalInfo) known_variables [name];
                }
 
                // <summary>
@@ -1473,10 +1468,20 @@ namespace Mono.CSharp {
                // </summary>
                public bool IsVariableNameUsedInChildBlock (string name)
                {
-                       if (child_variable_names == null)
-                               return false;
+                       LocalInfo vi = GetKnownVariableInfo (name);
+                       // Cheeky little test that knows that 'known_variables' is shared between
+                       // an implicit block and its enclosing real block.
+                       return vi != null && known_variables != vi.Block.known_variables;
+               }
 
-                       return child_variable_names.Contains (name);
+               // <summary>
+               //   Checks whether a variable name has already been used in this block, possibly by
+               //   an implicit block.
+               // </summary>
+               public bool IsVariableNameUsedInBlock (string name)
+               {
+                       LocalInfo vi = GetKnownVariableInfo (name);
+                       return vi != null && known_variables == vi.Block.known_variables;
                }
 
                // <summary>
@@ -1503,7 +1508,7 @@ namespace Mono.CSharp {
                        return this_variable;
                }
 
-               public LocalInfo AddVariable (Expression type, string name, Parameters pars, Location l)
+               public LocalInfo AddVariable (Expression type, string name, Location l)
                {
                        if (variables == null)
                                variables = new Hashtable ();
@@ -1538,27 +1543,23 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       if (pars != null) {
-                               int idx;
-                               Parameter p = pars.GetParameterByName (name, out idx);
-                               if (p != null) {
-                                       Report.Error (136, l, "A local variable named `" + name + "' " +
-                                                     "cannot be declared in this scope since it would " +
-                                                     "give a different meaning to `" + name + "', which " +
-                                                     "is already used in a `parent or current' scope to " +
-                                                     "denote something else");
-                                       return null;
-                               }
+                       int idx;
+                       Parameter p = Toplevel.Parameters.GetParameterByName (name, out idx);
+                       if (p != null) {
+                               Report.Error (136, l, "A local variable named `" + name + "' " +
+                                             "cannot be declared in this scope since it would " +
+                                             "give a different meaning to `" + name + "', which " +
+                                             "is already used in a `parent or current' scope to " +
+                                             "denote something else");
+                               return null;
                        }
 
                        vi = new LocalInfo (type, name, this, l);
 
                        variables.Add (name, vi);
 
-                       // Mark 'name' as "used by a child block" in every surrounding block
-                       if (cur != null)
-                               for (Block par = cur.Parent; par != null; par = par.Parent)
-                                       par.AddChildVariableName (name);
+                       for (Block b = cur; b != null; b = b.Parent)
+                               b.AddKnownVariable (name, vi);
 
                        if ((flags & Flags.VariablesInitialized) != 0)
                                throw new Exception ();
@@ -1567,9 +1568,9 @@ namespace Mono.CSharp {
                        return vi;
                }
 
-               public bool AddConstant (Expression type, string name, Expression value, Parameters pars, Location l)
+               public bool AddConstant (Expression type, string name, Expression value, Location l)
                {
-                       if (AddVariable (type, name, pars, l) == null)
+                       if (AddVariable (type, name, l) == null)
                                return false;
                        
                        if (constants == null)
@@ -1637,22 +1638,15 @@ namespace Mono.CSharp {
                //
                public ParameterReference GetParameterReference (string name, Location loc)
                {
-                       Block b = this;
+                       Parameter par;
+                       int idx;
 
-                       do {
-                               Parameters pars = b.parameters;
-                               
-                               if (pars != null){
-                                       Parameter par;
-                                       int idx;
-                                       
-                                       par = pars.GetParameterByName (name, out idx);
-                                       if (par != null){
-                                               return new ParameterReference (pars, this, idx, name, loc);
-                                       }
-                               }
-                               b = b.Parent;
-                       } while (b != null);
+                       for (Block b = this; b != null; b = b.Toplevel.Parent) {
+                               Parameters pars = b.Toplevel.Parameters;
+                               par = pars.GetParameterByName (name, out idx);
+                               if (par != null)
+                                       return new ParameterReference (pars, this, idx, name, loc);
+                       }
                        return null;
                }
 
@@ -1662,24 +1656,7 @@ namespace Mono.CSharp {
                //
                public bool IsLocalParameter (string name)
                {
-                       Block b = this;
-                       int toplevel_count = 0;
-
-                       do {
-                               if (this is ToplevelBlock)
-                                       toplevel_count++;
-
-                               Parameters pars = b.parameters;
-                               if (pars != null){
-                                       if (pars.GetParameterByName (name) != null)
-                                               return true;
-                                       return false;
-                               }
-                               if (toplevel_count > 0)
-                                       return false;
-                               b = b.Parent;
-                       } while (b != null);
-                       return false;
+                       return Toplevel.Parameters.GetParameterByName (name) != null;
                }
                
                //
@@ -1687,16 +1664,14 @@ namespace Mono.CSharp {
                //
                public bool IsParameterReference (string name)
                {
-                       Block b = this;
+                       Parameter par;
+                       int idx;
 
-                       do {
-                               Parameters pars = b.parameters;
-                               
-                               if (pars != null)
-                                       if (pars.GetParameterByName (name) != null)
-                                               return true;
-                               b = b.Parent;
-                       } while (b != null);
+                       for (Block b = this; b != null; b = b.Toplevel.Parent) {
+                               par = b.Toplevel.Parameters.GetParameterByName (name, out idx);
+                               if (par != null)
+                                       return true;
+                       }
                        return false;
                }
                
@@ -2104,19 +2079,6 @@ namespace Mono.CSharp {
                        ec.CurrentBlock = prev_block;
                }
 
-               public ToplevelBlock Toplevel {
-                       get {
-                               Block b = this;
-                               while (b.Parent != null){
-                                       if ((b.flags & Flags.IsToplevel) != 0)
-                                               break;
-                                       b = b.Parent;
-                               }
-
-                               return (ToplevelBlock) b;
-                       }
-               }
-
                //
                // Returns true if we ar ea child of `b'.
                //
@@ -2152,7 +2114,11 @@ namespace Mono.CSharp {
                Hashtable capture_contexts;
 
                static int did = 0;
-               
+
+               //
+               // The parameters for the block.
+               //
+               public readonly Parameters Parameters;
                        
                public void RegisterCaptureContext (CaptureContext cc)
                {
@@ -2182,22 +2148,28 @@ namespace Mono.CSharp {
                // parents
                //
                public ToplevelBlock (ToplevelBlock container, Parameters parameters, Location start) :
-                       base (null, Flags.IsToplevel, parameters, start, Location.Null)
+                       this (container, (Flags) 0, parameters, start)
                {
-                       Container = container;
                }
                
                public ToplevelBlock (Parameters parameters, Location start) :
-                       base (null, Flags.IsToplevel, parameters, start, Location.Null)
+                       this (null, (Flags) 0, parameters, start)
                {
                }
 
                public ToplevelBlock (Flags flags, Parameters parameters, Location start) :
-                       base (null, flags | Flags.IsToplevel, parameters, start, Location.Null)
+                       this (null, flags, parameters, start)
                {
                }
 
-               public ToplevelBlock (Location loc) : base (null, Flags.IsToplevel, loc, loc)
+               public ToplevelBlock (ToplevelBlock container, Flags flags, Parameters parameters, Location start) :
+                       base (null, flags | Flags.IsToplevel, start, Location.Null)
+               {
+                       Parameters = parameters == null ? Parameters.EmptyReadOnlyParameters : parameters;
+                       Container = container;
+               }
+
+               public ToplevelBlock (Location loc) : this (null, (Flags) 0, null, loc)
                {
                }