@@ -175,45 +175,6 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
175
175
let tests = mem:: replace ( & mut self . tests , tests) ;
176
176
let tested_submods = mem:: replace ( & mut self . tested_submods , tested_submods) ;
177
177
178
- // Remove any #[main] from the AST so it doesn't clash with
179
- // the one we're going to add. Only if compiling an executable.
180
-
181
- mod_folded. items = mem:: replace ( & mut mod_folded. items , vec ! [ ] ) . move_map ( |item| {
182
- match entry:: entry_point_type ( & item, self . cx . path . len ( ) + 1 ) {
183
- EntryPointType :: MainNamed |
184
- EntryPointType :: MainAttr |
185
- EntryPointType :: Start =>
186
- item. map ( |ast:: Item { id, ident, attrs, node, vis, span} | {
187
- let allow_str = InternedString :: new ( "allow" ) ;
188
- let dead_code_str = InternedString :: new ( "dead_code" ) ;
189
- let allow_dead_code_item =
190
- attr:: mk_list_item ( allow_str,
191
- vec ! [ attr:: mk_word_item( dead_code_str) ] ) ;
192
- let allow_dead_code = attr:: mk_attr_outer ( attr:: mk_attr_id ( ) ,
193
- allow_dead_code_item) ;
194
-
195
- ast:: Item {
196
- id : id,
197
- ident : ident,
198
- attrs : attrs. into_iter ( ) . filter_map ( |attr| {
199
- if !attr. check_name ( "main" ) {
200
- Some ( attr)
201
- } else {
202
- None
203
- }
204
- } )
205
- . chain ( iter:: once ( allow_dead_code) )
206
- . collect ( ) ,
207
- node : node,
208
- vis : vis,
209
- span : span
210
- }
211
- } ) ,
212
- EntryPointType :: None |
213
- EntryPointType :: OtherMain => item,
214
- }
215
- } ) ;
216
-
217
178
if !tests. is_empty ( ) || !tested_submods. is_empty ( ) {
218
179
let ( it, sym) = mk_reexport_mod ( & mut self . cx , tests, tested_submods) ;
219
180
mod_folded. items . push ( it) ;
@@ -230,6 +191,58 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
230
191
}
231
192
}
232
193
194
+ struct EntryPointCleaner {
195
+ // Current depth in the ast
196
+ depth : usize ,
197
+ }
198
+
199
+ impl fold:: Folder for EntryPointCleaner {
200
+ fn fold_item ( & mut self , i : P < ast:: Item > ) -> SmallVector < P < ast:: Item > > {
201
+ self . depth += 1 ;
202
+ let folded = fold:: noop_fold_item ( i, self ) . expect_one ( "noop did something" ) ;
203
+ self . depth -= 1 ;
204
+
205
+ // Remove any #[main] from the AST so it doesn't clash with
206
+ // the one we're going to add, but mark it as
207
+ // #[allow(dead_code)] to avoid printing warnings.
208
+ let folded = match entry:: entry_point_type ( & * folded, self . depth ) {
209
+ EntryPointType :: MainNamed |
210
+ EntryPointType :: MainAttr |
211
+ EntryPointType :: Start =>
212
+ folded. map ( |ast:: Item { id, ident, attrs, node, vis, span} | {
213
+ let allow_str = InternedString :: new ( "allow" ) ;
214
+ let dead_code_str = InternedString :: new ( "dead_code" ) ;
215
+ let allow_dead_code_item =
216
+ attr:: mk_list_item ( allow_str,
217
+ vec ! [ attr:: mk_word_item( dead_code_str) ] ) ;
218
+ let allow_dead_code = attr:: mk_attr_outer ( attr:: mk_attr_id ( ) ,
219
+ allow_dead_code_item) ;
220
+
221
+ ast:: Item {
222
+ id : id,
223
+ ident : ident,
224
+ attrs : attrs. into_iter ( ) . filter_map ( |attr| {
225
+ if !attr. check_name ( "main" ) {
226
+ Some ( attr)
227
+ } else {
228
+ None
229
+ }
230
+ } )
231
+ . chain ( iter:: once ( allow_dead_code) )
232
+ . collect ( ) ,
233
+ node : node,
234
+ vis : vis,
235
+ span : span
236
+ }
237
+ } ) ,
238
+ EntryPointType :: None |
239
+ EntryPointType :: OtherMain => folded,
240
+ } ;
241
+
242
+ SmallVector :: one ( folded)
243
+ }
244
+ }
245
+
233
246
fn mk_reexport_mod ( cx : & mut TestCtxt , tests : Vec < ast:: Ident > ,
234
247
tested_submods : Vec < ( ast:: Ident , ast:: Ident ) > ) -> ( P < ast:: Item > , ast:: Ident ) {
235
248
let super_ = token:: str_to_ident ( "super" ) ;
@@ -265,6 +278,10 @@ fn generate_test_harness(sess: &ParseSess,
265
278
krate : ast:: Crate ,
266
279
cfg : & ast:: CrateConfig ,
267
280
sd : & diagnostic:: SpanHandler ) -> ast:: Crate {
281
+ // Remove the entry points
282
+ let mut cleaner = EntryPointCleaner { depth : 0 } ;
283
+ let krate = cleaner. fold_crate ( krate) ;
284
+
268
285
let mut feature_gated_cfgs = vec ! [ ] ;
269
286
let mut cx: TestCtxt = TestCtxt {
270
287
sess : sess,
0 commit comments