Probable bug in SemanticModel.GetCollectionInitializerSymbolInfo()

Topics: APIs
Aug 28, 2014 at 9:48 PM
Edited Aug 28, 2014 at 9:51 PM
I expect this program to output True, but it does not
class Program {
    static void Main(string[] args) {
        var syntaxTree = CSharpSyntaxTree.ParseText(@"
using System;
using System.Collections.Generic;
class Test {
    public List<string> List { get; set; }

    void M() {
        var x = new Test {
            List = { ""Hello"", ""World"" },
        };
    }
}");
        var compilation = CSharpCompilation.Create("Test", new[] { syntaxTree }, new[] { new MetadataFileReference(typeof(object).Assembly.Location) }, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
        var semanticModel = compilation.GetSemanticModel(syntaxTree);
        var root = syntaxTree.GetRoot();
        var objectCreation = root.DescendantNodes().OfType<ObjectCreationExpressionSyntax>().Single();
        var listAssignment = objectCreation.DescendantNodes().OfType<BinaryExpressionSyntax>().Single(be => be.CSharpKind() == SyntaxKind.SimpleAssignmentExpression && be.Left.ToString() == "List");
        var listInitializer = (InitializerExpressionSyntax)listAssignment.Right;
        Console.WriteLine(semanticModel.GetCollectionInitializerSymbolInfo(listInitializer.Expressions[0]).Symbol != null);
        // I expect this to be true, but it is not. 
    }
}
I guess this is because CSharpSemanticModel.GetCollectionInitializerSymbolInfo checks whether the parent syntax is an ObjectCreationExpressionSyntax, but in this case it is an InitializerExpressionSyntax.
Nov 17, 2014 at 9:16 PM
This is not fixed in 1.0.0-beta1-20141031-01. Will it be?
Developer
Nov 18, 2014 at 12:08 AM
This is a bug, could you please open an issue so we can track it?
Nov 18, 2014 at 6:42 AM