Merge pull request #615 from nealef/master
[mono.git] / mcs / mcs / ecore.cs
index 2c469f94a3864242755f6e6cb6b1a0a9b3361ab7..a9edf6f470bad70c426c02ca64767695036647cb 100644 (file)
@@ -152,6 +152,35 @@ namespace Mono.CSharp {
                        }
                }
 
+               //
+               // Used to workaround parser limitation where we cannot get
+               // start of statement expression location
+               //
+               public virtual Location StartLocation {
+                       get {
+                               return loc;
+                       }
+               }
+
+               public virtual MethodGroupExpr CanReduceLambda (AnonymousMethodBody body)
+               {
+                       //
+                       // Return method-group expression when the expression can be used as
+                       // lambda replacement. A good example is array sorting where instead of
+                       // code like
+                       //
+                       //  Array.Sort (s, (a, b) => String.Compare (a, b));
+                       //
+                       // we can use method group directly
+                       //
+                       //  Array.Sort (s, String.Compare);
+                       //
+                       // Correct overload will be used because we do the reduction after
+                       // best candidate was found.
+                       //
+                       return null;
+               }
+
                //
                // Returns true when the expression during Emit phase breaks stack
                // by using await expression
@@ -222,7 +251,7 @@ namespace Mono.CSharp {
                public void Error_ConstantCanBeInitializedWithNullOnly (ResolveContext rc, TypeSpec type, Location loc, string name)
                {
                        rc.Report.Error (134, loc, "A constant `{0}' of reference type `{1}' can only be initialized with null",
-                               name, TypeManager.CSharpName (type));
+                               name, type.GetSignatureForError ());
                }
 
                protected virtual void Error_InvalidExpressionStatement (Report report, Location loc)
