-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Analyze flow in receiver of extension method group in delegate creation #60093
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1513,15 +1513,15 @@ public override BoundNode VisitDelegateCreationExpression(BoundDelegateCreationE | |
var methodGroup = node.Argument as BoundMethodGroup; | ||
if (methodGroup != null) | ||
{ | ||
if ((object)node.MethodOpt != null && node.MethodOpt.RequiresInstanceReceiver) | ||
if (node.MethodOpt?.OriginalDefinition is LocalFunctionSymbol localFunc) | ||
{ | ||
EnterRegionIfNeeded(methodGroup); | ||
VisitRvalue(methodGroup.ReceiverOpt); | ||
LeaveRegionIfNeeded(methodGroup); | ||
VisitLocalFunctionUse(localFunc, node.Syntax, isCall: false); | ||
} | ||
else if (node.MethodOpt?.OriginalDefinition is LocalFunctionSymbol localFunc) | ||
else if (node.MethodOpt is { } method && methodGroup.ReceiverOpt is { } receiver && !ignoreReceiver(receiver, method)) | ||
{ | ||
VisitLocalFunctionUse(localFunc, node.Syntax, isCall: false); | ||
EnterRegionIfNeeded(methodGroup); | ||
VisitRvalue(receiver); | ||
LeaveRegionIfNeeded(methodGroup); | ||
} | ||
} | ||
else | ||
|
@@ -1530,6 +1530,12 @@ public override BoundNode VisitDelegateCreationExpression(BoundDelegateCreationE | |
} | ||
|
||
return null; | ||
|
||
static bool ignoreReceiver(BoundExpression receiver, MethodSymbol method) | ||
{ | ||
// ignore the implicit `this` receiver on a static method | ||
return method.IsStatic && receiver is { Kind: BoundKind.ThisReference, WasCompilerGenerated: true }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That might be too broad. AnalyzeDataFlow should still see the invalid receiver as being read for extract method to work as intended. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I am not sure if I agree with that. Clearly, the receiver for static methods (excluding extension scenario) is not going to be evaluated, so I think we have a flexibility in terms of how flow analysis should work in the error scenario. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general, I do not see us actually making sure that region analysis will work for any misplaced expression. For example, there are |
||
} | ||
} | ||
|
||
public override BoundNode VisitTypeExpression(BoundTypeExpression node) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps we should add a comment here:
we may need to visit the receiver / can't ignore static methods at all because of extension methods.