@@ -41,6 +41,7 @@ pub(crate) struct DocTestBuilder {
41
41
pub ( crate ) supports_color : bool ,
42
42
pub ( crate ) already_has_extern_crate : bool ,
43
43
pub ( crate ) has_main_fn : bool ,
44
+ pub ( crate ) global_crate_attrs : Vec < String > ,
44
45
pub ( crate ) crate_attrs : String ,
45
46
/// If this is a merged doctest, it will be put into `everything_else`, otherwise it will
46
47
/// put into `crate_attrs`.
@@ -57,6 +58,7 @@ impl DocTestBuilder {
57
58
source : & str ,
58
59
crate_name : Option < & str > ,
59
60
edition : Edition ,
61
+ global_crate_attrs : Vec < String > ,
60
62
can_merge_doctests : bool ,
61
63
// If `test_id` is `None`, it means we're generating code for a code example "run" link.
62
64
test_id : Option < String > ,
@@ -88,6 +90,7 @@ impl DocTestBuilder {
88
90
// If the AST returned an error, we don't want this doctest to be merged with the
89
91
// others.
90
92
return Self :: invalid (
93
+ Vec :: new ( ) ,
91
94
String :: new ( ) ,
92
95
String :: new ( ) ,
93
96
String :: new ( ) ,
@@ -104,12 +107,16 @@ impl DocTestBuilder {
104
107
let can_be_merged = can_merge_doctests
105
108
&& !has_global_allocator
106
109
&& 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 ( )
107
113
// If this is a merged doctest and a defined macro uses `$crate`, then the path will
108
114
// not work, so better not put it into merged doctests.
109
115
&& !( has_macro_def && everything_else. contains ( "$crate" ) ) ;
110
116
Self {
111
117
supports_color,
112
118
has_main_fn,
119
+ global_crate_attrs,
113
120
crate_attrs,
114
121
maybe_crate_attrs,
115
122
crates,
@@ -122,6 +129,7 @@ impl DocTestBuilder {
122
129
}
123
130
124
131
fn invalid (
132
+ global_crate_attrs : Vec < String > ,
125
133
crate_attrs : String ,
126
134
maybe_crate_attrs : String ,
127
135
crates : String ,
@@ -131,6 +139,7 @@ impl DocTestBuilder {
131
139
Self {
132
140
supports_color : false ,
133
141
has_main_fn : false ,
142
+ global_crate_attrs,
134
143
crate_attrs,
135
144
maybe_crate_attrs,
136
145
crates,
@@ -160,7 +169,8 @@ impl DocTestBuilder {
160
169
let mut line_offset = 0 ;
161
170
let mut prog = String :: new ( ) ;
162
171
let everything_else = self . everything_else . trim ( ) ;
163
- if opts. attrs . is_empty ( ) {
172
+
173
+ if self . global_crate_attrs . is_empty ( ) {
164
174
// If there aren't any attributes supplied by #![doc(test(attr(...)))], then allow some
165
175
// lints that are commonly triggered in doctests. The crate-level test attributes are
166
176
// commonly used to make tests fail in case they trigger warnings, so having this there in
@@ -169,8 +179,8 @@ impl DocTestBuilder {
169
179
line_offset += 1 ;
170
180
}
171
181
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 {
174
184
prog. push_str ( & format ! ( "#![{attr}]\n " ) ) ;
175
185
line_offset += 1 ;
176
186
}
0 commit comments