-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Fix a NullReferenceException in DocumentExtensions #10819
Conversation
Hi @aelij, Thanks for the submission! However, I think I personally would prefer to have this throw. It's generally a programming error to ask for a span outside the span of the document. Did you run into this with a regular file in Visual Studio, or did was it when you were using the API? Anyone else on @dotnet/roslyn-ide want to comment? @mattwar? |
if (node == null) | ||
{ | ||
return await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false); | ||
} |
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.
Style Nit: We generally prefer a blank line after a }
.
Agreed -- this should throw in that case. (I wonder what bugs we'll uncover with that!) |
Is @Pilchie I ran into this while using the API to hook up syntax highlighting in AvalonEdit. |
@@ -59,6 +59,10 @@ public static bool IsOpen(this Document document) | |||
} | |||
|
|||
var node = token.Parent.AncestorsAndSelf().FirstOrDefault(a => a.FullSpan.Contains(span)); | |||
if (node == 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.
Hrmmm... how could node be null? When would we have a span that wasn't contained in the full span of some node in the document?
I would "throw an ArgumentOutOfRangeException". The span is not within bounds. There is something very wrong with the caller. |
In case it got missed - I pushed the requested changes to my branch. |
@dotnet/roslyn-ide Can you take another look at it? |
@@ -59,6 +59,11 @@ public static bool IsOpen(this Document document) | |||
} | |||
|
|||
var node = token.Parent.AncestorsAndSelf().FirstOrDefault(a => a.FullSpan.Contains(span)); | |||
if (node == null) | |||
{ | |||
throw new ArgumentOutOfRangeException(nameof(span)); |
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.
Seems more reasonable, should we just do if (!root.FullSpan.Contains(span))
earlier and throw instead? This seems to be a fairly circuitous way to check the simpler case.
@Pilchie, @CyrusNajmabadi, @jasonmalinowski: What's the status on this PR? |
I guess we are waiting for @aelij to respond to my suggestion? |
Oh, I was waiting for something actionable... :) I'd leave it as is, since obviously this isn't a common code path, so it's a bit of a waste to check the span's range on every call. |
We apologize, but we are closing this PR due to code drift. We regret letting this PR go so long without attention, but at this point the code base has changed too much for us to revisit older PRs. Going forward, we will manage PRs better under an SLA currently under draft in issue #26266 – we encourage you to add comments to that draft as you see fit. Although that SLA is still in draft form, we nevertheless want to move forward with the identification of older PRs to give contributors as much early notice as possible to review and update them. If you are interested in pursuing this PR, please reset it against the head of master and we will reopen it. Thank you! |
I encountered this while using the
Classifier
. This happens if you specify a span outside the the document. I wasn't sure if it should return the entire document's semantic model or throw anArgumentOutOfRangeException
; I chose the former.Stack trace: