Browse Source

Fixed bug in type inference.

newNRvisualizers
Mike Krüger 13 years ago
parent
commit
df57e1dad2
  1. 27
      ICSharpCode.NRefactory.CSharp/Resolver/TypeInference.cs
  2. 43
      ICSharpCode.NRefactory.Tests/CSharp/Resolver/TypeInferenceTests.cs

27
ICSharpCode.NRefactory.CSharp/Resolver/TypeInference.cs

@ -776,6 +776,28 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -776,6 +776,28 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
Log.WriteLine(" T was fixed " + (types.Count >= 1 ? "successfully" : "(with errors)") + " to " + tp.FixedTo);
return types.Count >= 1;
} else {
if (types.Count > 1) {
// Try to search a unique type among the candidate types from which there is an implicit conversion
// to all other candate types.
IType uniqueType = null;
for (int i = 0; i < types.Count; i++) {
if (types.All (t => conversions.ImplicitConversion (t, types[i]).IsValid)) {
if (uniqueType != null) {
Log.WriteLine("Can't determine a single unique type!");
uniqueType = null;
break;
}
uniqueType = types[i];
}
}
if (uniqueType != null) {
tp.FixedTo = uniqueType;
Log.WriteLine(" T was fixed successfully to " + tp.FixedTo);
return true;
}
// fixing with errors
}
tp.FixedTo = GetFirstTypePreferNonInterfaces(types);
Log.WriteLine(" T was fixed " + (types.Count == 1 ? "successfully" : "(with errors)") + " to " + tp.FixedTo);
return types.Count == 1;
@ -857,10 +879,11 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -857,10 +879,11 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
.Where(c => lowerBounds.All(b => conversions.ImplicitConversion(b, c).IsValid))
.Where(c => upperBounds.All(b => conversions.ImplicitConversion(c, b).IsValid))
.ToList(); // evaluate the query only once
candidateTypes = candidateTypes.Where(
c => candidateTypes.All(o => conversions.ImplicitConversion(c, o).IsValid)
c => candidateTypes.All(o => conversions.ImplicitConversion(o, c).IsValid)
).ToList();
// If the specified algorithm produces a single candidate, we return
// that candidate.
// We also return the whole candidate list if we're not using the improved

43
ICSharpCode.NRefactory.Tests/CSharp/Resolver/TypeInferenceTests.cs

@ -30,7 +30,7 @@ using NUnit.Framework; @@ -30,7 +30,7 @@ using NUnit.Framework;
namespace ICSharpCode.NRefactory.CSharp.Resolver
{
[TestFixture]
public class TypeInferenceTests
public class TypeInferenceTests : ResolverTestBase
{
readonly ICompilation compilation = new SimpleCompilation(CecilLoaderTests.Mscorlib);
TypeInference ti;
@ -460,5 +460,46 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver @@ -460,5 +460,46 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
FindAllTypesInBounds(Resolve(), Resolve(typeof(IEnumerable<ICloneable>), typeof(IEnumerable<IComparable>), typeof(IList))));
}
#endregion
/// <summary>
/// Bug 9300 - Unknown Resolve Error
/// </summary>
[Test]
public void TestBug9300()
{
string program = @"struct S
{
public static implicit operator string (S s)
{
return ""a"";
}
}
interface I<in T>
{
}
class C : I<string>
{
static T Foo<T> (T a, I<T> b)
{
return a;
}
public static int Main ()
{
S s = new S ();
I<string> i = new C ();
var result = Foo (s, i);
Console.WriteLine ($result$);
return 0;
}
}
";
var mrr = Resolve<LocalResolveResult>(program);
Assert.AreEqual("System.String", mrr.Type.FullName);
}
}
}

Loading…
Cancel
Save