[svn] r7043: nemerle/trunk/ncc: testsuite/negative/conversion.n testsuite/positive/array.n typing/Typer.n

malekith svnadmin at nemerle.org
Mon Dec 4 13:42:21 CET 2006


Log:
Allow casting between arrays, when the type they contain is sub/supertype of each other. Resolves #688.

Author: malekith
Date: Mon Dec  4 13:42:20 2006
New Revision: 7043

Modified:
   nemerle/trunk/ncc/testsuite/negative/conversion.n
   nemerle/trunk/ncc/testsuite/positive/array.n
   nemerle/trunk/ncc/typing/Typer.n

Modified: nemerle/trunk/ncc/testsuite/negative/conversion.n
==============================================================================
--- nemerle/trunk/ncc/testsuite/negative/conversion.n	(original)
+++ nemerle/trunk/ncc/testsuite/negative/conversion.n	Mon Dec  4 13:42:20 2006
@@ -5,6 +5,13 @@
   public static Main () : void
   {
     _ = S1 () :> S2; // E: cannot convert S1 to S2
+
+    // bug #688
+    def a = array [1,2] : array[int];
+    _ = a :> array [string]; // E: cannot convert array \[int\] to array \[string\]
+
+    def a = array[S1()];
+    _ = a :> array[S2]; // E: cannot convert array \[S1-\] to array \[S2\]
   }
 
   public static foo () : void

Modified: nemerle/trunk/ncc/testsuite/positive/array.n
==============================================================================
--- nemerle/trunk/ncc/testsuite/positive/array.n	(original)
+++ nemerle/trunk/ncc/testsuite/positive/array.n	Mon Dec  4 13:42:20 2006
@@ -97,6 +97,12 @@
         data[i] = 42;
         System.Console.WriteLine("data[{0}] = {1}", i, data[i]);
     }
+
+
+    // bug #688
+    def a1 = array["qq", "aa"];
+    def a2 = a1 :> array[object];
+    System.Console.WriteLine(a2.GetType());
   }
 }
 
@@ -119,5 +125,6 @@
 data[1] = 42
 data[2] = 42
 data[3] = 42
+System.String[]
 END-OUTPUT
 */

Modified: nemerle/trunk/ncc/typing/Typer.n
==============================================================================
--- nemerle/trunk/ncc/typing/Typer.n	(original)
+++ nemerle/trunk/ncc/typing/Typer.n	Mon Dec  4 13:42:20 2006
@@ -3153,9 +3153,15 @@
             } else if (is_enum (from) && is_enum (to)) {
               TExpr.TypeConversion (expr, to, ConversionKind.IL (local_context.IsChecked))
             } else {
+              match ((from.Hint, to.Hint)) {
+                | (Some (Array (t1, n1)), Some (Array (t2, n2))) 
+                  when n1 == n2 && t1.TryRequire (t2) || t2.TryRequire (t1) =>
+                  TExpr.TypeConversion (expr, to, ConversionKind.DownCast ())
+                | _ =>
               ReportError (messenger, $ "cannot convert $from to $to");
               TExpr.TypeConversion (expr, to, ConversionKind.DownCast ())
             }
+            }
           | lst =>
             ReportError (messenger, $"overload ambiguity in type conversion $lst");
             TExpr.Error ()



More information about the svn mailing list