2005-12-09 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / mcs / statement.cs
index 8277e894d88e82069b05a85d13cc751d5c0a8569..7d70945eca849c1b7146c0755aaf7a127d440dbb 100644 (file)
@@ -46,8 +46,8 @@ namespace Mono.CSharp {
                        // in unreachable code, for instance.
                        //
 
-                       if (warn && (RootContext.WarningLevel >= 2))
-                               Report.Warning (162, loc, "Unreachable code detected");
+                       if (warn)
+                               Report.Warning (162, 2, loc, "Unreachable code detected");
 
                        ec.StartFlowBranching (FlowBranching.BranchingType.Block, loc);
                        bool ok = Resolve (ec);
@@ -55,16 +55,7 @@ namespace Mono.CSharp {
 
                        return ok;
                }
-               
-               protected void CheckObsolete (Type type)
-               {
-                       ObsoleteAttribute obsolete_attr = AttributeTester.GetObsoleteAttribute (type);
-                       if (obsolete_attr == null)
-                               return;
-
-                       AttributeTester.Report_ObsoleteMessage (obsolete_attr, type.FullName, loc);
-               }
-               
+                               
                /// <summary>
                ///   Return value indicates whether all code paths emitted return.
                /// </summary>
@@ -812,12 +803,12 @@ namespace Mono.CSharp {
 
                        object val = c.GetValue ();
                        if (val == null)
-                               val = c;
+                               val = SwitchLabel.NullStringCase;
                                        
                        sl = (SwitchLabel) ec.Switch.Elements [val];
 
                        if (sl == null){
-                               Report.Error (159, loc, "No such label `case {0}:' within the scope of the goto statement", c.GetValue () == null ? "null" : val);
+                               Report.Error (159, loc, "No such label `case {0}:' within the scope of the goto statement", c.GetValue () == null ? "null" : val.ToString ());
                                return false;
                        }
 
@@ -1248,7 +1239,7 @@ namespace Mono.CSharp {
                //
                // Keeps track of (name, type) pairs
                //
-               Hashtable variables;
+               IDictionary variables;
 
                //
                // Keeps track of constants
@@ -1318,10 +1309,10 @@ namespace Mono.CSharp {
                        get { return this_id; }
                }
 
-               protected Hashtable Variables {
+               protected IDictionary Variables {
                        get {
                                if (variables == null)
-                                       variables = new Hashtable ();
+                                       variables = new ListDictionary ();
                                return variables;
                        }
                }
@@ -1572,6 +1563,9 @@ namespace Mono.CSharp {
                                constants = new Hashtable ();
 
                        constants.Add (name, value);
+
+                       // A block is considered used if we perform an initialization in a local declaration, even if it is constant.
+                       Use ();
                        return true;
                }
 
@@ -1621,15 +1615,6 @@ namespace Mono.CSharp {
                        return null;
                }
                
-               /// <summary>
-               ///   True if the variable named @name is a constant
-               ///  </summary>
-               public bool IsConstant (string name)
-               {
-                       Expression e = GetConstantExpression (name);
-                       return e != null;
-               }
-
                public void AddStatement (Statement s)
                {
                        statements.Add (s);
@@ -1685,7 +1670,7 @@ namespace Mono.CSharp {
                ///   tc: is our typecontainer (to resolve type references)
                ///   ig: is the code generator:
                /// </remarks>
-               public void ResolveMeta (ToplevelBlock toplevel, EmitContext ec, InternalParameters ip)
+               public void ResolveMeta (ToplevelBlock toplevel, EmitContext ec, Parameters ip)
                {
                        bool old_unsafe = ec.InUnsafe;
 
@@ -1752,8 +1737,15 @@ namespace Mono.CSharp {
                                        if (cv == null)
                                                continue;
 
+                                       // Don't let 'const int Foo = Foo;' succeed.
+                                       // Removing the name from 'constants' ensures that we get a LocalVariableReference below,
+                                       // which in turn causes the 'must be constant' error to be triggered.
+                                       constants.Remove (name);
+
                                        ec.CurrentBlock = this;
                                        Expression e = cv.Resolve (ec);
+                                       if (e == null)
+                                               continue;
 
                                        Constant ce = e as Constant;
                                        if (ce == null){
@@ -1765,7 +1757,6 @@ namespace Mono.CSharp {
                                        if (e == null)
                                                continue;
 
-                                       constants.Remove (name);
                                        constants.Add (name, e);
                                }
                        }
@@ -1846,9 +1837,9 @@ namespace Mono.CSharp {
                                        name = (string) de.Key;
 
                                        if (vector.IsAssigned (vi.VariableInfo)){
-                                               Report.Warning (219, vi.Location, "The variable `{0}' is assigned but its value is never used", name);
+                                               Report.Warning (219, 3, vi.Location, "The variable `{0}' is assigned but its value is never used", name);
                                        } else {
-                                               Report.Warning (168, vi.Location, "The variable `{0}' is declared but never used", name);
+                                               Report.Warning (168, 3, vi.Location, "The variable `{0}' is declared but never used", name);
                                        }
                                }
                        }
@@ -1912,12 +1903,14 @@ namespace Mono.CSharp {
                                // Warn if we detect unreachable code.
                                //
                                if (unreachable) {
+                                       if (s is EmptyStatement)
+                                               continue;
+
                                        if (s is Block)
                                                ((Block) s).unreachable = true;
 
-                                       if (!unreachable_shown && (RootContext.WarningLevel >= 2)) {
-                                               Report.Warning (
-                                                       162, loc, "Unreachable code detected");
+                                       if (!unreachable_shown) {
+                                               Report.Warning (162, 2, s.loc, "Unreachable code detected");
                                                unreachable_shown = true;
                                        }
                                }
@@ -1963,7 +1956,7 @@ namespace Mono.CSharp {
                        if ((labels != null) && (RootContext.WarningLevel >= 2)) {
                                foreach (LabeledStatement label in labels.Values)
                                        if (!label.HasBeenReferenced)
-                                               Report.Warning (164, label.loc,
+                                               Report.Warning (164, 2, label.loc,
                                                                "This label has not been referenced");
                        }
 
@@ -1987,8 +1980,8 @@ namespace Mono.CSharp {
                        unreachable_shown = true;
                        unreachable = true;
 
-                       if (warn && (RootContext.WarningLevel >= 2))
-                               Report.Warning (162, loc, "Unreachable code detected");
+                       if (warn)
+                               Report.Warning (162, 2, loc, "Unreachable code detected");
 
                        ec.StartFlowBranching (FlowBranching.BranchingType.Block, loc);
                        bool ok = Resolve (ec);
@@ -2214,7 +2207,7 @@ namespace Mono.CSharp {
                                Parameters pars = t.Parameters;
                                par = pars.GetParameterByName (name, out idx);
                                if (par != null)
-                                       return new ParameterReference (pars, this, idx, name, loc);
+                                       return new ParameterReference (par, this, idx, loc);
                        }
                        return null;
                }
@@ -2276,7 +2269,7 @@ namespace Mono.CSharp {
                        return this_variable == null || this_variable.IsThisAssigned (ec, loc);
                }
 
-               public bool ResolveMeta (EmitContext ec, InternalParameters ip)
+               public bool ResolveMeta (EmitContext ec, Parameters ip)
                {
                        int errors = Report.Errors;
 
@@ -2301,6 +2294,8 @@ namespace Mono.CSharp {
                Label il_label_code;
                bool  il_label_code_set;
 
+               public static readonly object NullStringCase = new object ();
+
                //
                // if expr == null, then it is the default case.
                //
@@ -2357,8 +2352,8 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       if (required_type == TypeManager.string_type && e is NullLiteral) {
-                               converted = e;
+                       if (required_type == TypeManager.string_type && c.GetValue () == null) {
+                               converted = NullStringCase;
                                return true;
                        }
 
@@ -2375,7 +2370,7 @@ namespace Mono.CSharp {
                        string label;
                        if (converted == null)
                                label = "default";
-                       else if (converted is NullLiteral)
+                       else if (converted == NullStringCase)
                                label = "null";
                        else
                                label = converted.ToString ();
@@ -2856,7 +2851,7 @@ namespace Mono.CSharp {
 
                        ig.Emit (OpCodes.Ldloc, val);
                        
-                       if (Elements.Contains (NullLiteral.Null)){
+                       if (Elements.Contains (SwitchLabel.NullStringCase)){
                                ig.Emit (OpCodes.Brfalse, null_target);
                        } else
                                ig.Emit (OpCodes.Brfalse, default_target);
@@ -2895,7 +2890,7 @@ namespace Mono.CSharp {
                                        if (sl.Label != null){
                                                object lit = sl.Converted;
 
-                                               if (lit is NullLiteral){
+                                               if (lit == SwitchLabel.NullStringCase){
                                                        null_found = true;
                                                        if (label_count == 1)
                                                                ig.Emit (OpCodes.Br, next_test);
@@ -3087,7 +3082,7 @@ namespace Mono.CSharp {
        public class Lock : ExceptionStatement {
                Expression expr;
                public Statement Statement;
-               LocalBuilder temp;
+               TemporaryVariable temp;
                        
                public Lock (Expression expr, Statement stmt, Location l)
                {
@@ -3104,8 +3099,8 @@ namespace Mono.CSharp {
 
                        if (expr.Type.IsValueType){
                                Report.Error (185, loc,
-                                       "`{0}' is not a reference type as required by the lock statement",
-                              TypeManager.CSharpName (expr.Type));
+                                             "`{0}' is not a reference type as required by the lock statement",
+                                             TypeManager.CSharpName (expr.Type));
                                return false;
                        }
 
@@ -3128,19 +3123,18 @@ namespace Mono.CSharp {
                                ec.NeedReturnLabel ();
                        }
 
+                       temp = new TemporaryVariable (expr.Type, loc);
+                       temp.Resolve (ec);
+                       
                        return true;
                }
                
                protected override void DoEmit (EmitContext ec)
                {
-                       Type type = expr.Type;
-                       
                        ILGenerator ig = ec.ig;
-                       temp = ig.DeclareLocal (type);
-                               
-                       expr.Emit (ec);
-                       ig.Emit (OpCodes.Dup);
-                       ig.Emit (OpCodes.Stloc, temp);
+
+                       temp.Store (ec, expr);
+                       temp.Emit (ec);
                        ig.Emit (OpCodes.Call, TypeManager.void_monitor_enter_object);
 
                        // try
@@ -3156,9 +3150,8 @@ namespace Mono.CSharp {
 
                public override void EmitFinally (EmitContext ec)
                {
-                       ILGenerator ig = ec.ig;
-                       ig.Emit (OpCodes.Ldloc, temp);
-                       ig.Emit (OpCodes.Call, TypeManager.void_monitor_exit_object);
+                       temp.Emit (ec);
+                       ec.ig.Emit (OpCodes.Call, TypeManager.void_monitor_exit_object);
                }
        }
 
@@ -3291,7 +3284,7 @@ namespace Mono.CSharp {
                        public abstract void EmitExit (ILGenerator ig);
                }
 
-               class ExpressionEmitter: Emitter {
+               class ExpressionEmitter : Emitter {
                        public ExpressionEmitter (Expression converted, LocalInfo li) :
                                base (converted, li)
                        {
@@ -3313,7 +3306,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               class StringEmitter: Emitter {
+               class StringEmitter : Emitter {
                        LocalBuilder pinned_string;
                        Location loc;
 
@@ -3374,8 +3367,6 @@ namespace Mono.CSharp {
 
                        expr_type = texpr.ResolveType (ec);
 
-                       CheckObsolete (expr_type);
-
                        data = new Emitter [declarators.Count];
 
                        if (!expr_type.IsPointer){
@@ -3433,6 +3424,11 @@ namespace Mono.CSharp {
                                        if (!TypeManager.VerifyUnManaged (child.Type, loc))
                                                return false;
 
+                                       if (!Convert.ImplicitConversionExists (ec, e, expr_type)) {
+                                               e.Error_ValueCannotBeConverted (e.Location, expr_type, false);
+                                               return false;
+                                       }
+
                                        data [i] = new ExpressionEmitter (e, vi);
                                        i++;
 
@@ -3551,7 +3547,7 @@ namespace Mono.CSharp {
                }
        }
        
-       public class Catch: Statement {
+       public class Catch : Statement {
                public readonly string Name;
                public readonly Block  Block;
 
@@ -3594,8 +3590,6 @@ namespace Mono.CSharp {
 
                                        type = te.ResolveType (ec);
 
-                                       CheckObsolete (type);
-
                                        if (type != TypeManager.exception_type && !type.IsSubclassOf (TypeManager.exception_type)){
                                                Error (155, "The type caught or thrown must be derived from System.Exception");
                                                return false;
@@ -3683,6 +3677,14 @@ namespace Mono.CSharp {
                        Report.Debug (1, "END OF CATCH BLOCKS", ec.CurrentBranching);
 
                        if (General != null){
+                               if (CodeGen.Assembly.WrapNonExceptionThrows) {
+                                       foreach (Catch c in Specific){
+                                               if (c.CatchType == TypeManager.exception_type) {
+                                                       Report.Warning (1058, 1, c.loc, "A previous catch clause already catches all exceptions. All non-exceptions thrown will be wrapped in a `System.Runtime.CompilerServices.RuntimeWrappedException'");
+                                               }
+                                       }
+                               }
+
                                ec.CurrentBranching.CreateSibling (
                                        General.Block, FlowBranching.SiblingType.Catch);
 
@@ -3754,12 +3756,15 @@ namespace Mono.CSharp {
                                        if (vi == null)
                                                throw new Exception ("Variable does not exist in this block");
 
-                                       ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
                                        if (vi.IsCaptured){
+                                               LocalBuilder e = ig.DeclareLocal (vi.VariableType);
+                                               ig.Emit (OpCodes.Stloc, e);
+                                               
                                                ec.EmitCapturedVariableInstance (vi);
-                                               ig.Emit (OpCodes.Ldloc, vi.LocalBuilder);
+                                               ig.Emit (OpCodes.Ldloc, e);
                                                ig.Emit (OpCodes.Stfld, vi.FieldBuilder);
-                                       }
+                                       } else
+                                               ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
                                } else
                                        ig.Emit (OpCodes.Pop);
                                
@@ -3797,7 +3802,6 @@ namespace Mono.CSharp {
                ArrayList var_list;
                Expression expr;
                Type expr_type;
-               Expression conv;
                Expression [] resolved_vars;
                Expression [] converted_vars;
                ExpressionStatement [] assign;
@@ -3848,11 +3852,13 @@ namespace Mono.CSharp {
                                        continue;
                                }
 
-                               converted_vars [i] = Convert.ImplicitConversionRequired (
+                               converted_vars [i] = Convert.ImplicitConversion (
                                        ec, var, TypeManager.idisposable_type, loc);
 
-                               if (converted_vars [i] == null)
+                               if (converted_vars [i] == null) {
+                                       Error_IsNotConvertibleToIDisposable ();
                                        return false;
+                               }
 
                                i++;
                        }
@@ -3877,12 +3883,17 @@ namespace Mono.CSharp {
                        return true;
                }
 
+               void Error_IsNotConvertibleToIDisposable ()
+               {
+                       Report.Error (1674, loc, "`{0}': type used in a using statement must be implicitly convertible to `System.IDisposable'",
+                               TypeManager.CSharpName (expr_type));
+               }
+
                bool ResolveExpression (EmitContext ec)
                {
                        if (!TypeManager.ImplementsInterface (expr_type, TypeManager.idisposable_type)){
                                if (Convert.ImplicitConversion (ec, expr, TypeManager.idisposable_type, loc) == null) {
-                                       Report.Error (1674, loc, "`{0}': type used in a using statement must be implicitly convertible to 'System.IDisposable'",
-                                               TypeManager.CSharpName (expr_type));
+                                       Error_IsNotConvertibleToIDisposable ();
                                        return false;
                                }
                        }
@@ -3935,7 +3946,7 @@ namespace Mono.CSharp {
                                                MethodInfo mi = null;
 
                                                foreach (MethodInfo mk in ((MethodGroupExpr) ml).Methods) {
-                                                       if (TypeManager.GetArgumentTypes (mk).Length == 0) {
+                                                       if (TypeManager.GetParameterData (mk).Count == 0) {
                                                                mi = mk;
                                                                break;
                                                        }
@@ -3970,10 +3981,8 @@ namespace Mono.CSharp {
                        //
                        ILGenerator ig = ec.ig;
                        local_copy = ig.DeclareLocal (expr_type);
-                       if (conv != null)
-                               conv.Emit (ec);
-                       else
-                               expr.Emit (ec);
+
+                       expr.Emit (ec);
                        ig.Emit (OpCodes.Stloc, local_copy);
 
                        if (emit_finally)
@@ -4007,7 +4016,7 @@ namespace Mono.CSharp {
                                        MethodInfo mi = null;
 
                                        foreach (MethodInfo mk in ((MethodGroupExpr) ml).Methods) {
-                                               if (TypeManager.GetArgumentTypes (mk).Length == 0) {
+                                               if (TypeManager.GetParameterData (mk).Count == 0) {
                                                        mi = mk;
                                                        break;
                                                }
@@ -4117,7 +4126,8 @@ namespace Mono.CSharp {
                        if (expr == null)
                                return false;
 
-                       if (expr is NullLiteral) {
+                       Constant c = expr as Constant;
+                       if (c != null && c.GetValue () == null) {
                                Report.Error (186, loc, "Use of null is not valid in this context");
                                return false;
                        }
@@ -4128,6 +4138,12 @@ namespace Mono.CSharp {
 
                        Type var_type = texpr.Type;
 
+                       if (expr.eclass == ExprClass.MethodGroup || expr is AnonymousMethod) {
+                               Report.Error (446, expr.Location, "Foreach statement cannot operate on a `{0}'",
+                                       expr.ExprClassName);
+                               return false;
+                       }
+
                        //
                        // We need an instance variable.  Not sure this is the best
                        // way of doing this.
@@ -4137,7 +4153,7 @@ namespace Mono.CSharp {
                        //
                        if (!(expr.eclass == ExprClass.Variable || expr.eclass == ExprClass.Value ||
                              expr.eclass == ExprClass.PropertyAccess || expr.eclass == ExprClass.IndexerAccess)){
-                               collection.error1579 ();
+                               collection.Error_Enumerator ();
                                return false;
                        }
 
@@ -4168,92 +4184,6 @@ namespace Mono.CSharp {
                        ec.LoopEnd = old_end;
                }
 
-               protected class TemporaryVariable : Expression, IMemoryLocation
-               {
-                       LocalInfo li;
-
-                       public TemporaryVariable (Type type, Location loc)
-                       {
-                               this.type = type;
-                               this.loc = loc;
-                               eclass = ExprClass.Value;
-                       }
-
-                       public override Expression DoResolve (EmitContext ec)
-                       {
-                               if (li != null)
-                                       return this;
-
-                               TypeExpr te = new TypeExpression (type, loc);
-                               li = ec.CurrentBlock.AddTemporaryVariable (te, loc);
-                               if (!li.Resolve (ec))
-                                       return null;
-
-                               AnonymousContainer am = ec.CurrentAnonymousMethod;
-                               if ((am != null) && am.IsIterator)
-                                       ec.CaptureVariable (li);
-
-                               return this;
-                       }
-
-                       public override void Emit (EmitContext ec)
-                       {
-                               ILGenerator ig = ec.ig;
-
-                               if (li.FieldBuilder != null) {
-                                       ig.Emit (OpCodes.Ldarg_0);
-                                       ig.Emit (OpCodes.Ldfld, li.FieldBuilder);
-                               } else {
-                                       ig.Emit (OpCodes.Ldloc, li.LocalBuilder);
-                               }
-                       }
-
-                       public void EmitLoadAddress (EmitContext ec)
-                       {
-                               ILGenerator ig = ec.ig;
-
-                               if (li.FieldBuilder != null) {
-                                       ig.Emit (OpCodes.Ldarg_0);
-                                       ig.Emit (OpCodes.Ldflda, li.FieldBuilder);
-                               } else {
-                                       ig.Emit (OpCodes.Ldloca, li.LocalBuilder);
-                               }
-                       }
-
-                       public void Store (EmitContext ec, Expression right_side)
-                       {
-                               if (li.FieldBuilder != null)
-                                       ec.ig.Emit (OpCodes.Ldarg_0);
-
-                               right_side.Emit (ec);
-                               if (li.FieldBuilder != null) {
-                                       ec.ig.Emit (OpCodes.Stfld, li.FieldBuilder);
-                               } else {
-                                       ec.ig.Emit (OpCodes.Stloc, li.LocalBuilder);
-                               }
-                       }
-
-                       public void EmitThis (EmitContext ec)
-                       {
-                               if (li.FieldBuilder != null) {
-                                       ec.ig.Emit (OpCodes.Ldarg_0);
-                               }
-                       }
-
-                       public void EmitStore (ILGenerator ig)
-                       {
-                               if (li.FieldBuilder != null)
-                                       ig.Emit (OpCodes.Stfld, li.FieldBuilder);
-                               else
-                                       ig.Emit (OpCodes.Stloc, li.LocalBuilder);
-                       }
-
-                       public void AddressOf (EmitContext ec, AddressOp mode)
-                       {
-                               EmitLoadAddress (ec);
-                       }
-               }
-
                protected class ArrayCounter : TemporaryVariable
                {
                        public ArrayCounter (Location loc)
@@ -4405,6 +4335,7 @@ namespace Mono.CSharp {
                        MethodInfo move_next;
                        Type var_type, enumerator_type;
                        bool is_disposable;
+                       bool enumerator_found;
 
                        public CollectionForeach (Type var_type, Expression var,
                                                  Expression expr, Statement stmt, Location l)
@@ -4418,20 +4349,9 @@ namespace Mono.CSharp {
 
                        bool GetEnumeratorFilter (EmitContext ec, MethodInfo mi)
                        {
-                               Type [] args = TypeManager.GetArgumentTypes (mi);
-                               if (args != null){
-                                       if (args.Length != 0)
-                                               return false;
-                               }
-
-                               if (TypeManager.IsOverride (mi))
-                                       return false;
-                       
-                               // Check whether GetEnumerator is public
-                               if ((mi.Attributes & MethodAttributes.Public) != MethodAttributes.Public)
-                                       return false;
+                               Type return_type = mi.ReturnType;
 
-                               if ((mi.ReturnType == TypeManager.ienumerator_type) && (mi.DeclaringType == TypeManager.string_type))
+                               if ((return_type == TypeManager.ienumerator_type) && (mi.DeclaringType == TypeManager.string_type))
                                        //
                                        // Apply the same optimization as MS: skip the GetEnumerator
                                        // returning an IEnumerator, and use the one returning a 
@@ -4445,8 +4365,7 @@ namespace Mono.CSharp {
                                // with this `GetEnumerator'
                                //
 
-                               Type return_type = mi.ReturnType;
-                               if (mi.ReturnType == TypeManager.ienumerator_type ||
+                               if (return_type == TypeManager.ienumerator_type ||
                                    TypeManager.ienumerator_type.IsAssignableFrom (return_type) ||
                                    (!RootContext.StdLib && TypeManager.ImplementsInterface (return_type, TypeManager.ienumerator_type))) {
                                        //
@@ -4477,11 +4396,11 @@ namespace Mono.CSharp {
                                        // find if they support the GetEnumerator pattern.
                                        //
 
-                                       if (!FetchMoveNext (ec, return_type))
-                                               return false;
-
-                                       if (!FetchGetCurrent (ec, return_type))
+                                       if (TypeManager.HasElementType (return_type) || !FetchMoveNext (ec, return_type) || !FetchGetCurrent (ec, return_type)) {
+                                               Report.Error (202, loc, "foreach statement requires that the return type `{0}' of `{1}' must have a suitable public MoveNext method and public Current property",
+                                                       TypeManager.CSharpName (return_type), TypeManager.CSharpSignature (mi));
                                                return false;
+                                       }
                                }
 
                                enumerator_type = return_type;
@@ -4508,10 +4427,8 @@ namespace Mono.CSharp {
 
                                foreach (MemberInfo m in move_next_list){
                                        MethodInfo mi = (MethodInfo) m;
-                                       Type [] args;
                                
-                                       args = TypeManager.GetArgumentTypes (mi);
-                                       if ((args != null) && (args.Length == 0) &&
+                                       if ((TypeManager.GetParameterData (mi).Count == 0) &&
                                            TypeManager.TypeToCoreType (mi.ReturnType) == TypeManager.bool_type) {
                                                move_next = mi;
                                                return true;
@@ -4552,10 +4469,8 @@ namespace Mono.CSharp {
 
                                foreach (MemberInfo m in dispose_list){
                                        MethodInfo mi = (MethodInfo) m;
-                                       Type [] args;
 
-                                       args = TypeManager.GetArgumentTypes (mi);
-                                       if (args != null && args.Length == 0){
+                                       if (TypeManager.GetParameterData (mi).Count == 0){
                                                if (mi.ReturnType == TypeManager.void_type)
                                                        return mi;
                                        }
@@ -4563,11 +4478,15 @@ namespace Mono.CSharp {
                                return null;
                        }
 
-                       public void error1579 ()
+                       public void Error_Enumerator ()
                        {
+                               if (enumerator_found) {
+                                       return;
+                               }
+
                            Report.Error (1579, loc,
-                               "foreach statement cannot operate on variables of type `{0}' because it does not contain a definition for `GetEnumerator' or is not accessible",
-                               TypeManager.CSharpName (expr.Type));
+                                       "foreach statement cannot operate on variables of type `{0}' because it does not contain a definition for `GetEnumerator' or is not accessible",
+                                       TypeManager.CSharpName (expr.Type));
                        }
 
                        bool TryType (EmitContext ec, Type t)
@@ -4579,6 +4498,18 @@ namespace Mono.CSharp {
                                        return false;
 
                                foreach (MethodBase mb in mg.Methods) {
+                                       if (TypeManager.GetParameterData (mb).Count != 0)
+                                               continue;
+                       
+                                       // Check whether GetEnumerator is public
+                                       if ((mb.Attributes & MethodAttributes.Public) != MethodAttributes.Public)
+                                               continue;
+
+                                       if (TypeManager.IsOverride (mb))
+                                               continue;
+
+                                       enumerator_found = true;
+
                                        if (!GetEnumeratorFilter (ec, (MethodInfo) mb))
                                                continue;
 
@@ -4641,7 +4572,7 @@ namespace Mono.CSharp {
                                is_disposable = true;
 
                                if (!ProbeCollectionType (ec, expr.Type)) {
-                                       error1579 ();
+                                       Error_Enumerator ();
                                        return false;
                                }