-
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
crates/ty_python_semantic/resources/corpus/88_regression_issue_738.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| [. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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?
Uh oh!
There was an error while loading. Please reload this page.
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: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 theUseDefMap. 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
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.
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_contextfunction.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 levelvalueexpression of the attribute expression that's invalid but infoo(1+).bar, it's the inner expression (+1) that's invalid instead of the outer expressionfoo(...)and the outer expression corresponds to thevaluefield 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.
The only expressions that should have invalid context propagated are those that are simple enough that
PlaceExpr::try_fromsucceeds.A complex expression like
f(1+).baris not recorded as a place in theUseDefMap, so the outer attributef(...).barcan 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
PlaceExpror not. And, I don't think it really matters, right? Like, if there are any invalid expressions nested in theExprAttribute, then the entireExprAttributeis 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 theExprAttributewhich could possibly be achieved by storing it inParsedExpr(is_invalid: boolfield) and using it inparse_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.