Skip to content

Comments

[ty] Refactor CheckSuppressionContext to use DiagnosticGuard#21587

Merged
MichaReiser merged 4 commits intomainfrom
micha/suppressions-diagnostic-guard
Nov 25, 2025
Merged

[ty] Refactor CheckSuppressionContext to use DiagnosticGuard#21587
MichaReiser merged 4 commits intomainfrom
micha/suppressions-diagnostic-guard

Conversation

@MichaReiser
Copy link
Member

Summary

This is PR is a first step towards adding a "Remove unused suppression" code action in the editor.

Adding the fix for removing unused suppression requires setting the Fix on the
diagnostic created by CheckSuppressionContext::report_unchecked. To make this possible,
change report_unchecked and report_lint to return a DiagnosticGuard.

Doing so requires extracting DiagnosticGuard from InferContext.

I tried very hard to not having to change CheckSuppressionContext to store a RefCell<TypeCheckDiagnostic> but
I couldn't find a way to do so. I tried a enum DiagnosticSink { Cell(RefCell<TypeCheckDiagnostics>), Mut(&mut TypeCheckDiagnostic) }
with a emit method that takes &self. However, the borrow checker (correctly?), disallowes me to mutate the &mut TypeCheckDiagnostic in that case.

I gave up after 30min or so and I sort of like how check_suppressions takes the Diagnostics with suppressions and returns only
the diagnostics once it's done, as the suppressions have now been handled.

Test Plan

cargo test

@MichaReiser MichaReiser added internal An internal refactor or improvement ty Multi-file analysis & type inference labels Nov 23, 2025
buffer
}

/// An abstraction for mutating a diagnostic.
Copy link
Member Author

@MichaReiser MichaReiser Nov 23, 2025

Choose a reason for hiding this comment

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

This is just a move except that DiagnosticGuard now stores a file and sink instead of a &InferContext

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 23, 2025

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@astral-sh-bot
Copy link

astral-sh-bot bot commented Nov 23, 2025

mypy_primer results

No ecosystem changes detected ✅

No memory usage changes detected ✅

@MichaReiser MichaReiser force-pushed the micha/suppressions-diagnostic-guard branch from 95230f4 to 75c3782 Compare November 23, 2025 10:49
Comment on lines -634 to -636
if let DiagnosticId::Lint(lint_name) = diag.id() {
diag.set_documentation_url(Some(lint_documentation_url(lint_name)));
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I moved this logic into DiagnosticGuard::drop, to reduce the code duplication

@MichaReiser MichaReiser force-pushed the micha/suppressions-diagnostic-guard branch from 75c3782 to acc9702 Compare November 23, 2025 10:51
@MichaReiser MichaReiser force-pushed the micha/suppressions-diagnostic-guard branch from acc9702 to b97e848 Compare November 23, 2025 11:02
@MichaReiser MichaReiser changed the title [ty] Refactor CheckSuppressionContext to use DiagnosticGuard" [ty] Refactor CheckSuppressionContext to use DiagnosticGuard Nov 23, 2025
@MichaReiser MichaReiser marked this pull request as ready for review November 23, 2025 11:13
Copy link
Member

@BurntSushi BurntSushi left a comment

Choose a reason for hiding this comment

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

Makes sense to me!

pub(super) struct DiagnosticGuard<'sink> {
file: File,

sink: &'sink std::cell::RefCell<TypeCheckDiagnostics>,
Copy link
Member

Choose a reason for hiding this comment

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

What goes wrong when you use &mut TypeCheckDiagnostics here?

It might be worth a brief comment saying why interior mutability is being used here.

Copy link
Member Author

Choose a reason for hiding this comment

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

It would panic if you do:

let diag = context.report_lint(...);
let diag2 = context.report_lint(...); // we now borrow `&mut TypeCheckDiagnostics` twice

This seems worse as an API caller shouldn't be aware of the internal ref-cell borrow

Copy link
Member

@BurntSushi BurntSushi Nov 24, 2025

Choose a reason for hiding this comment

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

I guess I meant the higher level reason. But I think I now see: you wouldn't be able to have two guards alive at the same time?

Copy link
Member Author

@MichaReiser MichaReiser Nov 24, 2025

Choose a reason for hiding this comment

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

Yes, the higher level reason is that the current approach correctly isolates the RefCell so that callers don't need to know about it (I'll add a comment).

@MichaReiser MichaReiser force-pushed the micha/suppressions-diagnostic-guard branch from b97e848 to a67e6f2 Compare November 25, 2025 08:08
@MichaReiser MichaReiser enabled auto-merge (squash) November 25, 2025 08:21
@MichaReiser MichaReiser merged commit eddb9ad into main Nov 25, 2025
66 of 67 checks passed
@MichaReiser MichaReiser deleted the micha/suppressions-diagnostic-guard branch November 25, 2025 10:54
carljm added a commit to mtshiba/ruff that referenced this pull request Nov 25, 2025
* main:
  [ty] Implement `typing.override` (astral-sh#21627)
  [ty] Avoid expression reinference for diagnostics (astral-sh#21267)
  [ty] Improve autocomplete suppressions of keywords in variable bindings
  [ty] Only suggest completions based on text before the cursor
  Implement goto-definition and find-references for global/nonlocal statements (astral-sh#21616)
  [ty] Inlay Hint edit follow up (astral-sh#21621)
  [ty] Implement lsp support for string annotations (astral-sh#21577)
  [ty] Add 'remove unused ignore comment' code action (astral-sh#21582)
  [ty] Refactor `CheckSuppressionContext` to use `DiagnosticGuard` (astral-sh#21587)
  [ty] Improve several "Did you mean?" suggestions (astral-sh#21597)
  [ty] Add more and update existing projects in `ty_benchmark` (astral-sh#21536)
  [ty] fix ty playground initialization and vite optimization issues (astral-sh#21471)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal An internal refactor or improvement ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants