8000 Use `const fn` where possible by ShaharNaveh · Pull Request #5894 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Use const fn where possible #5894

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 17 commits into from
Jul 4, 2025
Prev Previous commit
Next Next commit
compiler
  • Loading branch information
ShaharNaveh committed Jul 3, 2025
commit a18f0c2a412ab2b67d341b90107559df4a141a8c
12 changes: 6 additions & 6 deletions compiler/core/src/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl OpArg {

/// Returns how many CodeUnits a instruction with this op_arg will be encoded as
#[inline]
pub fn instr_size(self) -> usize {
pub const fn instr_size(self) -> usize {
(self.0 > 0xff) as usize + (self.0 > 0xff_ff) as usize + (self.0 > 0xff_ff_ff) as usize + 1
}

Expand Down Expand Up @@ -214,7 +214,7 @@ impl OpArgState {
OpArg(self.state)
}
#[inline(always)]
pub fn reset(&mut self) {
pub const fn reset(&mut self) {
self.state = 0
}
}
Expand Down Expand Up @@ -280,7 +280,7 @@ pub struct Arg<T: OpArgType>(PhantomData<T>);

impl<T: OpArgType> Arg<T> {
#[inline]
pub fn marker() -> Self {
pub const fn marker() -> Self {
Arg(PhantomData)
}
#[inline]
Expand Down Expand Up @@ -660,7 +660,7 @@ pub struct CodeUnit {
const _: () = assert!(mem::size_of::<CodeUnit>() == 2);

impl CodeUnit {
pub fn new(op: Instruction, arg: OpArgByte) -> Self {
pub const fn new(op: Instruction, arg: OpArgByte) -> Self {
Self { op, arg }
}
}
Expand Down Expand Up @@ -1150,7 +1150,7 @@ impl<C: Constant> fmt::Display for CodeObject<C> {
impl Instruction {
/// Gets the label stored inside this instruction, if it exists
#[inline]
pub fn label_arg(&self) -> Option<Arg<Label>> {
pub const fn label_arg(&self) -> Option<Arg<Label>> {
match self {
Jump { target: l }
| JumpIfTrue { target: l }
Expand All @@ -1177,7 +1177,7 @@ impl Instruction {
/// let jump_inst = Instruction::Jump { target: Arg::marker() };
/// assert!(jump_inst.unconditional_branch())
/// ```
pub fn unconditional_branch(&self) -> bool {
pub const fn unconditional_branch(&self) -> bool {
matches!(
self,
Jump { .. }
Expand Down
0