@@ -12,10 +12,10 @@ use rustc_errors::emitter::stderr_destination;
12
12
use rustc_errors:: { ColorConfig , DiagCtxtHandle } ;
13
13
use rustc_parse:: new_parser_from_source_str;
14
14
use rustc_session:: parse:: ParseSess ;
15
- use rustc_span:: edition:: Edition ;
15
+ use rustc_span:: edition:: { DEFAULT_EDITION , Edition } ;
16
16
use rustc_span:: source_map:: SourceMap ;
17
17
use rustc_span:: symbol:: sym;
18
- use rustc_span:: { FileName , Span , kw} ;
18
+ use rustc_span:: { DUMMY_SP , FileName , Span , kw} ;
19
19
use tracing:: debug;
20
20
21
21
use super :: GlobalTestOptions ;
@@ -35,35 +35,78 @@ struct ParseSourceInfo {
35
35
maybe_crate_attrs : String ,
36
36
}
37
37
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 ,
53
48
}
54
49
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 ;
67
110
let can_merge_doctests = can_merge_doctests
68
111
&& lang_str. is_some_and ( |lang_str| {
69
112
!lang_str. compile_fail && !lang_str. test_harness && !lang_str. standalone_crate
@@ -89,7 +132,7 @@ impl DocTestBuilder {
89
132
else {
90
133
// If the AST returned an error, we don't want this doctest to be merged with the
91
134
// others.
92
- return Self :: invalid (
135
+ return DocTestBuilder :: invalid (
93
136
String :: new ( ) ,
94
137
String :: new ( ) ,
95
138
String :: new ( ) ,
@@ -109,7 +152,7 @@ impl DocTestBuilder {
109
152
// If this is a merged doctest and a defined macro uses `$crate`, then the path will
110
153
// not work, so better not put it into merged doctests.
111
154
&& !( has_macro_def && everything_else. contains ( "$crate" ) ) ;
112
- Self {
155
+ DocTestBuilder {
113
156
supports_color,
114
157
has_main_fn,
115
158
crate_attrs,
@@ -122,7 +165,26 @@ impl DocTestBuilder {
122
165
can_be_merged,
123
166
}
124
167
}
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
+ }
125
186
187
+ impl DocTestBuilder {
126
188
fn invalid (
127
189
crate_attrs : String ,
128
190
maybe_crate_attrs : String ,
0 commit comments