提交 6dd3ecd5 编写于 作者: A Andy Gocke 提交者: GitHub

Change visit order for local function definite assignment (#14835)

This change visits the parameters before "replaying" reads+writes from
calls of local functions for the purposes of definite assignment. This
ordering is observable because a variable can be definitely assigned in
the argument to the call, and then used inside the call.

Fixes #14243
上级 825044fb
...@@ -1761,12 +1761,16 @@ public override BoundNode VisitLocalDeclaration(BoundLocalDeclaration node) ...@@ -1761,12 +1761,16 @@ public override BoundNode VisitLocalDeclaration(BoundLocalDeclaration node)
public override BoundNode VisitCall(BoundCall node) public override BoundNode VisitCall(BoundCall node)
{ {
// Always visit the arguments first
var result = base.VisitCall(node);
if (node.Method.MethodKind == MethodKind.LocalFunction) if (node.Method.MethodKind == MethodKind.LocalFunction)
{ {
var localFunc = (LocalFunctionSymbol)node.Method.OriginalDefinition; var localFunc = (LocalFunctionSymbol)node.Method.OriginalDefinition;
ReplayReadsAndWrites(localFunc, node.Syntax, writes: true); ReplayReadsAndWrites(localFunc, node.Syntax, writes: true);
} }
return base.VisitCall(node);
return result;
} }
public override BoundNode VisitConversion(BoundConversion node) public override BoundNode VisitConversion(BoundConversion node)
......
...@@ -7,6 +7,38 @@ namespace Microsoft.CodeAnalysis.CSharp.UnitTests ...@@ -7,6 +7,38 @@ namespace Microsoft.CodeAnalysis.CSharp.UnitTests
[CompilerTrait(CompilerFeature.LocalFunctions)] [CompilerTrait(CompilerFeature.LocalFunctions)]
public class LocalFunctions : FlowTestBase public class LocalFunctions : FlowTestBase
{ {
[Fact]
[WorkItem(14243, "https://github.com/dotnet/roslyn/issues/14243")]
public void AssignInsideCallToLocalFunc()
{
var comp = CreateCompilationWithMscorlib(@"
class C
{
public void M()
{
int x;
int Local(int p1) => x++;
Local(x = 0);
int z;
int Local2(int p1, int p2) => z++;
Local2(z = 0, z++);
}
public void M2()
{
int x;
int Local(int p1) => x++;
int Local2(int p1) => Local(p1);
int Local3(int p1) => x + Local2(p1);
Local3(x = 0);
}
}");
comp.VerifyDiagnostics();
}
[Fact] [Fact]
[WorkItem(14046, "https://github.com/dotnet/roslyn/issues/14046")] [WorkItem(14046, "https://github.com/dotnet/roslyn/issues/14046")]
public void UnreachableAfterThrow() public void UnreachableAfterThrow()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册