Skip to content

Commit 1196dfe

Browse files
committed
Move test harness generation into libsyntax_ext
1 parent 995429d commit 1196dfe

File tree

8 files changed

+56
-96
lines changed

8 files changed

+56
-96
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3620,6 +3620,7 @@ name = "syntax_ext"
36203620
version = "0.0.0"
36213621
dependencies = [
36223622
"fmt_macros 0.0.0",
3623+
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
36233624
"rustc_data_structures 0.0.0",
36243625
"rustc_errors 0.0.0",
36253626
"rustc_target 0.0.0",

src/librustc_interface/passes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ fn configure_and_expand_inner<'a>(
452452
sess.profiler(|p| p.end_activity("macro expansion"));
453453

454454
time(sess, "maybe building test harness", || {
455-
syntax::test::modify_for_testing(
455+
syntax_ext::test_harness::modify_for_testing(
456456
&sess.parse_sess,
457457
&mut resolver,
458458
sess.opts.test,

src/libsyntax/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ pub mod show_span;
157157
pub mod std_inject;
158158
pub use syntax_pos::edition;
159159
pub use syntax_pos::symbol;
160-
pub mod test;
161160
pub mod tokenstream;
162161
pub mod visit;
163162

src/libsyntax_ext/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ path = "lib.rs"
1010
doctest = false
1111

1212
[dependencies]
13-
fmt_macros = { path = "../libfmt_macros" }
1413
errors = { path = "../librustc_errors", package = "rustc_errors" }
15-
syntax = { path = "../libsyntax" }
16-
syntax_pos = { path = "../libsyntax_pos" }
14+
fmt_macros = { path = "../libfmt_macros" }
15+
log = "0.4"
1716
rustc_data_structures = { path = "../librustc_data_structures" }
1817
rustc_target = { path = "../librustc_target" }
1918
smallvec = { version = "0.6.7", features = ["union", "may_dangle"] }
19+
syntax = { path = "../libsyntax" }
20+
syntax_pos = { path = "../libsyntax_pos" }

src/libsyntax_ext/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![deny(unused_lifetimes)]
77

88
#![feature(decl_macro)]
9+
#![feature(mem_take)]
910
#![feature(nll)]
1011
#![feature(rustc_diagnostic_macros)]
1112

@@ -27,10 +28,10 @@ mod global_asm;
2728
mod log_syntax;
2829
mod source_util;
2930
mod test;
30-
mod test_case;
3131
mod trace_macros;
3232

3333
pub mod proc_macro_decls;
34+
pub mod test_harness;
3435

3536
use rustc_data_structures::sync::Lrc;
3637
use syntax::ast;
@@ -128,7 +129,7 @@ pub fn register_builtins(resolver: &mut dyn syntax::ext::base::Resolver,
128129
)),
129130
allow_internal_unstable: allow_internal_unstable.clone(),
130131
..SyntaxExtension::default(
131-
SyntaxExtensionKind::LegacyAttr(Box::new(test_case::expand)), edition
132+
SyntaxExtensionKind::LegacyAttr(Box::new(test::expand_test_case)), edition
132133
)
133134
});
134135
register(sym::test, SyntaxExtension {

src/libsyntax_ext/test.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,41 @@ use syntax::ext::build::AstBuilder;
66
use syntax::ext::hygiene::SyntaxContext;
77
use syntax::attr;
88
use syntax::ast;
9+
use syntax::source_map::respan;
910
use syntax::symbol::{Symbol, sym};
1011
use syntax_pos::Span;
1112
use std::iter;
1213

14+
// #[test_case] is used by custom test authors to mark tests
15+
// When building for test, it needs to make the item public and gensym the name
16+
// Otherwise, we'll omit the item. This behavior means that any item annotated
17+
// with #[test_case] is never addressable.
18+
//
19+
// We mark item with an inert attribute "rustc_test_marker" which the test generation
20+
// logic will pick up on.
21+
pub fn expand_test_case(
22+
ecx: &mut ExtCtxt<'_>,
23+
attr_sp: Span,
24+
_meta_item: &ast::MetaItem,
25+
anno_item: Annotatable
26+
) -> Vec<Annotatable> {
27+
if !ecx.ecfg.should_test { return vec![]; }
28+
29+
let sp = attr_sp.with_ctxt(SyntaxContext::empty().apply_mark(ecx.current_expansion.mark));
30+
let mut item = anno_item.expect_item();
31+
item = item.map(|mut item| {
32+
item.vis = respan(item.vis.span, ast::VisibilityKind::Public);
33+
item.ident = item.ident.gensym();
34+
item.attrs.push(
35+
ecx.attribute(sp,
36+
ecx.meta_word(sp, sym::rustc_test_marker))
37+
);
38+
item
39+
});
40+
41+
return vec![Annotatable::Item(item)]
42+
}
43+
1344
pub fn expand_test(
1445
cx: &mut ExtCtxt<'_>,
1546
attr_sp: Span,

src/libsyntax_ext/test_case.rs

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/libsyntax/test.rs renamed to src/libsyntax_ext/test_harness.rs

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,23 @@
11
// Code that generates a test runner to run all the tests in a crate
22

3-
#![allow(dead_code)]
4-
#![allow(unused_imports)]
5-
6-
use HasTestSignature::*;
7-
8-
use std::iter;
9-
use std::slice;
10-
use std::mem;
11-
use std::vec;
12-
133
use log::debug;
144
use smallvec::{smallvec, SmallVec};
15-
use syntax_pos::{DUMMY_SP, NO_EXPANSION, Span, SourceFile, BytePos};
16-
17-
use crate::attr::{self, HasAttrs};
18-
use crate::source_map::{self, SourceMap, ExpnInfo, ExpnKind, dummy_spanned, respan};
19-
use crate::config;
20-
use crate::entry::{self, EntryPointType};
21-
use crate::ext::base::{ExtCtxt, Resolver};
22-
use crate::ext::build::AstBuilder;
23-
use crate::ext::expand::ExpansionConfig;
24-
use crate::ext::hygiene::{self, ExpnId, SyntaxContext, MacroKind};
25-
use crate::mut_visit::{*, ExpectOne};
26-
use crate::feature_gate::Features;
27-
use crate::util::map_in_place::MapInPlace;
28-
use crate::parse::{token, ParseSess};
29-
use crate::ast::{self, Ident};
30-
use crate::ptr::P;
31-
use crate::symbol::{self, Symbol, kw, sym};
32-
use crate::ThinVec;
5+
use syntax::ast::{self, Ident};
6+
use syntax::attr;
7+
use syntax::entry::{self, EntryPointType};
8+
use syntax::ext::base::{ExtCtxt, Resolver};
9+
use syntax::ext::build::AstBuilder;
10+
use syntax::ext::expand::ExpansionConfig;
11+
use syntax::ext::hygiene::{ExpnId, MacroKind};
12+
use syntax::feature_gate::Features;
13+
use syntax::mut_visit::{*, ExpectOne};
14+
use syntax::parse::ParseSess;
15+
use syntax::ptr::P;
16+
use syntax::source_map::{ExpnInfo, ExpnKind, dummy_spanned};
17+
use syntax::symbol::{kw, sym, Symbol};
18+
use syntax_pos::{Span, DUMMY_SP};
19+
20+
use std::{iter, mem};
3321

3422
struct Test {
3523
span: Span,
@@ -42,10 +30,7 @@ struct TestCtxt<'a> {
4230
ext_cx: ExtCtxt<'a>,
4331
test_cases: Vec<Test>,
4432
reexport_test_harness_main: Option<Symbol>,
45-
is_libtest: bool,
46-
features: &'a Features,
4733
test_runner: Option<ast::Path>,
48-
4934
// top-level re-export submodule, filled out after folding is finished
5035
toplevel_reexport: Option<Ident>,
5136
}
@@ -267,11 +252,7 @@ fn generate_test_harness(sess: &ParseSess,
267252
path: Vec::new(),
268253
test_cases: Vec::new(),
269254
reexport_test_harness_main,
270-
// N.B., doesn't consider the value of `--crate-name` passed on the command line.
271-
is_libtest: attr::find_crate_name(&krate.attrs)
272-
.map(|s| s == sym::test).unwrap_or(false),
273255
toplevel_reexport: None,
274-
features,
275256
test_runner
276257
};
277258

@@ -282,19 +263,6 @@ fn generate_test_harness(sess: &ParseSess,
282263
}.visit_crate(krate);
283264
}
284265

285-
enum HasTestSignature {
286-
Yes,
287-
No(BadTestSignature),
288-
}
289-
290-
#[derive(PartialEq)]
291-
enum BadTestSignature {
292-
NotEvenAFunction,
293-
WrongTypeSignature,
294-
NoArgumentsAllowed,
295-
ShouldPanicOnlyWithNoArgs,
296-
}
297-
298266
/// Creates a function item for use as the main function of a test build.
299267
/// This function will call the `test_runner` as specified by the crate attribute
300268
fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {

0 commit comments

Comments
 (0)