Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
lambda-fairy opened this issue Apr 17, 2017 · 3 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@lambda-fairy
Copy link
Contributor

lambda-fairy commented Apr 17, 2017

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.

@Mark-Simulacrum Mark-Simulacrum added the A-macros-2.0 Area: Declarative macros 2.0 (#39412) label Jun 23, 2017
@Mark-Simulacrum Mark-Simulacrum added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints labels Jul 27, 2017
@pnkfelix
Copy link
Member

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.

@jseyfried jseyfried self-assigned this Dec 1, 2017
@MauriceKayser
Copy link

MauriceKayser commented Jul 18, 2018

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.. ;)

@lambda-fairy lambda-fairy changed the title Unhelpful diagnostic when using proc macros 2.0 without #![feature(proc_macro)] Unhelpful diagnostic when using proc macros 2.0 without #![feature(use_extern_macros)] Jul 19, 2018
MabezDev added a commit to MabezDev/cortex-m-rtfm that referenced this issue Aug 8, 2018
@alexcrichton
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-macros-2.0 Area: Declarative macros 2.0 (#39412) C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

6 participants