diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2019-07-18 21:29:15 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2019-07-27 13:56:54 +0300 |
| commit | f6eda9937956667c01bc567ab4a9bd6a19f71635 (patch) | |
| tree | 74e7efcf49348e9fecfd83f99550cd06c4b82075 | |
| parent | 4ad0daa220fd5e00ce425c9208869ea0d6f4b981 (diff) | |
| download | rust-f6eda9937956667c01bc567ab4a9bd6a19f71635.tar.gz rust-f6eda9937956667c01bc567ab4a9bd6a19f71635.zip | |
Move test harness generation into libsyntax_ext
| -rw-r--r-- | src/librustc_interface/passes.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/lib.rs | 1 | ||||
| -rw-r--r-- | src/libsyntax_ext/Cargo.toml | 8 | ||||
| -rw-r--r-- | src/libsyntax_ext/lib.rs | 5 | ||||
| -rw-r--r-- | src/libsyntax_ext/test.rs | 33 | ||||
| -rw-r--r-- | src/libsyntax_ext/test_case.rs | 44 | ||||
| -rw-r--r-- | src/libsyntax_ext/test_harness.rs (renamed from src/libsyntax/test.rs) | 64 |
7 files changed, 57 insertions, 100 deletions
diff --git a/src/librustc_interface/passes.rs b/src/librustc_interface/passes.rs index 2754c55ea06..58edb898c25 100644 --- a/src/librustc_interface/passes.rs +++ b/src/librustc_interface/passes.rs @@ -456,7 +456,7 @@ fn configure_and_expand_inner<'a>( sess.profiler(|p| p.end_activity("macro expansion")); time(sess, "maybe building test harness", || { - syntax::test::modify_for_testing( + syntax_ext::test_harness::modify_for_testing( &sess.parse_sess, &mut resolver, sess.opts.test, diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index a4102ce45aa..17f379f31b2 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -156,7 +156,6 @@ pub mod show_span; pub mod std_inject; pub use syntax_pos::edition; pub use syntax_pos::symbol; -pub mod test; pub mod tokenstream; pub mod visit; diff --git a/src/libsyntax_ext/Cargo.toml b/src/libsyntax_ext/Cargo.toml index eafbe6371a3..73310df305b 100644 --- a/src/libsyntax_ext/Cargo.toml +++ b/src/libsyntax_ext/Cargo.toml @@ -10,11 +10,11 @@ path = "lib.rs" doctest = false [dependencies] -fmt_macros = { path = "../libfmt_macros" } errors = { path = "../librustc_errors", package = "rustc_errors" } -syntax = { path = "../libsyntax" } -syntax_pos = { path = "../libsyntax_pos" } +fmt_macros = { path = "../libfmt_macros" } +log = "0.4" rustc_data_structures = { path = "../librustc_data_structures" } rustc_target = { path = "../librustc_target" } smallvec = { version = "0.6.7", features = ["union", "may_dangle"] } -log = "0.4" +syntax = { path = "../libsyntax" } +syntax_pos = { path = "../libsyntax_pos" } diff --git a/src/libsyntax_ext/lib.rs b/src/libsyntax_ext/lib.rs index d208d34eea1..cd0d0886239 100644 --- a/src/libsyntax_ext/lib.rs +++ b/src/libsyntax_ext/lib.rs @@ -7,6 +7,7 @@ #![feature(crate_visibility_modifier)] #![feature(decl_macro)] +#![feature(mem_take)] #![feature(nll)] #![feature(rustc_diagnostic_macros)] #![feature(unicode_internals)] @@ -38,11 +39,11 @@ mod global_asm; mod log_syntax; mod source_util; mod test; -mod test_case; mod trace_macros; pub mod plugin_macro_defs; pub mod proc_macro_decls; +pub mod test_harness; pub fn register_builtin_macros(resolver: &mut dyn syntax::ext::base::Resolver, edition: Edition) { let mut register = |name, kind| resolver.register_builtin_macro( @@ -89,7 +90,7 @@ pub fn register_builtin_macros(resolver: &mut dyn syntax::ext::base::Resolver, e bench: test::expand_bench, global_allocator: global_allocator::expand, test: test::expand_test, - test_case: test_case::expand, + test_case: test::expand_test_case, } register_derive! { diff --git a/src/libsyntax_ext/test.rs b/src/libsyntax_ext/test.rs index d381c42f9ce..36aeb3065ff 100644 --- a/src/libsyntax_ext/test.rs +++ b/src/libsyntax_ext/test.rs @@ -7,11 +7,44 @@ use syntax::ext::base::*; use syntax::ext::build::AstBuilder; use syntax::ext::hygiene::SyntaxContext; use syntax::print::pprust; +use syntax::source_map::respan; use syntax::symbol::{Symbol, sym}; use syntax_pos::Span; use std::iter; +// #[test_case] is used by custom test authors to mark tests +// When building for test, it needs to make the item public and gensym the name +// Otherwise, we'll omit the item. This behavior means that any item annotated +// with #[test_case] is never addressable. +// +// We mark item with an inert attribute "rustc_test_marker" which the test generation +// logic will pick up on. +pub fn expand_test_case( + ecx: &mut ExtCtxt<'_>, + attr_sp: Span, + meta_item: &ast::MetaItem, + anno_item: Annotatable +) -> Vec<Annotatable> { + check_builtin_macro_attribute(ecx, meta_item, sym::test_case); + + if !ecx.ecfg.should_test { return vec![]; } + + let sp = attr_sp.with_ctxt(SyntaxContext::empty().apply_mark(ecx.current_expansion.mark)); + let mut item = anno_item.expect_item(); + item = item.map(|mut item| { + item.vis = respan(item.vis.span, ast::VisibilityKind::Public); + item.ident = item.ident.gensym(); + item.attrs.push( + ecx.attribute(sp, + ecx.meta_word(sp, sym::rustc_test_marker)) + ); + item + }); + + return vec![Annotatable::Item(item)] +} + pub fn expand_test( cx: &mut ExtCtxt<'_>, attr_sp: Span, diff --git a/src/libsyntax_ext/test_case.rs b/src/libsyntax_ext/test_case.rs deleted file mode 100644 index ea4a8d541ab..00000000000 --- a/src/libsyntax_ext/test_case.rs +++ /dev/null @@ -1,44 +0,0 @@ -// http://rust-lang.org/COPYRIGHT. -// - -// #[test_case] is used by custom test authors to mark tests -// When building for test, it needs to make the item public and gensym the name -// Otherwise, we'll omit the item. This behavior means that any item annotated -// with #[test_case] is never addressable. -// -// We mark item with an inert attribute "rustc_test_marker" which the test generation -// logic will pick up on. - -use syntax::ast; -use syntax::attr::check_builtin_macro_attribute; -use syntax::ext::base::*; -use syntax::ext::build::AstBuilder; -use syntax::ext::hygiene::SyntaxContext; -use syntax::source_map::respan; -use syntax::symbol::sym; -use syntax_pos::Span; - -pub fn expand( - ecx: &mut ExtCtxt<'_>, - attr_sp: Span, - meta_item: &ast::MetaItem, - anno_item: Annotatable -) -> Vec<Annotatable> { - check_builtin_macro_attribute(ecx, meta_item, sym::test_case); - - if !ecx.ecfg.should_test { return vec![]; } - - let sp = attr_sp.with_ctxt(SyntaxContext::empty().apply_mark(ecx.current_expansion.id)); - let mut item = anno_item.expect_item(); - item = item.map(|mut item| { - item.vis = respan(item.vis.span, ast::VisibilityKind::Public); - item.ident = item.ident.gensym(); - item.attrs.push( - ecx.attribute(sp, - ecx.meta_word(sp, sym::rustc_test_marker)) - ); - item - }); - - return vec![Annotatable::Item(item)] -} diff --git a/src/libsyntax/test.rs b/src/libsyntax_ext/test_harness.rs index 2ada7104843..061f5c3408b 100644 --- a/src/libsyntax/test.rs +++ b/src/libsyntax_ext/test_harness.rs @@ -1,35 +1,23 @@ // Code that generates a test runner to run all the tests in a crate -#![allow(dead_code)] -#![allow(unused_imports)] - -use HasTestSignature::*; - -use std::iter; -use std::slice; -use std::mem; -use std::vec; - use log::debug; use smallvec::{smallvec, SmallVec}; -use syntax_pos::{DUMMY_SP, NO_EXPANSION, Span, SourceFile, BytePos}; - -use crate::attr::{self, HasAttrs}; -use crate::source_map::{self, SourceMap, ExpnInfo, ExpnKind, dummy_spanned, respan}; -use crate::config; -use crate::entry::{self, EntryPointType}; -use crate::ext::base::{ExtCtxt, Resolver}; -use crate::ext::build::AstBuilder; -use crate::ext::expand::ExpansionConfig; -use crate::ext::hygiene::{self, ExpnId, SyntaxContext, MacroKind}; -use crate::mut_visit::{*, ExpectOne}; -use crate::feature_gate::Features; -use crate::util::map_in_place::MapInPlace; -use crate::parse::{token, ParseSess}; -use crate::ast::{self, Ident}; -use crate::ptr::P; -use crate::symbol::{self, Symbol, kw, sym}; -use crate::ThinVec; +use syntax::ast::{self, Ident}; +use syntax::attr; +use syntax::entry::{self, EntryPointType}; +use syntax::ext::base::{ExtCtxt, Resolver}; +use syntax::ext::build::AstBuilder; +use syntax::ext::expand::ExpansionConfig; +use syntax::ext::hygiene::{ExpnId, MacroKind}; +use syntax::feature_gate::Features; +use syntax::mut_visit::{*, ExpectOne}; +use syntax::parse::ParseSess; +use syntax::ptr::P; +use syntax::source_map::{ExpnInfo, ExpnKind, dummy_spanned}; +use syntax::symbol::{kw, sym, Symbol}; +use syntax_pos::{Span, DUMMY_SP}; + +use std::{iter, mem}; struct Test { span: Span, @@ -42,10 +30,7 @@ struct TestCtxt<'a> { ext_cx: ExtCtxt<'a>, test_cases: Vec<Test>, reexport_test_harness_main: Option<Symbol>, - is_libtest: bool, - features: &'a Features, test_runner: Option<ast::Path>, - // top-level re-export submodule, filled out after folding is finished toplevel_reexport: Option<Ident>, } @@ -267,11 +252,7 @@ fn generate_test_harness(sess: &ParseSess, path: Vec::new(), test_cases: Vec::new(), reexport_test_harness_main, - // N.B., doesn't consider the value of `--crate-name` passed on the command line. - is_libtest: attr::find_crate_name(&krate.attrs) - .map(|s| s == sym::test).unwrap_or(false), toplevel_reexport: None, - features, test_runner }; @@ -282,19 +263,6 @@ fn generate_test_harness(sess: &ParseSess, }.visit_crate(krate); } -enum HasTestSignature { - Yes, - No(BadTestSignature), -} - -#[derive(PartialEq)] -enum BadTestSignature { - NotEvenAFunction, - WrongTypeSignature, - NoArgumentsAllowed, - ShouldPanicOnlyWithNoArgs, -} - /// Creates a function item for use as the main function of a test build. /// This function will call the `test_runner` as specified by the crate attribute fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> { |
