Skip to content

Extract 'use var' from 'simplify type names'#40535

Merged
sharwell merged 16 commits intodotnet:masterfrom
CyrusNajmabadi:simplifyTypeNamesWork
Dec 30, 2019
Merged

Extract 'use var' from 'simplify type names'#40535
sharwell merged 16 commits intodotnet:masterfrom
CyrusNajmabadi:simplifyTypeNamesWork

Conversation

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

@CyrusNajmabadi CyrusNajmabadi commented Dec 20, 2019

Fixes #27819

Note: the problem is as follows: If you have the use var setting enabled, then both the Use var analyzer and the Simplify type names analyzer fire for types and offer to simplify to var.

For example: If you write System.Int32 you will get two diagnostics:

image

image

Both these offer the exact same thing. This PR separates out the code from simplify-type-names so it never does 'var' replacement. That's at least better than our current state. However, there's still a slight open question on what we want the experience to be.

Specifically, in this case:

  1. should we only offer use var, and not offer simplify type names at all? or
  2. should we offer use var and offer simplify type names with the simplification producing int.

I've taken the approach of 2 under the thinking that both are valid and offer two separate changes the user might want in a case like this. However, i can be convinced that '1' is the approach we should go.

--

Note: this is also part of a larger effort i want to take to improve the performance of hte "simplify type names" feature. Right now it's super slow, and i'm trying to make it more amenable to optimizing by simplifying and reducing what it does.

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner December 20, 2019 21:50
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

called throuh two codepaths. one is only called from SimplifyTypeNames. The other is called through a codepath from simplify type names and by CSharpNameReducer. So i removed this entirely from the simplification codepaths, but then added into the reduction codepath with a specialized reducer just for this purpose.

@CyrusNajmabadi CyrusNajmabadi changed the title WIP - Extract 'use var' from 'simplify type names' Extract 'use var' from 'simplify type names' Dec 21, 2019
@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

@dotnet/roslyn-ide This is ready for review. Not hte open question in the original post.

public override bool IsApplicable(OptionSet optionSet)
=> optionSet.GetOption(CSharpCodeStyleOptions.VarForBuiltInTypes).Value ||
optionSet.GetOption(CSharpCodeStyleOptions.VarWhenTypeIsApparent).Value ||
optionSet.GetOption(CSharpCodeStyleOptions.VarElsewhere).Value;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

don't bother running us if the user never even wants 'var'.

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

tagging @dpoeschl @jinujoseph can this get a buddy?

@jnm2
Copy link
Copy Markdown
Contributor

jnm2 commented Dec 21, 2019

Specifically, in this case:

  1. should we only offer use var, and not offer simplify type names at all? or

  2. should we offer use var and offer simplify type names with the simplification producing int.

They feel to me like things that should be presented as independent. Tying them together would be to make the assumption that a fix for one analyzer/preference should not produce code that another analyzer/preference would have an opinion on. That assumption wouldn't match the general picture, would it?

// ever want the simplifier to produce 'var' in the 'Simplify type
// names' fixer. only the 'Use var' fixer should produce 'var'.
var annotatedexpressionSyntax = expressionSyntax.WithAdditionalAnnotations(
Simplifier.Annotation, Formatter.Annotation, DoNotAllowVarAnnotation.Annotation);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

note: the fact that SimplifyTypeNames works by acutally simplifying during analysis, and then doing and just rerunning the full simplifier is pretty awful. I intend to clean this up a lot in a later PRs. For now, this is a nice and simple way to say: SimplifyTypeNames should not produce var, even if it thinks it can simplify the name of something.


namespace Microsoft.CodeAnalysis.CSharp.Simplification
{
internal partial class CSharpVarReducer
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this is now a nice and tiny type with the isolated logic to try to convert a TypeSyntax to 'var' during normal simplification (i.e. the Simplification subsystem, not the SimplifyTypeNames feature). It's single-purpose and easy to understand, and isn't just interspersed randomly into other parts of the infrastructure.

internal partial class CSharpTypeStyleHelper
{
protected class State
protected struct State
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

An instance of this is produced for every node we want ot try to simplify/expand 'var' for. No need to allocate here.

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

They feel to me like things that should be presented as independent.

Agreed. Thanks for the feedback!

@sharwell
Copy link
Copy Markdown
Contributor

I have no issue with proposal 2 (simplify type names does not consider 'var' as a simplified name). However, we need to ensure that Simplifier.Annotation still replaces code with var for code generation scenarios.

Comment thread src/Workspaces/CSharp/Portable/Utilities/CSharpTypeStyleHelper.State.cs Outdated
@sharwell sharwell added Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee. labels Dec 21, 2019
@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

However, we need to ensure that Simplifier.Annotation still replaces code with var for code generation scenarios.

We have test for this everywhere. For example: There are tests in convert-foreach-to-for (and back), in 'introduce-local', convert-to-conditional. And we just have TypeNameSimplifierTests which tests precicely this :)

Copy link
Copy Markdown
Contributor Author

@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.

c

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

@sharwell can we move forward with this? thanks!

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

@sharwell perf numbers:

Before:
CSharpSimplifyTypeNamesDiagnosticAnalyzer: 8540910
  IDE0001: 1158 instances
  IDE0002: 1663 instances
  IDE0049: 985 instances

After:
CSharpSimplifyTypeNamesDiagnosticAnalyzer: 7161555
  IDE0001: 1149 instances
  IDE0002: 1655 instances
  IDE0049: 984 instances

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

@sharwell can we move forward with this? Thanks!

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.

Three questions

Comment thread src/Workspaces/CSharp/Portable/Utilities/CSharpTypeStyleHelper.State.cs Outdated
Copy link
Copy Markdown
Contributor Author

@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.

c

@sharwell sharwell merged commit 136edb1 into dotnet:master Dec 30, 2019
@sharwell sharwell added this to the 16.5.P2 milestone Dec 30, 2019
@CyrusNajmabadi
Copy link
Copy Markdown
Contributor Author

Thanks!

@CyrusNajmabadi CyrusNajmabadi deleted the simplifyTypeNamesWork branch December 30, 2019 18:48
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Simplify name" has started enforcing var & this. usage opposite to editorconfig settings

4 participants