[ty] Fix missing registration of identifiers during semantic index construction#18441
[ty] Fix missing registration of identifiers during semantic index construction#18441BurntSushi wants to merge 1 commit intomainfrom
Conversation
|
(This is kind of a wild guess on my part.) |
|
cb04b73 to
cc466be
Compare
|
Could you say a bit more about what the cause of the panic is? Where are you using the |
|
It's this: ruff/crates/ty_python_semantic/src/semantic_model.rs Lines 48 to 67 in f23d2c9 Where the node given to that function is identified via: ruff/crates/ty_ide/src/completion.rs Lines 31 to 46 in f23d2c9 Basically, we're looking for the most constraining AST node, looking up that node's scope and then listing out the identifiers in that scope (and its parent scopes). |
|
You also need to recurse into match object():
case fo<CURSOR> |
|
This still causes a panic on your branch if I run the playground locally: def f(x):<CURSOR> |
| fn record_scope_for_identifier(&mut self, name: &ast::Identifier) { | ||
| self.scopes_by_expression | ||
| .insert(name.into(), self.current_scope()); | ||
| } |
There was a problem hiding this comment.
It feels a bit weird to be inserting Identifiers into this map, since Identifiers aren't really expressions. I wondered if we should be using a separate map entirely for completions logic, but that would probably be overkill. It could be worth adding a comment about why we do this, though.
I'm also slightly concerned that it's easy to forget to insert into this map if we're just manually adding the self.record_scope_for_identifier() calls into various branches. But I don't necessarily have a better suggestion
There was a problem hiding this comment.
Yeah I don't have a good sense of what is "right" to do here. I went this direction because it seems like the surrounding code already acknowledges an Identifier as something that can be addressed like an expression because of the impl From<&ast::Identifier> for ExpressionNodeKey.
Interesting! I can't reproduce this locally on this branch. It doesn't seem to panic on
Ah yeah I see the problem. There are some identifiers I missed in the match cases. |
|
I've to think this through a bit more. The initial design of |
This reproduces it very consistently for me on your branch: Screen.Recording.2025-06-03.at.15.59.34.mov |
Do parameter nodes have identifier nodes inside them? It only repros for me if it's a function with parameters |
|
Looking at the change history, enabling Recording the scopes for more nodes does seem reasonable to me if we have use cases beyond just expressions. But we should then rename The alternative is that we change the ruff/crates/ty_ide/src/goto.rs Lines 205 to 251 in 67d94d9 Related: astral-sh/ty#144 |
All righty. I'll put in a pin in this PR for now until I have a chance to work on improving target detection. I guess if we can side-step this issue entirely, then it probably makes sense to avoid storing identifiers. If we end up abdicating this PR, then I can submit another one beefing up the docs on the existing methods. Particularly their panic conditions. :-) When I used this method, I falsely assumed that any expression in the |
Yes, this was done by me (in #17366) to allow recording a use on identifiers which is how the overload implementation is being done. The main reason is that the function name is stored as |
|
Sounds good to me and thanks for jumping on this. It might be good to have a "fix" for now because it seems very easy to trigger this bug in the LSP (which results in a crash). A short term fix could be to add a |
## Summary Implement a hotfix for the playground/LSP crashes related to missing `expression_scope_id`s. relates to: astral-sh/ty#572 ## Test Plan * Regression tests from #18441 * Ran the playground locally to check if panics occur / completions still work. --------- Co-authored-by: Andrew Gallant <[email protected]>
Fixes astral-sh/ty#572