Rust: Unhelpful diagnostic when using proc macros 2.0 without #![feature(use_extern_macros)]

Created on 17 Apr 2017  路  3Comments  路  Source: rust-lang/rust

Suppose I had these two files:

// foo.rs
#![crate_type = "proc-macro"]

extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro]
pub fn foo(x: TokenStream) -> TokenStream {
    x
}
// bar.rs
extern crate foo;
use foo::foo;

fn main() {
    foo!();
}

When I try to compile these files with rustc foo.rs && rustc bar.rs -L., I get the following errors:

warning: proc macro crates and `#[no_link]` crates have no effect without `#[macro_use]`
 --> bar.rs:1:1
  |
1 | extern crate foo;
  | ^^^^^^^^^^^^^^^^^

error[E0432]: unresolved import `foo::foo`
 --> bar.rs:2:5
  |
2 | use foo::foo;
  |     ^^^^^^^^ no `foo` in the root

error: cannot find macro `foo!` in this scope
 --> bar.rs:5:5
  |
5 |     foo!();
  |     ^^^

error: aborting due to 2 previous errors

Now, if I follow the compiler's advice and try #[macro_use] instead, I'm presented with this error:

error: procedural macros cannot be imported with `#[macro_use]`
 --> bar.rs:5:5
  |
5 |     foo!();
  |     ^^^
  |
  = help: instead, import the procedural macro like any other item: `use foo::foo;`

error: aborting due to previous error

But that's what I had in the first place!

The real solution to this problem is to add #![feature(use_extern_macros)] to bar.rs. The compiler should suggest this instead when it encounters a function-like proc macro.

A-diagnostics A-macros-2.0 C-enhancement

Most helpful comment

Now with nightly I even get: warning: this feature has been stable since 1.29.0. Attribute no longer needed for #![feature(proc_macro)].

But I still have the same issues as @lfairy. And now if I want to use my macro I get error: cannot find macro 'my_macro!' in this scope. But If I add use my_crate::my_macro; I get error[E0432]: unresolved import 'my_crate::my_macro'.

It seems one now needs to replace the feature proc_macro with use_extern_macros. That would be a helpful hint in the Attribute is no longer needed message.. ;)

All 3 comments

It might be a easy fix to at least change the diagnostic message to also suggest adding #![feature(proc_macro)] to bar.rs for the short term. (Or have the diagnostic first check if that feature is enable before making the suggestion.) I don't mind having a redundant portion of the suggestion as long as the it covers the real problem as well.

Now with nightly I even get: warning: this feature has been stable since 1.29.0. Attribute no longer needed for #![feature(proc_macro)].

But I still have the same issues as @lfairy. And now if I want to use my macro I get error: cannot find macro 'my_macro!' in this scope. But If I add use my_crate::my_macro; I get error[E0432]: unresolved import 'my_crate::my_macro'.

It seems one now needs to replace the feature proc_macro with use_extern_macros. That would be a helpful hint in the Attribute is no longer needed message.. ;)

Many features here have been stabilized, so this is likely stale, so closing.

Was this page helpful?
0 / 5 - 0 ratings