This project is read-only.
1

Resolved

Probable bug in SemanticModel.GetCollectionInitializerSymbolInfo()

description

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.

comments

angocke wrote Dec 4, 2014 at 9:06 PM

Fixed in changeset 422201a53cc6e1429c778d777839fa2726aec660