@@ -12,10 +12,10 @@ use rustc_errors::emitter::stderr_destination;
1212use rustc_errors:: { ColorConfig , DiagCtxtHandle } ;
1313use rustc_parse:: new_parser_from_source_str;
1414use rustc_session:: parse:: ParseSess ;
15- use rustc_span:: edition:: Edition ;
15+ use rustc_span:: edition:: { DEFAULT_EDITION , Edition } ;
1616use rustc_span:: source_map:: SourceMap ;
1717use rustc_span:: symbol:: sym;
18- use rustc_span:: { FileName , Span , kw} ;
18+ use rustc_span:: { DUMMY_SP , FileName , Span , kw} ;
1919use tracing:: debug;
2020
2121use super :: GlobalTestOptions ;
@@ -35,35 +35,78 @@ struct ParseSourceInfo {
3535 maybe_crate_attrs : String ,
3636}
3737
38- /// This struct contains information about the doctest itself which is then used to generate
39- /// doctest source code appropriately.
40- pub ( crate ) struct DocTestBuilder {
41- pub ( crate ) supports_color : bool ,
42- pub ( crate ) already_has_extern_crate : bool ,
43- pub ( crate ) has_main_fn : bool ,
44- pub ( crate ) crate_attrs : String ,
45- /// If this is a merged doctest, it will be put into `everything_else`, otherwise it will
46- /// put into `crate_attrs`.
47- pub ( crate ) maybe_crate_attrs : String ,
48- pub ( crate ) crates : String ,
49- pub ( crate ) everything_else : String ,
50- pub ( crate ) test_id : Option < String > ,
51- pub ( crate ) invalid_ast : bool ,
52- pub ( crate ) can_be_merged : bool ,
38+ /// Builder type for `DocTestBuilder`.
39+ pub ( crate ) struct BuildDocTestBuilder < ' a > {
40+ source : & ' a str ,
41+ crate_name : Option < & ' a str > ,
42+ edition : Edition ,
43+ can_merge_doctests : bool ,
44+ // If `test_id` is `None`, it means we're generating code for a code example "run" link.
45+ test_id : Option < String > ,
46+ lang_str : Option < & ' a LangString > ,
47+ span : Span ,
5348}
5449
55- impl DocTestBuilder {
56- pub ( crate ) fn new (
57- source : & str ,
58- crate_name : Option < & str > ,
59- edition : Edition ,
60- can_merge_doctests : bool ,
61- // If `test_id` is `None`, it means we're generating code for a code example "run" link.
62- test_id : Option < String > ,
63- lang_str : Option < & LangString > ,
64- dcx : Option < DiagCtxtHandle < ' _ > > ,
65- span : Span ,
66- ) -> Self {
50+ impl < ' a > BuildDocTestBuilder < ' a > {
51+ pub ( crate ) fn new ( source : & ' a str ) -> Self {
52+ Self {
53+ source,
54+ crate_name : None ,
55+ edition : DEFAULT_EDITION ,
56+ can_merge_doctests : false ,
57+ test_id : None ,
58+ lang_str : None ,
59+ span : DUMMY_SP ,
60+ }
61+ }
62+
63+ #[ inline]
64+ pub ( crate ) fn crate_name ( mut self , crate_name : & ' a str ) -> Self {
65+ self . crate_name = Some ( crate_name) ;
66+ self
67+ }
68+
69+ #[ inline]
70+ pub ( crate ) fn can_merge_doctests ( mut self , can_merge_doctests : bool ) -> Self {
71+ self . can_merge_doctests = can_merge_doctests;
72+ self
73+ }
74+
75+ #[ inline]
76+ pub ( crate ) fn test_id ( mut self , test_id : String ) -> Self {
77+ self . test_id = Some ( test_id) ;
78+ self
79+ }
80+
81+ #[ inline]
82+ pub ( crate ) fn lang_str ( mut self , lang_str : & ' a LangString ) -> Self {
83+ self . lang_str = Some ( lang_str) ;
84+ self
85+ }
86+
87+ #[ inline]
88+ pub ( crate ) fn span ( mut self , span : Span ) -> Self {
89+ self . span = span;
90+ self
91+ }
92+
93+ #[ inline]
94+ pub ( crate ) fn edition ( mut self , edition : Edition ) -> Self {
95+ self . edition = edition;
96+ self
97+ }
98+
99+ pub ( crate ) fn build ( self , dcx : Option < DiagCtxtHandle < ' _ > > ) -> DocTestBuilder {
100+ let BuildDocTestBuilder {
101+ source,
102+ crate_name,
103+ edition,
104+ can_merge_doctests,
105+ // If `test_id` is `None`, it means we're generating code for a code example "run" link.
106+ test_id,
107+ lang_str,
108+ span,
109+ } = self ;
67110 let can_merge_doctests = can_merge_doctests
68111 && lang_str. is_some_and ( |lang_str| {
69112 !lang_str. compile_fail && !lang_str. test_harness && !lang_str. standalone_crate
@@ -89,7 +132,7 @@ impl DocTestBuilder {
89132 else {
90133 // If the AST returned an error, we don't want this doctest to be merged with the
91134 // others.
92- return Self :: invalid (
135+ return DocTestBuilder :: invalid (
93136 String :: new ( ) ,
94137 String :: new ( ) ,
95138 String :: new ( ) ,
@@ -109,7 +152,7 @@ impl DocTestBuilder {
109152 // If this is a merged doctest and a defined macro uses `$crate`, then the path will
110153 // not work, so better not put it into merged doctests.
111154 && !( has_macro_def && everything_else. contains ( "$crate" ) ) ;
112- Self {
155+ DocTestBuilder {
113156 supports_color,
114157 has_main_fn,
115158 crate_attrs,
@@ -122,7 +165,26 @@ impl DocTestBuilder {
122165 can_be_merged,
123166 }
124167 }
168+ }
169+
170+ /// This struct contains information about the doctest itself which is then used to generate
171+ /// doctest source code appropriately.
172+ pub ( crate ) struct DocTestBuilder {
173+ pub ( crate ) supports_color : bool ,
174+ pub ( crate ) already_has_extern_crate : bool ,
175+ pub ( crate ) has_main_fn : bool ,
176+ pub ( crate ) crate_attrs : String ,
177+ /// If this is a merged doctest, it will be put into `everything_else`, otherwise it will
178+ /// put into `crate_attrs`.
179+ pub ( crate ) maybe_crate_attrs : String ,
180+ pub ( crate ) crates : String ,
181+ pub ( crate ) everything_else : String ,
182+ pub ( crate ) test_id : Option < String > ,
183+ pub ( crate ) invalid_ast : bool ,
184+ pub ( crate ) can_be_merged : bool ,
185+ }
125186
187+ impl DocTestBuilder {
126188 fn invalid (
127189 crate_attrs : String ,
128190 maybe_crate_attrs : String ,
0 commit comments