@@ -84,9 +84,35 @@ struct TestHarnessGenerator<'a> {
8484 tests : Vec < Test > ,
8585}
8686
87+ impl TestHarnessGenerator < ' _ > {
88+ fn add_test_cases ( & mut self , node_id : ast:: NodeId , span : Span , prev_tests : Vec < Test > ) {
89+ let mut tests = mem:: replace ( & mut self . tests , prev_tests) ;
90+
91+ if !tests. is_empty ( ) {
92+ // Create an identifier that will hygienically resolve the test
93+ // case name, even in another module.
94+ let expn_id = self . cx . ext_cx . resolver . expansion_for_ast_pass (
95+ span,
96+ AstPass :: TestHarness ,
97+ & [ ] ,
98+ Some ( node_id) ,
99+ ) ;
100+ for test in & mut tests {
101+ // See the comment on `mk_main` for why we're using
102+ // `apply_mark` directly.
103+ test. ident . span =
104+ test. ident . span . apply_mark ( expn_id. to_expn_id ( ) , Transparency :: Opaque ) ;
105+ }
106+ self . cx . test_cases . extend ( tests) ;
107+ }
108+ }
109+ }
110+
87111impl < ' a > MutVisitor for TestHarnessGenerator < ' a > {
88112 fn visit_crate ( & mut self , c : & mut ast:: Crate ) {
113+ let prev_tests = mem:: take ( & mut self . tests ) ;
89114 noop_visit_crate ( c, self ) ;
115+ self . add_test_cases ( ast:: CRATE_NODE_ID , c. span , prev_tests) ;
90116
91117 // Create a main function to run our tests
92118 c. items . push ( mk_main ( & mut self . cx ) ) ;
@@ -103,34 +129,10 @@ impl<'a> MutVisitor for TestHarnessGenerator<'a> {
103129
104130 // We don't want to recurse into anything other than mods, since
105131 // mods or tests inside of functions will break things
106- if let ast:: ItemKind :: Mod ( .. ) = item. kind {
107- let tests = mem:: take ( & mut self . tests ) ;
132+ if let ast:: ItemKind :: Mod ( _ , ModKind :: Loaded ( .. , span ) ) = item. kind {
133+ let prev_tests = mem:: take ( & mut self . tests ) ;
108134 noop_visit_item_kind ( & mut item. kind , self ) ;
109- let mut tests = mem:: replace ( & mut self . tests , tests) ;
110-
111- if !tests. is_empty ( ) {
112- let parent =
113- if item. id == ast:: DUMMY_NODE_ID { ast:: CRATE_NODE_ID } else { item. id } ;
114- // Create an identifier that will hygienically resolve the test
115- // case name, even in another module.
116- let inner_span = match item. kind {
117- ast:: ItemKind :: Mod ( _, ModKind :: Loaded ( .., span) ) => span,
118- _ => unreachable ! ( ) ,
119- } ;
120- let expn_id = self . cx . ext_cx . resolver . expansion_for_ast_pass (
121- inner_span,
122- AstPass :: TestHarness ,
123- & [ ] ,
124- Some ( parent) ,
125- ) ;
126- for test in & mut tests {
127- // See the comment on `mk_main` for why we're using
128- // `apply_mark` directly.
129- test. ident . span =
130- test. ident . span . apply_mark ( expn_id. to_expn_id ( ) , Transparency :: Opaque ) ;
131- }
132- self . cx . test_cases . extend ( tests) ;
133- }
135+ self . add_test_cases ( item. id , span, prev_tests) ;
134136 }
135137 smallvec ! [ P ( item) ]
136138 }
@@ -146,7 +148,7 @@ fn entry_point_type(sess: &Session, item: &ast::Item, depth: usize) -> EntryPoin
146148 } else if sess. contains_name ( & item. attrs , sym:: rustc_main) {
147149 EntryPointType :: MainAttr
148150 } else if item. ident . name == sym:: main {
149- if depth == 1 {
151+ if depth == 0 {
150152 // This is a top-level function so can be 'main'
151153 EntryPointType :: MainNamed
152154 } else {
0 commit comments