[642437] Nested types have to check type arguments only and not full class
[mono.git] / mcs / mcs / generic.cs
index c49c8466da3730e748225f9a20c8c8d65c6568c5..3f3d7be5a396a70a07f2e257908421c2c20d4fcb 100644 (file)
@@ -1169,6 +1169,29 @@ namespace Mono.CSharp {
                        this.var = var;
                }
 
+               #region Properties
+
+               public TypeParameter[] MethodTypeParameters {
+                       get {
+                               return mvar;
+                       }
+               }
+
+               #endregion
+
+               public static TypeSpec GetMemberDeclaringType (TypeSpec type)
+               {
+                       if (type is InflatedTypeSpec) {
+                               if (type.DeclaringType == null)
+                                       return type.GetDefinition ();
+
+                               var parent = GetMemberDeclaringType (type.DeclaringType);
+                               type = MemberCache.GetMember<TypeSpec> (parent, type);
+                       }
+
+                       return type;
+               }
+
                public TypeSpec Mutate (TypeSpec ts)
                {
                        TypeSpec value;
@@ -1180,12 +1203,6 @@ namespace Mono.CSharp {
                        return value;
                }
 
-               public FieldInfo Mutate (FieldSpec fs)
-               {
-                       // TODO:
-                       return fs.GetMetaInfo ();
-               }
-
                public TypeParameterSpec Mutate (TypeParameterSpec tp)
                {
                        for (int i = 0; i < mvar.Length; ++i) {
@@ -1765,7 +1782,7 @@ namespace Mono.CSharp {
                        if (constraints == null)
                                return true;
 
-                       return ConstraintChecker.CheckAll (ec, open_type, args.Arguments, constraints, loc);
+                       return new ConstraintChecker(ec).CheckAll (open_type, args.Arguments, constraints, loc);
                }
        
                public override bool CheckAccessLevel (IMemberContext mc)
@@ -1841,46 +1858,84 @@ namespace Mono.CSharp {
                }
        }
 
-       static class ConstraintChecker
+       struct ConstraintChecker
        {
-               /// <summary>
-               ///   Check the constraints; we're called from ResolveAsTypeTerminal()
-               ///   after fully resolving the constructed type.
-               /// </summary>
-               public static bool CheckAll (IMemberContext mc, MemberSpec context, TypeSpec[] targs, TypeParameterSpec[] tparams, Location loc)
+               IMemberContext mc;
+               bool ignore_inferred_dynamic;
+
+               public ConstraintChecker (IMemberContext ctx)
+               {
+                       this.mc = ctx;
+                       ignore_inferred_dynamic = false;
+               }
+
+               #region Properties
+
+               public bool IgnoreInferredDynamic {
+                       get {
+                               return ignore_inferred_dynamic;
+                       }
+                       set {
+                               ignore_inferred_dynamic = value;
+                       }
+               }
+
+               #endregion
+
+               //
+               // Checks all type arguments againts type parameters constraints
+               // NOTE: It can run in probing mode when `mc' is null
+               //
+               public bool CheckAll (MemberSpec context, TypeSpec[] targs, TypeParameterSpec[] tparams, Location loc)
                {
                        for (int i = 0; i < tparams.Length; i++) {
-                               if (!CheckConstraint (mc, context, targs [i], tparams [i], loc))
+                               if (ignore_inferred_dynamic && targs[i] == InternalType.Dynamic)
+                                       continue;
+
+                               if (!CheckConstraint (context, targs [i], tparams [i], loc))
                                        return false;
                        }
 
                        return true;
                }
 
-               static bool CheckConstraint (IMemberContext mc, MemberSpec context, TypeSpec atype, TypeParameterSpec tparam, Location loc)
+               bool CheckConstraint (MemberSpec context, TypeSpec atype, TypeParameterSpec tparam, Location loc)
                {
                        //
                        // First, check the `class' and `struct' constraints.
                        //
                        if (tparam.HasSpecialClass && !TypeManager.IsReferenceType (atype)) {
-                               mc.Compiler.Report.Error (452, loc,
-                                       "The type `{0}' must be a reference type in order to use it as type parameter `{1}' in the generic type or method `{2}'",
-                                       TypeManager.CSharpName (atype), tparam.GetSignatureForError (), context.GetSignatureForError ());
+                               if (mc != null) {
+                                       mc.Compiler.Report.Error (452, loc,
+                                               "The type `{0}' must be a reference type in order to use it as type parameter `{1}' in the generic type or method `{2}'",
+                                               TypeManager.CSharpName (atype), tparam.GetSignatureForError (), context.GetSignatureForError ());
+                               }
+
                                return false;
                        }
 
                        if (tparam.HasSpecialStruct && (!TypeManager.IsValueType (atype) || TypeManager.IsNullableType (atype))) {
-                               mc.Compiler.Report.Error (453, loc,
-                                       "The type `{0}' must be a non-nullable value type in order to use it as type parameter `{1}' in the generic type or method `{2}'",
-                                       TypeManager.CSharpName (atype), tparam.GetSignatureForError (), context.GetSignatureForError ());
+                               if (mc != null) {
+                                       mc.Compiler.Report.Error (453, loc,
+                                               "The type `{0}' must be a non-nullable value type in order to use it as type parameter `{1}' in the generic type or method `{2}'",
+                                               TypeManager.CSharpName (atype), tparam.GetSignatureForError (), context.GetSignatureForError ());
+                               }
+
                                return false;
                        }
 
+                       bool ok = true;
+
                        //
                        // The class constraint comes next.
                        //
                        if (tparam.HasTypeConstraint) {
-                               CheckConversion (mc, context, atype, tparam, tparam.BaseType, loc);
+                               if (!CheckConversion (mc, context, atype, tparam, tparam.BaseType, loc)) {
+                                       if (mc == null)
+                                               return false;
+
+                                       ok = false;
+                               }
                        }
 
                        //
@@ -1888,12 +1943,21 @@ namespace Mono.CSharp {
                        //
                        if (tparam.Interfaces != null) {
                                if (TypeManager.IsNullableType (atype)) {
+                                       if (mc == null)
+                                               return false;
+
                                        mc.Compiler.Report.Error (313, loc,
                                                "The type `{0}' cannot be used as type parameter `{1}' in the generic type or method `{2}'. The nullable type `{0}' never satisfies interface constraint",
                                                atype.GetSignatureForError (), tparam.GetSignatureForError (), context.GetSignatureForError ());
+                                       ok = false;
                                } else {
                                        foreach (TypeSpec iface in tparam.Interfaces) {
-                                               CheckConversion (mc, context, atype, tparam, iface, loc);
+                                               if (!CheckConversion (mc, context, atype, tparam, iface, loc)) {
+                                                       if (mc == null)
+                                                               return false;
+
+                                                       ok = false;
+                                               }
                                        }
                                }
                        }
@@ -1902,38 +1966,70 @@ namespace Mono.CSharp {
                        // Finally, check the constructor constraint.
                        //
                        if (!tparam.HasSpecialConstructor)
-                               return true;
+                               return ok;
 
                        if (!HasDefaultConstructor (atype)) {
-                               mc.Compiler.Report.SymbolRelatedToPreviousError (atype);
-                               mc.Compiler.Report.Error (310, loc,
-                                       "The type `{0}' must have a public parameterless constructor in order to use it as parameter `{1}' in the generic type or method `{2}'",
-                                       TypeManager.CSharpName (atype), tparam.GetSignatureForError (), context.GetSignatureForError ());
+                               if (mc != null) {
+                                       mc.Compiler.Report.SymbolRelatedToPreviousError (atype);
+                                       mc.Compiler.Report.Error (310, loc,
+                                               "The type `{0}' must have a public parameterless constructor in order to use it as parameter `{1}' in the generic type or method `{2}'",
+                                               TypeManager.CSharpName (atype), tparam.GetSignatureForError (), context.GetSignatureForError ());
+                               }
                                return false;
                        }
 
-                       return true;
+                       return ok;
                }
 
-               static void CheckConversion (IMemberContext mc, MemberSpec context, TypeSpec atype, TypeParameterSpec tparam, TypeSpec ttype, Location loc)
+               static bool HasDynamicTypeArgument (TypeSpec[] targs)
+               {
+                       for (int i = 0; i < targs.Length; ++i) {
+                               var targ = targs [i];
+                               if (targ == InternalType.Dynamic)
+                                       return true;
+
+                               if (HasDynamicTypeArgument (targ.TypeArguments))
+                                       return true;
+                       }
+
+                       return false;
+               }
+
+               bool CheckConversion (IMemberContext mc, MemberSpec context, TypeSpec atype, TypeParameterSpec tparam, TypeSpec ttype, Location loc)
                {
                        var expr = new EmptyExpression (atype);
-                       if (!Convert.ImplicitStandardConversionExists (expr, ttype)) {
+                       if (Convert.ImplicitStandardConversionExists (expr, ttype))
+                               return true;
+
+                       //
+                       // When partial/full type inference finds a dynamic type argument delay
+                       // the constraint check to runtime, it can succeed for real underlying
+                       // dynamic type
+                       //
+                       if (ignore_inferred_dynamic && HasDynamicTypeArgument (ttype.TypeArguments))
+                               return true;
+
+                       if (mc != null) {
                                mc.Compiler.Report.SymbolRelatedToPreviousError (tparam);
                                if (TypeManager.IsValueType (atype)) {
-                                       mc.Compiler.Report.Error (315, loc, "The type `{0}' cannot be used as type parameter `{1}' in the generic type or method `{2}'. There is no boxing conversion from `{0}' to `{3}'",
+                                       mc.Compiler.Report.Error (315, loc,
+                                               "The type `{0}' cannot be used as type parameter `{1}' in the generic type or method `{2}'. There is no boxing conversion from `{0}' to `{3}'",
                                                atype.GetSignatureForError (), tparam.GetSignatureForError (), context.GetSignatureForError (), ttype.GetSignatureForError ());
                                } else if (atype.IsGenericParameter) {
-                                       mc.Compiler.Report.Error (314, loc, "The type `{0}' cannot be used as type parameter `{1}' in the generic type or method `{2}'. There is no boxing or type parameter conversion from `{0}' to `{3}'",
+                                       mc.Compiler.Report.Error (314, loc,
+                                               "The type `{0}' cannot be used as type parameter `{1}' in the generic type or method `{2}'. There is no boxing or type parameter conversion from `{0}' to `{3}'",
                                                atype.GetSignatureForError (), tparam.GetSignatureForError (), context.GetSignatureForError (), ttype.GetSignatureForError ());
                                } else {
-                                       mc.Compiler.Report.Error (311, loc, "The type `{0}' cannot be used as type parameter `{1}' in the generic type or method `{2}'. There is no implicit reference conversion from `{0}' to `{3}'",
+                                       mc.Compiler.Report.Error (311, loc,
+                                               "The type `{0}' cannot be used as type parameter `{1}' in the generic type or method `{2}'. There is no implicit reference conversion from `{0}' to `{3}'",
                                                atype.GetSignatureForError (), tparam.GetSignatureForError (), context.GetSignatureForError (), ttype.GetSignatureForError ());
                                }
                        }
+
+                       return false;
                }
 
-               static bool HasDefaultConstructor (TypeSpec atype)
+               bool HasDefaultConstructor (TypeSpec atype)
                {
                        var tp = atype as TypeParameterSpec;
                        if (tp != null) {
@@ -2030,25 +2126,26 @@ namespace Mono.CSharp {
                {
                        TypeParameterName[] names = MemberName.TypeArguments.GetDeclarations ();
                        string[] snames = new string [names.Length];
+                       var block = m.Block;
                        for (int i = 0; i < names.Length; i++) {
                                string type_argument_name = names[i].Name;
-                               int idx = parameters.GetParameterIndexByName (type_argument_name);
 
-                               if (idx >= 0) {
-                                       var b = m.Block;
-                                       if (b == null)
-                                               b = new ToplevelBlock (Compiler, Location);
+                               if (block == null) {
+                                       int idx = parameters.GetParameterIndexByName (type_argument_name);
+                                       if (idx >= 0) {
+                                               var b = m.Block;
+                                               if (b == null)
+                                                       b = new ToplevelBlock (Compiler, Location);
 
-                                       b.Error_AlreadyDeclaredTypeParameter (parameters [i].Location,
-                                               type_argument_name, "method parameter");
+                                               b.Error_AlreadyDeclaredTypeParameter (type_argument_name, parameters[i].Location);
+                                       }
+                               } else {
+                                       INamedBlockVariable variable = null;
+                                       block.GetLocalName (type_argument_name, m.Block, ref variable);
+                                       if (variable != null)
+                                               variable.Block.Error_AlreadyDeclaredTypeParameter (type_argument_name, variable.Location);
                                }
 
-                               if (m.Block != null) {
-                                       var ikv = m.Block.GetKnownVariable (type_argument_name);
-                                       if (ikv != null)
-                                               ikv.Block.Error_AlreadyDeclaredTypeParameter (ikv.Location, type_argument_name, "local variable");
-                               }
-                               
                                snames[i] = type_argument_name;
                        }
 
@@ -2120,76 +2217,34 @@ namespace Mono.CSharp {
 
                        return Variance.None;
                }
-
-               /// <summary>
-               ///   Type inference.  Try to infer the type arguments from `method',
-               ///   which is invoked with the arguments `arguments'.  This is used
-               ///   when resolving an Invocation or a DelegateInvocation and the user
-               ///   did not explicitly specify type arguments.
-               /// </summary>
-               public static int InferTypeArguments (ResolveContext ec, Arguments arguments, ref MethodSpec method)
-               {
-                       ATypeInference ti = ATypeInference.CreateInstance (arguments);
-                       TypeSpec[] i_args = ti.InferMethodArguments (ec, method);
-                       if (i_args == null)
-                               return ti.InferenceScore;
-
-                       if (i_args.Length == 0)
-                               return 0;
-
-                       method = method.MakeGenericMethod (i_args);
-                       return 0;
-               }
-       }
-
-       abstract class ATypeInference
-       {
-               protected readonly Arguments arguments;
-               protected readonly int arg_count;
-
-               protected ATypeInference (Arguments arguments)
-               {
-                       this.arguments = arguments;
-                       if (arguments != null)
-                               arg_count = arguments.Count;
-               }
-
-               public static ATypeInference CreateInstance (Arguments arguments)
-               {
-                       return new TypeInference (arguments);
-               }
-
-               public virtual int InferenceScore {
-                       get {
-                               return int.MaxValue;
-                       }
-               }
-
-               public abstract TypeSpec[] InferMethodArguments (ResolveContext ec, MethodSpec method);
        }
 
        //
        // Implements C# type inference
        //
-       class TypeInference : ATypeInference
+       class TypeInference
        {
                //
                // Tracks successful rate of type inference
                //
                int score = int.MaxValue;
+               readonly Arguments arguments;
+               readonly int arg_count;
 
                public TypeInference (Arguments arguments)
-                       : base (arguments)
                {
+                       this.arguments = arguments;
+                       if (arguments != null)
+                               arg_count = arguments.Count;
                }
 
-               public override int InferenceScore {
+               public int InferenceScore {
                        get {
                                return score;
                        }
                }
 
-               public override TypeSpec[] InferMethodArguments (ResolveContext ec, MethodSpec method)
+               public TypeSpec[] InferMethodArguments (ResolveContext ec, MethodSpec method)
                {
                        var method_generic_args = method.GenericDefinition.TypeParameters;
                        TypeInferenceContext context = new TypeInferenceContext (method_generic_args);
@@ -2328,17 +2383,13 @@ namespace Mono.CSharp {
                        Upper   = 2
                }
 
-               class BoundInfo
+               class BoundInfo : IEquatable<BoundInfo>
                {
                        public readonly TypeSpec Type;
                        public readonly BoundKind Kind;
 
                        public BoundInfo (TypeSpec type, BoundKind kind)
                        {
-                               // Unify dynamic and object to simplify best candidate resolution
-                               if (type == InternalType.Dynamic)
-                                       type = TypeManager.object_type;
-
                                this.Type = type;
                                this.Kind = kind;
                        }
@@ -2348,11 +2399,14 @@ namespace Mono.CSharp {
                                return Type.GetHashCode ();
                        }
 
-                       public override bool Equals (object obj)
+                       #region IEquatable<BoundInfo> Members
+
+                       public bool Equals (BoundInfo other)
                        {
-                               BoundInfo a = (BoundInfo) obj;
-                               return Type == a.Type && Kind == a.Kind;
+                               return Type == other.Type && Kind == other.Kind;
                        }
+
+                       #endregion
                }
 
                readonly TypeSpec[] unfixed_types;
@@ -2609,13 +2663,20 @@ namespace Mono.CSharp {
                                        }
 
                                        if (bound.Kind == BoundKind.Exact || cbound.Kind == BoundKind.Exact) {
-                                               if (cbound.Kind != BoundKind.Exact) {
+                                               if (cbound.Kind == BoundKind.Lower) {
                                                        if (!Convert.ImplicitConversionExists (ec, new TypeExpression (cbound.Type, Location.Null), bound.Type)) {
                                                                break;
                                                        }
 
                                                        continue;
                                                }
+                                               if (cbound.Kind == BoundKind.Upper) {
+                                                       if (!Convert.ImplicitConversionExists (ec, new TypeExpression (bound.Type, Location.Null), cbound.Type)) {
+                                                               break;
+                                                       }
+
+                                                       continue;
+                                               }
                                                
                                                if (bound.Kind != BoundKind.Exact) {
                                                        if (!Convert.ImplicitConversionExists (ec, new TypeExpression (bound.Type, Location.Null), cbound.Type)) {
@@ -2630,21 +2691,46 @@ namespace Mono.CSharp {
                                        }
 
                                        if (bound.Kind == BoundKind.Lower) {
-                                               if (!Convert.ImplicitConversionExists (ec, new TypeExpression (cbound.Type, Location.Null), bound.Type)) {
-                                                       break;
+                                               if (cbound.Kind == BoundKind.Lower) {
+                                                       if (!Convert.ImplicitConversionExists (ec, new TypeExpression (cbound.Type, Location.Null), bound.Type)) {
+                                                               break;
+                                                       }
+                                               } else {
+                                                       if (!Convert.ImplicitConversionExists (ec, new TypeExpression (bound.Type, Location.Null), cbound.Type)) {
+                                                               break;
+                                                       }
+
+                                                       bound = cbound;
                                                }
-                                       } else {
+
+                                               continue;
+                                       }
+
+                                       if (bound.Kind == BoundKind.Upper) {
                                                if (!Convert.ImplicitConversionExists (ec, new TypeExpression (bound.Type, Location.Null), cbound.Type)) {
                                                        break;
                                                }
+                                       } else {
+                                               throw new NotImplementedException ("variance conversion");
                                        }
                                }
 
                                if (cii != candidates_count)
                                        continue;
 
-                               if (best_candidate != null && best_candidate != bound.Type)
-                                       return false;
+                               //
+                               // We already have the best candidate, break if thet are different
+                               //
+                               // Dynamic is never ambiguous as we prefer dynamic over other best candidate types
+                               //
+                               if (best_candidate != null) {
+
+                                       if (best_candidate == InternalType.Dynamic)
+                                               continue;
+
+                                       if (bound.Type != InternalType.Dynamic && best_candidate != bound.Type)
+                                               return false;
+                               }
 
                                best_candidate = bound.Type;
                        }
@@ -2915,7 +3001,7 @@ namespace Mono.CSharp {
 
                                MethodGroupExpr mg = (MethodGroupExpr) e;
                                Arguments args = DelegateCreation.CreateDelegateMethodArguments (invoke.Parameters, param_types, e.Location);
-                               mg = mg.OverloadResolve (ec, ref args, null, OverloadResolver.Restrictions.Covariant | OverloadResolver.Restrictions.ProbingOnly);
+                               mg = mg.OverloadResolve (ec, ref args, null, OverloadResolver.Restrictions.CovariantDelegate | OverloadResolver.Restrictions.ProbingOnly);
                                if (mg == null)
                                        return 0;