@@ -51,8 +51,17 @@ impl DocTestBuilder {
51
51
!lang_str. compile_fail && !lang_str. test_harness && !lang_str. standalone_crate
52
52
} ) ;
53
53
54
- let SourceInfo { crate_attrs, maybe_crate_attrs, crates, everything_else } =
55
- partition_source ( source, edition) ;
54
+ let Some ( SourceInfo { crate_attrs, maybe_crate_attrs, crates, everything_else } ) =
55
+ partition_source ( source, edition)
56
+ else {
57
+ return Self :: invalid (
58
+ String :: new ( ) ,
59
+ String :: new ( ) ,
60
+ String :: new ( ) ,
61
+ source. to_string ( ) ,
62
+ test_id,
63
+ ) ;
64
+ } ;
56
65
57
66
// Uses librustc_ast to parse the doctest and find if there's a main fn and the extern
58
67
// crate already is included.
@@ -77,18 +86,7 @@ impl DocTestBuilder {
77
86
else {
78
87
// If the parser panicked due to a fatal error, pass the test code through unchanged.
79
88
// The error will be reported during compilation.
80
- return Self {
81
- supports_color : false ,
82
- has_main_fn : false ,
83
- crate_attrs,
84
- maybe_crate_attrs,
85
- crates,
86
- everything_else,
87
- already_has_extern_crate : false ,
88
- test_id,
89
- failed_ast : true ,
90
- can_be_merged : false ,
91
- } ;
89
+ return Self :: invalid ( crate_attrs, maybe_crate_attrs, crates, everything_else, test_id) ;
92
90
} ;
93
91
// If the AST returned an error, we don't want this doctest to be merged with the
94
92
// others. Same if it contains `#[feature]` or `#[no_std]`.
@@ -113,6 +111,27 @@ impl DocTestBuilder {
113
111
}
114
112
}
115
113
114
+ fn invalid (
115
+ crate_attrs : String ,
116
+ maybe_crate_attrs : String ,
117
+ crates : String ,
118
+ everything_else : String ,
119
+ test_id : Option < String > ,
120
+ ) -> Self {
121
+ Self {
122
+ supports_color : false ,
123
+ has_main_fn : false ,
124
+ crate_attrs,
125
+ maybe_crate_attrs,
126
+ crates,
127
+ everything_else,
128
+ already_has_extern_crate : false ,
129
+ test_id,
130
+ failed_ast : true ,
131
+ can_be_merged : false ,
132
+ }
133
+ }
134
+
116
135
/// Transforms a test into code that can be compiled into a Rust binary, and returns the number of
117
136
/// lines before the test code begins.
118
137
pub ( crate ) fn generate_unique_doctest (
@@ -533,7 +552,7 @@ struct SourceInfo {
533
552
everything_else : String ,
534
553
}
535
554
536
- fn partition_source ( s : & str , edition : Edition ) -> SourceInfo {
555
+ fn partition_source ( s : & str , edition : Edition ) -> Option < SourceInfo > {
537
556
#[ derive( Copy , Clone , PartialEq ) ]
538
557
enum PartitionState {
539
558
Attrs ,
@@ -608,11 +627,16 @@ fn partition_source(s: &str, edition: Edition) -> SourceInfo {
608
627
}
609
628
}
610
629
630
+ if !mod_attr_pending. is_empty ( ) {
631
+ debug ! ( "invalid doctest code: {s:?}" ) ;
632
+ return None ;
633
+ }
634
+
611
635
source_info. everything_else = source_info. everything_else . trim ( ) . to_string ( ) ;
612
636
613
637
debug ! ( "crate_attrs:\n {}{}" , source_info. crate_attrs, source_info. maybe_crate_attrs) ;
614
638
debug ! ( "crates:\n {}" , source_info. crates) ;
615
639
debug ! ( "after:\n {}" , source_info. everything_else) ;
616
640
617
- source_info
641
+ Some ( source_info)
618
642
}
0 commit comments