dotnet/roslyn

Explicit conversion in foreach is incorrect

已关闭

#35,918 创建于 2019年5月23日

 (0 条评论) (0 个反应) (0 位负责人)C# (4,257 个派生)batch import
Area-CompilersBughelp wanted

仓库指标

星标
 (20,414 个星标)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 256 个 PR)

描述

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.

贡献者指南