This project is read-only.
2
Vote

GetSymbolInfo returns a weird result in a specific case.

description

Given this code:
using System.Linq;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace RoslynOddity
{
    internal class Program {

        private const string testProgram = @"
    using System.Linq;
    using System.Reflection;
    using Microsoft.CodeAnalysis;
    using Microsoft.CodeAnalysis.CSharp;
    using Microsoft.CodeAnalysis.CSharp.Syntax;

    class TestClass {
        void Foo() {
            var expr = SyntaxFactory.ParseExpression(""a is string"");
            if (expr.IsKind(SyntaxKind.IsExpression)) {
            }
        }
    }";

        private static void Main()
        {
            var tree = SyntaxFactory.ParseSyntaxTree(testProgram);

            var metadataReferences =
                Assembly.GetExecutingAssembly()
                    .GetReferencedAssemblies()
                    .Select(Assembly.Load)
                    .Select(MetadataReference.CreateFromAssembly);

            var compilation = CSharpCompilation.Create(
                "RoslynOddity.dll",
                new[] {tree},
                metadataReferences);

            var semanticModel = compilation.GetSemanticModel(tree);
            var condition = semanticModel.SyntaxTree.GetRoot().DescendantNodesAndSelf()
                .OfType<IfStatementSyntax>()
                .First()
                .Condition;

            var symbolInfo = semanticModel.GetSymbolInfo(condition);
        }
    }
}
symbolInfo.Symbol is null, and symbolInfo.CandidateReason is OverloadResolutionFailure. This seems to be a bug. The code in the syntax tree seems fine, so there doesn't seem to be any reason for a failure here.

file attachments

comments

OmerRaviv2 wrote Feb 25, 2015 at 1:32 PM

Ported this over to GitHub - https://github.com/dotnet/roslyn/issues/855 .