[ty] Narrow on negative subscript indexing#22682
Merged
charliermarsh merged 3 commits intomainfrom Jan 19, 2026
Merged
Conversation
Typing conformance resultsNo changes detected ✅ |
|
AlexWaygood
approved these changes
Jan 18, 2026
Member
AlexWaygood
left a comment
There was a problem hiding this comment.
I can't believe this went unnoticed for so long 😅 Thank you!
Comment on lines
+196
to
+213
| // Handle negative integer subscripts, like `x[-1]`. | ||
| ast::Expr::UnaryOp(ast::ExprUnaryOp { | ||
| op: ast::UnaryOp::USub, | ||
| operand, | ||
| .. | ||
| }) => match operand.as_ref() { | ||
| ast::Expr::NumberLiteral(ast::ExprNumberLiteral { | ||
| value: ast::Number::Int(index), | ||
| .. | ||
| }) => { | ||
| let _ = write!(path, "-{index}"); | ||
| segments.push(SegmentInfo::new( | ||
| SegmentKind::IntSubscript, | ||
| start_offset, | ||
| )); | ||
| } | ||
| _ => return None, | ||
| }, |
Member
There was a problem hiding this comment.
For completeness (though it's obviously unlikely to come up), shall we also handle unary +? x[+5], etc.
Member
Author
There was a problem hiding this comment.
Yeah will do. I was also gonna put some other variants in a separate PR.
Member
There was a problem hiding this comment.
yes, I guess ideally we'd also handle things like ++42 and --42... definitely puntable, this fixes loads of false positives as-is!
02fcdbc to
afc19fd
Compare
afc19fd to
7a2563f
Compare
90dcce0 to
b9b6d69
Compare
7a2563f to
a598617
Compare
Member
Author
|
(Rebased on main and simplified the tests a bit so we can merge separately from the larger intersection PR.) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Negative subscripts are also indicative of a thing being subcriptable: