Skip to content

Avoid duplicate syntax node callbacks for record declarations - #58482

Merged
mavasani merged 7 commits into
dotnet:mainfrom
mavasani:SyntaxNodeDupe
Jan 25, 2022
Merged

mavasani merged 7 commits into
dotnet:mainfrom
mavasani:SyntaxNodeDupe

Conversation

@mavasani

Copy link
Copy Markdown
Contributor

Fixes #53136

Verified that the added unit test fails with duplicate diagnostics before the product fix.

Fixes dotnet#53136

Verified that the added unit test fails with duplicate diagnostics before the product fix.
@AlekseyTs

Copy link
Copy Markdown
Contributor

@mavasani Do we have similar issue with top-level statements?

@mavasani
mavasani marked this pull request as ready for review January 3, 2022 13:13
@mavasani

mavasani commented Jan 3, 2022

Copy link
Copy Markdown
Contributor Author

Do we have similar issue with top-level statements?

@AlekseyTs - did you mean issue with records as top-level statements or just top-level statements in general? I am not really sure about the existing behavior for either of these, but can verify and add tests as appropriate. It would be helpful if you can also point me to any existing test files with analyzer tests for top-level statements, if you are aware.

@Youssef1313

Copy link
Copy Markdown
Member

@mavasani

private class AnalyzerActions_01_Analyzer : DiagnosticAnalyzer

@Youssef1313

Copy link
Copy Markdown
Member

Here is a seemingly suspicious assert:

var text1 = @"System.Console.WriteLine(1);";
var analyzer = new AnalyzerActions_01_Analyzer();
var comp = CreateCompilation(text1, options: TestOptions.DebugExe, parseOptions: DefaultParseOptions);
comp.GetAnalyzerDiagnostics(new[] { analyzer }, null).Verify();
Assert.Equal(1, analyzer.FireCount1);
Assert.Equal(0, analyzer.FireCount2);
Assert.Equal(1, analyzer.FireCount3);
Assert.Equal(0, analyzer.FireCount4);
Assert.Equal(1, analyzer.FireCount5);
Assert.Equal(0, analyzer.FireCount6);

Specifically, both FireCount3 and FireCount5 are 1

Here is where those counters are incremented:

context.RegisterSyntaxNodeAction(Handle2, SyntaxKind.CompilationUnit);

