@@ -25,7 +25,7 @@ use crate::html::markdown::LangString;
2525#[ derive( Default ) ]
2626struct ParseSourceInfo {
2727 has_main_fn : bool ,
28- found_extern_crate : bool ,
28+ already_has_extern_crate : bool ,
2929 supports_color : bool ,
3030 has_global_allocator : bool ,
3131 has_macro_def : bool ,
@@ -48,7 +48,7 @@ pub(crate) struct DocTestBuilder {
4848 pub ( crate ) crates : String ,
4949 pub ( crate ) everything_else : String ,
5050 pub ( crate ) test_id : Option < String > ,
51- pub ( crate ) failed_ast : bool ,
51+ pub ( crate ) invalid_ast : bool ,
5252 pub ( crate ) can_be_merged : bool ,
5353}
5454
@@ -75,7 +75,7 @@ impl DocTestBuilder {
7575
7676 let Ok ( Ok ( ParseSourceInfo {
7777 has_main_fn,
78- found_extern_crate ,
78+ already_has_extern_crate ,
7979 supports_color,
8080 has_global_allocator,
8181 has_macro_def,
@@ -114,9 +114,9 @@ impl DocTestBuilder {
114114 maybe_crate_attrs,
115115 crates,
116116 everything_else,
117- already_has_extern_crate : found_extern_crate ,
117+ already_has_extern_crate,
118118 test_id,
119- failed_ast : false ,
119+ invalid_ast : false ,
120120 can_be_merged,
121121 }
122122 }
@@ -137,7 +137,7 @@ impl DocTestBuilder {
137137 everything_else,
138138 already_has_extern_crate : false ,
139139 test_id,
140- failed_ast : true ,
140+ invalid_ast : true ,
141141 can_be_merged : false ,
142142 }
143143 }
@@ -151,10 +151,10 @@ impl DocTestBuilder {
151151 opts : & GlobalTestOptions ,
152152 crate_name : Option < & str > ,
153153 ) -> ( String , usize ) {
154- if self . failed_ast {
154+ if self . invalid_ast {
155155 // If the AST failed to compile, no need to go generate a complete doctest, the error
156156 // will be better this way.
157- debug ! ( "failed AST:\n {test_code}" ) ;
157+ debug ! ( "invalid AST:\n {test_code}" ) ;
158158 return ( test_code. to_string ( ) , 0 ) ;
159159 }
160160 let mut line_offset = 0 ;
@@ -279,7 +279,7 @@ impl DocTestBuilder {
279279 }
280280}
281281
282- fn cancel_error_count ( psess : & ParseSess ) {
282+ fn reset_error_count ( psess : & ParseSess ) {
283283 // Reset errors so that they won't be reported as compiler bugs when dropping the
284284 // dcx. Any errors in the tests will be reported when the test file is compiled,
285285 // Note that we still need to cancel the errors above otherwise `Diag` will panic on
@@ -295,7 +295,7 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
295295 use rustc_span:: source_map:: FilePathMapping ;
296296
297297 let mut info =
298- ParseSourceInfo { found_extern_crate : crate_name. is_none ( ) , ..Default :: default ( ) } ;
298+ ParseSourceInfo { already_has_extern_crate : crate_name. is_none ( ) , ..Default :: default ( ) } ;
299299
300300 let wrapped_source = format ! ( "{DOCTEST_CODE_WRAPPER}{source}\n }}" ) ;
301301
@@ -322,27 +322,21 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
322322 Ok ( p) => p,
323323 Err ( errs) => {
324324 errs. into_iter ( ) . for_each ( |err| err. cancel ( ) ) ;
325- cancel_error_count ( & psess) ;
325+ reset_error_count ( & psess) ;
326326 return Err ( ( ) ) ;
327327 }
328328 } ;
329329
330- fn push_to_s (
331- s : & mut String ,
332- source : & str ,
333- span : rustc_span:: Span ,
334- prev_span_hi : & mut Option < usize > ,
335- ) {
330+ fn push_to_s ( s : & mut String , source : & str , span : rustc_span:: Span , prev_span_hi : & mut usize ) {
336331 let extra_len = DOCTEST_CODE_WRAPPER . len ( ) ;
337332 // We need to shift by the length of `DOCTEST_CODE_WRAPPER` because we
338333 // added it at the beginning of the source we provided to the parser.
339- let lo = prev_span_hi. unwrap_or ( 0 ) ;
340334 let mut hi = span. hi ( ) . 0 as usize - extra_len;
341335 if hi > source. len ( ) {
342336 hi = source. len ( ) ;
343337 }
344- s. push_str ( & source[ lo ..hi] ) ;
345- * prev_span_hi = Some ( hi ) ;
338+ s. push_str ( & source[ * prev_span_hi ..hi] ) ;
339+ * prev_span_hi = hi ;
346340 }
347341
348342 // Recurse through functions body. It is necessary because the doctest source code is
@@ -354,7 +348,7 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
354348 crate_name : & Option < & str > ,
355349 is_top_level : bool ,
356350 ) -> bool {
357- let mut is_crate = false ;
351+ let mut is_extern_crate = false ;
358352 if !info. has_global_allocator
359353 && item. attrs . iter ( ) . any ( |attr| attr. name_or_empty ( ) == sym:: global_allocator)
360354 {
@@ -370,17 +364,17 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
370364 if let Some ( ref body) = fn_item. body {
371365 for stmt in & body. stmts {
372366 if let StmtKind :: Item ( ref item) = stmt. kind {
373- is_crate |= check_item ( item, info, crate_name, false ) ;
367+ is_extern_crate |= check_item ( item, info, crate_name, false ) ;
374368 }
375369 }
376370 }
377371 }
378372 ast:: ItemKind :: ExternCrate ( original) => {
379- is_crate = true ;
380- if !info. found_extern_crate
373+ is_extern_crate = true ;
374+ if !info. already_has_extern_crate
381375 && let Some ( crate_name) = crate_name
382376 {
383- info. found_extern_crate = match original {
377+ info. already_has_extern_crate = match original {
384378 Some ( name) => name. as_str ( ) == * crate_name,
385379 None => item. ident . as_str ( ) == * crate_name,
386380 } ;
@@ -391,10 +385,10 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
391385 }
392386 _ => { }
393387 }
394- is_crate
388+ is_extern_crate
395389 }
396390
397- let mut prev_span_hi = None ;
391+ let mut prev_span_hi = 0 ;
398392 let not_crate_attrs = [ sym:: forbid, sym:: allow, sym:: warn, sym:: deny, sym:: expect] ;
399393 let parsed = parser. parse_item ( rustc_parse:: parser:: ForceCollect :: No ) ;
400394
@@ -430,13 +424,13 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
430424 }
431425 }
432426 for stmt in & body. stmts {
433- let mut is_crate = false ;
427+ let mut is_extern_crate = false ;
434428 match stmt. kind {
435429 StmtKind :: Item ( ref item) => {
436- is_crate = check_item ( & item, & mut info, crate_name, true ) ;
430+ is_extern_crate = check_item ( & item, & mut info, crate_name, true ) ;
437431 }
438432 StmtKind :: Expr ( ref expr) if matches ! ( expr. kind, ast:: ExprKind :: Err ( _) ) => {
439- cancel_error_count ( & psess) ;
433+ reset_error_count ( & psess) ;
440434 return Err ( ( ) ) ;
441435 }
442436 StmtKind :: MacCall ( ref mac_call) if !info. has_main_fn => {
@@ -471,11 +465,12 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
471465 if info. everything_else . is_empty ( )
472466 && ( !info. maybe_crate_attrs . is_empty ( ) || !info. crate_attrs . is_empty ( ) )
473467 {
474- // We add potential backlines/comments if there are some in items generated
475- // before the wrapping function.
468+ // To keep the doctest code "as close as possible" to the original, we insert
469+ // all the code located between this new span and the previous span which
470+ // might contain code comments and backlines.
476471 push_to_s ( & mut info. crates , source, span. shrink_to_lo ( ) , & mut prev_span_hi) ;
477472 }
478- if !is_crate {
473+ if !is_extern_crate {
479474 push_to_s ( & mut info. everything_else , source, span, & mut prev_span_hi) ;
480475 } else {
481476 push_to_s ( & mut info. crates , source, span, & mut prev_span_hi) ;
@@ -490,6 +485,6 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
490485 _ => Err ( ( ) ) ,
491486 } ;
492487
493- cancel_error_count ( & psess) ;
488+ reset_error_count ( & psess) ;
494489 result
495490}
0 commit comments