[red-knot] Corrections and improvements to intersection simplification#15475
Merged
AlexWaygood merged 6 commits intomainfrom Jan 14, 2025
Merged
[red-knot] Corrections and improvements to intersection simplification#15475AlexWaygood merged 6 commits intomainfrom
AlexWaygood merged 6 commits intomainfrom
Conversation
Contributor
|
AlexWaygood
commented
Jan 14, 2025
Member
Author
|
2% speedup on the Codspeed incremental benchmark?? I'll take it! https://codspeed.io/astral-sh/ruff/branches/alex%2Ftruthy-bools |
AlexWaygood
commented
Jan 14, 2025
Comment on lines
-351
to
+424
| // bool & ~Literal[True] = Literal[False] | ||
| // bool & ~AlwaysTruthy = Literal[False] | ||
| Type::BooleanLiteral(_) | Type::AlwaysFalsy | Type::AlwaysTruthy | ||
| if self.positive.contains(&KnownClass::Bool.to_instance(db)) => | ||
| { | ||
| *self = Self::default(); | ||
| self.positive.insert(Type::BooleanLiteral( | ||
| new_negative.bool(db) != Truthiness::AlwaysTrue, | ||
| )); | ||
| // `bool & ~AlwaysTruthy` -> `bool & Literal[False]` | ||
| // `bool & ~Literal[True]` -> `bool & Literal[False]` | ||
| Type::AlwaysTruthy | Type::BooleanLiteral(true) if contains_bool() => { | ||
| self.add_positive(db, Type::BooleanLiteral(false)); | ||
| } |
Member
Author
There was a problem hiding this comment.
I suspect the codspeed improvement is because we're now doing fewer Salsa lookups eagerly due to changes like this (KnownClass::Bool.to_instance(db) eagerly looked up the bool type in the Salsa db) whereas the new contains_bool() closure here is lazier
sharkdp
reviewed
Jan 14, 2025
crates/red_knot_python_semantic/resources/mdtest/intersection_types.md
Outdated
Show resolved
Hide resolved
sharkdp
reviewed
Jan 14, 2025
MichaReiser
reviewed
Jan 14, 2025
57b0b30 to
3bf1a6a
Compare
3bf1a6a to
793be94
Compare
119f291 to
147b3d7
Compare
carljm
approved these changes
Jan 14, 2025
Contributor
carljm
left a comment
There was a problem hiding this comment.
This looks good to me, no notes!
Thank you!
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
I noticed that we were incorrectly oversimplifying certain intersections, for example:
Here,
bool & ~AlwaysTruthycan indeed be simplified toLiteral[False], and thereforestr & ~AlwaysTruthy & boolcan be simplified tostr & Literal[False], which then simplifies further toNeversincestrandLiteral[False]are disjoint. But that's not what we're doing in the example above: instead, we're attempting to simplifystrout of the intersection as part of the initial simplification, which then means that we never realise further along in the intersection simplification algorithm that the entire intersection simplifies toNever.This PR fixes the bug, and also adds some more simplifications for intersections that we weren't doing previously:
bool & AlwaysTruthy->Literal[True]bool & AlwaysFalsy->Literal[False]LiteralString & AlwaysTruthy->LiteralString & ~Literal[""]LiteralString & AlwaysFalsy->Literal[""]This furthers our aim of having equivalent types use identical internal representations wherever it's feasible to do so.
Test Plan
cargo test -p red_knot_python_semantic --test mdtestuvx pre-commit run -a