This project is read-only.

GetSymbolInfo bug

Topics: APIs
Nov 17, 2014 at 11:25 PM
The following program will run to the line that writes FAILURE, meaning that the invocation expression did not return a valid symbol, even though the call is valid and no diagnostic error was reported. This bug is new in 1.0-beta, it worked fine in 0.7.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace RoslynTest {
    class Program {
        static void Main(string[] args) {
            var syntaxTree = CSharpSyntaxTree.ParseText(@"
using System;
public class C {
    public static void Main() {
        var c = new C();
        typeof(C).GetField(""F1"").SetValue(c, 14);
    }
}");

            var compilation = CSharpCompilation.Create("Test", new[] { syntaxTree }, new[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location) });
            bool hasErrors = false;
            foreach (var d in compilation.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error)) {
                Console.WriteLine(d.GetMessage());
                hasErrors = true;
            }
            if (hasErrors) {
                Console.WriteLine("Test was invalid, there were errors in the code");
                return;
            }

            var semanticModel = compilation.GetSemanticModel(syntaxTree);

            var root = syntaxTree.GetRoot();
            var expr = root.DescendantNodes().OfType<MethodDeclarationSyntax>().First().Body.Statements.OfType<ExpressionStatementSyntax>().Single().Expression;

            var iex = (InvocationExpressionSyntax)expr;
            var sym = semanticModel.GetSymbolInfo(iex);

            if (sym.Symbol == null)
                Console.WriteLine("FAILURE: symbol was null even though the code is valid and no errors were reported by the diagnostics");
            else
                Console.WriteLine("Everything is fine!");
        }
    }
}
Nov 18, 2014 at 1:03 AM
When I run this on a newer version of Roslyn, I don't get null for the symbol, I get the symbol "Method void System.Reflection.FieldInfo.SetValue(System.Object obj, System.Object value)". This regression appears to have been fixed in between when we snapped for Preview and now.
Nov 18, 2014 at 7:42 AM
Did you manage to reproduce it with the official package?