Skip to content

Cannot Use NoOp Procedural Macro as Custom Inner Attribute #143003

Open
@benwu25

Description

@benwu25

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

added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Jun 25, 2025
theemathas

theemathas commented on Jun 25, 2025

@theemathas
Contributor

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

benwu25 commented on Jun 25, 2025

@benwu25
Author

Ok, if I adjust the implementation of the macro to be--

#[proc_macro_attribute]
pub fn noop(_args: TokenStream, input: TokenStream) -> TokenStream {
    input.into()
}

I only receive one error:

error: #[prelude_import] is for use by rustc only

If I add #![allow(rust_2024_prelude_collisions)], the error still occurs.
After running cargo +nightly expand, the top of main.rs looks like

#![feature(prelude_import)]
#![feature(custom_inner_attributes)]
#![allow(rust_2024_prelude_collisions)]
#[prelude_import]
use std::prelude::rust_2024::*;
#[macro_use]
added
C-discussionCategory: Discussion or questions that doesn't represent real issues.
A-attributesArea: Attributes (`#[…]`, `#![…]`)
and removed
C-bugCategory: This is a bug.
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Jun 25, 2025
benwu25

benwu25 commented on Jun 26, 2025

@benwu25
Author

I guess #41430 being still open means I can't get this to work currently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)A-proc-macrosArea: Procedural macrosC-discussionCategory: Discussion or questions that doesn't represent real issues.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @theemathas@fmease@rustbot@benwu25

        Issue actions

          Cannot Use NoOp Procedural Macro as Custom Inner Attribute · Issue #143003 · rust-lang/rust