1
1
// Code that generates a test runner to run all the tests in a crate
2
2
3
- use std:: { iter, mem} ;
3
+ use std:: ops:: DerefMut ;
4
+ use std:: mem;
4
5
5
6
use rustc_ast as ast;
6
7
use rustc_ast:: entry:: EntryPointType ;
@@ -19,7 +20,7 @@ use rustc_span::hygiene::{AstPass, SyntaxContext, Transparency};
19
20
use rustc_span:: symbol:: { Ident , Symbol , sym} ;
20
21
use rustc_span:: { DUMMY_SP , Span } ;
21
22
use rustc_target:: spec:: PanicStrategy ;
22
- use smallvec:: { SmallVec , smallvec} ;
23
+ use smallvec:: smallvec;
23
24
use thin_vec:: { ThinVec , thin_vec} ;
24
25
use tracing:: debug;
25
26
@@ -129,8 +130,8 @@ impl<'a> MutVisitor for TestHarnessGenerator<'a> {
129
130
c. items . push ( mk_main ( & mut self . cx ) ) ;
130
131
}
131
132
132
- fn flat_map_item ( & mut self , mut i : P < ast :: Item > ) -> SmallVec < [ P < ast:: Item > ; 1 ] > {
133
- let item = & mut * i ;
133
+ fn visit_item ( & mut self , i : & mut P < ast:: Item > ) {
134
+ let item = i . deref_mut ( ) ;
134
135
if let Some ( name) = get_test_name ( & item) {
135
136
debug ! ( "this is a test item" ) ;
136
137
@@ -158,7 +159,6 @@ impl<'a> MutVisitor for TestHarnessGenerator<'a> {
158
159
// But in those cases, we emit a lint to warn the user of these missing tests.
159
160
walk_item ( & mut InnerItemLinter { sess : self . cx . ext_cx . sess } , & item) ;
160
161
}
161
- smallvec ! [ i]
162
162
}
163
163
}
164
164
@@ -198,40 +198,32 @@ struct EntryPointCleaner<'a> {
198
198
}
199
199
200
200
impl < ' a > MutVisitor for EntryPointCleaner < ' a > {
201
- fn flat_map_item ( & mut self , i : P < ast :: Item > ) -> SmallVec < [ P < ast:: Item > ; 1 ] > {
201
+ fn visit_item ( & mut self , i : & mut P < ast:: Item > ) {
202
202
self . depth += 1 ;
203
- let item = walk_flat_map_item ( self , i) . expect_one ( "noop did something" ) ;
203
+ ast :: mut_visit :: walk_item ( self , i, ( ) ) ;
204
204
self . depth -= 1 ;
205
205
206
+ let item = i. deref_mut ( ) ;
207
+
206
208
// Remove any #[rustc_main] or #[start] from the AST so it doesn't
207
209
// clash with the one we're going to add, but mark it as
208
210
// #[allow(dead_code)] to avoid printing warnings.
209
- let item = match entry_point_type ( & item, self . depth == 0 ) {
211
+ match entry_point_type ( & item, self . depth == 0 ) {
210
212
EntryPointType :: MainNamed | EntryPointType :: RustcMainAttr | EntryPointType :: Start => {
211
- item. map ( |ast:: Item { id, ident, attrs, kind, vis, span, tokens } | {
212
- let allow_dead_code = attr:: mk_attr_nested_word (
213
- & self . sess . psess . attr_id_generator ,
214
- ast:: AttrStyle :: Outer ,
215
- ast:: Safety :: Default ,
216
- sym:: allow,
217
- sym:: dead_code,
218
- self . def_site ,
219
- ) ;
220
- let attrs = attrs
221
- . into_iter ( )
222
- . filter ( |attr| {
223
- !attr. has_name ( sym:: rustc_main) && !attr. has_name ( sym:: start)
224
- } )
225
- . chain ( iter:: once ( allow_dead_code) )
226
- . collect ( ) ;
227
-
228
- ast:: Item { id, ident, attrs, kind, vis, span, tokens }
229
- } )
213
+ let allow_dead_code = attr:: mk_attr_nested_word (
214
+ & self . sess . psess . attr_id_generator ,
215
+ ast:: AttrStyle :: Outer ,
216
+ ast:: Safety :: Default ,
217
+ sym:: allow,
218
+ sym:: dead_code,
219
+ self . def_site ,
220
+ ) ;
221
+ item. attrs
222
+ . retain ( |attr| !attr. has_name ( sym:: rustc_main) && !attr. has_name ( sym:: start) ) ;
223
+ item. attrs . push ( allow_dead_code) ;
230
224
}
231
- EntryPointType :: None | EntryPointType :: OtherMain => item ,
225
+ EntryPointType :: None | EntryPointType :: OtherMain => { }
232
226
} ;
233
-
234
- smallvec ! [ item]
235
227
}
236
228
}
237
229
@@ -292,7 +284,7 @@ fn generate_test_harness(
292
284
/// Most of the Ident have the usual def-site hygiene for the AST pass. The
293
285
/// exception is the `test_const`s. These have a syntax context that has two
294
286
/// opaque marks: one from the expansion of `test` or `test_case`, and one
295
- /// generated in `TestHarnessGenerator::flat_map_item `. When resolving this
287
+ /// generated in `TestHarnessGenerator::visit_item `. When resolving this
296
288
/// identifier after failing to find a matching identifier in the root module
297
289
/// we remove the outer mark, and try resolving at its def-site, which will
298
290
/// then resolve to `test_const`.
0 commit comments