void Init(string code) { syntaxTree = SyntaxTree.Parse(code, "test.cs"); unresolvedFile = syntaxTree.ToTypeSystem(); compilation = TypeSystemHelper.CreateCompilation(unresolvedFile); findReferences = new FindReferences(); }
public void ExpansiveInheritance() { var a = new DefaultUnresolvedTypeDefinition(string.Empty, "A"); var b = new DefaultUnresolvedTypeDefinition(string.Empty, "B"); // interface A<in U> a.Kind = TypeKind.Interface; a.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "U") { Variance = VarianceModifier.Contravariant }); // interface B<X> : A<A<B<X>>> { } b.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "X")); b.BaseTypes.Add(new ParameterizedTypeReference( a, new[] { new ParameterizedTypeReference( a, new [] { new ParameterizedTypeReference( b, new [] { new TypeParameterReference(EntityType.TypeDefinition, 0) } ) }) })); ICompilation compilation = TypeSystemHelper.CreateCompilation(a, b); ITypeDefinition resolvedA = compilation.MainAssembly.GetTypeDefinition(a); ITypeDefinition resolvedB = compilation.MainAssembly.GetTypeDefinition(b); IType type1 = new ParameterizedType(resolvedB, new [] { compilation.FindType(KnownTypeCode.Double) }); IType type2 = new ParameterizedType(resolvedA, new [] { new ParameterizedType(resolvedB, new[] { compilation.FindType(KnownTypeCode.String) }) }); Assert.IsFalse(conversions.ImplicitConversion(type1, type2).IsValid); }
void Init(string code) { compilationUnit = new CSharpParser().Parse(new StringReader(code), "test.cs"); parsedFile = compilationUnit.ToTypeSystem(); compilation = TypeSystemHelper.CreateCompilation(parsedFile); findReferences = new FindReferences(); }
void CollectMembers(string code, string memberName, bool includeOverloads = true) { StringBuilder sb = new StringBuilder(); List <int> offsets = new List <int>(); foreach (var ch in code) { if (ch == '$') { offsets.Add(sb.Length); continue; } sb.Append(ch); } var syntaxTree = SyntaxTree.Parse(sb.ToString(), "test.cs"); var unresolvedFile = syntaxTree.ToTypeSystem(); var compilation = TypeSystemHelper.CreateCompilation(unresolvedFile); var symbol = FindReferencesTest.GetSymbol(compilation, memberName); var col = new SymbolCollector(); col.IncludeOverloads = includeOverloads; col.GroupForRenaming = true; var result = col.GetRelatedSymbols(new Lazy <TypeGraph>(() => new TypeGraph(compilation.Assemblies)), symbol); if (offsets.Count != result.Count()) { foreach (var a in result) { Console.WriteLine(a); } } Assert.AreEqual(offsets.Count, result.Count()); var doc = new ReadOnlyDocument(sb.ToString()); result .Select(r => doc.GetOffset((r as IEntity).Region.Begin)) .SequenceEqual(offsets); }
public void SkeetEvilOverloadResolution() { // http://msmvps.com/blogs/jon_skeet/archive/2010/11/02/evil-code-overload-resolution-workaround.aspx // static void Foo<T>(T? ignored = default(T?)) where T : struct var m1 = MakeUnresolvedMethod(); m1.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.Method, 0, "T") { HasValueTypeConstraint = true }); m1.Parameters.Add(MakeOptionalParameter( NullableType.Create(new TypeParameterReference(EntityType.Method, 0)), "ignored" )); // class ClassConstraint<T> where T : class {} var classConstraint = new DefaultUnresolvedTypeDefinition(string.Empty, "ClassConstraint"); classConstraint.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.TypeDefinition, 0, "T") { HasReferenceTypeConstraint = true }); // static void Foo<T>(ClassConstraint<T> ignored = default(ClassConstraint<T>)) // where T : class var m2 = MakeUnresolvedMethod(); m2.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.Method, 0, "T") { HasReferenceTypeConstraint = true }); m2.Parameters.Add(MakeOptionalParameter( new ParameterizedTypeReference(classConstraint, new[] { new TypeParameterReference(EntityType.Method, 0) }), "ignored" )); // static void Foo<T>() var m3 = MakeUnresolvedMethod(); m3.TypeParameters.Add(new DefaultUnresolvedTypeParameter(EntityType.Method, 0, "T")); ICompilation compilation = TypeSystemHelper.CreateCompilation(classConstraint); var context = new SimpleTypeResolveContext(compilation.MainAssembly); IMethod resolvedM1 = (IMethod)m1.CreateResolved(context); IMethod resolvedM2 = (IMethod)m2.CreateResolved(context); IMethod resolvedM3 = (IMethod)m3.CreateResolved(context); // Call: Foo<int>(); OverloadResolution o; o = new OverloadResolution(compilation, new ResolveResult[0], typeArguments: new[] { compilation.FindType(typeof(int)) }); Assert.AreEqual(OverloadResolutionErrors.None, o.AddCandidate(resolvedM1)); Assert.AreEqual(OverloadResolutionErrors.ConstructedTypeDoesNotSatisfyConstraint, o.AddCandidate(resolvedM2)); Assert.AreSame(resolvedM1, o.BestCandidate); // Call: Foo<string>(); o = new OverloadResolution(compilation, new ResolveResult[0], typeArguments: new[] { compilation.FindType(typeof(string)) }); Assert.AreEqual(OverloadResolutionErrors.ConstructedTypeDoesNotSatisfyConstraint, o.AddCandidate(resolvedM1)); Assert.AreEqual(OverloadResolutionErrors.None, o.AddCandidate(resolvedM2)); Assert.AreSame(resolvedM2, o.BestCandidate); // Call: Foo<int?>(); o = new OverloadResolution(compilation, new ResolveResult[0], typeArguments: new[] { compilation.FindType(typeof(int?)) }); Assert.AreEqual(OverloadResolutionErrors.ConstructedTypeDoesNotSatisfyConstraint, o.AddCandidate(resolvedM1)); Assert.AreEqual(OverloadResolutionErrors.ConstructedTypeDoesNotSatisfyConstraint, o.AddCandidate(resolvedM2)); Assert.AreEqual(OverloadResolutionErrors.None, o.AddCandidate(resolvedM3)); Assert.AreSame(resolvedM3, o.BestCandidate); }