private void Handle2(SyntaxNodeAnalysisContext context)
{
var model = context.SemanticModel;
var unit = (CompilationUnitSyntax)context.Node;
var syntaxTreeModel = ((SyntaxTreeSemanticModel)model);
switch (unit.ToString())
{
case "System.Console.WriteLine(1);":
Interlocked.Increment(ref context.ContainingSymbol.Kind == SymbolKind.Namespace ? ref FireCount5 : ref FireCount3);
break;

So it looks like Handle2 was called for the same syntax node but different ContainingSymbol.

@mavasani

mavasani commented Jan 3, 2022

Copy link
Copy Markdown
Contributor Author

Here is a seemingly suspicious assert:

var text1 = @"System.Console.WriteLine(1);";
var analyzer = new AnalyzerActions_01_Analyzer();
var comp = CreateCompilation(text1, options: TestOptions.DebugExe, parseOptions: DefaultParseOptions);
comp.GetAnalyzerDiagnostics(new[] { analyzer }, null).Verify();
Assert.Equal(1, analyzer.FireCount1);
Assert.Equal(0, analyzer.FireCount2);
Assert.Equal(1, analyzer.FireCount3);
Assert.Equal(0, analyzer.FireCount4);
Assert.Equal(1, analyzer.FireCount5);
Assert.Equal(0, analyzer.FireCount6);

Specifically, both FireCount3 and FireCount5 are 1

Here is where those counters are incremented:

context.RegisterSyntaxNodeAction(Handle2, SyntaxKind.CompilationUnit);

private void Handle2(SyntaxNodeAnalysisContext context)
{
var model = context.SemanticModel;
var unit = (CompilationUnitSyntax)context.Node;
var syntaxTreeModel = ((SyntaxTreeSemanticModel)model);
switch (unit.ToString())
{
case "System.Console.WriteLine(1);":
Interlocked.Increment(ref context.ContainingSymbol.Kind == SymbolKind.Namespace ? ref FireCount5 : ref FireCount3);
break;

So it looks like Handle2 was called for the same syntax node but different ContainingSymbol.

Thanks @Youssef1313. That confirms similar issue exists with top-level statements. I am going to create a separate tracking issue and PR to address that one.

@Youssef1313 Youssef1313 left a comment •

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 think it's worth adding tests (or opening issues after confirming there is an issue) for syntax checked in this method:

internal override Func<SyntaxNode, bool> GetSyntaxNodesToAnalyzeFilter(SyntaxNode declaredNode, ISymbol declaredSymbol)

  • SyntaxKind.CompilationUnit (there are already tests that likely need to be adjusted)
  • SyntaxKind.RecordDeclaration (already done)
  • SyntaxKind.RecordStructDeclaration (already done)
  • SyntaxKind.PrimaryConstructorBaseType
  • SyntaxKind.Parameter

@AlekseyTs

Copy link
Copy Markdown
Contributor

Do we have similar issue with top-level statements?

did you mean issue with records as top-level statements or just top-level statements in general?

No the question is not related to records in any way. I think we might have similar "duplicate syntax node callbacks" problem for top-level statements. In particular around a compilation unit node, it declares a namespace, an entry point method and its containing type at the same time.

@mavasani

mavasani commented Jan 4, 2022

Copy link
Copy Markdown
Contributor Author

Thanks @AlekseyTs - I have filed #58561 for top-level statements.

@AlekseyTs

AlekseyTs commented Jan 6, 2022 •

Copy link
Copy Markdown
Contributor

My intuition tells me that we are likely taking a "wrong" approach for achieving the goal. I feel like a better approach would be to revert relevant parts of #46308 instead of adding special APIs to neutralize their effect. We could simply remove the effect in the first place. I also think that it might make sense to deal with top level statements at the same time and in the same manner (finding the PR that changed the behavior for them), because they are likely to be closely related in terms of implementation strategy. Records probably build on top of what was added for top level statements, etc. And dealing with them together is likely to be much easier, more code can be removed at once rather than carefully trying to maintain an undesired behavior for one feature, but not for the other.


In reply to: 1006710484

@Youssef1313 Youssef1313 left a comment

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.

Note: It might worth adding some comments around the interesting parts of code of #46308 so that whenever a developer adds a new syntax kind that is to cause a problem, hopefully the comment will remind the developer to have the appropriate fix.

@AlekseyTs

Copy link
Copy Markdown
Contributor

Done with review pass (commit 5)

@AlekseyTs AlekseyTs left a comment

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.

LGTM (commit 7), assuming CI is passing.

@mavasani

Copy link
Copy Markdown
Contributor Author

Thanks @AlekseyTs! @dotnet/roslyn-compiler for second review.

@mavasani

Copy link
Copy Markdown
Contributor Author

@dotnet/roslyn-compiler for second review.

1 similar comment
@mavasani

Copy link
Copy Markdown
Contributor Author

@dotnet/roslyn-compiler for second review.

@mavasani

Copy link
Copy Markdown
Contributor Author

Thank you @cston

@mavasani
mavasani merged commit 4ad380e into dotnet:main Jan 25, 2022
@ghost ghost modified the milestones: 17.1, Next Jan 25, 2022
@mavasani
mavasani deleted the SyntaxNodeDupe branch January 25, 2022 03:08
@mavasani

Copy link
Copy Markdown
Contributor Author

@AlekseyTs @Youssef1313 I will create a follow-up PR on the same lines for top level statements.

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.

SyntaxNodeAction is invoked twice for the same syntax node in case of positional records

7 participants