Description
Hi, I am currently trying to use a procedural macro as a custom inner attribute to apply my macro to every function in my crate without manually adding it.
I am first trying to use a noop procedural macro which does not modify anything. This is the implementation of that macro--
#[proc_macro_attribute]
pub fn noop(_args: TokenStream, input: TokenStream) -> TokenStream {
let f = parse_macro_input!(input as ItemFn);
quote!(#f).into()
}
Then, in my main crate, I added this to the top of main.rs
--
#![feature(custom_inner_attributes)]
#![my_macro_crate::noop]
In main.rs
, there is my main
function, and a few other functions to test if the macro will work when I use my real macro.
After running cargo +nightly run
, I receive 4 errors--
error: expected square brackets
--> main.rs:1:1
(it also red highlights the end of the file)
error: #[panic_handler]
function required, but not found
error: unwinding panics are not supported without std
error: main
function not found
Is it expected for any procedural macros to work as custom inner attributes? Since this NoOp macro does not compile, is there something I did incorrectly in my setup?
Also, when I run cargo +nightly expand > output.rs
, the only error is "expected square brackets", which also underlines the end of the file. And the only contents of output.rs
is
#![feature(prelude_import)]
Meta
I am using a nightly build of rustc
, installed with cargo
a few days ago.
Activity
theemathas commentedon Jun 25, 2025
That proc macro is not a noop. It attempts to parse one function definition (since you specified
ItemFn
), and output just that one function. Since what the macro received is not just one function definition, it probably output an error or something.benwu25 commentedon Jun 25, 2025
Ok, if I adjust the implementation of the macro to be--
I only receive one error:
error:
#[prelude_import]
is for use byrustc
onlyIf I add
#![allow(rust_2024_prelude_collisions)]
, the error still occurs.After running
cargo +nightly expand
, the top ofmain.rs
looks likebenwu25 commentedon Jun 26, 2025
I guess #41430 being still open means I can't get this to work currently.