Skip to content

Add "grouped diagnostics" that allows rendering across source files #1350

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

Merged
merged 10 commits into from
Feb 27, 2023

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Feb 18, 2023

Extend the diagnostics renderer to handle a set of "grouped"
diagnostics, where the diagnostics occur across different source files
that are related through source code generation, for example because
one is a macro expansion buffer that was triggered within the other.
These inner source files are shown as nested source files in the
output, underneath the location of where the outer source file where
they logically occur.

Introduce a few other small improvements to the colorization in the
diagnostics formatter that became more apparent when we do this:

  • Underline highlighted ranges in the source text
  • Make the boxes and line numbers cyan so they feel separate from the code

Here's an example from the tests:

=== main.swift ===
1 │ let pi = 3.14159
2 │ #myAssert(pi == 3)
  │ ╰─ in expansion of macro 'myAssert' here
  ╭-── #myAssert ──────────────────────────────────────────────────────
  │1 │ let __a = pi
  │2 │ let __b = 3
  │3 │ if #invertedEqualityCheck(__a, __b) {
  │  │    ╰─ in expansion of macro 'invertedEqualityCheck' here
  │  ╭-── #invertedEqualityCheck ─────────────────────────────────────
  │  │1 │ !(__a == __b)
  │  │  │       ╰─ no matching operator '==' for types 'Double' and 'Int'
  │  ╰-───────────────────────────────────────────────────────────────
  │4 │   fatalError("assertion failed: pi != 3")
  │5 │ }
  ╰-───────────────────────────────────────────────────────────────────
3 │ print("hello"
  │              ╰─ expected ')' to end function call

@DougGregor DougGregor requested a review from ahoppen as a code owner February 18, 2023 16:15
@DougGregor
Copy link
Member Author

@swift-ci please test

Extend the diagnostics renderer to handle a set of "grouped"
diagnostics, where the diagnostics occur across different source files
that are related through source code generation, for example because
one is a macro expansion buffer that was triggered within the other.
These inner source files are shown as nested source files in the
output, underneath the location of where the outer source file where
they logically occur.
@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please test

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

This looks nice! I’ve got a few minor comments inline.


/// Mapping from the root source file syntax nodes to the corresponding
/// source file IDs.
var rootIndexes: [SourceFileSyntax: SourceFileID] = [:]
Copy link
Member

Choose a reason for hiding this comment

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

Just a thought: Could we use the root SourceFileSynax as the SourceFileID?

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe? It felt more fragile than if we made this explicit.

Copy link
Member

Choose a reason for hiding this comment

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

Since we are maintaining a map from SourceFileSyntax to SourceFileID, don’t we already have any fragility from SourceFileSyntax now?

Also, note that equality that the identity of Syntax is defined by its SyntaxIdentifier, so we don’t do any tree comparisons or anything of the sort when comparing two syntax nodes.

Copy link
Member Author

Choose a reason for hiding this comment

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

The mapping is only used in cases where the client isn't supplying a buffer ID, so you never need to depend on it. We could either drop the mapping internally (and have clients always supply the buffer ID when adding a diagnostic) or drop the buffer ID (and rely on the mapping everywhere), but this does feel like a weird middle ground.

Help frame the formatter buffer for color terminals by making
the buffer outline and line numbers cyan.
When colorizing output, underline highlighted source ranges from
diagnostic messages.
@DougGregor
Copy link
Member Author

@swift-ci please test

tree: SyntaxType,
diags: [Diagnostic],
indentString: String,
suffixTexts: [(AbsolutePosition, String)],
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn’t it be conceptually simpler if this was [AbsolutePosition: String] or [AbsolutePosition: [String]] (I’d prefer the former if it’s possible). That would suggest better to me there’s a key value relationship between AbsolutePosition and String here.

That being said, I understand that we’re never accessing suffixTexts by its AbsolutePosition.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is a little nicer, so I went ahead and did it in #1365


/// Mapping from the root source file syntax nodes to the corresponding
/// source file IDs.
var rootIndexes: [SourceFileSyntax: SourceFileID] = [:]
Copy link
Member

Choose a reason for hiding this comment

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

Since we are maintaining a map from SourceFileSyntax to SourceFileID, don’t we already have any fragility from SourceFileSyntax now?

Also, note that equality that the identity of Syntax is defined by its SyntaxIdentifier, so we don’t do any tree comparisons or anything of the sort when comparing two syntax nodes.

@DougGregor DougGregor merged commit 1615c39 into swiftlang:main Feb 27, 2023
@DougGregor DougGregor deleted the grouped-diagnostics branch February 27, 2023 14:38
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.

2 participants