Skip to content
Closed
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
Next Next commit
Basic struct support
  • Loading branch information
ShaharNaveh committed Mar 23, 2026
commit e599382ac4b50d4f98e76b86dc0b345c96370e88
10 changes: 10 additions & 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ rustpython-stdlib = { path = "crates/stdlib", default-features = false, version
rustpython-sre_engine = { path = "crates/sre_engine", version = "0.5.0" }
rustpython-wtf8 = { path = "crates/wtf8", version = "0.5.0" }
rustpython-doc = { path = "crates/doc", version = "0.5.0" }
rustpython-macros = { path = "crates/macros", version = "0.5.0" }

# Ruff tag 0.15.6 is based on commit e4c7f357777a2fdd34dbe6a98b1b7d3e7488f675
# at the time of this capture. We use the commit hash to ensure reproducible builds.
Expand Down
1 change: 1 addition & 0 deletions crates/compiler-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ license.workspace = true
# rustpython-parser-core = { workspace = true, features=["location"] }
ruff_source_file = { workspace = true }
rustpython-wtf8 = { workspace = true }
rustpython-macros = { workspace = true }

bitflags = { workspace = true }
bitflagset = { workspace = true }
Expand Down
107 changes: 15 additions & 92 deletions crates/compiler-core/src/bytecode/oparg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use core::fmt;

use crate as rustpython_compiler_core; // Required for newtype_oparg macro
use rustpython_macros::newtype_oparg;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, this is circular. unhappy to add a crate only for these a few types

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw, this is only needed because I'm implementing OpArgType for any struct/enum that uses the macro.

I can remove that and write:

// oparg.rs

impl OpArgType for A {}

impl OpArgType for B {}

impl OpArgType for C {}

...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if avoidable with reasonable effort, not using proc macro is better

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think the proc macro adds value, but not a hill I'm willing to die on tbh

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets go without proc macro. it is worth not to add too many crates when publishing


use crate::{
bytecode::{CodeUnit, instruction::Instruction},
marshal::MarshalError,
Expand Down Expand Up @@ -745,103 +748,23 @@ impl fmt::Display for UnpackExArgs {
}
}

macro_rules! newtype_oparg {
(
$(#[$oparg_meta:meta])*
$vis:vis struct $name:ident(u32)
) => {
$(#[$oparg_meta])*
$vis struct $name(u32);

impl $name {
/// Creates a new [`$name`] instance.
#[must_use]
pub const fn new(value: u32) -> Self {
Self(value)
}
#[newtype_oparg]
pub struct ConstIdx;

/// Alias to [`$name::new`].
#[must_use]
pub const fn from_u32(value: u32) -> Self {
Self::new(value)
}
#[newtype_oparg]
pub struct VarNum;

/// Returns the oparg as a `u32` value.
#[must_use]
pub const fn as_u32(self) -> u32 {
self.0
}
#[newtype_oparg]
pub struct VarNums;

/// Returns the oparg as a `usize` value.
#[must_use]
pub const fn as_usize(self) -> usize {
self.0 as usize
}
}
#[newtype_oparg]
pub struct LoadAttr;

impl From<u32> for $name {
fn from(value: u32) -> Self {
Self::from_u32(value)
}
}

impl From<$name> for u32 {
fn from(value: $name) -> Self {
value.as_u32()
}
}

impl From<$name> for usize {
fn from(value: $name) -> Self {
value.as_usize()
}
}
#[newtype_oparg]
pub struct LoadSuperAttr;

impl ::core::fmt::Display for $name {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
self.0.fmt(f)
}
}

impl OpArgType for $name {}
}
}

newtype_oparg!(
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct ConstIdx(u32)
);

newtype_oparg!(
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct VarNum(u32)
);

newtype_oparg!(
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct VarNums(u32)
);

newtype_oparg!(
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct LoadAttr(u32)
);

newtype_oparg!(
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct LoadSuperAttr(u32)
);

newtype_oparg!(
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
#[repr(transparent)]
pub struct Label(u32)
);
#[newtype_oparg]
pub struct Label;

impl VarNums {
#[must_use]
Expand Down
20 changes: 20 additions & 0 deletions crates/macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "rustpython-macros"
version.workspace = true
authors.workspace = true
edition.workspace = true
rust-version.workspace = true
repository.workspace = true
license.workspace = true

[lib]
proc-macro = true
doctest = false

[dependencies]
proc-macro2 = { workspace = true }
quote = { workspace = true }
syn = { workspace = true }
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

[lints]
workspace = true
24 changes: 24 additions & 0 deletions crates/macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//! This crate implements internal macros for the `rustpython` library.

use proc_macro::TokenStream;
use syn::{Item, parse_macro_input};

mod newtype_oparg;

#[proc_macro_attribute]
pub fn newtype_oparg(_metadata: TokenStream, input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as Item);

let output = match input {
Item::Enum(data) => {
newtype_oparg::handle_enum(data).unwrap_or_else(|e| e.to_compile_error())
}
Item::Struct(data) => {
newtype_oparg::handle_struct(data).unwrap_or_else(|e| e.to_compile_error())
}
_ => syn::Error::new_spanned(input, "newtype_oparg only supports structs and enums")
.to_compile_error(),
};

output.into()
}
107 changes: 107 additions & 0 deletions crates/macros/src/newtype_oparg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use syn::{Error, ItemEnum, ItemStruct, spanned::Spanned};

pub(super) fn handle_struct(item: ItemStruct) -> syn::Result<proc_macro2::TokenStream> {
if !item.fields.is_empty() {
return Err(Error::new(
item.span(),
"A new type oparg cannot have any fields.",
));
}

if !item.generics.params.is_empty() {
return Err(Error::new(
item.span(),
"A new type oparg cannot be generic.",
));
}

let ItemStruct {
attrs,
vis,
struct_token,
ident,
generics: _,
fields: _,
semi_token,
} = item;

let semi_token = semi_token.unwrap_or_default();
let output = quote::quote! {
#(#attrs)*
#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)]
#vis #struct_token #ident(u32)#semi_token

impl #ident {
#[must_use]
#vis const fn new(value: u32) -> Self {
Self::from_u32(value)
}

#[must_use]
#vis const fn from_u32(value: u32) -> Self {
Self(value)
}

/// Returns the oparg as a `u32` value.
#[must_use]
#vis const fn as_u32(self) -> u32 {
self.0
}

/// Returns the oparg as a `usize` value.
#[must_use]
#vis const fn as_usize(self) -> usize {
self.0 as usize
}
}

impl From<u32> for #ident {
fn from(value: u32) -> Self {
Self::from_u32(value)
}
}

impl From<#ident> for u32 {
fn from(value: #ident) -> Self {
value.0
}
}

impl From<#ident> for usize {
fn from(value: #ident) -> Self {
value.as_usize()
}
}

impl ::core::fmt::Display for #ident {
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
self.0.fmt(f)
}
}

impl rustpython_compiler_core::bytecode::OpArgType for #ident {}
};

Ok(output)
}

pub(super) fn handle_enum(item: ItemEnum) -> syn::Result<proc_macro2::TokenStream> {
let ItemEnum {
attrs,
vis,
enum_token,
ident,
generics: _,
fields: _,
variants,
} = item;

let output = quote::quote! {
#(#attrs)*
#[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd)]
#vis #enum_token #ident {
}
};

Ok(output)
}