public void TestTupleWithLocalTypeReferences2() { var source = @" using System.Linq; class C { void Method((C a, int b) t) { } }"; // Tuples store locations along with them. But we can only recover those locations // if we're re-resolving into a compilation with the same files. var compilation1 = GetCompilation(source, LanguageNames.CSharp, "File1.cs"); var compilation2 = GetCompilation(source, LanguageNames.CSharp, "File2.cs"); var symbol = GetAllSymbols( compilation1.GetSemanticModel(compilation1.SyntaxTrees.Single()), n => n is CSharp.Syntax.MethodDeclarationSyntax).Single(); // Ensure we don't crash getting these symbol keys. var id = SymbolKey.ToString(symbol); Assert.NotNull(id); // Validate that if the client does ask to resolve locations that we // do not crash if those locations cannot be found. var found = SymbolKey.Resolve(id, compilation2, resolveLocations: true).GetAnySymbol(); Assert.NotNull(found); Assert.Equal(symbol.Name, found.Name); Assert.Equal(symbol.Kind, found.Kind); var method = found as IMethodSymbol; Assert.True(method.Parameters[0].Type.IsTupleType); }
private void TestRoundTrip(ISymbol symbol, Compilation compilation, Func<ISymbol, object> fnId = null) { var id = SymbolKey.ToString(symbol); Assert.NotNull(id); var found = SymbolKey.Resolve(id, compilation).GetAnySymbol(); Assert.NotNull(found); if (fnId != null) { var expected = fnId(symbol); var actual = fnId(found); Assert.Equal(expected, actual); } else { Assert.Equal(symbol, found); } }
public void TestConstructedMethodInsideLocalFunctionWithTypeParameters() { var source = @" using System.Linq; class C { void Method() { object LocalFunction<T>() { return Enumerable.Empty<T>(); } } }"; var compilation = GetCompilation(source, LanguageNames.CSharp); var symbols = GetAllSymbols( compilation.GetSemanticModel(compilation.SyntaxTrees.Single()), n => n is CSharp.Syntax.MemberAccessExpressionSyntax || n is CSharp.Syntax.InvocationExpressionSyntax); var tested = false; foreach (var symbol in symbols) { // Ensure we don't crash getting these symbol keys. var id = SymbolKey.ToString(symbol); Assert.NotNull(id); var found = SymbolKey.Resolve(id, compilation: compilation).GetAnySymbol(); Assert.NotNull(found); // note: we don't check that the symbols are equal. That's because the compiler // doesn't guarantee that the TypeParameters will be hte same across successive // invocations. Assert.Equal(symbol.OriginalDefinition, found.OriginalDefinition); tested = true; } Assert.True(tested); }
public static string EncodeSymbol(ISymbol symbol) { return(SymbolKey.ToString(symbol)); }