Skip to content
New issue

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

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

Already on GitHub? # to your account

'cargo test' with proc_macro_derive fails #37480

Closed
rukai opened this issue Oct 30, 2016 · 10 comments · Fixed by #37826
Closed

'cargo test' with proc_macro_derive fails #37480

rukai opened this issue Oct 30, 2016 · 10 comments · Fixed by #37826

Comments

@rukai
Copy link
Contributor

rukai commented Oct 30, 2016

I setup a crate using the example from http://www.ncameron.org/blog/macros-and-syntax-extensions-and-compiler-plugins-where-are-we-at/
There are no tests setup: no tests folder, no #[test] attributes.

  • cargo run succeeds
  • cargo test fails with: error: the #[proc_macro_derive]attribute is only usable with crates of theproc-macro crate type

Naturally I expected this error not to occur.

cargo.toml

name = "foobar"
version = "0.1.0"
authors = ["Rukai]

[lib]
proc-macro = true

lib.rs

#![feature(proc_macro, proc_macro_lib)]
#![crate_type = "proc-macro"]

extern crate proc_macro;

use proc_macro::TokenStream;

#[proc_macro_derive(B)]
pub fn derive(input: TokenStream) -> TokenStream {  
    let input = input.to_string();
    format!("{}\n impl B for A {{ fn b(&self) {{}} }}", input).parse().unwrap()
}

rust version

Run on the latest nightly via rustup:
rustc 1.14.0-nightly (3f4408347 2016-10-27) binary: rustc commit-hash: 3f4408347d2109803edbf53c89c8bce575de4b67 commit-date: 2016-10-27 host: x86_64-unknown-linux-gnu release: 1.14.0-nightly LLVM version: 3.9

@TimNN
Copy link
Contributor

TimNN commented Oct 30, 2016

You can probably work around this by using #[cfg_attr(not(test), proc_macro_derive(B))] (although I haven't tested that).

@alexcrichton
Copy link
Member

Thanks for the report! I'm not sure what the best way to solve this would be. We could either tell the compiler that --test doesn't work for these crate types, or we could tell Cargo to not test proc-macro libraries. Both should have the end goal though of having cargo test work by default.

@keeperofdakeys
Copy link
Contributor

Would a simple fix for this be to add && !sess.opts.test to https://github.com/rust-lang/rust/blob/c19cb9c1/src/librustc_driver/driver.rs#L707 ? Or should syntax_ext::proc_macro_registrar::modify - where it currently panics - quietly bail out if this is a test crate?

@alexcrichton
Copy link
Member

@keeperofdakeys I'd personally prefer for --test to keep doing the same feature gating it's doing today, so in that sense I don't think it should take the same bail-out route that rustdoc takes. Adding some logic to modify though sounds good to me.

bors added a commit that referenced this issue Nov 19, 2016
Show a better error when using --test with #[proc_macro_derive]

Fixes #37480

Currently using `--test` with a crate that contains a `#[proc_macro_derive]` attribute causes an error. This PR doesn't attempt to fix the issue itself, or determine what tesing of a proc_macro_derive crate should be - just to provide a better error message.
@colin-kiegel
Copy link

I think it was wrong that this ticket was closed with #37826 since it states

this PR doesn't attempt to fix the issue itself ... just to provide a better error message.

@keeperofdakeys
Copy link
Contributor

keeperofdakeys commented Nov 20, 2016

@alexcrichton Actually fixing this appears simpler than I expected. I'm guessing a return here should be enough. This will ensure all the feature gating is still done, but no expansion is performed. Did you have any further thoughts?

@alexcrichton
Copy link
Member

@keeperofdakeys of it works it works for me!

@alexcrichton alexcrichton reopened this Nov 20, 2016
bors added a commit that referenced this issue Dec 5, 2016
Allow --test to be used on proc-macro crates

Fixes #37480

This patch allows `--test` to work for proc-macro crates, removing the previous error.
@dtolnay
Copy link
Member

dtolnay commented Jan 19, 2017

@keeperofdakeys @alexcrichton this is failing again. It works correctly on nightly-2017-01-17 rustc 1.16.0-nightly (4ce7acc 2017-01-17) but fails on nightly-2017-01-18 rustc 1.16.0-nightly (c07a6ae 2017-01-17).

error: the `#[proc_macro_derive]` attribute is only usable with crates of the `proc-macro` crate type
  |
8 | #[proc_macro_derive(B)]
  |   ^^^^^^^^^^^^^^^^^^^^

@keeperofdakeys
Copy link
Contributor

keeperofdakeys commented Jan 19, 2017

This could be related to recently enabling doc-testing of proc-macro crates in cargo rust-lang/cargo#3552. This rustc change should fix that #39136.

If you look at the cargo output, does the error happen when running the normal tests, or after trying to run doc-tests?

Edit: I've tested this in the latest nightly from rustup, and this is definitely what is happening. Hopefully it will be fixed soon.

@alexcrichton
Copy link
Member

@keeperofdakeys thanks for staying on top of this! I've approved #39136 and p=1'd it to hopefully get it in soon.

@dtolnay sorry for the breakage!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants