Skip to content

Remove diagnostics when documents change#484

Merged
thomasmarshall merged 7 commits into
mainfrom
remove-diagnostics-alt
Jan 22, 2026
Merged

Remove diagnostics when documents change#484
thomasmarshall merged 7 commits into
mainfrom
remove-diagnostics-alt

Conversation

@thomasmarshall

Copy link
Copy Markdown
Contributor

This PR is an alternative approach to #441.

We can add diagnostics to documents, declarations, definitions, and references. Different kinds of diagnostics would be attached to different entities. For example, parse errors would be attached to the document, and resolution errors (like circular dependencies) could be attached to declarations, definitions, or references.

When the document is cleared, each of its definitions and references is removed, along with the diagnostics attached to those entities. Any declaration that is impacted (i.e. because one if its definitions is removed) would have its diagnostics explicitly cleared.

We can access all diagnostics from the top of the graph by iterating through all the entities in the graph and collection the diagnostics. This would be slow, but I'm not sure what the use-case would be. Perhaps it can be improved if we understand when we would want to do this, outside of tests.

We can access all the diagnostics for a particular document via its definitions, references, and associated declarations.

@vinistock vinistock left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I believe this is a good approach 👍

Comment thread rust/rubydex/src/model/graph.rs Outdated
#[must_use]
pub fn diagnostics(&self) -> &Vec<Diagnostic> {
&self.diagnostics
pub fn diagnostics(&self) -> Vec<&Diagnostic> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe we can name this all_diagnostics to make it clear that this returns the combination of all of them?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

When we extend declarations (i.e.: find a second definition for the same thing), we need to ensure we're also extending the diagnostics.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added a fixup commit for this, but I couldn't find any tests for it, nor where it is called from. Maybe my Rust code navigation skills aren't up to scratch!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We might not be doing it yet, but it should happen in Graph#add_declaration.

@thomasmarshall
thomasmarshall force-pushed the remove-diagnostics-alt branch 2 times, most recently from a7ef984 to bfe69c7 Compare January 20, 2026 14:59
@thomasmarshall
thomasmarshall marked this pull request as ready for review January 20, 2026 15:58
@thomasmarshall
thomasmarshall requested a review from a team as a code owner January 20, 2026 15:58
Comment thread rust/rubydex/src/model/graph.rs
Comment thread rust/rubydex/src/diagnostic.rs Outdated
use crate::{model::ids::UriId, offset::Offset};

#[derive(Debug)]
#[derive(Debug, Clone)]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do we need to clone diagnostics?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I pushed a fixup that takes them instead of cloning, but it's possible I misunderstood what you were suggesting here, in which case neither cloning nor taking might make sense.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe my comment was not correct. I wanted to make sure we were not accidentally discarding Declaration level diagnostics, but I think this scenario doesn't happen.

And that is probably why we're not using extend yet.

@vinistock vinistock left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I believe this will be flexible enough for all diagnostic types 👍

@thomasmarshall
thomasmarshall merged commit 7ff5bbf into main Jan 22, 2026
27 checks passed
@thomasmarshall
thomasmarshall deleted the remove-diagnostics-alt branch January 22, 2026 10:54

#[must_use]
pub fn diagnostics(&self) -> &[Diagnostic] {
all_definitions!(self, it => &it.diagnostics)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a costly change when most definitions won't have diagnostics associated.

For declarations I understand the need, but do we need to associate diagnostics to a specific definition, when is it not enough to associate it to the document?

If we do need it, why not store them on the document as a hash?

document.diagnostics_for_definition(definition) -> Diagnostic[]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah that's a good point. I opened a PR here to move them to the document, as you suggest (and references too).

As for why we need diagnostics linked to specific definitions, I thought there might be some resolution related diagnostics that couldn't be linked to a declaration (maybe in cases where it doesn't exist yet?). Would your circular dependency be one example? @vinistock we discussed this together, but I can't remember the definition-specific example.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think that should be fine. At the end of the day, all diagnostics are in some way related to a document because you have to display them somewhere.

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.

3 participants