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

[DiagnosticBridge] Make sure that diagnostic queues up its child notes #79812

Merged

Conversation

xedin
Copy link
Contributor

@xedin xedin commented Mar 6, 2025

DiagnosticEngine has an API that allows to attach notes to a "primary" diagnostic (an error or a warning). This works well with old formatting (llvm) but swift formatter doesn't display attached notes which makes some diagnostics very hard to work with i.e. invalid_redecl.

@xedin xedin marked this pull request as ready for review March 6, 2025 01:57
@xedin xedin requested review from hborla and slavapestov as code owners March 6, 2025 01:57
@xedin
Copy link
Contributor Author

xedin commented Mar 6, 2025

@swift-ci please smoke test

Copy link
Contributor

@ktoso ktoso left a comment

Choose a reason for hiding this comment

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

Thanks a lot @xedin !

Diagnosing redeclarations was a nightmare without this in #79600

Thanks!

@xedin xedin requested a review from cachemeifyoucan March 6, 2025 08:47
@cachemeifyoucan
Copy link
Contributor

Can you add some test case? I see the CAS diagnostics test actually needs to be updated for new child diagnostics but it will be good to have a test that is outside CAS to exercise the new code.

@xedin
Copy link
Contributor Author

xedin commented Mar 6, 2025

@cachemeifyoucan That’s why I added you, looking for suggestions for how to test this properly.

@xedin xedin force-pushed the swift-formatter-should-print-child-notes branch from 22adfb0 to 782f7f1 Compare March 6, 2025 21:30
@xedin xedin requested review from artemcm and tshortli as code owners March 6, 2025 21:30
xedin added 2 commits March 6, 2025 15:23
`DiagnosticEngine` has an API that allows to attach notes to a "primary"
diagnostic (an error or a warning). This works well with old formatting
(`llvm`) but `swift` formatter doesn't display attached notes which makes
some diagnostics very hard to work with i.e. `invalid_redecl`.
Serialized diagnostic stores an `std::string` which means that
the category gets marked as present even if its empty, which is
incorrect.
@xedin xedin force-pushed the swift-formatter-should-print-child-notes branch from 0c00433 to 1c86dd7 Compare March 6, 2025 23:24
@xedin
Copy link
Contributor Author

xedin commented Mar 6, 2025

@swift-ci please test

@xedin
Copy link
Contributor Author

xedin commented Mar 7, 2025

@swift-ci please test

@cachemeifyoucan
Copy link
Contributor

LGTM

Copy link
Contributor

@bnbarham bnbarham left a comment

Choose a reason for hiding this comment

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

Thanks for fixing 🙇‍♂️

@@ -85,3 +88,7 @@ foo(b:
// CHECK: [[#LINE]] | foo(b: 1, a: 2)
// CHECK: | `- error: argument 'a' must precede argument 'b'
// CHECK: [[#LINE+1]] |

// CHECK: SOURCE_DIR{{[/\]+}}test{{[/\]+}}diagnostics{{[/\]+}}pretty-printed-diagnostics.swift:[[#LINE:]]:6
Copy link
Contributor

Choose a reason for hiding this comment

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

It'd be nice to check for the error here too, I was initially very confused by this and thought that it was somehow a note off on its own 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Comment on lines +84 to +86
for (auto *childNote : info.ChildDiagnosticInfo) {
addQueueDiagnostic(queuedDiagnostics, *childNote, SM);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be clearer to move this up into enqueueDiagnostic?

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'd prefer to keep this in here for now since this is technically an implementation detail a "primary" diagnostic and ideally should be dispatched as an argument like what I mentioned in a TODO.

Copy link
Contributor

Choose a reason for hiding this comment

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

Unless I'm misunderstanding, the infrastructure currently assumes that notes are attached to the first primary diagnostic in the group. Ie. enqueueDiagnostic isn't necessarily a primary diagnostic anyway. But 🤷‍♂️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You might be talking about lower level things but there is a dedicated API in DiagnosticEngine which let's the users attach notes directly to the diagnostic they want. I think we should replace all of the disjoint emission of notes with diagnoseWithNotes and remove any inference that happens later on...

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, all for moving to diagnoseWithNotes. Happy to leave it where it is 👍

xedin added 2 commits March 6, 2025 18:34
Default value for `Category` for serialization purposes is
an empty string but it should be handled as `nil` while
bridging because category name cannot be empty when present.
@xedin xedin force-pushed the swift-formatter-should-print-child-notes branch from 40c92eb to a7a9a4d Compare March 7, 2025 02:35
@xedin
Copy link
Contributor Author

xedin commented Mar 7, 2025

@swift-ci please test

@xedin
Copy link
Contributor Author

xedin commented Mar 7, 2025

@swift-ci please test Linux platform

@xedin xedin merged commit fd92e27 into swiftlang:main Mar 7, 2025
5 checks passed
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.

4 participants