Skip to content

Commit

Permalink
[red knot] Use memmem::find instead of custom version (#13750)
Browse files Browse the repository at this point in the history
This is a follow-up on #13746:

- Use `memmem::find` instead of rolling our own inferior version.
- Avoid `x.as_ref()` calls using `&**x`
  • Loading branch information
sharkdp authored Oct 14, 2024
1 parent 6048f33 commit 04b636c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/red_knot_python_semantic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ hashbrown = { workspace = true }
smallvec = { workspace = true }
static_assertions = { workspace = true }
test-case = { workspace = true }
memchr = { workspace = true }

[dev-dependencies]
ruff_db = { workspace = true, features = ["os", "testing"] }
Expand Down
22 changes: 8 additions & 14 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2661,27 +2661,21 @@ impl<'db> TypeInferenceBuilder<'db> {
}

(Type::BytesLiteral(salsa_b1), Type::BytesLiteral(salsa_b2)) => {
let contains_subsequence = |needle: &[u8], haystack: &[u8]| {
if needle.is_empty() {
true
} else {
haystack
.windows(needle.len())
.any(|window| window == needle)
}
};

let b1 = salsa_b1.value(self.db).as_ref();
let b2 = salsa_b2.value(self.db).as_ref();
let b1 = &**salsa_b1.value(self.db);
let b2 = &**salsa_b2.value(self.db);
match op {
ast::CmpOp::Eq => Some(Type::BooleanLiteral(b1 == b2)),
ast::CmpOp::NotEq => Some(Type::BooleanLiteral(b1 != b2)),
ast::CmpOp::Lt => Some(Type::BooleanLiteral(b1 < b2)),
ast::CmpOp::LtE => Some(Type::BooleanLiteral(b1 <= b2)),
ast::CmpOp::Gt => Some(Type::BooleanLiteral(b1 > b2)),
ast::CmpOp::GtE => Some(Type::BooleanLiteral(b1 >= b2)),
ast::CmpOp::In => Some(Type::BooleanLiteral(contains_subsequence(b1, b2))),
ast::CmpOp::NotIn => Some(Type::BooleanLiteral(!contains_subsequence(b1, b2))),
ast::CmpOp::In => {
Some(Type::BooleanLiteral(memchr::memmem::find(b2, b1).is_some()))
}
ast::CmpOp::NotIn => {
Some(Type::BooleanLiteral(memchr::memmem::find(b2, b1).is_none()))
}
ast::CmpOp::Is => {
if b1 == b2 {
Some(KnownClass::Bool.to_instance(self.db))
Expand Down

0 comments on commit 04b636c

Please sign in to comment.