-
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
Conversation
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 comment
The 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 comment
The 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
From discussion with @agocke and @AlekseyTs, there are two bugs:
- the compiler
LocalFunctionSymbol.IsStatic
should always returnfalse
, but currently always returnstrue
. - given that, the IDE should not consider
LocalFunctionSymbol.IsStatic
to decide whetherthis
can be used in the current context.
Given that, I will revert this PR to my original fix.
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.
the compiler LocalFunctionSymbol.IsStatic should always return false, but currently always returns true.
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 comment
The 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 true
after all) ;-)
82d915d
to
3a06d09
Compare
@dotnet/roslyn-ide for a tiny review. Thanks |
Thanks! |
Approved to merge for 15.8.Preview4 |
@@ -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) |
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
Customer scenario
Try to type
this
inside a local function that is inside an instance method.this
should be offered as a completion.Bugs this fixes
Fixes #27028
Workarounds, if any
Type
this
in full.Risk
Performance impact
Low. The change only impacts the case of typing inside a local function.
Is this a regression from a previous update?
No.
Root cause analysis
The semantic model shows local functions as static (since that is how they are emitted).
How was the bug found?
Internally.