Author: martin
Date: 2006-01-18 10:32:55 -0500 (Wed, 18 Jan 2006)
New Revision: 55728
Added:
trunk/mcs/tests/gtest-236.cs
trunk/mcs/tests/gtest-237.cs
trunk/mcs/tests/gtest-238-lib.cs
trunk/mcs/tests/gtest-238.cs
trunk/mcs/tests/gtest-239.cs
Removed:
trunk/mcs/errors/gcs0408-2.cs
trunk/mcs/errors/gcs0408-3.cs
trunk/mcs/errors/gcs0408-4.cs
trunk/mcs/errors/gcs0408.cs
Modified:
trunk/mcs/gmcs/ChangeLog
trunk/mcs/gmcs/class.cs
trunk/mcs/gmcs/expression.cs
Log:
2006-01-18 Martin Baulig <[EMAIL PROTECTED]>
Fix #76417: a generic class may now have methods which may unify
for some type parameter substitutions.
* class.cs (Method.IsDuplicateImplementation): Don't report CS0408
for methods which may unify anymore.
* expression.cs (Invocation.MoreSpecific): New private static
method; checks whether one method is more specific than another
according to 14.4.2.2 of the spec.
(Invocation.BetterFunction): Implement the tie-breaking rules from
14.4.2.2 of the spec: if two methods unify for some type parameter
substitution, we need to pick the more specific one.
Deleted: trunk/mcs/errors/gcs0408-2.cs
===================================================================
--- trunk/mcs/errors/gcs0408-2.cs 2006-01-18 15:28:08 UTC (rev 55727)
+++ trunk/mcs/errors/gcs0408-2.cs 2006-01-18 15:32:55 UTC (rev 55728)
@@ -1,16 +0,0 @@
-// CS0408: `X<T>' cannot define overload members that may unify for some type
parameter substitutions
-// Line: 9
-class X<T>
-{
- void Foo (T[] t)
- { }
-
- void Foo (int[] t)
- { }
-}
-
-class X
-{
- static void Main ()
- { }
-}
Deleted: trunk/mcs/errors/gcs0408-3.cs
===================================================================
--- trunk/mcs/errors/gcs0408-3.cs 2006-01-18 15:28:08 UTC (rev 55727)
+++ trunk/mcs/errors/gcs0408-3.cs 2006-01-18 15:32:55 UTC (rev 55728)
@@ -1,16 +0,0 @@
-// CS0408: `X<T>' cannot define overload members that may unify for some type
parameter substitutions
-// Line: 9
-class X<T>
-{
- void Foo (T[] t)
- { }
-
- void Foo (int[][] t)
- { }
-}
-
-class X
-{
- static void Main ()
- { }
-}
Deleted: trunk/mcs/errors/gcs0408-4.cs
===================================================================
--- trunk/mcs/errors/gcs0408-4.cs 2006-01-18 15:28:08 UTC (rev 55727)
+++ trunk/mcs/errors/gcs0408-4.cs 2006-01-18 15:32:55 UTC (rev 55728)
@@ -1,16 +0,0 @@
-// CS0408: `X<T>' cannot define overload members that may unify for some type
parameter substitutions
-// Line: 9
-class X<T>
-{
- void Foo (T t)
- { }
-
- void Foo (int[] t)
- { }
-}
-
-class X
-{
- static void Main ()
- { }
-}
Deleted: trunk/mcs/errors/gcs0408.cs
===================================================================
--- trunk/mcs/errors/gcs0408.cs 2006-01-18 15:28:08 UTC (rev 55727)
+++ trunk/mcs/errors/gcs0408.cs 2006-01-18 15:32:55 UTC (rev 55728)
@@ -1,16 +0,0 @@
-// CS0408: `X<T>' cannot define overload members that may unify for some type
parameter substitutions
-// Line: 9
-class X<T>
-{
- public void Foo (T t)
- { }
-
- public void Foo (int i)
- { }
-}
-
-class X
-{
- static void Main ()
- { }
-}
Modified: trunk/mcs/gmcs/ChangeLog
===================================================================
--- trunk/mcs/gmcs/ChangeLog 2006-01-18 15:28:08 UTC (rev 55727)
+++ trunk/mcs/gmcs/ChangeLog 2006-01-18 15:32:55 UTC (rev 55728)
@@ -1,3 +1,18 @@
+2006-01-18 Martin Baulig <[EMAIL PROTECTED]>
+
+ Fix #76417: a generic class may now have methods which may unify
+ for some type parameter substitutions.
+
+ * class.cs (Method.IsDuplicateImplementation): Don't report CS0408
+ for methods which may unify anymore.
+
+ * expression.cs (Invocation.MoreSpecific): New private static
+ method; checks whether one method is more specific than another
+ according to 14.4.2.2 of the spec.
+ (Invocation.BetterFunction): Implement the tie-breaking rules from
+ 14.4.2.2 of the spec: if two methods unify for some type parameter
+ substitution, we need to pick the more specific one.
+
2006-01-18 Raja R Harinath <[EMAIL PROTECTED]>
Fix #76656, cs0231-2.cs.
Modified: trunk/mcs/gmcs/class.cs
===================================================================
--- trunk/mcs/gmcs/class.cs 2006-01-18 15:28:08 UTC (rev 55727)
+++ trunk/mcs/gmcs/class.cs 2006-01-18 15:32:55 UTC (rev 55728)
@@ -3776,34 +3776,6 @@
return true;
}
- bool MayUnify (MethodCore first, MethodCore second)
- {
- int a_type_params = 0;
- if (first.GenericMethod != null)
- a_type_params =
first.GenericMethod.CountTypeParameters;
-
- int b_type_params = 0;
- if (second.GenericMethod != null)
- b_type_params =
second.GenericMethod.CountTypeParameters;
-
- if (a_type_params != b_type_params)
- return false;
-
- Type[] class_infered, method_infered;
- if (Parent.CountTypeParameters > 0)
- class_infered = new Type
[Parent.CountTypeParameters];
- else
- class_infered = null;
-
- if (a_type_params > 0)
- method_infered = new Type [a_type_params];
- else
- method_infered = null;
-
- return TypeManager.MayBecomeEqualGenericInstances (
- first.ParameterTypes, second.ParameterTypes,
class_infered, method_infered);
- }
-
protected bool IsDuplicateImplementation (MethodCore method)
{
if (method == this || !(method.MemberName.Equals
(MemberName)))
@@ -3823,7 +3795,6 @@
return false;
bool equal = true;
- bool may_unify = MayUnify (this, method);
for (int i = 0; i < param_types.Length; i++) {
if (param_types [i] != ParameterTypes [i])
@@ -3831,12 +3802,12 @@
}
if (IsExplicitImpl && (method.InterfaceType !=
InterfaceType))
- equal = may_unify = false;
+ equal = false;
// TODO: make operator compatible with MethodCore to
avoid this
if (this is Operator && method is Operator) {
if (MemberType != method.MemberType)
- equal = may_unify = false;
+ equal = false;
}
if (equal) {
@@ -3865,12 +3836,6 @@
Report.Error (111, Location,
TypeContainer.Error111, GetSignatureForError ());
return true;
- } else if (may_unify) {
- Report.Error (408, Location,
- "`{0}' cannot define overload
members that " +
- "may unify for some type
parameter substitutions",
- Parent.MemberName.ToString ());
- return true;
}
return false;
Modified: trunk/mcs/gmcs/expression.cs
===================================================================
--- trunk/mcs/gmcs/expression.cs 2006-01-18 15:28:08 UTC (rev 55727)
+++ trunk/mcs/gmcs/expression.cs 2006-01-18 15:32:55 UTC (rev 55728)
@@ -4295,6 +4295,27 @@
return null;
}
+
+ static int MoreSpecific (EmitContext ec, Type p, Type q,
Location loc)
+ {
+ if (p.IsGenericParameter && !q.IsGenericParameter)
+ return -1;
+ if (!p.IsGenericParameter && q.IsGenericParameter)
+ return 1;
+
+ if (p.IsGenericInstance) {
+ Type[] pargs = TypeManager.GetTypeArguments (p);
+ Type[] qargs = TypeManager.GetTypeArguments (q);
+
+ for (int i = 0; i < pargs.Length; i++) {
+ int better = MoreSpecific (ec, pargs
[i], qargs [i], loc);
+ if (better != 0)
+ return better;
+ }
+ }
+
+ return 0;
+ }
/// <summary>
/// Determines "Better function" between candidate
@@ -4306,12 +4327,12 @@
/// true if candidate is better than the current best match
/// </remarks>
static bool BetterFunction (EmitContext ec, ArrayList args, int
argument_count,
- MethodBase candidate, bool
candidate_params,
- MethodBase best, bool best_params,
Location loc)
+ MethodBase candidate, bool
candidate_params,
+ MethodBase best, bool best_params,
Location loc)
{
ParameterData candidate_pd =
TypeManager.GetParameterData (candidate);
ParameterData best_pd = TypeManager.GetParameterData
(best);
-
+
bool better_at_least_one = false;
bool same = true;
for (int j = 0; j < argument_count; ++j) {
@@ -4359,6 +4380,25 @@
else if (!TypeManager.IsGenericMethod (best) &&
TypeManager.IsGenericMethod (candidate))
return false;
+ MethodBase orig_candidate =
candidate.Mono_IsInflatedMethod ?
+ candidate.GetGenericMethodDefinition () :
candidate;
+ MethodBase orig_best = best.Mono_IsInflatedMethod ?
+ best.GetGenericMethodDefinition () : best;
+
+ ParameterData orig_candidate_pd =
TypeManager.GetParameterData (orig_candidate);
+ ParameterData orig_best_pd =
TypeManager.GetParameterData (orig_best);
+
+ for (int j = 0; j < argument_count; ++j) {
+ Type ct = TypeManager.TypeToCoreType
(orig_candidate_pd.ParameterType (j));
+ Type bt = TypeManager.TypeToCoreType
(orig_best_pd.ParameterType (j));
+
+ int more_specific = MoreSpecific (ec, ct, bt,
loc);
+ if (more_specific > 0)
+ return true;
+ else if (more_specific < 0)
+ return false;
+ }
+
//
// Note that this is not just an optimization. This
handles the case
// This handles the case
Added: trunk/mcs/tests/gtest-236.cs
===================================================================
--- trunk/mcs/tests/gtest-236.cs 2006-01-18 15:28:08 UTC (rev 55727)
+++ trunk/mcs/tests/gtest-236.cs 2006-01-18 15:32:55 UTC (rev 55728)
@@ -0,0 +1,30 @@
+using System;
+
+class Foo<T>
+{
+ public int Test (Foo<T> foo)
+ {
+ return 1;
+ }
+
+ public int Test (Foo<int> foo)
+ {
+ return 2;
+ }
+}
+
+class X
+{
+ static int Main ()
+ {
+ Foo<long> foo = new Foo<long> ();
+ Foo<int> bar = new Foo<int> ();
+ if (foo.Test (foo) != 1)
+ return 1;
+ if (foo.Test (bar) != 2)
+ return 2;
+ if (bar.Test (bar) != 2)
+ return 3;
+ return 0;
+ }
+}
Added: trunk/mcs/tests/gtest-237.cs
===================================================================
--- trunk/mcs/tests/gtest-237.cs 2006-01-18 15:28:08 UTC (rev 55727)
+++ trunk/mcs/tests/gtest-237.cs 2006-01-18 15:32:55 UTC (rev 55728)
@@ -0,0 +1,30 @@
+using System;
+
+class Foo<T>
+{
+ public int Test (T foo)
+ {
+ return 1;
+ }
+
+ public int Test (int foo)
+ {
+ return 2;
+ }
+}
+
+class X
+{
+ static int Main ()
+ {
+ Foo<long> foo = new Foo<long> ();
+ Foo<int> bar = new Foo<int> ();
+ if (foo.Test (4L) != 1)
+ return 1;
+ if (foo.Test (3) != 2)
+ return 2;
+ if (bar.Test (3) != 2)
+ return 3;
+ return 0;
+ }
+}
Added: trunk/mcs/tests/gtest-238-lib.cs
===================================================================
--- trunk/mcs/tests/gtest-238-lib.cs 2006-01-18 15:28:08 UTC (rev 55727)
+++ trunk/mcs/tests/gtest-238-lib.cs 2006-01-18 15:32:55 UTC (rev 55728)
@@ -0,0 +1,17 @@
+// Compiler options: /t:library
+using System;
+
+public class Foo<T>
+{
+ public int Test (int index)
+ {
+ Console.WriteLine ("Test 1: {0}", index);
+ return 1;
+ }
+
+ public int Test (T index)
+ {
+ Console.WriteLine ("Test 2: {0}", index);
+ return 2;
+ }
+}
Property changes on: trunk/mcs/tests/gtest-238-lib.cs
___________________________________________________________________
Name: svn:executable
+ *
Added: trunk/mcs/tests/gtest-238.cs
===================================================================
--- trunk/mcs/tests/gtest-238.cs 2006-01-18 15:28:08 UTC (rev 55727)
+++ trunk/mcs/tests/gtest-238.cs 2006-01-18 15:32:55 UTC (rev 55728)
@@ -0,0 +1,14 @@
+// Compiler options: /r:gtest-238-lib.dll
+// Dependencies: gtest-238-lib.cs
+class X
+{
+ static int Main ()
+ {
+ Foo<long> foo = new Foo<long> ();
+ if (foo.Test (3) != 1)
+ return 1;
+ if (foo.Test (5L) != 2)
+ return 2;
+ return 0;
+ }
+}
Property changes on: trunk/mcs/tests/gtest-238.cs
___________________________________________________________________
Name: svn:executable
+ *
Added: trunk/mcs/tests/gtest-239.cs
===================================================================
--- trunk/mcs/tests/gtest-239.cs 2006-01-18 15:28:08 UTC (rev 55727)
+++ trunk/mcs/tests/gtest-239.cs 2006-01-18 15:32:55 UTC (rev 55728)
@@ -0,0 +1,60 @@
+using System;
+
+class Foo<T,U>
+{
+ public int Test (T t, U u)
+ {
+ return 1;
+ }
+
+ public int Test (int t, U u)
+ {
+ return 2;
+ }
+
+ public int Test (T t, float u)
+ {
+ return 3;
+ }
+
+ public int Test (int t, float u)
+ {
+ return 4;
+ }
+}
+
+class X
+{
+ static int Main ()
+ {
+ Foo<long,float> a = new Foo<long,float> ();
+ if (a.Test (3L, 3.14F) != 3)
+ return 1;
+ if (a.Test (3L, 8) != 3)
+ return 2;
+ if (a.Test (3, 3.14F) != 4)
+ return 3;
+ if (a.Test (3, 8) != 4)
+ return 4;
+
+ Foo<long,double> b = new Foo<long,double> ();
+ if (b.Test (3L, 3.14F) != 3)
+ return 5;
+ if (b.Test (3, 3.14F) != 4)
+ return 6;
+ if (b.Test (3L, 3.14F) != 3)
+ return 7;
+ if (b.Test (3L, 5) != 3)
+ return 8;
+
+ Foo<string,float> c = new Foo<string,float> ();
+ if (c.Test ("Hello", 3.14F) != 3)
+ return 9;
+
+ Foo<int,string> d = new Foo<int,string> ();
+ if (d.Test (3, "Hello") != 2)
+ return 10;
+
+ return 0;
+ }
+}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches