@@ -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.
@@ -276,7 +291,6 @@ const DOCTEST_CODE_WRAPPER: &str = "fn f(){";
276291fn parse_source ( source : & str , crate_name : & Option < & str > ) -> Result < ParseSourceInfo , ( ) > {
277292 use rustc_errors:: DiagCtxt ;
278293 use rustc_errors:: emitter:: { Emitter , HumanEmitter } ;
279- // use rustc_parse::parser::ForceCollect;
280294 use rustc_span:: source_map:: FilePathMapping ;
281295
282296 let mut info =
@@ -338,7 +352,8 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
338352 info : & mut ParseSourceInfo ,
339353 crate_name : & Option < & str > ,
340354 is_top_level : bool ,
341- ) {
355+ ) -> bool {
356+ let mut is_crate = false ;
342357 if !info. has_global_allocator
343358 && item. attrs . iter ( ) . any ( |attr| attr. name_or_empty ( ) == sym:: global_allocator)
344359 {
@@ -354,12 +369,13 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
354369 if let Some ( ref body) = fn_item. body {
355370 for stmt in & body. stmts {
356371 if let StmtKind :: Item ( ref item) = stmt. kind {
357- check_item ( item, info, crate_name, false )
372+ is_crate |= check_item ( item, info, crate_name, false ) ;
358373 }
359374 }
360375 }
361376 }
362377 ast:: ItemKind :: ExternCrate ( original) => {
378+ is_crate = true ;
363379 if !info. found_extern_crate
364380 && let Some ( crate_name) = crate_name
365381 {
@@ -374,6 +390,7 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
374390 }
375391 _ => { }
376392 }
393+ is_crate
377394 }
378395
379396 let mut prev_span_hi = None ;
@@ -412,8 +429,11 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
412429 }
413430 }
414431 for stmt in & body. stmts {
432+ let mut is_crate = false ;
415433 match stmt. kind {
416- StmtKind :: Item ( ref item) => check_item ( & item, & mut info, crate_name, true ) ,
434+ StmtKind :: Item ( ref item) => {
435+ is_crate = check_item ( & item, & mut info, crate_name, true ) ;
436+ }
417437 StmtKind :: Expr ( ref expr) if matches ! ( expr. kind, ast:: ExprKind :: Err ( _) ) => {
418438 cancel_error_count ( & psess) ;
419439 return Err ( ( ) ) ;
@@ -450,15 +470,15 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
450470 if info. everything_else . is_empty ( )
451471 && ( !info. maybe_crate_attrs . is_empty ( ) || !info. crate_attrs . is_empty ( ) )
452472 {
453- // We add potential backlines/comments into attributes if there are some.
454- push_to_s (
455- & mut info. maybe_crate_attrs ,
456- source,
457- span. shrink_to_lo ( ) ,
458- & mut prev_span_hi,
459- ) ;
473+ // We add potential backlines/comments if there are some in items generated
474+ // before the wrapping function.
475+ push_to_s ( & mut info. crates , source, span. shrink_to_lo ( ) , & mut prev_span_hi) ;
476+ }
477+ if !is_crate {
478+ push_to_s ( & mut info. everything_else , source, span, & mut prev_span_hi) ;
479+ } else {
480+ push_to_s ( & mut info. crates , source, span, & mut prev_span_hi) ;
460481 }
461- push_to_s ( & mut info. everything_else , source, span, & mut prev_span_hi) ;
462482 }
463483 Ok ( info)
464484 }
0 commit comments