rust-lang / rust-lang/rust

Cannot Use NoOp Procedural Macro as Custom Inner Attribute

Open
#143,003 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-attributes A-proc-macros C-discussion
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the example from main.rs with cargo +nightly run, then compare the result from cargo +nightly expand. Read the noop procedural macro implementation and the custom_inner_attributes usage together; done means the expected support or limitation is established and the reported expansion errors are addressed or documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.