-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Recommend 'this' inside local functions #27690
Changes from all 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 |
---|---|---|
|
@@ -1771,6 +1771,13 @@ public static bool IsInstanceContext(this SyntaxTree syntaxTree, SyntaxToken tar | |
#endif | ||
|
||
var enclosingSymbol = semanticModel.GetEnclosingSymbol(targetToken.SpanStart, cancellationToken); | ||
|
||
while (enclosingSymbol is IMethodSymbol method && method.MethodKind == MethodKind.LocalFunction) | ||
{ | ||
// It is allowed to reference the instance (`this`) within a local function, as long as the containing method allows it | ||
enclosingSymbol = method.ContainingSymbol; | ||
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 find this fix surprising. Is a local function treated as static (even though it can use 'this')? 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. Sounds wrong to me. If they're emitted as static methods, that's just a codegen decision. 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. From discussion with @agocke and @AlekseyTs, there are two bugs:
Given that, I will revert this PR to my original fix. 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.
Not disagreeing... but can an explanation be provided as to why? This seems similar to props+accessors. Accessors can't have static/non-static info on them. They just 'inherit' from the containing prop. Seems similar to local-functions/containers. And it would seem to make the most sense. It would also tie into one of the main concepts of Static/Instance (namely, whether or not you can use 'this'..). Thoughts? 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. It looks like there is some discussion going on for the compiler side (maybe it should always be |
||
} | ||
|
||
return !enclosingSymbol.IsStatic; | ||
} | ||
|
||
|
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.
what about
MethodKind.AnonymousFunction
?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.
it seems
this
is always offered inside them regardless of whether the containing method is static