[red-knot] Detect version-related syntax errors (#16379)

## Summary
This PR extends version-related syntax error detection to red-knot. The
main changes here are:

1. Passing `ParseOptions` specifying a `PythonVersion` to parser calls
2. Adding a `python_version` method to the `Db` trait to make this
possible
3. Converting `UnsupportedSyntaxError`s to `Diagnostic`s
4. Updating existing mdtests  to avoid unrelated syntax errors

My initial draft of (1) and (2) in #16090 instead tried passing a
`PythonVersion` down to every parser call, but @MichaReiser suggested
the `Db` approach instead
[here](https://github.com/astral-sh/ruff/pull/16090#discussion_r1969198407),
and I think it turned out much nicer.

All of the new `python_version` methods look like this:

```rust
fn python_version(&self) -> ruff_python_ast::PythonVersion {
    Program::get(self).python_version(self)
}
```

with the exception of the `TestDb` in `ruff_db`, which hard-codes
`PythonVersion::latest()`.

## Test Plan

Existing mdtests, plus a new mdtest to see at least one of the new
diagnostics.
This commit is contained in:
Brent Westbrook 2025-04-17 14:00:30 -04:00 committed by GitHub
parent d2ebfd6ed7
commit 9c47b6dbb0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
43 changed files with 353 additions and 14 deletions

View file

@ -497,11 +497,10 @@ impl FusedIterator for ChildrenIter<'_> {}
mod tests {
use ruff_db::files::{system_path_to_file, File};
use ruff_db::parsed::parsed_module;
use ruff_db::system::DbWithWritableSystem as _;
use ruff_python_ast as ast;
use ruff_python_ast::{self as ast};
use ruff_text_size::{Ranged, TextRange};
use crate::db::tests::TestDb;
use crate::db::tests::{TestDb, TestDbBuilder};
use crate::semantic_index::ast_ids::{HasScopedUseId, ScopedUseId};
use crate::semantic_index::definition::{Definition, DefinitionKind};
use crate::semantic_index::symbol::{
@ -528,11 +527,15 @@ mod tests {
file: File,
}
fn test_case(content: impl AsRef<str>) -> TestCase {
let mut db = TestDb::new();
db.write_file("test.py", content).unwrap();
fn test_case(content: &str) -> TestCase {
const FILENAME: &str = "test.py";
let file = system_path_to_file(&db, "test.py").unwrap();
let db = TestDbBuilder::new()
.with_file(FILENAME, content)
.build()
.unwrap();
let file = system_path_to_file(&db, FILENAME).unwrap();
TestCase { db, file }
}