Skip to content

Commit eca246b

Browse files
committed
Fix regression
1 parent c4006e1 commit eca246b

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/Compilers/CSharp/Portable/FlowAnalysis/AbstractFlowPass.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1517,7 +1517,7 @@ public override BoundNode VisitDelegateCreationExpression(BoundDelegateCreationE
15171517
{
15181518
VisitLocalFunctionUse(localFunc, node.Syntax, isCall: false);
15191519
}
1520-
else if (node.MethodOpt is not null && methodGroup.ReceiverOpt is not null)
1520+
else if (node.MethodOpt is { } method && methodGroup.ReceiverOpt is { } receiver && !ignoreReceiver(receiver, method))
15211521
{
15221522
EnterRegionIfNeeded(methodGroup);
15231523
VisitRvalue(methodGroup.ReceiverOpt);
@@ -1530,6 +1530,12 @@ public override BoundNode VisitDelegateCreationExpression(BoundDelegateCreationE
15301530
}
15311531

15321532
return null;
1533+
1534+
static bool ignoreReceiver(BoundExpression receiver, MethodSymbol method)
1535+
{
1536+
// ignore the implicit `this` receiver on a static method
1537+
return method.IsStatic && receiver is { Kind: BoundKind.ThisReference, WasCompilerGenerated: true };
1538+
}
15331539
}
15341540

15351541
public override BoundNode VisitTypeExpression(BoundTypeExpression node)

src/Compilers/CSharp/Test/Semantic/FlowAnalysis/RegionAnalysisTests.cs

+49
Original file line numberDiff line numberDiff line change
@@ -9309,5 +9309,54 @@ static class Extension
93099309
Diagnostic(ErrorCode.ERR_UseDefViolation, "i").WithArguments("i").WithLocation(4, 42)
93109310
);
93119311
}
9312+
9313+
[Fact, WorkItem(59738, "https://github.com/dotnet/roslyn/issues/59738")]
9314+
public void DefiniteAssignmentShouldSkipImplicitThisInStaticMethodConversion()
9315+
{
9316+
var comp = CreateCompilation(@"
9317+
using System;
9318+
public struct C
9319+
{
9320+
private object field;
9321+
public C(Action a)
9322+
{
9323+
// implicit `this` receiver should be ignored in definite assignment
9324+
a = new(M);
9325+
field = 1;
9326+
}
9327+
9328+
public C(Action a, int ignored)
9329+
{
9330+
// implicit `this` receiver should be ignored in definite assignment
9331+
a = new Action(M);
9332+
field = 1;
9333+
}
9334+
9335+
public void Method1(Action a)
9336+
{
9337+
// explicit `this` disallowed
9338+
a = new Action(this.M);
9339+
}
9340+
9341+
public void Method2(Action a, C c)
9342+
{
9343+
// instance receiver disallowed
9344+
a = new Action(c.M);
9345+
}
9346+
9347+
private static void M()
9348+
{
9349+
}
9350+
}
9351+
");
9352+
comp.VerifyDiagnostics(
9353+
// (23,24): error CS0176: Member 'C.M()' cannot be accessed with an instance reference; qualify it with a type name instead
9354+
// a = new Action(this.M);
9355+
Diagnostic(ErrorCode.ERR_ObjectProhibited, "this.M").WithArguments("C.M()").WithLocation(23, 24),
9356+
// (29,24): error CS0176: Member 'C.M()' cannot be accessed with an instance reference; qualify it with a type name instead
9357+
// a = new Action(c.M);
9358+
Diagnostic(ErrorCode.ERR_ObjectProhibited, "c.M").WithArguments("C.M()").WithLocation(29, 24)
9359+
);
9360+
}
93129361
}
93139362
}

0 commit comments

Comments
 (0)