dotnet/roslyn

Explicit conversion in foreach is incorrect

Aperta

#35.918 aperta il 23 mag 2019

 (0 commenti) (0 reazioni) (0 assegnatari)C# (4257 fork)batch import
Area-CompilersBughelp wanted

Metriche repository

Star
 (20.414 stelle)
Metriche merge PR
 (Merge medio 6g 17h) (256 PR mergiate in 30 g)

Descrizione

See https://github.com/dotnet/roslyn/pull/35095#discussion_r277894589 for context

For an explicit conversion from foreach, the expected value should be NoConversion when the current type is explicit and the enumerable type is unknown

[@gafter added] The context is a test with the source code

class C1
{
}

class C2
{
    public void M() 
    {
        var c = new C1();
        foreach (string item in c.Items)
        {
        }
}

which tests

            var comp = CreateCompilation(source);
            var tree = comp.SyntaxTrees.Single();
            var model = comp.GetSemanticModel(tree);

            var root = tree.GetRoot();
            var foreachSyntaxNode = root.DescendantNodes().OfType<ForEachStatementSyntax>().Single();
            var foreachSymbolInfo = model.GetForEachStatementInfo(foreachSyntaxNode);

            Assert.Equal(Conversion.UnsetConversion, foreachSymbolInfo.CurrentConversion);
            Assert.True(foreachSymbolInfo.CurrentConversion.Exists);
            Assert.False(foreachSymbolInfo.CurrentConversion.IsImplicit);

and the comment was

I'd expect the conversion to be Conversion.NoConversion (and Exists to be false).

I think that was changed by PR https://github.com/dotnet/roslyn/pull/33648 which introduced UnsetConversion, but not intentionally.

Guida contributor