Skip to content

Show a better error when using --test with #[proc_macro_derive] #37826

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

Merged
merged 2 commits into from
Nov 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -709,10 +709,12 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
let crate_types = sess.crate_types.borrow();
let num_crate_types = crate_types.len();
let is_proc_macro_crate = crate_types.contains(&config::CrateTypeProcMacro);
let is_test_crate = sess.opts.test;
syntax_ext::proc_macro_registrar::modify(&sess.parse_sess,
&mut resolver,
krate,
is_proc_macro_crate,
is_test_crate,
num_crate_types,
sess.diagnostic(),
&sess.features.borrow())
Expand Down
9 changes: 9 additions & 0 deletions src/libsyntax_ext/proc_macro_registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ struct CollectCustomDerives<'a> {
in_root: bool,
handler: &'a errors::Handler,
is_proc_macro_crate: bool,
is_test_crate: bool,
}

pub fn modify(sess: &ParseSess,
resolver: &mut ::syntax::ext::base::Resolver,
mut krate: ast::Crate,
is_proc_macro_crate: bool,
is_test_crate: bool,
num_crate_types: usize,
handler: &errors::Handler,
features: &Features) -> ast::Crate {
Expand All @@ -55,6 +57,7 @@ pub fn modify(sess: &ParseSess,
in_root: true,
handler: handler,
is_proc_macro_crate: is_proc_macro_crate,
is_test_crate: is_test_crate,
};
visit::walk_crate(&mut collect, &krate);

Expand Down Expand Up @@ -137,6 +140,12 @@ impl<'a> Visitor for CollectCustomDerives<'a> {
attributes found");
}

if self.is_test_crate {
self.handler.span_err(attr.span(),
"`--test` cannot be used with proc-macro crates");
return;
}

if !self.is_proc_macro_crate {
self.handler.span_err(attr.span(),
"the `#[proc_macro_derive]` attribute is \
Expand Down
22 changes: 22 additions & 0 deletions src/test/compile-fail-fulldeps/proc-macro/error-on-test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// compile-flags: --test

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

extern crate proc_macro;

#[proc_macro_derive(A)]
//~^ ERROR: `--test` cannot be used with proc-macro crates
pub fn foo1(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
"".parse().unwrap()
}