@@ -51,8 +51,17 @@ impl DocTestBuilder {
5151 !lang_str. compile_fail && !lang_str. test_harness && !lang_str. standalone_crate
5252 } ) ;
5353
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+ } ;
5665
5766 // Uses librustc_ast to parse the doctest and find if there's a main fn and the extern
5867 // crate already is included.
@@ -77,18 +86,7 @@ impl DocTestBuilder {
7786 else {
7887 // If the parser panicked due to a fatal error, pass the test code through unchanged.
7988 // 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) ;
9290 } ;
9391 // If the AST returned an error, we don't want this doctest to be merged with the
9492 // others. Same if it contains `#[feature]` or `#[no_std]`.
@@ -113,6 +111,27 @@ impl DocTestBuilder {
113111 }
114112 }
115113
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+
116135 /// Transforms a test into code that can be compiled into a Rust binary, and returns the number of
117136 /// lines before the test code begins.
118137 pub ( crate ) fn generate_unique_doctest (
@@ -533,7 +552,7 @@ struct SourceInfo {
533552 everything_else : String ,
534553}
535554
536- fn partition_source ( s : & str , edition : Edition ) -> SourceInfo {
555+ fn partition_source ( s : & str , edition : Edition ) -> Option < SourceInfo > {
537556 #[ derive( Copy , Clone , PartialEq ) ]
538557 enum PartitionState {
539558 Attrs ,
@@ -608,11 +627,16 @@ fn partition_source(s: &str, edition: Edition) -> SourceInfo {
608627 }
609628 }
610629
630+ if !mod_attr_pending. is_empty ( ) {
631+ debug ! ( "invalid doctest code: {s:?}" ) ;
632+ return None ;
633+ }
634+
611635 source_info. everything_else = source_info. everything_else . trim ( ) . to_string ( ) ;
612636
613637 debug ! ( "crate_attrs:\n {}{}" , source_info. crate_attrs, source_info. maybe_crate_attrs) ;
614638 debug ! ( "crates:\n {}" , source_info. crates) ;
615639 debug ! ( "after:\n {}" , source_info. everything_else) ;
616640
617- source_info
641+ Some ( source_info)
618642}
0 commit comments