@@ -41,6 +41,7 @@ pub(crate) struct DocTestBuilder {
4141 pub ( crate ) supports_color : bool ,
4242 pub ( crate ) already_has_extern_crate : bool ,
4343 pub ( crate ) has_main_fn : bool ,
44+ pub ( crate ) global_crate_attrs : Vec < String > ,
4445 pub ( crate ) crate_attrs : String ,
4546 /// If this is a merged doctest, it will be put into `everything_else`, otherwise it will
4647 /// put into `crate_attrs`.
@@ -57,6 +58,7 @@ impl DocTestBuilder {
5758 source : & str ,
5859 crate_name : Option < & str > ,
5960 edition : Edition ,
61+ global_crate_attrs : Vec < String > ,
6062 can_merge_doctests : bool ,
6163 // If `test_id` is `None`, it means we're generating code for a code example "run" link.
6264 test_id : Option < String > ,
@@ -88,6 +90,7 @@ impl DocTestBuilder {
8890 // If the AST returned an error, we don't want this doctest to be merged with the
8991 // others.
9092 return Self :: invalid (
93+ Vec :: new ( ) ,
9194 String :: new ( ) ,
9295 String :: new ( ) ,
9396 String :: new ( ) ,
@@ -104,12 +107,16 @@ impl DocTestBuilder {
104107 let can_be_merged = can_merge_doctests
105108 && !has_global_allocator
106109 && crate_attrs. is_empty ( )
110+ // FIXME: We can probably merge tests which have the same global crate attrs,
111+ // like we already do for the edition
112+ && global_crate_attrs. is_empty ( )
107113 // If this is a merged doctest and a defined macro uses `$crate`, then the path will
108114 // not work, so better not put it into merged doctests.
109115 && !( has_macro_def && everything_else. contains ( "$crate" ) ) ;
110116 Self {
111117 supports_color,
112118 has_main_fn,
119+ global_crate_attrs,
113120 crate_attrs,
114121 maybe_crate_attrs,
115122 crates,
@@ -122,6 +129,7 @@ impl DocTestBuilder {
122129 }
123130
124131 fn invalid (
132+ global_crate_attrs : Vec < String > ,
125133 crate_attrs : String ,
126134 maybe_crate_attrs : String ,
127135 crates : String ,
@@ -131,6 +139,7 @@ impl DocTestBuilder {
131139 Self {
132140 supports_color : false ,
133141 has_main_fn : false ,
142+ global_crate_attrs,
134143 crate_attrs,
135144 maybe_crate_attrs,
136145 crates,
@@ -160,7 +169,8 @@ impl DocTestBuilder {
160169 let mut line_offset = 0 ;
161170 let mut prog = String :: new ( ) ;
162171 let everything_else = self . everything_else . trim ( ) ;
163- if opts. attrs . is_empty ( ) {
172+
173+ if self . global_crate_attrs . is_empty ( ) {
164174 // If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some
165175 // lints that are commonly triggered in doctests. The crate-level test attributes are
166176 // commonly used to make tests fail in case they trigger warnings, so having this there in
@@ -169,8 +179,8 @@ impl DocTestBuilder {
169179 line_offset += 1 ;
170180 }
171181
172- // Next, any attributes that came from the crate root via #![doc(test(attr(...)))].
173- for attr in & opts . attrs {
182+ // Next, any attributes that came from #![doc(test(attr(...)))].
183+ for attr in & self . global_crate_attrs {
174184 prog. push_str ( & format ! ( "#![{attr}]\n " ) ) ;
175185 line_offset += 1 ;
176186 }
0 commit comments