@@ -253,7 +282,10 @@ namespace Mono.CSharp {
                protected void Error_ValueCannotBeConvertedCore (ResolveContext ec, Location loc, TypeSpec target, bool expl)
                {
                        // The error was already reported as CS1660
-                       if (type == InternalType.AnonymousMethod || type == InternalType.ErrorType)
+                       if (type == InternalType.AnonymousMethod)
+                               return;
+
+                       if (type == InternalType.ErrorType || target == InternalType.ErrorType)
                                return;
 
                        string from_type = type.GetSignatureForError ();
@@ -322,13 +354,15 @@ namespace Mono.CSharp {
                {
                        ec.Report.SymbolRelatedToPreviousError (type);
                        ec.Report.Error (117, loc, "`{0}' does not contain a definition for `{1}'",
-                               TypeManager.CSharpName (type), name);
+                               type.GetSignatureForError (), name);
                }
 
                public virtual void Error_ValueAssignment (ResolveContext rc, Expression rhs)
                {
                        if (rhs == EmptyExpression.LValueMemberAccess || rhs == EmptyExpression.LValueMemberOutAccess) {
                                // Already reported as CS1612
+                       } else if (rhs == EmptyExpression.OutAccess) {
+                               rc.Report.Error (1510, loc, "A ref or out argument must be an assignable variable");
                        } else {
                                rc.Report.Error (131, loc, "The left-hand side of an assignment must be a variable, a property or an indexer");
                        }
@@ -463,10 +497,7 @@ namespace Mono.CSharp {
 
                        if (e == null) {
                                if (errors == ec.Report.Errors) {
-                                       if (out_access)
-                                               ec.Report.Error (1510, loc, "A ref or out argument must be an assignable variable");
-                                       else
-                                               Error_ValueAssignment (ec, right_side);
+                                       Error_ValueAssignment (ec, right_side);
                                }
                                return null;
                        }
@@ -1055,8 +1086,8 @@ namespace Mono.CSharp {
        ///   being that they would support an extra Emition interface that
        ///   does not leave a result on the stack.
        /// </summary>
-       public abstract class ExpressionStatement : Expression {
-
+       public abstract class ExpressionStatement : Expression
+       {
                public ExpressionStatement ResolveStatement (BlockContext ec)
                {
                        Expression e = Resolve (ec);
@@ -1067,7 +1098,10 @@ namespace Mono.CSharp {
                        if (es == null)
                                Error_InvalidExpressionStatement (ec);
 
-                       if (!(e is Assign) && (e.Type.IsGenericTask || e.Type == ec.Module.PredefinedTypes.Task.TypeSpec)) {
+                       //
+                       // This is quite expensive warning, try to limit the damage
+                       //
+                       if (MemberAccess.IsValidDotExpression (e.Type) && !(e is Assign || e is Await)) {
                                WarningAsyncWithoutWait (ec, e);
                        }
 
@@ -1077,6 +1111,30 @@ namespace Mono.CSharp {
                static void WarningAsyncWithoutWait (BlockContext bc, Expression e)
                {
                        if (bc.CurrentAnonymousMethod is AsyncInitializer) {
+                               var awaiter = new AwaitStatement.AwaitableMemberAccess (e) {
+                                       ProbingMode = true
+                               };
+
+                               //
+                               // Need to do full resolve because GetAwaiter can be extension method
+                               // available only in this context
+                               //
+                               var mg = awaiter.Resolve (bc) as MethodGroupExpr;
+                               if (mg == null)
+                                       return;
+
+                               var arguments = new Arguments (0);
+                               mg = mg.OverloadResolve (bc, ref arguments, null, OverloadResolver.Restrictions.ProbingOnly);
+                               if (mg == null)
+                                       return;
+
+                               //
+                               // Use same check rules as for real await
+                               //
+                               var awaiter_definition = bc.Module.GetAwaiter (mg.BestCandidateReturnType);
+                               if (!awaiter_definition.IsValidPattern || !awaiter_definition.INotifyCompletion)
+                                       return;
+
                                bc.Report.Warning (4014, 1, e.Location,
                                        "The statement is not awaited and execution of current method continues before the call is completed. Consider using `await' operator");
                                return;
@@ -1419,7 +1477,7 @@ namespace Mono.CSharp {
 
                public override string GetSignatureForError()
                {
-                       return TypeManager.CSharpName (Type);
+                       return Type.GetSignatureForError ();
                }
 
                public override object GetValue ()
@@ -3188,7 +3246,7 @@ namespace Mono.CSharp {
        class ExtensionMethodGroupExpr : MethodGroupExpr, OverloadResolver.IErrorHandler
        {
                ExtensionMethodCandidates candidates;
-               public readonly Expression ExtensionExpression;
+               public Expression ExtensionExpression;
 
                public ExtensionMethodGroupExpr (ExtensionMethodCandidates candidates, Expression extensionExpr, Location loc)
                        : base (candidates.Methods.Cast<MemberSpec>().ToList (), extensionExpr.Type, loc)
@@ -3232,8 +3290,16 @@ namespace Mono.CSharp {
                        if (arguments == null)
                                arguments = new Arguments (1);
 
+                       ExtensionExpression = ExtensionExpression.Resolve (ec);
+                       if (ExtensionExpression == null)
+                               return null;
+
+                       var cand = candidates;
                        arguments.Insert (0, new Argument (ExtensionExpression, Argument.AType.ExtensionType));
                        var res = base.OverloadResolve (ec, ref arguments, ehandler ?? this, restr);
+                       
+                       // Restore candidates in case we are running in probing mode 
+                       candidates = cand;
 
                        // Store resolved argument and restore original arguments
                        if (res == null) {
@@ -3443,7 +3509,7 @@ namespace Mono.CSharp {
                public override void Error_ValueCannotBeConverted (ResolveContext ec, TypeSpec target, bool expl)
                {
                        ec.Report.Error (428, loc, "Cannot convert method group `{0}' to non-delegate type `{1}'. Consider using parentheses to invoke the method",
-                               Name, TypeManager.CSharpName (target));
+                               Name, target.GetSignatureForError ());
                }
 
                public static bool IsExtensionMethodArgument (Expression expr)
@@ -3518,7 +3584,7 @@ namespace Mono.CSharp {
                                best_candidate_return = best_candidate.ReturnType;
                        }
 
-                       if (best_candidate.IsGeneric && TypeParameterSpec.HasAnyTypeParameterConstrained (best_candidate.GenericDefinition)) {
+                       if (best_candidate.IsGeneric && (restr & OverloadResolver.Restrictions.ProbingOnly) == 0 && TypeParameterSpec.HasAnyTypeParameterConstrained (best_candidate.GenericDefinition)) {
                                ConstraintChecker cc = new ConstraintChecker (ec);
                                cc.CheckAll (best_candidate.GetGenericMethodDefinition (), best_candidate.TypeArguments, best_candidate.Constraints, loc);
                        }
@@ -4495,17 +4561,18 @@ namespace Mono.CSharp {
                                // It can be applicable in expanded form (when not doing exact match like for delegates)
                                //
                                if (score != 0 && (p_mod & Parameter.Modifier.PARAMS) != 0 && (restrictions & Restrictions.CovariantDelegate) == 0) {
-                                       if (!params_expanded_form)
+                                       if (!params_expanded_form) {
                                                pt = ((ElementTypeSpec) pt).Element;
+                                       }
 
                                        if (score > 0)
                                                score = IsArgumentCompatible (ec, a, Parameter.Modifier.NONE, pt);
 
-                                       if (score == 0) {
-                                               params_expanded_form = true;
-                                       } else if (score < 0) {
+                                       if (score < 0) {
                                                params_expanded_form = true;
                                                dynamicArgument = true;
+                                       } else if (score == 0 || arg_count > pd.Count) {
+                                               params_expanded_form = true;
                                        }
                                }
 
@@ -4910,7 +4977,7 @@ namespace Mono.CSharp {
                                }
                        }
 
-                       if (invocable_member != null) {
+                       if (invocable_member != null && !IsProbingOnly) {
                                rc.Report.SymbolRelatedToPreviousError (best_candidate);
                                rc.Report.SymbolRelatedToPreviousError (invocable_member);
                                rc.Report.Warning (467, 2, loc, "Ambiguity between method `{0}' and invocable non-method `{1}'. Using method group",
@@ -4932,7 +4999,7 @@ namespace Mono.CSharp {
                        //
                        // Don't run possibly expensive checks in probing mode
                        //
-                       if (!rc.IsInProbingMode) {
+                       if (!IsProbingOnly && !rc.IsInProbingMode) {
                                //
                                // Check ObsoleteAttribute on the best method
                                //
@@ -4984,14 +5051,14 @@ namespace Mono.CSharp {
                        string index = (idx + 1).ToString ();
                        if (((mod & Parameter.Modifier.RefOutMask) ^ (a.Modifier & Parameter.Modifier.RefOutMask)) != 0) {
                                if ((mod & Parameter.Modifier.RefOutMask) == 0)
-                                       ec.Report.Error (1615, loc, "Argument `#{0}' does not require `{1}' modifier. Consider removing `{1}' modifier",
+                                       ec.Report.Error (1615, a.Expr.Location, "Argument `#{0}' does not require `{1}' modifier. Consider removing `{1}' modifier",
                                                index, Parameter.GetModifierSignature (a.Modifier));
                                else
-                                       ec.Report.Error (1620, loc, "Argument `#{0}' is missing `{1}' modifier",
+                                       ec.Report.Error (1620, a.Expr.Location, "Argument `#{0}' is missing `{1}' modifier",
                                                index, Parameter.GetModifierSignature (mod));
                        } else {
                                string p1 = a.GetSignatureForError ();
-                               string p2 = TypeManager.CSharpName (paramType);
+                               string p2 = paramType.GetSignatureForError ();
 
                                if (p1 == p2) {
                                        p1 = a.Type.GetSignatureForErrorIncludingAssemblyName ();
@@ -5584,6 +5651,11 @@ namespace Mono.CSharp {
                
                override public Expression DoResolveLValue (ResolveContext ec, Expression right_side)
                {
+                       if (spec is FixedFieldSpec) {
+                               // It could be much better error message but we want to be error compatible
+                               Error_ValueAssignment (ec, right_side);
+                       }
+
                        Expression e = DoResolve (ec, right_side);
 
                        if (e == null)
@@ -5902,6 +5974,21 @@ namespace Mono.CSharp {
 
                #endregion
 
+               public override MethodGroupExpr CanReduceLambda (AnonymousMethodBody body)
+               {
+                       if (best_candidate == null || !(best_candidate.IsStatic || InstanceExpression is This))
+                               return null;
+
+                       var args_count = arguments == null ? 0 : arguments.Count;
+                       if (args_count != body.Parameters.Count && args_count == 0)
+                               return null;
+
+                       var mg = MethodGroupExpr.CreatePredefined (best_candidate.Get, DeclaringType, loc);
+                       mg.InstanceExpression = InstanceExpression;
+
+                       return mg;
+               }
+
                public static PropertyExpr CreatePredefined (PropertySpec spec, Location loc)
                {
                        return new PropertyExpr (spec, loc) {
@@ -6523,8 +6610,9 @@ namespace Mono.CSharp {
                        // Don't capture temporary variables except when using
                        // state machine redirection and block yields
                        //
-                       if (ec.CurrentAnonymousMethod != null && ec.CurrentAnonymousMethod.IsIterator &&
-                               ec.CurrentBlock.Explicit.HasYield && ec.IsVariableCapturingRequired) {
+                       if (ec.CurrentAnonymousMethod != null && ec.CurrentAnonymousMethod is StateMachineInitializer &&
+                               (ec.CurrentBlock.Explicit.HasYield || ec.CurrentBlock.Explicit.HasAwait) &&
+                               ec.IsVariableCapturingRequired) {
                                AnonymousMethodStorey storey = li.Block.Explicit.CreateAnonymousMethodStorey (ec);
                                storey.CaptureLocalVariable (ec, li);
                        }