Skip to content
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

Merged
merged 1 commit into from
Jun 12, 2018
Merged

Conversation

jcouv
Copy link
Member

@jcouv jcouv commented Jun 10, 2018

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.

@jcouv jcouv added the Area-IDE label Jun 10, 2018
@jcouv jcouv added this to the 15.8 milestone Jun 10, 2018
@jcouv jcouv self-assigned this Jun 10, 2018
@jcouv jcouv requested a review from a team as a code owner June 10, 2018 20:14
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;
Copy link
Member

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')?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tagging @agocke @gafter to confirm expected semantic model behavior on local functions (they currently have IsStatic = true even if the enclosing method isn't static).

Copy link
Member

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.

Copy link
Member Author

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 return false, but currently always returns true.
  • given that, the IDE should not consider LocalFunctionSymbol.IsStatic to decide whether this can be used in the current context.

Given that, I will revert this PR to my original fix.

Copy link
Member

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?

Copy link
Member Author

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) ;-)

@jcouv jcouv added the PR For Personal Review Only The PR doesn’t require anyone other than the developer to review it. label Jun 11, 2018
@jcouv jcouv requested a review from a team as a code owner June 11, 2018 14:35
@jcouv jcouv force-pushed the recommend-this branch 2 times, most recently from 82d915d to 3a06d09 Compare June 11, 2018 19:11
@jcouv jcouv removed the PR For Personal Review Only The PR doesn’t require anyone other than the developer to review it. label Jun 11, 2018
@jcouv
Copy link
Member Author

jcouv commented Jun 11, 2018

@dotnet/roslyn-ide for a tiny review. Thanks

@jcouv
Copy link
Member Author

jcouv commented Jun 12, 2018

Thanks!
@jinujoseph for ask-mode approval for 15.8.

@jinujoseph
Copy link
Contributor

Approved to merge for 15.8.Preview4

@jcouv jcouv merged commit 98a1eab into dotnet:master Jun 12, 2018
@jcouv jcouv deleted the recommend-this branch June 12, 2018 21:40
@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about MethodKind.AnonymousFunction?

Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Completion: this not offered when typing in local function
6 participants