ruff/crates/ty_python_semantic/src
Shunsuke Shibayama 9dd666d677
[ty] fix global symbol lookup from eager scopes (#21317)
## Summary

cf. https://github.com/astral-sh/ruff/pull/20962

In the following code, `foo` in the comprehension was not reported as
unresolved:

```python
# error: [unresolved-reference] "Name `foo` used when not defined"
foo
foo = [
    # no error!
    # revealed: Divergent
    reveal_type(x) for _ in () for x in [foo]
]

baz = [
    # error: [unresolved-reference] "Name `baz` used when not defined"
    # revealed: Unknown
    reveal_type(x) for _ in () for x in [baz]
]
```

In fact, this is a more serious bug than it looks: for `foo`,
[`explicit_global_symbol` is
called](6cc3393ccd/crates/ty_python_semantic/src/types/infer/builder.rs (L8052)),
causing a symbol that should actually be `Undefined` to be reported as
being of type `Divergent`.

This PR fixes this bug. As a result, the code in
`mdtest/regression/pr_20962_comprehension_panics.md` no longer panics.

## Test Plan

`corpus\cyclic_symbol_in_comprehension.py` is added.
New tests are added in `mdtest/comprehensions/basic.md`.

---------

Co-authored-by: Micha Reiser <micha@reiser.io>
Co-authored-by: Carl Meyer <carl@astral.sh>
2025-11-12 10:15:51 -08:00
..
module_resolver [ty] add legacy namespace package support (#20897) 2025-10-17 03:16:37 +00:00
semantic_index [ty] fix global symbol lookup from eager scopes (#21317) 2025-11-12 10:15:51 -08:00
types [ty] fix global symbol lookup from eager scopes (#21317) 2025-11-12 10:15:51 -08:00
ast_node_ref.rs [ty] Improve semantic token classification for names (#21399) 2025-11-12 16:34:26 +00:00
db.rs [ty] Limit shown import paths to at most 5 unless ty runs with -v (#20912) 2025-10-16 13:18:09 +02:00
diagnostic.rs [ty] Remove mentions of VS Code from server logs (#21155) 2025-11-03 14:49:58 +00:00
dunder_all.rs [ty] follow the breaking API changes made in salsa-rs/salsa#1015 (#21117) 2025-10-29 14:56:12 +00:00
lib.rs [ty] Implement go-to for binary and unary operators (#21001) 2025-10-21 19:25:41 +02:00
lint.rs [ty] Add suggestion to unknown rule diagnostics, rename unknown-rule lint to ignore-comment-unknown-rule (#20948) 2025-10-18 12:44:21 +02:00
list.rs
module_name.rs [ty] support absolute from imports introducing local submodules in __init__.py files (#21372) 2025-11-11 13:04:42 -05:00
node_key.rs [ty] Implement the legacy PEP-484 convention for indicating positional-only parameters (#20248) 2025-09-05 17:56:06 +01:00
place.rs [ty] Use "cannot" consistently over "can not" (#21255) 2025-11-03 10:38:20 -05:00
program.rs [ty] Remove mentions of VS Code from server logs (#21155) 2025-11-03 14:49:58 +00:00
pull_types.rs
python_platform.rs Update Rust toolchain to 1.91 (#21179) 2025-11-01 01:50:58 +00:00
rank.rs
semantic_index.rs [ty] Skip eagerly evaluated scopes for attribute storing (#20856) 2025-11-11 14:45:34 -08:00
semantic_model.rs [ty] Add comment explaining why HasTrackedScope is implemented for Identifier and why it works (#21057) 2025-10-24 09:48:57 +02:00
site_packages.rs [ty] Discover site-packages from the environment that ty is installed in (#21286) 2025-11-06 09:27:49 -05:00
subscript.rs [ty] Use "cannot" consistently over "can not" (#21255) 2025-11-03 10:38:20 -05:00
suppression.rs [ty] Add suggestion to unknown rule diagnostics, rename unknown-rule lint to ignore-comment-unknown-rule (#20948) 2025-10-18 12:44:21 +02:00
types.rs [ty] Improve semantic token classification for names (#21399) 2025-11-12 16:34:26 +00:00
unpack.rs