-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Fix null refs when calling GetLocation() or GetDiagnostics() of freshly constructed syntax trivia
#81444
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
Conversation
CyrusNajmabadi
left a comment
There was a problem hiding this 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
left a comment
There was a problem hiding this 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.
|
Can I have a review here please?! |
Consider making the title more detailed. For example by mentioning what APIs are getting fixed/changed #Closed |
|
Done with review pass (commit 1) #Closed |
GetLocation() or GetDiagnostics() of freshly constructed syntax trivia
|
@AlekseyTs PTAL |
| { | ||
| // https://github.com/dotnet/roslyn/issues/40773 | ||
| return this.SyntaxTree!.GetDiagnostics(this); | ||
| if (UnderlyingNode is null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
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)
|
Done with review pass (commit 2) #Closed |
| ? SpecializedCollections.EmptyEnumerable<Diagnostic>() | ||
| : diagnostics.Select(Diagnostic.Create); | ||
| } | ||
| } |
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
CyrusNajmabadi
left a comment
There was a problem hiding this 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.
|
@AlekseyTs PTAL |
|
Done with review pass (commit 3) |
AlekseyTs
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM (commit 4)
|
@DoctorKrolic Thank you for the contribution |
Fixes: #40773
Matches behavior of syntax token in test above