@@ -176,9 +176,24 @@ impl DocTestBuilder {
176176
177177 // Now push any outer attributes from the example, assuming they
178178 // 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+ }
182197
183198 // Don't inject `extern crate std` because it's already injected by the
184199 // compiler.
@@ -338,7 +353,8 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
338353 info : & mut ParseSourceInfo ,
339354 crate_name : & Option < & str > ,
340355 is_top_level : bool ,
341- ) {
356+ ) -> bool {
357+ let mut is_crate = false ;
342358 if !info. has_global_allocator
343359 && item. attrs . iter ( ) . any ( |attr| attr. name_or_empty ( ) == sym:: global_allocator)
344360 {
@@ -354,12 +370,13 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
354370 if let Some ( ref body) = fn_item. body {
355371 for stmt in & body. stmts {
356372 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 ) ;
358374 }
359375 }
360376 }
361377 }
362378 ast:: ItemKind :: ExternCrate ( original) => {
379+ is_crate = true ;
363380 if !info. found_extern_crate
364381 && let Some ( crate_name) = crate_name
365382 {
@@ -374,6 +391,7 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
374391 }
375392 _ => { }
376393 }
394+ is_crate
377395 }
378396
379397 let mut prev_span_hi = None ;
@@ -412,8 +430,11 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
412430 }
413431 }
414432 for stmt in & body. stmts {
433+ let mut is_crate = false ;
415434 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+ }
417438 StmtKind :: Expr ( ref expr) if matches ! ( expr. kind, ast:: ExprKind :: Err ( _) ) => {
418439 cancel_error_count ( & psess) ;
419440 return Err ( ( ) ) ;
@@ -458,7 +479,11 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
458479 & mut prev_span_hi,
459480 ) ;
460481 }
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+ }
462487 }
463488 Ok ( info)
464489 }
0 commit comments