@@ -45,6 +45,7 @@ pub(crate) struct BuildDocTestBuilder<'a> {
45
45
test_id : Option < String > ,
46
46
lang_str : Option < & ' a LangString > ,
47
47
span : Span ,
48
+ global_crate_attrs : Vec < String > ,
48
49
}
49
50
50
51
impl < ' a > BuildDocTestBuilder < ' a > {
@@ -57,6 +58,7 @@ impl<'a> BuildDocTestBuilder<'a> {
57
58
test_id : None ,
58
59
lang_str : None ,
59
60
span : DUMMY_SP ,
61
+ global_crate_attrs : Vec :: new ( ) ,
60
62
}
61
63
}
62
64
@@ -96,6 +98,12 @@ impl<'a> BuildDocTestBuilder<'a> {
96
98
self
97
99
}
98
100
101
+ #[ inline]
102
+ pub ( crate ) fn global_crate_attrs ( mut self , global_crate_attrs : Vec < String > ) -> Self {
103
+ self . global_crate_attrs = global_crate_attrs;
104
+ self
105
+ }
106
+
99
107
pub ( crate ) fn build ( self , dcx : Option < DiagCtxtHandle < ' _ > > ) -> DocTestBuilder {
100
108
let BuildDocTestBuilder {
101
109
source,
@@ -106,6 +114,7 @@ impl<'a> BuildDocTestBuilder<'a> {
106
114
test_id,
107
115
lang_str,
108
116
span,
117
+ global_crate_attrs,
109
118
} = self ;
110
119
let can_merge_doctests = can_merge_doctests
111
120
&& lang_str. is_some_and ( |lang_str| {
@@ -133,6 +142,7 @@ impl<'a> BuildDocTestBuilder<'a> {
133
142
// If the AST returned an error, we don't want this doctest to be merged with the
134
143
// others.
135
144
return DocTestBuilder :: invalid (
145
+ Vec :: new ( ) ,
136
146
String :: new ( ) ,
137
147
String :: new ( ) ,
138
148
String :: new ( ) ,
@@ -155,6 +165,7 @@ impl<'a> BuildDocTestBuilder<'a> {
155
165
DocTestBuilder {
156
166
supports_color,
157
167
has_main_fn,
168
+ global_crate_attrs,
158
169
crate_attrs,
159
170
maybe_crate_attrs,
160
171
crates,
@@ -173,6 +184,7 @@ pub(crate) struct DocTestBuilder {
173
184
pub ( crate ) supports_color : bool ,
174
185
pub ( crate ) already_has_extern_crate : bool ,
175
186
pub ( crate ) has_main_fn : bool ,
187
+ pub ( crate ) global_crate_attrs : Vec < String > ,
176
188
pub ( crate ) crate_attrs : String ,
177
189
/// If this is a merged doctest, it will be put into `everything_else`, otherwise it will
178
190
/// put into `crate_attrs`.
@@ -186,6 +198,7 @@ pub(crate) struct DocTestBuilder {
186
198
187
199
impl DocTestBuilder {
188
200
fn invalid (
201
+ global_crate_attrs : Vec < String > ,
189
202
crate_attrs : String ,
190
203
maybe_crate_attrs : String ,
191
204
crates : String ,
@@ -195,6 +208,7 @@ impl DocTestBuilder {
195
208
Self {
196
209
supports_color : false ,
197
210
has_main_fn : false ,
211
+ global_crate_attrs,
198
212
crate_attrs,
199
213
maybe_crate_attrs,
200
214
crates,
@@ -224,7 +238,8 @@ impl DocTestBuilder {
224
238
let mut line_offset = 0 ;
225
239
let mut prog = String :: new ( ) ;
226
240
let everything_else = self . everything_else . trim ( ) ;
227
- if opts. attrs . is_empty ( ) {
241
+
242
+ if self . global_crate_attrs . is_empty ( ) {
228
243
// If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some
229
244
// lints that are commonly triggered in doctests. The crate-level test attributes are
230
245
// commonly used to make tests fail in case they trigger warnings, so having this there in
@@ -233,8 +248,8 @@ impl DocTestBuilder {
233
248
line_offset += 1 ;
234
249
}
235
250
236
- // Next, any attributes that came from the crate root via #![doc(test(attr(...)))].
237
- for attr in & opts . attrs {
251
+ // Next, any attributes that came from #![doc(test(attr(...)))].
252
+ for attr in & self . global_crate_attrs {
238
253
prog. push_str ( & format ! ( "#![{attr}]\n " ) ) ;
239
254
line_offset += 1 ;
240
255
}
0 commit comments