10000 Use `Self` where possible by ShaharNaveh · Pull Request #5892 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Use Self where possible #5892

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 11 commits into from
Jul 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
compiler core
  • Loading branch information
ShaharNaveh committed Jul 3, 2025
commit dbdecccac351debcf4f45d562a8d3342db84d26a
54 changes: 27 additions & 27 deletions compiler/core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ impl Constant for ConstantData {
fn borrow_constant(&self) -> BorrowedConstant<'_, Self> {
use BorrowedConstant::*;
match self {
ConstantData::Integer { value } => Integer { value },
ConstantData::Float { value } => Float { value: *value },
ConstantData::Complex { value } => Complex { value: *value },
ConstantData::Boolean { value } => Boolean { value: *value },
ConstantData::Str { value } => Str { value },
ConstantData::Bytes { value } => Bytes { value },
ConstantData::Code { code } => Code { code },
ConstantData::Tuple { elements } => Tuple { elements },
ConstantData::None => None,
ConstantData::Ellipsis => Ellipsis,
Self::Integer { value } => Integer { value },
Self::Float { value } => Float { value: *value },
Self::Complex { value } => Complex { value: *value },
Self::Boolean { value } => Boolean { value: *value },
Self::Str { value } => Str { value },
Self::Bytes { value } => Bytes { value },
Self::Code { code } => Code { code },
Self::Tuple { elements } => Tuple { elements },
Self::None => None,
Self::Ellipsis => Ellipsis,
}
}
}
Expand Down Expand Up @@ -136,15 +136,15 @@ bitflags! {
}

impl CodeFlags {
pub const NAME_MAPPING: &'static [(&'static str, CodeFlags)] = &[
("GENERATOR", CodeFlags::IS_GENERATOR),
("COROUTINE", CodeFlags::IS_COROUTINE),
pub const NAME_MAPPING: &'static [(&'static str, Self)] = &[
("GENERATOR", Self::IS_GENERATOR),
("COROUTINE", Self::IS_COROUTINE),
(
"ASYNC_GENERATOR",
Self::from_bits_truncate(Self::IS_GENERATOR.bits() | Self::IS_COROUTINE.bits()),
),
("VARARGS", CodeFlags::HAS_VARARGS),
("VARKEYWORDS", CodeFlags::HAS_VARKEYWORDS),
("VARARGS", Self::HAS_VARARGS),
("VARKEYWORDS", Self::HAS_VARKEYWORDS),
];
}

Expand All @@ -154,7 +154,7 @@ impl CodeFlags {
pub struct OpArgByte(pub u8);
impl OpArgByte {
pub const fn null() -> Self {
OpArgByte(0)
Self(0)
}
}
impl fmt::Debug for OpArgByte {
Expand All @@ -169,7 +169,7 @@ impl fmt::Debug for OpArgByte {
pub struct OpArg(pub u32);
impl OpArg {
pub const fn null() -> Self {
OpArg(0)
Self(0)
}

/// Returns how many CodeUnits a instruction with this op_arg will be encoded as
Expand Down Expand Up @@ -281,7 +281,7 @@ pub struct Arg<T: OpArgType>(PhantomData<T>);
impl<T: OpArgType> Arg<T> {
#[inline]
pub fn marker() -> Self {
Arg(PhantomData)
Self(PhantomData)
}
#[inline]
pub fn new(arg: T) -> (Self, OpArg) {
Expand Down Expand Up @@ -333,7 +333,7 @@ pub struct Label(pub u32);
impl OpArgType for Label {
#[inline(always)]
fn from_op_arg(x: u32) -> Option<Self> {
Some(Label(x))
Some(Self(x))
}
#[inline(always)]
fn to_op_arg(self) -> u32 {
Expand All @@ -351,10 +351,10 @@ impl OpArgType for ConversionFlag {
#[inline]
fn from_op_arg(x: u32) -> Option<Self> {
match x as u8 {
b's' => Some(ConversionFlag::Str),
b'a' => Some(ConversionFlag::Ascii),
b'r' => Some(ConversionFlag::Repr),
std::u8::MAX => Some(ConversionFlag::None),
b's' => Some(Self::Str),
b'a' => Some(Self::Ascii),
b'r' => Some(Self::Repr),
std::u8::MAX => Some(Self::None),
_ => None,
}
}
Expand Down Expand Up @@ -631,9 +631,9 @@ const _: () = assert!(mem::size_of::<Instruction>() == 1);

impl From<Instruction> for u8 {
#[inline]
fn from(ins: Instruction) -> u8 {
fn from(ins: Instruction) -> Self {
// SAFETY: there's no padding bits
unsafe { std::mem::transmute::<Instruction, u8>(ins) }
unsafe { std::mem::transmute::<Instruction, Self>(ins) }
}
}

Expand All @@ -643,7 +643,7 @@ impl TryFrom<u8> for Instruction {
#[inline]
fn try_from(value: u8) -> Result<Self, crate::marshal::MarshalError> {
if value <= u8::from(LAST_INSTRUCTION) {
Ok(unsafe { std::mem::transmute::<u8, Instruction>(value) })
Ok(unsafe { std::mem::transmute::<u8, Self>(value) })
} else {
Err(crate::marshal::MarshalError::InvalidBytecode)
}
Expand Down Expand Up @@ -680,7 +680,7 @@ bitflags! {
impl OpArgType for MakeFunctionFlags {
#[inline(always)]
fn from_op_arg(x: u32) -> Option<Self> {
MakeFunctionFlags::from_bits(x as u8)
Self::from_bits(x as u8)
}
#[inline(always)]
fn to_op_arg(self) -> u32 {
Expand Down
8 changes: 4 additions & 4 deletions compiler/core/src/frozen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl FrozenCodeObject<Vec<u8>> {
let mut data = Vec::new();
marshal::serialize_code(&mut data, code);
let bytes = lz4_flex::compress_prepend_size(&data);
FrozenCodeObject { bytes }
Self { bytes }
}
}

Expand All @@ -42,8 +42,8 @@ pub struct FrozenLib<B: ?Sized = [u8]> {
}

impl<B: AsRef<[u8]> + ?Sized> FrozenLib<B> {
pub const fn from_ref(b: &B) -> &FrozenLib<B> {
unsafe { &*(b as *const B as *const FrozenLib<B>) }
pub const fn from_ref(b: &B) -> &Self {
unsafe { &*(b as *const B as *const Self) }
}
8000
/// Decode a library to a iterable of frozen modules
Expand Down Expand Up @@ -100,7 +100,7 @@ fn read_entry<'a>(

impl FrozenLib<Vec<u8>> {
/// Encode the given iterator of frozen modules into a compressed vector of bytes
pub fn encode<'a, I, B: AsRef<[u8]>>(lib: I) -> FrozenLib<Vec<u8>>
pub fn encode<'a, I, B: AsRef<[u8]>>(lib: I) -> Self
where
I: IntoIterator<Item = (&'a str, FrozenModule<B>), IntoIter: ExactSizeIterator + Clone>,
{
Expand Down
6 changes: 3 additions & 3 deletions compiler/core/src/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ impl std::str::FromStr for Mode {
// To support `builtins.compile()` `mode` argument
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"exec" => Ok(Mode::Exec),
"eval" => Ok(Mode::Eval),
"single" => Ok(Mode::Single),
"exec" => Ok(Self::Exec),
"eval" => Ok(Self::Eval),
"single" => Ok(Self::Single),
_ => Err(ModeParseError),
}
}
Expand Down
0