@@ -45,6 +45,7 @@ pub(crate) struct BuildDocTestBuilder<'a> {
4545 test_id : Option < String > ,
4646 lang_str : Option < & ' a LangString > ,
4747 span : Span ,
48+ global_crate_attrs : Vec < String > ,
4849}
4950
5051impl < ' a > BuildDocTestBuilder < ' a > {
@@ -57,6 +58,7 @@ impl<'a> BuildDocTestBuilder<'a> {
5758 test_id : None ,
5859 lang_str : None ,
5960 span : DUMMY_SP ,
61+ global_crate_attrs : Vec :: new ( ) ,
6062 }
6163 }
6264
@@ -96,6 +98,12 @@ impl<'a> BuildDocTestBuilder<'a> {
9698 self
9799 }
98100
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+
99107 pub ( crate ) fn build ( self , dcx : Option < DiagCtxtHandle < ' _ > > ) -> DocTestBuilder {
100108 let BuildDocTestBuilder {
101109 source,
@@ -106,6 +114,7 @@ impl<'a> BuildDocTestBuilder<'a> {
106114 test_id,
107115 lang_str,
108116 span,
117+ global_crate_attrs,
109118 } = self ;
110119 let can_merge_doctests = can_merge_doctests
111120 && lang_str. is_some_and ( |lang_str| {
@@ -133,6 +142,7 @@ impl<'a> BuildDocTestBuilder<'a> {
133142 // If the AST returned an error, we don't want this doctest to be merged with the
134143 // others.
135144 return DocTestBuilder :: invalid (
145+ Vec :: new ( ) ,
136146 String :: new ( ) ,
137147 String :: new ( ) ,
138148 String :: new ( ) ,
@@ -155,6 +165,7 @@ impl<'a> BuildDocTestBuilder<'a> {
155165 DocTestBuilder {
156166 supports_color,
157167 has_main_fn,
168+ global_crate_attrs,
158169 crate_attrs,
159170 maybe_crate_attrs,
160171 crates,
@@ -173,6 +184,7 @@ pub(crate) struct DocTestBuilder {
173184 pub ( crate ) supports_color : bool ,
174185 pub ( crate ) already_has_extern_crate : bool ,
175186 pub ( crate ) has_main_fn : bool ,
187+ pub ( crate ) global_crate_attrs : Vec < String > ,
176188 pub ( crate ) crate_attrs : String ,
177189 /// If this is a merged doctest, it will be put into `everything_else`, otherwise it will
178190 /// put into `crate_attrs`.
@@ -186,6 +198,7 @@ pub(crate) struct DocTestBuilder {
186198
187199impl DocTestBuilder {
188200 fn invalid (
201+ global_crate_attrs : Vec < String > ,
189202 crate_attrs : String ,
190203 maybe_crate_attrs : String ,
191204 crates : String ,
@@ -195,6 +208,7 @@ impl DocTestBuilder {
195208 Self {
196209 supports_color : false ,
197210 has_main_fn : false ,
211+ global_crate_attrs,
198212 crate_attrs,
199213 maybe_crate_attrs,
200214 crates,
@@ -224,7 +238,8 @@ impl DocTestBuilder {
224238 let mut line_offset = 0 ;
225239 let mut prog = String :: new ( ) ;
226240 let everything_else = self . everything_else . trim ( ) ;
227- if opts. attrs . is_empty ( ) {
241+
242+ if self . global_crate_attrs . is_empty ( ) {
228243 // If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some
229244 // lints that are commonly triggered in doctests. The crate-level test attributes are
230245 // commonly used to make tests fail in case they trigger warnings, so having this there in
@@ -233,8 +248,8 @@ impl DocTestBuilder {
233248 line_offset += 1 ;
234249 }
235250
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 {
238253 prog. push_str ( & format ! ( "#![{attr}]\n " ) ) ;
239254 line_offset += 1 ;
240255 }
0 commit comments