未验证 提交 832d21f9 编写于 作者: H Heejae Chang 提交者: GitHub

made "foreach to for" to only support rank1 array. (#26750)

otherwise, it will pick up IList but Array implementation only support Rank==1 on runtime even if there is no compile time error.
上级 335268ce
......@@ -1721,5 +1721,23 @@ class Explicit : IReadOnlyList<int>
";
await TestInRegularAndScriptAsync(text, expected, options: ImplicitTypeEverywhere);
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsConvertForEachToFor)]
public async Task ArrayRank2()
{
var text = @"
class Test
{
void Method()
{
foreach [||] (int a in new int[,] { {1, 2} })
{
Console.WriteLine(a);
}
}
}
";
await TestMissingAsync(text);
}
}
}
......@@ -217,8 +217,17 @@ public override async Task ComputeRefactoringsAsync(CodeRefactoringContext conte
// go through explicit types first.
// check array case
if (collectionType is IArrayTypeSymbol array && array.Rank == 1)
if (collectionType is IArrayTypeSymbol array)
{
if (array.Rank != 1)
{
// array type supports IList and other interfaces, but implementation
// only supports Rank == 1 case. other case, it will throw on runtime
// even if there is no error on compile time.
// we explicitly mark that we only support Rank == 1 case
return;
}
if (!IsExchangable(semanticFact, array.ElementType, foreachType, model.Compilation))
{
return;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册