Skip to content

Add a code refactoring to convert local function to method#16808

Merged
sharwell merged 23 commits intodotnet:masterfrom
alrz:local-func
Aug 2, 2018
Merged

Add a code refactoring to convert local function to method#16808
sharwell merged 23 commits intodotnet:masterfrom
alrz:local-func

Conversation

@alrz
Copy link
Copy Markdown
Member

@alrz alrz commented Jan 27, 2017

Fixes #16288

@alrz
Copy link
Copy Markdown
Member Author

alrz commented Jan 28, 2017

Data-flow analysis fails for no appearant reason. Any thoughts?

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

@agocke May know more about data-flow.

@agocke
Copy link
Copy Markdown
Member

agocke commented Jan 29, 2017

@alrz Local functions currently aren't supported in region analysis. #14214 tracks fixing it.

@Pilchie Pilchie added this to the 2.1 milestone Jan 30, 2017
@agocke agocke changed the base branch from post-dev15 to master February 14, 2017 23:24
@jaredpar jaredpar modified the milestones: 15.1, 15.later Jun 27, 2017
@sharwell sharwell added Community The pull request was submitted by a contributor who is not a Microsoft employee. and removed Community The pull request was submitted by a contributor who is not a Microsoft employee. labels Aug 31, 2017
@Pilchie Pilchie requested review from a team and kuhlenh September 8, 2017 15:15
@alrz
Copy link
Copy Markdown
Member Author

alrz commented Sep 13, 2017

Since data-flow analysis on local functions is back, I synced the PR and added more tests. This is still a work-in-progress (see the opening description). Any early feedback is appreciated, though. thanks.

/cc @sharwell

@alrz
Copy link
Copy Markdown
Member Author

alrz commented Sep 20, 2017

Due to capturing, we might end up changing the behavior of the code e.g. examples are identical.

object M(object arg)
{
  object Local() => arg;
  arg = null;
  return Local();
}

->

object M(object arg)
{
  arg = null;
  return Local(arg);
}

private static object Local(object arg) => arg;

Do we want to not suggest the refactoring in this case? Thoughts?

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

It's a refactoring. So i feel they should be available to the user as their absence can be confusing. Since we're not going to be doin ga fix-all here it's not a problem for me. I do think that it would make sense to give a warning.

Copy link
Copy Markdown
Member

@DustinCampbell DustinCampbell left a comment

Choose a reason for hiding this comment

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

Just took a brief look through.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd love to see the type parameters reduced to just the ones that are used.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What about parameters from enclosing methods that are captured?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I expect this return those too! I'll add a test.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd recommend typeParameterList since there's a method.Decl.ParameterList property.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Again, typeParameterList is probably better for more readable code.

@alrz alrz force-pushed the local-func branch 2 times, most recently from 486d5f7 to f53bcb2 Compare September 22, 2017 17:40
@alrz
Copy link
Copy Markdown
Member Author

alrz commented Nov 18, 2017

@sharwell @CyrusNajmabadi would love to hear your feedback on this. made progress but the code needs serious cleanup. thanks.

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

Looking.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not a huge fan of this. Specifically, that it makes this code different than the rest of our features. I'd prefer another PR that changes the features layer to use this pattern FWIW.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

perhaps offer on the name of the local function as well.

Copy link
Copy Markdown
Member Author

@alrz alrz Nov 19, 2017

Choose a reason for hiding this comment

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

I'll add test for [||]LocalName(), Local[||]Name(), [|LocalName|]() and LocalName[||](). And negative case for other places.

@alrz
Copy link
Copy Markdown
Member Author

alrz commented May 11, 2018

@jcouv This is no longer blocked. Can you please remove the label so I can get a review? Thanks.

@jcouv jcouv removed the Blocked label May 11, 2018
@jcouv
Copy link
Copy Markdown
Member

jcouv commented May 11, 2018

Removed blocked label.

<value>Convert to regular method</value>
</data>
<data name="Convert_to_method" xml:space="preserve">
<value>Convert to method</value>
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

what is the difference between a method and a regular method?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Wait, I recall reverting to "Convert to method". The previous string is not supposed to be there.

return;
}

context.RegisterRefactoring(new MyCodeAction(c => UpdateDocumentAsync(root, document, parentBlock, localFunction, c)));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: wrap.

@alrz
Copy link
Copy Markdown
Member Author

alrz commented Jun 2, 2018

@jinujoseph @sharwell What is the state of this PR? I haven't heard back from the team. please let me know if this needs any further work.

Copy link
Copy Markdown
Contributor

@sharwell sharwell left a comment

Choose a reason for hiding this comment

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

This PR has an extraneous value is the resources file that needs to be removed, but is otherwise ready once tests pass and any merge conflicts are addressed.

<data name="Convert_to_for" xml:space="preserve">
<value>Convert to 'for'</value>
</data>
<data name="Convert_to_regular_method" xml:space="preserve">
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

❗️ Need to remove this extraneous value

@alrz
Copy link
Copy Markdown
Member Author

alrz commented Jun 5, 2018

There's an oddity with flow analysis and structs in which they are decided to be written into even if they are not (e.g. when they are only dotted-off). @sharwell, do you know why is that the case?

@jinujoseph
Copy link
Copy Markdown
Contributor

cc @mavasani

@jinujoseph jinujoseph modified the milestones: 15.8, 16.0 Jun 26, 2018
@sharwell
Copy link
Copy Markdown
Contributor

sharwell commented Aug 2, 2018

There's an oddity with flow analysis and structs in which they are decided to be written into even if they are not (e.g. when they are only dotted-off).

Instance properties and methods in a value type can reassign this.

@sharwell sharwell merged commit f251e7a into dotnet:master Aug 2, 2018
@alrz alrz deleted the local-func branch August 2, 2018 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-IDE cla-already-signed Community The pull request was submitted by a contributor who is not a Microsoft employee. Feature Specification

Projects

None yet

Development

Successfully merging this pull request may close these issues.