[ty] Track different uses of legacy typevars, including context when rendering typevars (#19604)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run

This PR introduces a few related changes:

- We now keep track of each time a legacy typevar is bound in a
different generic context (e.g. class, function), and internally create
a new `TypeVarInstance` for each usage. This means the rest of the code
can now assume that salsa-equivalent `TypeVarInstance`s refer to the
same typevar, even taking into account that legacy typevars can be used
more than once.

- We also go ahead and track the binding context of PEP 695 typevars.
That's _much_ easier to track since we have the binding context right
there during type inference.

- With that in place, we can now include the name of the binding context
when rendering typevars (e.g. `T@f` instead of `T`)
This commit is contained in:
Douglas Creager 2025-08-01 12:20:32 -04:00 committed by GitHub
parent 48d5bd13fa
commit 06cd249a9b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 394 additions and 128 deletions

View file

@ -60,6 +60,35 @@ impl<'db> Definition<'db> {
FileRange::new(self.file(db), self.kind(db).target_range(module))
}
/// Returns the name of the item being defined, if applicable.
pub fn name(self, db: &'db dyn Db) -> Option<String> {
let file = self.file(db);
let module = parsed_module(db, file).load(db);
let kind = self.kind(db);
match kind {
DefinitionKind::Function(def) => {
let node = def.node(&module);
Some(node.name.as_str().to_string())
}
DefinitionKind::Class(def) => {
let node = def.node(&module);
Some(node.name.as_str().to_string())
}
DefinitionKind::TypeAlias(def) => {
let node = def.node(&module);
Some(
node.name
.as_name_expr()
.expect("type alias name should be a NameExpr")
.id
.as_str()
.to_string(),
)
}
_ => None,
}
}
/// Extract a docstring from this definition, if applicable.
/// This method returns a docstring for function and class definitions.
/// The docstring is extracted from the first statement in the body if it's a string literal.