@@ -25,7 +25,7 @@ use crate::html::markdown::LangString;
25
25
#[ derive( Default ) ]
26
26
struct ParseSourceInfo {
27
27
has_main_fn : bool ,
28
- found_extern_crate : bool ,
28
+ already_has_extern_crate : bool ,
29
29
supports_color : bool ,
30
30
has_global_allocator : bool ,
31
31
has_macro_def : bool ,
@@ -48,7 +48,7 @@ pub(crate) struct DocTestBuilder {
48
48
pub ( crate ) crates : String ,
49
49
pub ( crate ) everything_else : String ,
50
50
pub ( crate ) test_id : Option < String > ,
51
- pub ( crate ) failed_ast : bool ,
51
+ pub ( crate ) invalid_ast : bool ,
52
52
pub ( crate ) can_be_merged : bool ,
53
53
}
54
54
@@ -75,7 +75,7 @@ impl DocTestBuilder {
75
75
76
76
let Ok ( Ok ( ParseSourceInfo {
77
77
has_main_fn,
78
- found_extern_crate ,
78
+ already_has_extern_crate ,
79
79
supports_color,
80
80
has_global_allocator,
81
81
has_macro_def,
@@ -114,9 +114,9 @@ impl DocTestBuilder {
114
114
maybe_crate_attrs,
115
115
crates,
116
116
everything_else,
117
- already_has_extern_crate : found_extern_crate ,
117
+ already_has_extern_crate,
118
118
test_id,
119
- failed_ast : false ,
119
+ invalid_ast : false ,
120
120
can_be_merged,
121
121
}
122
122
}
@@ -137,7 +137,7 @@ impl DocTestBuilder {
137
137
everything_else,
138
138
already_has_extern_crate : false ,
139
139
test_id,
140
- failed_ast : true ,
140
+ invalid_ast : true ,
141
141
can_be_merged : false ,
142
142
}
143
143
}
@@ -151,10 +151,10 @@ impl DocTestBuilder {
151
151
opts : & GlobalTestOptions ,
152
152
crate_name : Option < & str > ,
153
153
) -> ( String , usize ) {
154
- if self . failed_ast {
154
+ if self . invalid_ast {
155
155
// If the AST failed to compile, no need to go generate a complete doctest, the error
156
156
// will be better this way.
157
- debug ! ( "failed AST:\n {test_code}" ) ;
157
+ debug ! ( "invalid AST:\n {test_code}" ) ;
158
158
return ( test_code. to_string ( ) , 0 ) ;
159
159
}
160
160
let mut line_offset = 0 ;
@@ -279,7 +279,7 @@ impl DocTestBuilder {
279
279
}
280
280
}
281
281
282
- fn cancel_error_count ( psess : & ParseSess ) {
282
+ fn reset_error_count ( psess : & ParseSess ) {
283
283
// Reset errors so that they won't be reported as compiler bugs when dropping the
284
284
// dcx. Any errors in the tests will be reported when the test file is compiled,
285
285
// 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
295
295
use rustc_span:: source_map:: FilePathMapping ;
296
296
297
297
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 ( ) } ;
299
299
300
300
let wrapped_source = format ! ( "{DOCTEST_CODE_WRAPPER}{source}\n }}" ) ;
301
301
@@ -322,27 +322,21 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
322
322
Ok ( p) => p,
323
323
Err ( errs) => {
324
324
errs. into_iter ( ) . for_each ( |err| err. cancel ( ) ) ;
325
- cancel_error_count ( & psess) ;
325
+ reset_error_count ( & psess) ;
326
326
return Err ( ( ) ) ;
327
327
}
328
328
} ;
329
329
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 ) {
336
331
let extra_len = DOCTEST_CODE_WRAPPER . len ( ) ;
337
332
// We need to shift by the length of `DOCTEST_CODE_WRAPPER` because we
338
333
// added it at the beginning of the source we provided to the parser.
339
- let lo = prev_span_hi. unwrap_or ( 0 ) ;
340
334
let mut hi = span. hi ( ) . 0 as usize - extra_len;
341
335
if hi > source. len ( ) {
342
336
hi = source. len ( ) ;
343
337
}
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 ;
346
340
}
347
341
348
342
// 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
354
348
crate_name : & Option < & str > ,
355
349
is_top_level : bool ,
356
350
) -> bool {
357
- let mut is_crate = false ;
351
+ let mut is_extern_crate = false ;
358
352
if !info. has_global_allocator
359
353
&& item. attrs . iter ( ) . any ( |attr| attr. name_or_empty ( ) == sym:: global_allocator)
360
354
{
@@ -370,17 +364,17 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
370
364
if let Some ( ref body) = fn_item. body {
371
365
for stmt in & body. stmts {
372
366
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 ) ;
374
368
}
375
369
}
376
370
}
377
371
}
378
372
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
381
375
&& let Some ( crate_name) = crate_name
382
376
{
383
- info. found_extern_crate = match original {
377
+ info. already_has_extern_crate = match original {
384
378
Some ( name) => name. as_str ( ) == * crate_name,
385
379
None => item. ident . as_str ( ) == * crate_name,
386
380
} ;
@@ -391,10 +385,10 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
391
385
}
392
386
_ => { }
393
387
}
394
- is_crate
388
+ is_extern_crate
395
389
}
396
390
397
- let mut prev_span_hi = None ;
391
+ let mut prev_span_hi = 0 ;
398
392
let not_crate_attrs = [ sym:: forbid, sym:: allow, sym:: warn, sym:: deny, sym:: expect] ;
399
393
let parsed = parser. parse_item ( rustc_parse:: parser:: ForceCollect :: No ) ;
400
394
@@ -430,13 +424,13 @@ fn parse_source(source: &str, crate_name: &Option<&str>) -> Result<ParseSourceIn
430
424
}
431
425
}
432
426
for stmt in & body. stmts {
433
- let mut is_crate = false ;
427
+ let mut is_extern_crate = false ;
434
428
match stmt. kind {
435
429
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 ) ;
437
431
}
438
432
StmtKind :: Expr ( ref expr) if matches ! ( expr. kind, ast:: ExprKind :: Err ( _) ) => {
439
- cancel_error_count ( & psess) ;
433
+ reset_error_count ( & psess) ;
440
434
return Err ( ( ) ) ;
441
435
}
442
436
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
471
465
if info. everything_else . is_empty ( )
472
466
&& ( !info. maybe_crate_attrs . is_empty ( ) || !info. crate_attrs . is_empty ( ) )
473
467
{
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.
476
471
push_to_s ( & mut info. crates , source, span. shrink_to_lo ( ) , & mut prev_span_hi) ;
477
472
}
478
- if !is_crate {
473
+ if !is_extern_crate {
479
474
push_to_s ( & mut info. everything_else , source, span, & mut prev_span_hi) ;
480
475
} else {
481
476
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
490
485
_ => Err ( ( ) ) ,
491
486
} ;
492
487
493
- cancel_error_count ( & psess) ;
488
+ reset_error_count ( & psess) ;
494
489
result
495
490
}
0 commit comments