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

Introduce using statement #30106

Merged
merged 14 commits into from
Nov 2, 2018
Merged

Conversation

jnm2
Copy link
Contributor

@jnm2 jnm2 commented Sep 22, 2018

@jnm2 jnm2 requested review from a team as code owners September 22, 2018 23:34
@jnm2 jnm2 force-pushed the introduce_using_statement branch from d1dd366 to 08c88b5 Compare September 22, 2018 23:36
@jnm2 jnm2 force-pushed the introduce_using_statement branch from 08c88b5 to 391c2f4 Compare September 22, 2018 23:54
Copy link
Member

@CyrusNajmabadi CyrusNajmabadi left a comment

Choose a reason for hiding this comment

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

request changes.

CancellationToken cancellationToken)

where TStatementSyntax : SyntaxNode
{
Copy link
Member

Choose a reason for hiding this comment

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

an alternative approach that might would would be to get the parent, walk backward to find the last reference. That itself could be a generic helper elsewhere.

Then, when you have the node for the last reference, you simply ask for it's ancestor node that shares your parent. that would be your sibling node.

Copy link
Contributor Author

@jnm2 jnm2 Sep 23, 2018

Choose a reason for hiding this comment

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

The granularity of the search is at the sibling statement level, so this wouldn't provide any additional performance boost overall. It doesn't matter within each sibling statement whether we walk forwards or backwards. In fact, perf would probably go down due to the extra bookkeeping.

Copy link
Member

Choose a reason for hiding this comment

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

It was not about a performance boost. it was about building this out of more general helpers with a simple and clear algorithm.

Copy link
Member

Choose a reason for hiding this comment

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

Specifically, all you'd need was a helper to find the last-ref of a local in a specified node. That seems generally useful and sane to have as a general helper.

Then you could simply use the result of that with exsiting code we have to give the specific answer of "last sibling statement".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay. I'm not ready to generalize yet. Can I keep this PR more concrete?

@jnm2 jnm2 force-pushed the introduce_using_statement branch 5 times, most recently from 49f24a9 to 5ed48b3 Compare September 23, 2018 01:59
@jnm2
Copy link
Contributor Author

jnm2 commented Sep 23, 2018

Pushed all interesting logic into the abstract class and got all pre-existing tests passing.

Next up: writing more test cases.

protected override BlockSyntax WithStatements(BlockSyntax blockSyntax, SyntaxList<StatementSyntax> statements)
{
return blockSyntax.WithStatements(statements);
}
Copy link
Member

Choose a reason for hiding this comment

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

Note: CSharpIntroduceVariableService_IntroduceLocal has helpers for these (which also do the right thing for switch-cases). We should likely put those somewhere and use for both of these.

Copy link
Member

Choose a reason for hiding this comment

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

Note: SyntaxGenerator would be a good place for this as it already handles getting/adding statements to members.

Copy link
Contributor Author

@jnm2 jnm2 Oct 7, 2018

Choose a reason for hiding this comment

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

@CyrusNajmabadi Ready to do this, but where would IsBlockLike go? SyntaxGenerator? C# SyntaxFacts is public API and internal members aren't accessible to this assembly. I looked at doing it on the syntax facts language service, but VB doesn't have standalone blocks so I'd have to do a ton of work to get statements from every kind of syntax.

Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with SyntaxGenerator being used for this.

Copy link
Member

Choose a reason for hiding this comment

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

this does'nt seem to have been done.

Copy link
Member

Choose a reason for hiding this comment

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

this doesn't seem to have been done.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@CyrusNajmabadi I'm actually worried that my current methods aren't general enough for this. However, maybe this is the key.

@jnm2 jnm2 force-pushed the introduce_using_statement branch 3 times, most recently from 5b7b166 to c19f07d Compare November 2, 2018 00:31
@jnm2
Copy link
Contributor Author

jnm2 commented Nov 2, 2018

@jinujoseph @JoeRobich I resolved the merge conflicts. Let me know if there's anything else you'd like me to do!

@JoeRobich
Copy link
Member

@jnm2 After looking at the integration failures, it seems that the xlf files weren't rebuilt after making some resx changes. This is pretty common if you were fixing merge conflicts in the resx. Could you rebuild the solution and commit the rebuilt xlf files? Thanks!

@jnm2 jnm2 force-pushed the introduce_using_statement branch from c19f07d to 039ac27 Compare November 2, 2018 01:14
@jnm2
Copy link
Contributor Author

jnm2 commented Nov 2, 2018

Oops, fixed!

@JoeRobich
Copy link
Member

@jnm2 Thanks! Assuming everything ends up green I will merge this in the morning.

While playing with it some more, a separate enhancement PR might be to remove the final usage of the disposable variable, if it is an invocation of Dispose().

image

@jnm2
Copy link
Contributor Author

jnm2 commented Nov 2, 2018

@JoeRobich Yes, I thought about that! I couldn't imagine running into the scenario myself, so I thought I'd wait and see if people wanted it. (I wouldn't get as far as deciding to type .Dispose() before realizing that I want a using. In practice I decide I want a using statement the moment I learn that the variable I just declared is disposable.)

I think if we did this, we should place the ending brace where the .Dispose() call previously was and move any declarations that became surrounded before the using (while leaving their initialization where it was). So that could be a generally enhancement even before detecting an existing .Dispose() statement: separate declarations from initializers that became surrounded but are used outside the new using block.

@JoeRobich
Copy link
Member

The integration test failure was the IPC Error - #29001 - Build logs

@JoeRobich JoeRobich merged commit 330c20c into dotnet:master Nov 2, 2018
@JoeRobich
Copy link
Member

Thanks @jnm2!

As for future enhancements, when deciding whether to move a declaration out, could we use a similar find last usage and check if it is going to fall within the using body? Even if not, I agree that would help ensure we don't break working code.

@jnm2 jnm2 deleted the introduce_using_statement branch November 2, 2018 15:26
@jnm2
Copy link
Contributor Author

jnm2 commented Nov 2, 2018

@JoeRobich Yes, we would want to do that.

333fred added a commit to 333fred/roslyn that referenced this pull request Nov 7, 2018
…zyT-and-delegates

* dotnet/dev16.0-preview2: (117 commits)
  Do not warn for double nullable supression (dotnet#30936)
  Add -shared and -keepalive compiler options (dotnet#30818)
  Introduce using statement (dotnet#30106)
  Skip BinaryFileErrorTest
  Skip ParseFileOnBinaryFile
  Merge fixup
  Add missing binding redirects to csc.exe.config
  Add comments for default namespace in workspace
  Fix variable name
  Merge fixups
  Add support for default namespace to workspace
  Addressing feedback from Cyrus
  Embed optimization data to external compiler dependencies
  Skip optimizing System.Threading.Tasks.Extensions
  Skip optimizing System.Threading.Tasks.Extensions
  Add cert for PKT=b03f5f7f11d50a3a
  RoslynTools 18526.1
  Fix VS dependency deployment
  Fix msbuild test
  Deploy System.Runtime.CompilerServices.Unsafe
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee. Feature Request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants