Skip to content

Commit bfa709a

Browse files
authored
Auto merge of #37826 - keeperofdakeys:proc-macro-test, r=alexcrichton
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.
2 parents b1da18f + 3254fab commit bfa709a

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

src/librustc_driver/driver.rs

+2
Original file line numberDiff line numberDiff line change
@@ -709,10 +709,12 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
709709
let crate_types = sess.crate_types.borrow();
710710
let num_crate_types = crate_types.len();
711711
let is_proc_macro_crate = crate_types.contains(&config::CrateTypeProcMacro);
712+
let is_test_crate = sess.opts.test;
712713
syntax_ext::proc_macro_registrar::modify(&sess.parse_sess,
713714
&mut resolver,
714715
krate,
715716
is_proc_macro_crate,
717+
is_test_crate,
716718
num_crate_types,
717719
sess.diagnostic(),
718720
&sess.features.borrow())

src/libsyntax_ext/proc_macro_registrar.rs

+9
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ struct CollectCustomDerives<'a> {
3838
in_root: bool,
3939
handler: &'a errors::Handler,
4040
is_proc_macro_crate: bool,
41+
is_test_crate: bool,
4142
}
4243

4344
pub fn modify(sess: &ParseSess,
4445
resolver: &mut ::syntax::ext::base::Resolver,
4546
mut krate: ast::Crate,
4647
is_proc_macro_crate: bool,
48+
is_test_crate: bool,
4749
num_crate_types: usize,
4850
handler: &errors::Handler,
4951
features: &Features) -> ast::Crate {
@@ -55,6 +57,7 @@ pub fn modify(sess: &ParseSess,
5557
in_root: true,
5658
handler: handler,
5759
is_proc_macro_crate: is_proc_macro_crate,
60+
is_test_crate: is_test_crate,
5861
};
5962
visit::walk_crate(&mut collect, &krate);
6063

@@ -137,6 +140,12 @@ impl<'a> Visitor for CollectCustomDerives<'a> {
137140
attributes found");
138141
}
139142

143+
if self.is_test_crate {
144+
self.handler.span_err(attr.span(),
145+
"`--test` cannot be used with proc-macro crates");
146+
return;
147+
}
148+
140149
if !self.is_proc_macro_crate {
141150
self.handler.span_err(attr.span(),
142151
"the `#[proc_macro_derive]` attribute is \
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: --test
12+
13+
#![crate_type = "proc-macro"]
14+
#![feature(proc_macro)]
15+
16+
extern crate proc_macro;
17+
18+
#[proc_macro_derive(A)]
19+
//~^ ERROR: `--test` cannot be used with proc-macro crates
20+
pub fn foo1(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
21+
"".parse().unwrap()
22+
}

0 commit comments

Comments
 (0)