Convert Message::SyntaxError to use Diagnostic internally#17784
Conversation
|
4c2da7c to
4fce96a
Compare
|
crates/ruff_db/src/diagnostic/mod.rs
Outdated
| UnifiedFile::RedKnot(file) => resolver.path(*file), | ||
| UnifiedFile::Ruff(file) => file.name(), | ||
| } | ||
| } |
There was a problem hiding this comment.
FWIW, I think the main intent with FileResolver is that it would become a trait, and this would be how we'd handle Ruff using its own interning for files. i.e., Red Knot would pass its own Salsa-backed resolver, but Ruff would use its non-Salsa thing.
I think if you aren't doing the interning, then yeah, this API is probably fine and perhaps even slightly over-engineered. If you do still plan to do interning---and I think that will let you definitively make UnifiedFile and therefore Span implement Copy---then I think this is a good stepping stone toward that.
There was a problem hiding this comment.
I find having both path and file_path on UnifiedFile confusing because the only difference is whether they take a db or FileResolver, but that's not apparent from the method signature.
One option is to make FileResolver a trait as @BurntSushi suggests and implement it for Db (and Upcast). We would then need to implement a similar struct for Ruff.
Or we remove file_path (and all other methods that take db) and callers should instead call expect_ty and the path on the returned file. The renderer could call resolver.path(file) directly and we move this match into resolver.path. I think I sort of prefer this.
There was a problem hiding this comment.
I don't feel too strongly here primarily because path() is an internal API, so I think the blast radius of confusion is somewhat more limited.
I'm happy with either of @MichaReiser's suggestions though. I kinda like just jumping straight to the trait approach, which was the original intent.
There was a problem hiding this comment.
Is this close to what y'all had in mind? 1faa5e0
I feel like I may have gone astray here in the details, but FileResolver is now a trait, the code compiles, and I got rid of file_path, at least.
The docs on FileResolver are probably slightly outdated now, but they still sounded relevant enough to me to leave alone since we're not really fully decoupled from salsa yet.
There was a problem hiding this comment.
Yeah that looks about right to me! Thanks!
I'd probably go for dyn Trait here though since I don't think the virtual call is going to matter perf-wise, and it will hopefully save from duplicating the rendering code.
add RuffFile with a cached SourceText Revert "add RuffFile with a cached SourceText" This reverts commit 315e834eea6f34a2e1909bdc75e5f505c4597f85.
4fce96a to
f11b040
Compare
|
Okay, I think this is ready for review. The two main changes since y'all last had a look are:
|
Co-authored-by: Carl Meyer <carl@astral.sh>
Co-authored-by: Micha Reiser <micha@reiser.io>
|
I went ahead and pushed the minor changes, working on the |
| } | ||
|
|
||
| impl Message { | ||
| pub fn syntax_error(message: String, range: TextRange, file: SourceFile) -> Message { |
There was a problem hiding this comment.
It might be a little nicer to accept a impl std::fmt::Display here (which is what most of the Diagnostic APIs accept, at least, in spirit). Then you could avoid the various err.to_string() calls and just pass err.
crates/ruff_db/src/diagnostic/mod.rs
Outdated
| self.primary_annotation().map(|ann| ann.tags.as_slice()) | ||
| } | ||
|
|
||
| pub fn expect_span(&self) -> Span { |
There was a problem hiding this comment.
Can you add a comment documenting this? In particular, it would be good to state the panic conditions and the higher level motivation for why you might want to use this. (e.g., "When working with diagnostics in Ruff, the context requires that every diagnostic have a primary span.")
I think I'd also rename this to expect_primary_span() so that it's consistent with primary_span().
| pub enum UnifiedFile { | ||
| Ty(File), | ||
| Ruff(SourceFile), | ||
| } |
There was a problem hiding this comment.
Can you add docs to this type? This might be a nice place to put some prose about why this exists and the high level strategy for how we're trying to share the diagnostics system between both ty and ruff.
crates/ruff_db/src/diagnostic/mod.rs
Outdated
| UnifiedFile::RedKnot(file) => resolver.path(*file), | ||
| UnifiedFile::Ruff(file) => file.name(), | ||
| } | ||
| } |
There was a problem hiding this comment.
I don't feel too strongly here primarily because path() is an internal API, so I think the blast radius of confusion is somewhat more limited.
I'm happy with either of @MichaReiser's suggestions though. I kinda like just jumping straight to the trait approach, which was the original intent.
crates/ruff_db/src/source.rs
Outdated
| #[derive(Clone, Eq, PartialEq)] | ||
| pub struct SourceText { | ||
| inner: Arc<SourceTextInner>, | ||
| pub(crate) inner: Arc<SourceTextInner>, |
There was a problem hiding this comment.
I think using just SourceCode might be a little tricky here, because you need somewhere to anchor its lifetimes to.
I do agree that reusing SourceText here is a little wonky.
Maybe the way to go here is another enum:
enum DiagnosticSource {
Ty(SourceText),
Ruff(SourceFile),
)And then you should be able to define a method to return a SourceCode I think.
|
@ntBre can you ping me when this is ready for re-review (it is already)? |
|
It should be ready for another review! |
| pub enum UnifiedFile { | ||
| Ty(File), | ||
| Ruff(SourceFile), | ||
| } |
| enum DiagnosticSource { | ||
| Ty(Input), | ||
| Ruff(SourceFile), | ||
| } |
There was a problem hiding this comment.
Nice docs. And I like this type. Much clearer.
MichaReiser
left a comment
There was a problem hiding this comment.
This is excellent. Thank you
|
Thank you both! |
* main: [ty] Respect the gradual guarantee when reporting errors in resolving MROs (#17962) Convert `Message::SyntaxError` to use `Diagnostic` internally (#17784) [ty] Support extending `__all__` with a literal tuple or set as well as a literal list (#17948) [ty] Make `unused-ignore-comment` disabled by default for now (#17955) [ty] Change default severity for `unbound-reference` to `error` (#17936) [ty] Ignore `possibly-unresolved-reference` by default (#17934) [ty] Default to latest supported python version (#17938) [ty] Generate and add rules table (#17953) Update the schemastore script to match changes in ty (#17952) [ty] Implement `global` handling and `load-before-global-declaration` syntax error (#17637)
* dcreager/default-typevars: clean up the diff remove trait track in type again clippy Better expansion of default typevars specialize_partial enum for TypeMapping [ty] Respect the gradual guarantee when reporting errors in resolving MROs (#17962) Specialize trait Convert `Message::SyntaxError` to use `Diagnostic` internally (#17784) [ty] Support extending `__all__` with a literal tuple or set as well as a literal list (#17948) [ty] Make `unused-ignore-comment` disabled by default for now (#17955) [ty] Change default severity for `unbound-reference` to `error` (#17936) [ty] Ignore `possibly-unresolved-reference` by default (#17934) [ty] Default to latest supported python version (#17938) [ty] Generate and add rules table (#17953) Update the schemastore script to match changes in ty (#17952) [ty] Implement `global` handling and `load-before-global-declaration` syntax error (#17637)
Summary
This PR is a first step toward integration of the new
Diagnostictype into ruff. There are two main changes:UnifiedFileenum wrappingFilefor red-knot and aSourceFilefor ruffMessage::SyntaxErrorvariant is now aDiagnosticinstead of aSyntaxErrorMessageThe second of these changes was mostly just a proof of concept for the first, and it went pretty smoothly. Converting
DiagnosticMessages will be most of the work in replacingMessageentirely.Test Plan
Existing tests, which show no changes.