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

Fix crash when emitted diagnostics have incorrect source ranges #840

Merged

Conversation

d-ronnqvist
Copy link
Contributor

Bug/issue #, if applicable: rdar://122345738

Summary

This updates the default diagnostics formatter to skip highlighting the line if the diagnostic range is incorrect.

Currently the default diagnostics formatter assumes that all the diagnostic ranges are correct and crashes—instead of printing any diagnostics—if they are not.

With these changes we'll also assert in debug builds if the diagnostic range is incorrect with extra information for us to investigate where the diagnostic with incorrect ranges originated from to help us fix the root cause of the incorrect diagnostic range.

Because of this assertion we can't add a test that the diagnostic isn't highlighted when the range is incorrect. I feel that the assertion—helping us find and fix the root cause of these issues—is more valuable than that test.

Dependencies

None.

Testing

This issue currently reproduces with the main branch of the Swift Programming Language documentation catalog.

  • Clone git@github.com:apple/swift-book.git
  • Run swift run docc convert /path/to/swift-book/TSPL.docc
    This should fail with an assertion, since it's a debug build.
  • Run swift run --configuration release docc convert /path/to/swift-book/TSPL.docc
    This should succeed and print a few diagnostics, where not all of the diagnostics will highlight the range in the displayed lines of markup.

Checklist

Make sure you check off the following items. If they cannot be completed, provide a reason.

  • Added tests
  • Ran the ./bin/test script and it succeeded
  • Updated documentation if necessary

@@ -270,7 +270,7 @@ class DiagnosticConsoleWriterDefaultFormattingTest: XCTestCase {
let bundle = try XCTUnwrap(fs.bundles().first)
let baseURL = bundle.baseURL
let source = try XCTUnwrap(bundle.markupURLs.first)
let range = SourceLocation(line: 3, column: 18, source: source)..<SourceLocation(line: 3, column: 32, source: source)
let range = SourceLocation(line: 3, column: 18, source: source)..<SourceLocation(line: 3, column: 36, source: source)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed this test because it the range was specifying a byte in the middle of the emoji, which I don't consider to be a correct diagnostic range.

Copy link
Contributor

@patshaughnessy patshaughnessy left a comment

Choose a reason for hiding this comment

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

Looks good

return result
assert(highlightStart <= sourceLineUTF8.count, {
"""
Received diagnostic with incorrect source range; (\(range.lowerBound.column) ..< \(range.upperBound.column)) extends beyond the text on line \(lineNumber) (\(sourceLineUTF8.count) characters)
Copy link
Contributor

Choose a reason for hiding this comment

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

It's interesting when I tried the example from the Swift Book, this line number seems to be incorrect:

warning: '`' doesn't exist at '/The-Swift-Programming-Language/LexicalStructure'
   --> TSPL.docc/ReferenceManual/LexicalStructure.md:142:145-142:146
140 | > Grammar of an identifier:
141 | >
142 + > *identifier* → *identifier-head* *identifier-characters*_?_ \
143 | > *identifier* → **`` ` ``** *identifier-head* *identifier-characters*_?_ **`` ` ``** \
144 | > *identifier* → *implicit-parameter-name* \

The stray backtick is on line 143, not 142. I wonder if this discrepancy has anything to do with the crash we were seeing before? Now that we have this assert here, we should take some time to find the root cause of the invalid diagnostics.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I saw that as well.

The other diagnostics are on the correct line so it doesn't look like a systematic off-by-one error. We'll continue to investigate that separately.

@d-ronnqvist
Copy link
Contributor Author

@swift-ci please test

@d-ronnqvist d-ronnqvist merged commit 7a5bb68 into swiftlang:main Feb 22, 2024
2 checks passed
@d-ronnqvist d-ronnqvist deleted the highlight-diagnostic-range-crash branch February 22, 2024 08:09
@d-ronnqvist d-ronnqvist mentioned this pull request Apr 18, 2024
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants