-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[ty] Fix panic for attribute expressions with empty value #19069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| if expr_ref | ||
| .as_name_expr() | ||
| .is_some_and(|name| name.is_invalid()) | ||
| { | ||
| return (Place::Unbound, None); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably not the best (and not the broadest) fix here. Maybe @mtshiba could have a look at this if you find the time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late review. This fix looks good!
One thing I am wondering about is our current handling of (syntactically) invalid place expressions.
The parser seems to construct the following element for [.a., for example:
Attribute(ExprAttribute {
node_index: AtomicNodeIndex(3),
range: 1..3,
value: Name(ExprName { node_index: AtomicNodeIndex(4), range: 1..1, id: Name(""), ctx: Invalid }),
attr: Identifier { id: Name("a"), range: 2..3, node_index: AtomicNodeIndex(5) },
ctx: Load
})
This is also not a valid expression and we can immediately return Unbound. Rather, this seems to cause a panic, but it doesn't. Because it is recorded as a place in the UseDefMap. The inner name is invalid, but the attribute is in the load context. Not harmful, but a useless record.
ruff/crates/ty_python_semantic/src/semantic_index/builder.rs
Lines 1945 to 1962 in f15f930
| let (is_use, is_definition) = match (ctx, self.current_assignment()) { | |
| (ast::ExprContext::Store, Some(CurrentAssignment::AugAssign(_))) => { | |
| // For augmented assignment, the target expression is also used. | |
| (true, true) | |
| } | |
| (ast::ExprContext::Load, _) => (true, false), | |
| (ast::ExprContext::Store, _) => (false, true), | |
| (ast::ExprContext::Del, _) => (true, true), | |
| (ast::ExprContext::Invalid, _) => (false, false), | |
| }; | |
| let place_id = self.add_place(place_expr); | |
| if is_use { | |
| self.mark_place_used(place_id); | |
| let use_id = self.current_ast_ids().record_use(expr); | |
| self.current_use_def_map_mut() | |
| .record_use(place_id, use_id, node_key); | |
| } |
It might be better to validate that the inner expressions are also valid when building the SemanticIndex, or to let the parser propagate the invalid context to the outer expressions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dhruvmanila Would you mind also having a look at this, in particular the suggestion here to do part of the work in the parser?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be better to validate that the inner expressions are also valid when building the
SemanticIndex, or to let the parser propagate the invalid context to the outer expressions.
I think it makes sense that if an inner expression is invalid then the entire attribute expression is marked as invalid by the parser. This would then be automatically excluded by the semantic index builder. We do related changes using the helpers::set_expr_context function.
Do we only need to account for the top-level invalid expression or any nested invalid expressions as well? For example, in .a, it's only the top level value expression of the attribute expression that's invalid but in foo(1+).bar, it's the inner expression (+1) that's invalid instead of the outer expression foo(...) and the outer expression corresponds to the value field of an attribute expression. I'm assuming it's the former?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we only need to account for the top-level invalid expression or any nested invalid expressions as well? For example, in .a, it's only the top level value expression of the attribute expression that's invalid but in foo(1+).bar, it's the inner expression (+1) that's invalid instead of the outer expression foo(...) and the outer expression corresponds to the value field of an attribute expression. I'm assuming it's the former?
The only expressions that should have invalid context propagated are those that are simple enough that PlaceExpr::try_from succeeds.
A complex expression like f(1+).bar is not recorded as a place in the UseDefMap, so the outer attribute f(...).bar can be used as the load context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems a bit inconsistent because the parser shouldn't care whether the expression can be constructed by PlaceExpr or not. And, I don't think it really matters, right? Like, if there are any invalid expressions nested in the ExprAttribute, then the entire ExprAttribute is invalid and the parser shouldn't special case only specific expressions. But, if special casing is important, then I'd prefer to have this logic in ty and not in the parser.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is changing the context in the parser significantely simpler than doing it in ty? If not, I'd suggest doing it in ty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it won't be that simple change in the parser because we'd need to get this information (ExprContext::Invalid) up the AST to the ExprAttribute which could possibly be achieved by storing it in ParsedExpr (is_invalid: bool field) and using it in parse_attribute_expression. But, yeah, it might be simpler to do it in ty instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks everyone. If this really only causes unnecessary work, I'm not sure it's worth doing something here at all. I'll move this PR to in-review to fix the original crash. It comes up easily in the LSP/playground when editing code.
|
…re_help * 'main' of https://github.com/astral-sh/ruff: (34 commits) [docs] add capital one to who's using ruff (astral-sh#19248) [`pyupgrade`] Keyword arguments in `super` should suppress the `UP008` fix (astral-sh#19131) [`flake8-use-pathlib`] Add autofixes for `PTH100`, `PTH106`, `PTH107`, `PTH108`, `PTH110`, `PTH111`, `PTH112`, `PTH113`, `PTH114`, `PTH115`, `PTH117`, `PTH119`, `PTH120` (astral-sh#19213) [ty] Do not run `mypy_primer.yaml` when all changed files are Markdown files (astral-sh#19244) [`flake8-bandit`] Make example error out-of-the-box (`S412`) (astral-sh#19241) [`pydoclint`] Make example error out-of-the-box (`DOC501`) (astral-sh#19218) [ty] Add "kind" to completion suggestions [ty] Add type information to `all_members` API [ty] Expand API of `all_members` to return a struct [ty] Ecosystem analyzer PR comment workflow (astral-sh#19237) [ty] Merge `ty_macros` into `ruff_macros` (astral-sh#19229) [ty] Fix `ClassLiteral.into_callable` for dataclasses (astral-sh#19192) [ty] `dataclasses.field` support (astral-sh#19140) [ty] Fix panic for attribute expressions with empty value (astral-sh#19069) [ty] Return `CallableType` from `BoundMethodType.into_callable_type` (astral-sh#19193) [`flake8-bugbear`] Support non-context-manager calls in `B017` (astral-sh#19063) [ty] Improved diagnostic for reassignments of `Final` symbols (astral-sh#19214) [ty] Use full range for assignment definitions (astral-sh#19211) [`pylint`] Update `missing-maxsplit-arg` docs and error to suggest proper usage (`PLC0207`) (astral-sh#18949) [ty] Add `set -eu` to mypy-primer script (astral-sh#19212) ... # Conflicts: # crates/ty_python_semantic/src/types/class.rs
Summary
closes astral-sh/ty#738
Test Plan
Added corpus test