-
Notifications
You must be signed in to change notification settings - Fork 1.4k
#[newtype_oparg] derive macro
#7489
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e599382
Basic struct support
ShaharNaveh b60f972
Base enum support
ShaharNaveh 936a079
Modify code for new macro
ShaharNaveh 94d66dd
No need for full feature
ShaharNaveh f5c2f9b
Remove debug code
ShaharNaveh 9fbc6d3
Impl hash
ShaharNaveh 49d2fb6
Merge remote-tracking branch 'upstream/main' into macro-newtype-oparg
ShaharNaveh f7b11b6
syn full feature
ShaharNaveh f21d855
Remove unwrap
ShaharNaveh 77eae15
Use `TryFrom`
ShaharNaveh 4d6c725
Convert `ConvertValueOparg` to new macro
ShaharNaveh c702fbe
Fix bug where `catch_all` is not the last element
ShaharNaveh 796b61c
Don't match on 255
ShaharNaveh 9691476
Validate `catch_all` variant drfinition
ShaharNaveh File filter
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
commit e599382ac4b50d4f98e76b86dc0b345c96370e88
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| [lints] | ||
| workspace = true | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
OpArgTypefor any struct/enum that uses the macro.I can remove that and write:
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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