Skip to content

Commit 11137c6

Browse files
committed
Move test harness generation into libsyntax_ext
1 parent a0fb9c9 commit 11137c6

File tree

7 files changed

+57
-100
lines changed

7 files changed

+57
-100
lines changed

src/librustc_interface/passes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ fn configure_and_expand_inner<'a>(
453453
sess.profiler(|p| p.end_activity("macro expansion"));
454454

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

src/libsyntax/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ pub mod show_span;
156156
pub mod std_inject;
157157
pub use syntax_pos::edition;
158158
pub use syntax_pos::symbol;
159-
pub mod test;
160159
pub mod tokenstream;
161160
pub mod visit;
162161

src/libsyntax_ext/Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +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"] }
20-
log = "0.4"
19+
syntax = { path = "../libsyntax" }
20+
syntax_pos = { path = "../libsyntax_pos" }

src/libsyntax_ext/lib.rs

+3-2
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
#![feature(unicode_internals)]
@@ -29,10 +30,10 @@ mod global_asm;
2930
mod log_syntax;
3031
mod source_util;
3132
mod test;
32-
mod test_case;
3333
mod trace_macros;
3434

3535
pub mod proc_macro_decls;
36+
pub mod test_harness;
3637

3738
use rustc_data_structures::sync::Lrc;
3839
use syntax::ast;
@@ -130,7 +131,7 @@ pub fn register_builtins(resolver: &mut dyn syntax::ext::base::Resolver,
130131
)),
131132
allow_internal_unstable: allow_internal_unstable.clone(),
132133
..SyntaxExtension::default(
133-
SyntaxExtensionKind::LegacyAttr(Box::new(test_case::expand)), edition
134+
SyntaxExtensionKind::LegacyAttr(Box::new(test::expand_test_case)), edition
134135
)
135136
});
136137
register(sym::test, SyntaxExtension {

src/libsyntax_ext/test.rs

+33
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,44 @@ use syntax::ext::base::*;
77
use syntax::ext::build::AstBuilder;
88
use syntax::ext::hygiene::SyntaxContext;
99
use syntax::print::pprust;
10+
use syntax::source_map::respan;
1011
use syntax::symbol::{Symbol, sym};
1112
use syntax_pos::Span;
1213

1314
use std::iter;
1415

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

src/libsyntax_ext/test_case.rs

-44
This file was deleted.

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

+16-48
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)