Skip to content

proc_macro_attribute doesn't work on extern functions #48747

Closed
@mjbshaw

Description

@mjbshaw

Given a proc-macro crate named demo_macros, which contains a single nop_attribute procedural macro:

#![feature(proc_macro)]
extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro_attribute]
pub fn nop_attribute(_: TokenStream, input: TokenStream) -> TokenStream {
  input
}

Given a lib crate named demo, which attempts to use the nop_attribute procedural macro on a function within an extern block:

#![feature(proc_macro)]
extern crate demo_macros;
use demo_macros::nop_attribute;

extern {
  #[nop_attribute]
  pub fn fabs(_: f64) -> f64;
}

Attempting to cargo +nightly build this results in the following error:

   Compiling demo_macros v0.1.0 (file:///private/tmp/demo/demo_macros)
   Compiling demo v0.1.0 (file:///private/tmp/demo)
error[E0658]: The attribute `nop_attribute` is currently unknown to the compiler and may have meaning added to it in the future (see issue #29642)
 --> src/lib.rs:6:3
  |
6 |   #[nop_attribute]
  |   ^^^^^^^^^^^^^^^^
  |
  = help: add #![feature(custom_attribute)] to the crate attributes to enable

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0658"
error: Could not compile `demo`.

To learn more, run the command again with --verbose

If I delete the extern block and change the code to just be #[nop_attribute] pub fn fabs(_: f64) -> f64 { 0.0 } then it compiles just fine. For some reason, attempting to apply a procedural macro attribute to an extern function doesn't work.

My rustc --version:

rustc 1.26.0-nightly (e026b59cf 2018-03-03)

You can fully reproduce the issue by executing the following sequence of shell commands:

$ cargo +nightly new demo_macros
$ cat >>  demo_macros/Cargo.toml << EOF
[lib]
proc-macro = true
EOF
$ cat >| demo_macros/src/lib.rs << EOF
#![feature(proc_macro)]
extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro_attribute]
pub fn nop_attribute(_: TokenStream, input: TokenStream) -> TokenStream {
  input
}
EOF
$ cargo +nightly init .
$ echo 'demo_macros = { path = "demo_macros" }' >> Cargo.toml
$ cat >| src/lib.rs << EOF
#![feature(proc_macro)]
extern crate demo_macros;
use demo_macros::nop_attribute;

extern {
  #[nop_attribute]
  pub fn fabs(_: f64) -> f64;
}
EOF
$ cargo +nightly build

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions