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

Improve sharing of trees when changing project configuration #65974

Merged
merged 3 commits into from
Dec 14, 2022

Conversation

CyrusNajmabadi
Copy link
Member

No description provided.

[InlineData("// File", true, LanguageNames.CSharp)]
[InlineData("#if DEBUG", false, LanguageNames.VisualBasic)]
[InlineData(@"#region ""goo""", true, LanguageNames.VisualBasic)]
Copy link
Member Author

@CyrusNajmabadi CyrusNajmabadi Dec 13, 2022

Choose a reason for hiding this comment

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

woot. now documents with other pp directives can be reused when changing project configs. In roslyn this is ~4000 docs.

if (onlyPreprocessorDirectiveChange &&
_treeSource.TryGetValue(out var existingTreeAndVersion))
{
var existingTree = existingTreeAndVersion.Tree;

SyntaxTree? newTree = null;

if (existingTree.TryGetRoot(out var existingRoot) && !existingRoot.ContainsDirectives)
var syntaxKinds = _languageServices.GetRequiredService<ISyntaxKindsService>();
if (existingTree.TryGetRoot(out var existingRoot) && !existingRoot.ContainsDirective(syntaxKinds.IfDirectiveTrivia))
Copy link
Member Author

Choose a reason for hiding this comment

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

utilizes new compiler API we added for this exact purpose. .ContainsDirectives is too broad, firing even for things that do not affect parsing. .ContainsDirective(SyntaxKind.IfDirectiveTrivia) means we only fire on trees hat could genuinely parse differently.

Copy link
Member

Choose a reason for hiding this comment

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

Is there any broken code scenarios we need to worry about, like there's not an #if but there's an #elif that still results in different trees? I sure don't care about optimizing this case; this is entirely a "I hope we don't crash" question.

if (onlyPreprocessorDirectiveChange &&
_treeSource.TryGetValue(out var existingTreeAndVersion))
{
var existingTree = existingTreeAndVersion.Tree;

SyntaxTree? newTree = null;

if (existingTree.TryGetRoot(out var existingRoot) && !existingRoot.ContainsDirectives)
var syntaxKinds = _languageServices.GetRequiredService<ISyntaxKindsService>();
if (existingTree.TryGetRoot(out var existingRoot) && !existingRoot.ContainsDirective(syntaxKinds.IfDirectiveTrivia))
Copy link
Member

Choose a reason for hiding this comment

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

Is there any broken code scenarios we need to worry about, like there's not an #if but there's an #elif that still results in different trees? I sure don't care about optimizing this case; this is entirely a "I hope we don't crash" question.

@CyrusNajmabadi
Copy link
Member Author

Is there any broken code scenarios we need to worry about, like there's not an #if but there's an #elif that still results in different trees? I sure don't care about optimizing this case; this is entirely a "I hope we don't crash" question.

Tested at compiler layer already.

Note: elif without an if produces a BadDirectiveSyntax. That said, even if it didn't, it wouldn't matter as it's fine in error cases for the trees to share that same bad tree. I'll add tests though to demontrate we don't crash.

@CyrusNajmabadi CyrusNajmabadi merged commit 3cd5509 into dotnet:main Dec 14, 2022
@ghost ghost added this to the Next milestone Dec 14, 2022
@CyrusNajmabadi CyrusNajmabadi deleted the betterSharing branch December 14, 2022 17:37
@Cosifne Cosifne modified the milestones: Next, 17.5 P3 Jan 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants