dotnet/roslyn

Explicit conversion in foreach is incorrect

オープン

#35,918 opened on 2019/05/23

 (0 件のコメント) (0 件のリアクション) (0 人の担当者)C# (4,257 件のフォーク)batch import
Area-CompilersBughelp wanted

Repository metrics

Stars
 (20,414 個のスター)
PR merge metrics
 (平均マージ 6d 17h) (30d で 256 merged PRs)

説明

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.

コントリビューターガイド