Skip to content

Commit c6e254f

Browse files
authored
General code nits (#8135)
* General nitpicks * Move Compiler::constant_truthiness to a ConstantData method * More code cleanup * bytecode::CodeFlags -> CodeFlags * More code clenaups
1 parent 866f7ca commit c6e254f

14 files changed

Lines changed: 364 additions & 490 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/codegen/src/compile.rs‎

Lines changed: 165 additions & 229 deletions
Large diffs are not rendered by default.

‎crates/codegen/src/ir.rs‎

Lines changed: 112 additions & 233 deletions
Large diffs are not rendered by default.

‎crates/codegen/src/string_parser.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustpython_wtf8::{CodePoint, Wtf8, Wtf8Buf};
1313
// use ruff_python_parser::{LexicalError, LexicalErrorType};
1414
type LexicalError = Infallible;
1515

16+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
1617
enum EscapedChar {
1718
Literal(CodePoint),
1819
Escape(char),

‎crates/compiler-core/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ bitflagset = { workspace = true }
1818
itertools = { workspace = true }
1919
malachite-bigint = { workspace = true }
2020
num-complex = { workspace = true }
21+
num-traits = { workspace = true }
2122

2223
lz4_flex = { workspace = true }
2324

‎crates/compiler-core/src/bytecode.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use core::{
1717
use itertools::Itertools;
1818
use malachite_bigint::BigInt;
1919
use num_complex::Complex64;
20+
use num_traits::Zero;
2021
use rustpython_wtf8::{Wtf8, Wtf8Buf};
2122

2223
pub use crate::bytecode::{
@@ -909,6 +910,31 @@ pub enum ConstantData {
909910
Ellipsis,
910911
}
911912

913+
impl ConstantData {
914+
/// Whether or not python would return True/False for the given constant data.
915+
///
916+
/// ```py
917+
/// bool(0) # False
918+
/// bool(1) # True
919+
/// bool([]) # False
920+
/// bool(...) # True
921+
/// ```
922+
#[must_use]
923+
pub fn truthiness(&self) -> bool {
924+
match self {
925+
Self::Tuple { elements } | Self::Frozenset { elements } => !elements.is_empty(),
926+
Self::Integer { value } => !value.is_zero(),
927+
Self::Float { value } => *value != 0.0,
928+
Self::Complex { value } => value.re != 0.0 || value.im != 0.0,
929+
Self::Boolean { value } => *value,
930+
Self::Str { value } => !value.is_empty(),
931+
Self::Bytes { value } => !value.is_empty(),
932+
Self::Code { .. } | Self::Slice { .. } | Self::Ellipsis => true,
933+
Self::None => false,
934+
}
935+
}
936+
}
937+
912938
impl PartialEq for ConstantData {
913939
fn eq(&self, other: &Self) -> bool {
914940
match (self, other) {

‎crates/vm/src/function/buffer.rs‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,21 @@ use crate::{
1313
pub struct ArgBytesLike(PyBuffer);
1414

1515
impl PyObject {
16-
pub fn try_bytes_like<R>(
17-
&self,
18-
vm: &VirtualMachine,
19-
f: impl FnOnce(&[u8]) -> R,
20-
) -> PyResult<R> {
16+
pub fn try_bytes_like<F, R>(&self, vm: &VirtualMachine, f: F) -> PyResult<R>
17+
where
18+
F: FnOnce(&[u8]) -> R,
19+
{
2120
let buffer = PyBuffer::try_from_borrowed_object(vm, self)?;
2221
buffer
2322
.as_contiguous()
2423
.map(|x| f(&x))
2524
.ok_or_else(|| vm.new_buffer_error("non-contiguous buffer is not a bytes-like object"))
2625
}
2726

28-
pub fn try_rw_bytes_like<R>(
29-
&self,
30-
vm: &VirtualMachine,
31-
f: impl FnOnce(&mut [u8]) -> R,
32-
) -> PyResult<R> {
27+
pub fn try_rw_bytes_like<F, R>(&self, vm: &VirtualMachine, f: F) -> PyResult<R>
28+
where
29+
F: FnOnce(&mut [u8]) -> R,
30+
{
3331
let buffer = PyBuffer::try_from_borrowed_object(vm, self)?;
3432
buffer
3533
.as_contiguous_mut()
@@ -207,7 +205,10 @@ impl ArgAsciiBuffer {
207205
}
208206

209207
#[inline]
210-
pub fn with_ref<R>(&self, f: impl FnOnce(&[u8]) -> R) -> R {
208+
pub fn with_ref<F, R>(&self, f: F) -> R
209+
where
210+
F: FnOnce(&[u8]) -> R,
211+
{
211212
match self {
212213
Self::String(s) => f(s.as_bytes()),
213214
Self::Buffer(buffer) => buffer.with_ref(f),

‎crates/vm/src/function/builtin.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ pub trait PyNativeFn:
1111
Fn(&VirtualMachine, FuncArgs) -> PyResult + PyThreadingConstraint + 'static
1212
{
1313
}
14-
impl<F: Fn(&VirtualMachine, FuncArgs) -> PyResult + PyThreadingConstraint + 'static> PyNativeFn
15-
for F
14+
15+
impl<F> PyNativeFn for F where
16+
F: Fn(&VirtualMachine, FuncArgs) -> PyResult + PyThreadingConstraint + 'static
1617
{
1718
}
1819

@@ -103,8 +104,10 @@ use sealed::PyNativeFnInternal;
103104

104105
#[doc(hidden)]
105106
pub struct OwnedParam<T>(PhantomData<T>);
107+
106108
#[doc(hidden)]
107109
pub struct BorrowedParam<T>(PhantomData<T>);
110+
108111
#[doc(hidden)]
109112
pub struct RefParam<T>(PhantomData<T>);
110113

‎crates/vm/src/function/either.rs‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ pub enum Either<A, B> {
88
B(B),
99
}
1010

11-
impl<A: Borrow<PyObject>, B: Borrow<PyObject>> Borrow<PyObject> for Either<A, B> {
11+
impl<A, B> Borrow<PyObject> for Either<A, B>
12+
where
13+
A: Borrow<PyObject>,
14+
B: Borrow<PyObject>,
15+
{
1216
#[inline(always)]
1317
fn borrow(&self) -> &PyObject {
1418
match self {
@@ -18,7 +22,11 @@ impl<A: Borrow<PyObject>, B: Borrow<PyObject>> Borrow<PyObject> for Either<A, B>
1822
}
1923
}
2024

21-
impl<A: AsRef<PyObject>, B: AsRef<PyObject>> AsRef<PyObject> for Either<A, B> {
25+
impl<A, B> AsRef<PyObject> for Either<A, B>
26+
where
27+
A: AsRef<PyObject>,
28+
B: AsRef<PyObject>,
29+
{
2230
#[inline(always)]
2331
fn as_ref(&self) -> &PyObject {
2432
match self {
@@ -28,7 +36,11 @@ impl<A: AsRef<PyObject>, B: AsRef<PyObject>> AsRef<PyObject> for Either<A, B> {
2836
}
2937
}
3038

31-
impl<A: Into<Self>, B: Into<Self>> From<Either<A, B>> for PyObjectRef {
39+
impl<A, B> From<Either<A, B>> for PyObjectRef
40+
where
41+
A: Into<Self>,
42+
B: Into<Self>,
43+
{
3244
#[inline(always)]
3345
fn from(value: Either<A, B>) -> Self {
3446
match value {
@@ -38,7 +50,11 @@ impl<A: Into<Self>, B: Into<Self>> From<Either<A, B>> for PyObjectRef {
3850
}
3951
}
4052

41-
impl<A: ToPyObject, B: ToPyObject> ToPyObject for Either<A, B> {
53+
impl<A, B> ToPyObject for Either<A, B>
54+
where
55+
A: ToPyObject,
56+
B: ToPyObject,
57+
{
4258
#[inline(always)]
4359
fn to_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
4460
match self {

‎crates/vm/src/function/getset.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
/*! Python `attribute` descriptor class. (PyGetSet)
2-
3-
*/
1+
//! Python `attribute` descriptor class. (PyGetSet)
42
use crate::{
53
Py, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
64
convert::ToPyResult,

0 commit comments

Comments
 (0)