[614924] Use proper best type expression for implicit array
authorMarek Safar <marek.safar@gmail.com>
Tue, 17 Aug 2010 14:13:00 +0000 (15:13 +0100)
committerMarek Safar <marek.safar@gmail.com>
Tue, 17 Aug 2010 14:34:18 +0000 (15:34 +0100)
mcs/mcs/expression.cs
mcs/tests/gtest-implicitarray-01.cs

index f16996cbc6fe328329ffcde16fb85ec0e8496729..5ab86439f0ce2751c65721cdafb9b10c7b59e3ff 100644 (file)
@@ -6356,6 +6356,8 @@ namespace Mono.CSharp {
        //
        class ImplicitlyTypedArrayCreation : ArrayCreation
        {
+               TypeInferenceContext best_type_inference;
+
                public ImplicitlyTypedArrayCreation (ComposedTypeSpecifier rank, ArrayInitializer initializers, Location loc)
                        : base (null, rank, initializers, loc)
                {                       
@@ -6373,14 +6375,19 @@ namespace Mono.CSharp {
 
                        dimensions = rank.Dimension;
 
+                       best_type_inference = new TypeInferenceContext ();
+
                        if (!ResolveInitializers (ec))
                                return null;
 
-                       if (array_element_type == null || array_element_type == InternalType.Null ||
-                               array_element_type == TypeManager.void_type || array_element_type == InternalType.AnonymousMethod ||
-                               array_element_type == InternalType.MethodGroup ||
+                       best_type_inference.FixAllTypes (ec);
+                       array_element_type = best_type_inference.InferredTypeArguments[0];
+                       best_type_inference = null;
+
+                       if (array_element_type == null || array_element_type == InternalType.MethodGroup || array_element_type == InternalType.AnonymousMethod ||
                                arguments.Count != rank.Dimension) {
-                               Error_NoBestType (ec);
+                               ec.Report.Error (826, loc,
+                                       "The type of an implicitly typed array cannot be inferred from the initializer. Try specifying array type explicitly");
                                return null;
                        }
 
@@ -6396,12 +6403,6 @@ namespace Mono.CSharp {
                        return this;
                }
 
-               void Error_NoBestType (ResolveContext ec)
-               {
-                       ec.Report.Error (826, loc,
-                               "The type of an implicitly typed array cannot be inferred from the initializer. Try specifying array type explicitly");
-               }
-
                //
                // Converts static initializer only
                //
@@ -6417,27 +6418,10 @@ namespace Mono.CSharp {
                protected override Expression ResolveArrayElement (ResolveContext ec, Expression element)
                {
                        element = element.Resolve (ec);
-                       if (element == null)
-                               return null;
-                       
-                       if (array_element_type == null) {
-                               if (element.Type != InternalType.Null)
-                                       array_element_type = element.Type;
-
-                               return element;
-                       }
-
-                       if (Convert.ImplicitConversionExists (ec, element, array_element_type)) {
-                               return element;
-                       }
-
-                       if (Convert.ImplicitConversionExists (ec, new TypeExpression (array_element_type, loc), element.Type)) {
-                               array_element_type = element.Type;
-                               return element;
-                       }
+                       if (element != null)
+                               best_type_inference.AddCommonTypeBound (element.Type);
 
-                       Error_NoBestType (ec);
-                       return null;
+                       return element;
                }
        }       
        
index 66e9083015ca76d827941c99644f497ba49ab706..184b5138ca0b8d9ef4c53795bfa33368ac5c8ac4 100644 (file)
@@ -23,6 +23,8 @@ public class Test
                const byte b = 100;
                var a7 = new[] { b, 10, b, 999, b };
                
+               var a8 = new[] { new Test (), 22,  new object(), string.Empty, null };
+               
                return 0;
        }
 }