@@ -176,9 +176,24 @@ impl DocTestBuilder {
176
176
177
177
// Now push any outer attributes from the example, assuming they
178
178
// are intended to be crate attributes.
179
- prog. push_str ( & self . crate_attrs ) ;
180
- prog. push_str ( & self . maybe_crate_attrs ) ;
181
- prog. push_str ( & self . crates ) ;
179
+ if !self . crate_attrs . is_empty ( ) {
180
+ prog. push_str ( & self . crate_attrs ) ;
181
+ if !self . crate_attrs . ends_with ( '\n' ) {
182
+ prog. push ( '\n' ) ;
183
+ }
184
+ }
185
+ if !self . maybe_crate_attrs . is_empty ( ) {
186
+ prog. push_str ( & self . maybe_crate_attrs ) ;
187
+ if !self . maybe_crate_attrs . ends_with ( '\n' ) {
188
+ prog. push ( '\n' ) ;
189
+ }
190
+ }
191
+ if !self . crates . is_empty ( ) {
192
+ prog. push_str ( & self . crates ) ;
193
+ if !self . crates . ends_with ( '\n' ) {
194
+ prog. push ( '\n' ) ;
195
+ }
196
+ }
182
197
183
198
// Don't inject `extern crate std` because it's already injected by the
184
199
// compiler.
@@ -338,7 +353,8 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
338
353
info : & mut ParseSourceInfo ,
339
354
crate_name : & Option < & str > ,
340
355
is_top_level : bool ,
341
- ) {
356
+ ) -> bool {
357
+ let mut is_crate = false ;
342
358
if !info. has_global_allocator
343
359
&& item. attrs . iter ( ) . any ( |attr| attr. name_or_empty ( ) == sym:: global_allocator)
344
360
{
@@ -354,12 +370,13 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
354
370
if let Some ( ref body) = fn_item. body {
355
371
for stmt in & body. stmts {
356
372
if let StmtKind :: Item ( ref item) = stmt. kind {
357
- check_item ( item, info, crate_name, false )
373
+ is_crate |= check_item ( item, info, crate_name, false ) ;
358
374
}
359
375
}
360
376
}
361
377
}
362
378
ast:: ItemKind :: ExternCrate ( original) => {
379
+ is_crate = true ;
363
380
if !info. found_extern_crate
364
381
&& let Some ( crate_name) = crate_name
365
382
{
@@ -374,6 +391,7 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
374
391
}
375
392
_ => { }
376
393
}
394
+ is_crate
377
395
}
378
396
379
397
let mut prev_span_hi = None ;
@@ -412,8 +430,11 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
412
430
}
413
431
}
414
432
for stmt in & body. stmts {
433
+ let mut is_crate = false ;
415
434
match stmt. kind {
416
- StmtKind :: Item ( ref item) => check_item ( & item, & mut info, crate_name, true ) ,
435
+ StmtKind :: Item ( ref item) => {
436
+ is_crate = check_item ( & item, & mut info, crate_name, true ) ;
437
+ }
417
438
StmtKind :: Expr ( ref expr) if matches ! ( expr. kind, ast:: ExprKind :: Err ( _) ) => {
418
439
cancel_error_count ( & psess) ;
419
440
return Err ( ( ) ) ;
@@ -458,7 +479,11 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
458
479
& mut prev_span_hi,
459
480
) ;
460
481
}
461
- push_to_s ( & mut info. everything_else , source, span, & mut prev_span_hi) ;
482
+ if !is_crate {
483
+ push_to_s ( & mut info. everything_else , source, span, & mut prev_span_hi) ;
484
+ } else {
485
+ push_to_s ( & mut info. crates , source, span, & mut prev_span_hi) ;
486
+ }
462
487
}
463
488
Ok ( info)
464
489
}
0 commit comments