Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@DoctorKrolic
Copy link
Contributor

Fixes: #40773

Matches behavior of syntax token in test above

@DoctorKrolic DoctorKrolic requested a review from a team as a code owner November 25, 2025 18:00
@dotnet-policy-service dotnet-policy-service bot added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Nov 25, 2025
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.

Yup. Seems to match SyntaxToken now.

CyrusNajmabadi
CyrusNajmabadi previously approved these changes Nov 25, 2025
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.

Yup. Seems to match SyntaxToken now.

@DoctorKrolic
Copy link
Contributor Author

Can I have a review here please?!

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Dec 3, 2025

@DoctorKrolic

Fix null refs of freshly constructed syntax trivia

Consider making the title more detailed. For example by mentioning what APIs are getting fixed/changed #Closed

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Dec 3, 2025

Done with review pass (commit 1) #Closed

@DoctorKrolic DoctorKrolic changed the title Fix null refs of freshly constructed syntax trivia Fix null refs when calling GetLocation() or GetDiagnostics() of freshly constructed syntax trivia Dec 3, 2025
@DoctorKrolic
Copy link
Contributor Author

@AlekseyTs PTAL

{
// https://github.com/dotnet/roslyn/issues/40773
return this.SyntaxTree!.GetDiagnostics(this);
if (UnderlyingNode is null)
Copy link
Contributor

@AlekseyTs AlekseyTs Dec 9, 2025

Choose a reason for hiding this comment

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

if (UnderlyingNode is null)

It feels like all individual code paths in this function should have dedicated code coverage. That should include both positive and negative outcomes, when applicable. Also, I have to admit, I am not an expert in this area, and, at the moment, I do not have a good idea about how expected implementation should actually look like. Therefore, it would be good to provide an explanation why this is the right implementation. If the logic was "copied" from some other place, it would be good to mention that as well. #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When I copied this logic I was not able to come up with a test for the scenario where there is no syntax tree, but the underlying green node exists. After thinking about it for a while I managed to find the case: when parsing trivia via SyntaxFactory.Parse[Leading|Trailing]Trivia we get precisely this scenario, so added tests for both branches (where we have diagnostics and where we don't)

@AlekseyTs
Copy link
Contributor

AlekseyTs commented Dec 9, 2025

Done with review pass (commit 2) #Closed

? SpecializedCollections.EmptyEnumerable<Diagnostic>()
: diagnostics.Select(Diagnostic.Create);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

any problem keeping the logic directly equivalent to SyntaxToken? like line for line. In other words, looking like:

            if (UnderlyingNode == null)
            {
                return SpecializedCollections.EmptyEnumerable<Diagnostic>();
            }

            var tree = SyntaxTree;

            if (tree == null)
            {
                var diagnostics = UnderlyingNode.GetDiagnostics();

                return diagnostics.Length == 0
                    ? SpecializedCollections.EmptyEnumerable<Diagnostic>()
                    : diagnostics.Select(s_createDiagnosticWithoutLocation);
            }

            return tree.GetDiagnostics(this);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Where should we extract this logic? Is there a good existing helper class for such things?

Copy link
Member

Choose a reason for hiding this comment

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

feels like it could all be on SyntaxTree. It just needs a GreenNode+pos to be able to operate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The VB part also needs to know whether the node/token is in doc comments section, for which it walks the tree upwards from the given node, which is not available for green nodes. I mean we can extract the "getting diagnostic from the tree" part into a delegate, but that will make code more complex, introduce allocation of a delegate etc

Copy link
Member

Choose a reason for hiding this comment

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

My primary feedback was really just to keep the code written the same way. It's ok to still have the duplicated impl.

Copy link
Member

Choose a reason for hiding this comment

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

My primary feedback was really just to keep the code written the same way. It's ok to still have the duplicated impl.

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.

lgtm. though keeping the different impls synced would be a nice touch for me.

@DoctorKrolic
Copy link
Contributor Author

@AlekseyTs PTAL

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 3)

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (commit 4)

@AlekseyTs AlekseyTs enabled auto-merge (squash) December 18, 2025 23:34
@AlekseyTs AlekseyTs merged commit a8098d4 into dotnet:main Dec 19, 2025
24 of 25 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Dec 19, 2025
@AlekseyTs
Copy link
Contributor

@DoctorKrolic Thank you for the contribution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-Compilers 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.

SyntaxTrivia doesn't properly validate it's SyntaxTree

3 participants