@@ -42,16 +42,19 @@ pub trait CfgFolder: fold::Folder {
42
42
/// configuration.
43
43
pub struct StripUnconfigured < ' a > {
44
44
diag : CfgDiagReal < ' a , ' a > ,
45
+ should_test : bool ,
45
46
config : & ' a ast:: CrateConfig ,
46
47
}
47
48
48
49
impl < ' a > StripUnconfigured < ' a > {
49
50
pub fn new ( config : & ' a ast:: CrateConfig ,
51
+ should_test : bool ,
50
52
diagnostic : & ' a Handler ,
51
53
feature_gated_cfgs : & ' a mut Vec < GatedCfgAttr > )
52
54
-> Self {
53
55
StripUnconfigured {
54
56
config : config,
57
+ should_test : should_test,
55
58
diag : CfgDiagReal { diag : diagnostic, feature_gated_cfgs : feature_gated_cfgs } ,
56
59
}
57
60
}
@@ -96,6 +99,11 @@ impl<'a> CfgFolder for StripUnconfigured<'a> {
96
99
// configuration based on the item's attributes
97
100
fn in_cfg ( & mut self , attrs : & [ ast:: Attribute ] ) -> bool {
98
101
attrs. iter ( ) . all ( |attr| {
102
+ // When not compiling with --test we should not compile the #[test] functions
103
+ if !self . should_test && is_test_or_bench ( attr) {
104
+ return false ;
105
+ }
106
+
99
107
let mis = match attr. node . value . node {
100
108
ast:: MetaItemKind :: List ( _, ref mis) if is_cfg ( & attr) => mis,
101
109
_ => return true
@@ -135,12 +143,12 @@ impl<'a> CfgFolder for StripUnconfigured<'a> {
135
143
136
144
// Support conditional compilation by transforming the AST, stripping out
137
145
// any items that do not belong in the current configuration
138
- pub fn strip_unconfigured_items ( diagnostic : & Handler , krate : ast:: Crate ,
146
+ pub fn strip_unconfigured_items ( diagnostic : & Handler , krate : ast:: Crate , should_test : bool ,
139
147
feature_gated_cfgs : & mut Vec < GatedCfgAttr > )
140
148
-> ast:: Crate
141
149
{
142
150
let config = & krate. config . clone ( ) ;
143
- StripUnconfigured :: new ( config, diagnostic, feature_gated_cfgs) . fold_crate ( krate)
151
+ StripUnconfigured :: new ( config, should_test , diagnostic, feature_gated_cfgs) . fold_crate ( krate)
144
152
}
145
153
146
154
impl < T : CfgFolder > fold:: Folder for T {
@@ -278,6 +286,10 @@ fn is_cfg(attr: &ast::Attribute) -> bool {
278
286
attr. check_name ( "cfg" )
279
287
}
280
288
289
+ fn is_test_or_bench ( attr : & ast:: Attribute ) -> bool {
290
+ attr. check_name ( "test" ) || attr. check_name ( "bench" )
291
+ }
292
+
281
293
pub trait CfgDiag {
282
294
fn emit_error < F > ( & mut self , f : F ) where F : FnMut ( & Handler ) ;
283
295
fn flag_gated < F > ( & mut self , f : F ) where F : FnMut ( & mut Vec < GatedCfgAttr > ) ;
0 commit comments