Debian Codegraph @

debian/missing-sources/cfg-expr/tests/eval.rs

raw
1 use cfg_expr::{
2 Expression, TargetPredicate,
3 expr::{Predicate, TargetMatcher},
4 targets::{ALL_BUILTINS as all, get_builtin_target_by_triple},
5 };
6
7 struct Target {
8 builtin: &'static cfg_expr::targets::TargetInfo,
9 #[cfg(feature = "targets")]
10 lexicon: Option<target_lexicon::Triple>,
11 }
12
13 impl Target {
14 fn make(s: &str) -> Self {
15 Self {
16 builtin: get_builtin_target_by_triple(s).unwrap(),
17 #[cfg(feature = "targets")]
18 lexicon: {
19 // Hack to workaround the addition in 1.48.0 of this weird, non-conformant
20 // target triple, until https://github.com/bytecodealliance/target-lexicon/issues/63 is
21 // resolved in a satisfactory manner, not really concerned about
22 // the presence of this triple in most normal cases
23 use target_lexicon as tl;
24 match s {
25 "avr-unknown-gnu-atmega328" => Some(tl::Triple {
26 architecture: tl::Architecture::Avr,
27 vendor: tl::Vendor::Unknown,
28 operating_system: tl::OperatingSystem::Unknown,
29 environment: tl::Environment::Unknown,
30 binary_format: tl::BinaryFormat::Unknown,
31 }),
32 triple => match triple.parse::<tl::Triple>() {
33 Ok(l) => Some(l),
34 Err(e) => {
35 // There are enough new weird architectures added in each version of
36 // Rust that it is difficult to keep target-lexicon aware of all of
37 // them. So try parsing this triple, but don't fail if it doesn't work.
38 eprintln!("failed to parse '{triple}': {e:?}");
39 None
40 }
41 },
42 }
43 },
44 }
45 }
46 }
47
48 macro_rules! tg_match {
49 ($pred:expr, $target:expr) => {
50 match $pred {
51 Predicate::Target(tg) => {
52 let tinfo = tg.matches($target.builtin);
53
54 #[cfg(feature = "targets")]
55 if !matches!(tg, TargetPredicate::HasAtomic(_))
56 && !matches!(tg, TargetPredicate::Panic(_))
57 {
58 if let Some(l) = &$target.lexicon {
59 let linfo = tg.matches(l);
60 assert_eq!(
61 tinfo, linfo,
62 "{:#?} builtin didn't match lexicon {:#?} for predicate {tg:#?}",
63 $target.builtin, $target.lexicon,
64 );
65
66 return linfo;
67 }
68 }
69
70 tinfo
71 }
72 _ => panic!("not a target predicate"),
73 }
74 };
75
76 ($pred:expr, $target:expr, $feats:expr) => {
77 match $pred {
78 Predicate::Target(tg) => {
79 let tinfo = tg.matches($target.builtin);
80
81 #[cfg(feature = "targets")]
82 if !matches!(tg, TargetPredicate::HasAtomic(_))
83 && !matches!(tg, TargetPredicate::Panic(_))
84 {
85 if let Some(l) = &$target.lexicon {
86 let linfo = tg.matches(l);
87 assert_eq!(
88 tinfo, linfo,
89 "{:#?} builtin didn't match lexicon {:#?} for predicate {tg:#?}",
90 $target.builtin, $target.lexicon,
91 );
92
93 return linfo;
94 }
95 }
96
97 tinfo
98 }
99 Predicate::TargetFeature(feat) => $feats.iter().find(|f| *f == feat).is_some(),
100 _ => panic!("not a target predicate"),
101 }
102 };
103 }
104
105 #[test]
106 fn target_family() {
107 let matches_any_family =
108 Expression::parse("any(unix, target_family = \"windows\", target_family = \"wasm\")")
109 .unwrap();
110 let impossible = Expression::parse("all(windows, target_family = \"unix\")").unwrap();
111
112 for target in all {
113 let target = Target::make(target.triple.as_str());
114 if target.builtin.families.is_empty() {
115 assert!(!matches_any_family.eval(|pred| { tg_match!(pred, target) }));
116 } else {
117 assert!(matches_any_family.eval(|pred| { tg_match!(pred, target) }));
118 }
119 assert!(!impossible.eval(|pred| { tg_match!(pred, target) }));
120 }
121 }
122
123 #[test]
124 fn tiny() {
125 assert!(Expression::parse("all()").unwrap().eval(|_| false));
126 assert!(!Expression::parse("any()").unwrap().eval(|_| true));
127 assert!(!Expression::parse("not(all())").unwrap().eval(|_| false));
128 assert!(Expression::parse("not(any())").unwrap().eval(|_| true));
129 assert!(Expression::parse("all(not(blah))").unwrap().eval(|_| false));
130 assert!(!Expression::parse("any(not(blah))").unwrap().eval(|_| true));
131 }
132
133 #[test]
134 fn very_specific() {
135 let specific = Expression::parse(
136 r#"all(
137 target_os = "windows",
138 target_arch = "x86",
139 windows,
140 target_env = "msvc",
141 target_feature = "fxsr",
142 target_feature = "sse",
143 target_feature = "sse2",
144 target_pointer_width = "32",
145 target_endian = "little",
146 not(target_vendor = "uwp"),
147 target_has_atomic = "8",
148 target_has_atomic = "16",
149 target_has_atomic = "32",
150 target_has_atomic = "64",
151 not(target_has_atomic = "128"),
152 target_has_atomic = "ptr",
153 panic = "unwind",
154 not(panic = "abort"),
155 )"#,
156 )
157 .unwrap();
158
159 for target in all {
160 let t = Target::make(target.triple.as_str());
161 assert_eq!(
162 matches!(
163 target.triple.as_str(),
164 "i686-pc-windows-msvc" | "i586-pc-windows-msvc" | "i686-win7-windows-msvc"
165 ),
166 specific.eval(|pred| { tg_match!(pred, t, &["fxsr", "sse", "sse2"]) }),
167 "expected true for i686-pc-windows-msvc, but got true for {}",
168 target.triple,
169 );
170 }
171
172 for target in all {
173 let expr = format!(
174 r#"cfg(
175 all(
176 target_arch = "{}",
177 {}
178 {}
179 target_env = "{}"
180 )
181 )"#,
182 target.arch.0,
183 if let Some(v) = &target.vendor {
184 format!(r#"target_vendor = "{}","#, v.0)
185 } else {
186 "".to_owned()
187 },
188 if let Some(v) = &target.os {
189 format!(r#"target_os = "{}","#, v.0)
190 } else {
191 "".to_owned()
192 },
193 target.env.as_ref().map_or("", |e| e.as_str()),
194 );
195
196 let specific = Expression::parse(&expr).unwrap();
197
198 let t = Target::make(target.triple.as_str());
199 assert!(
200 specific.eval(|pred| {
201 if target.triple.as_str() == "mips64-openwrt-linux-musl" {
202 if let Predicate::Target(TargetPredicate::Vendor(vendor)) = pred {
203 // This is a special predicate that doesn't follow the usual rules for
204 // target-lexicon.
205 return t.builtin.matches(&TargetPredicate::Vendor(vendor.clone()));
206 }
207 }
208 tg_match!(pred, t)
209 }),
210 "failed expression '{}' for {:#?}",
211 expr,
212 t.builtin,
213 );
214 }
215 }
216
217 #[test]
218 fn complex() {
219 let complex = Expression::parse(r#"cfg(all(unix, not(any(target_os="macos", target_os="android", target_os="emscripten"))))"#).unwrap();
220
221 // Should match linuxes
222 let linux_gnu = Target::make("x86_64-unknown-linux-gnu");
223 let linux_musl = Target::make("x86_64-unknown-linux-musl");
224
225 assert!(complex.eval(|pred| tg_match!(pred, linux_gnu)));
226 assert!(complex.eval(|pred| tg_match!(pred, linux_musl)));
227
228 // Should *not* match windows or mac or android
229 let windows_msvc = Target::make("x86_64-pc-windows-msvc");
230 let mac = Target::make("x86_64-apple-darwin");
231 let android = Target::make("aarch64-linux-android");
232
233 assert!(!complex.eval(|pred| tg_match!(pred, windows_msvc)));
234 assert!(!complex.eval(|pred| tg_match!(pred, mac)));
235 assert!(!complex.eval(|pred| tg_match!(pred, android)));
236
237 let complex =
238 Expression::parse(r#"all(not(target_os = "ios"), not(target_os = "android"))"#).unwrap();
239
240 assert!(complex.eval(|pred| tg_match!(pred, linux_gnu)));
241 assert!(complex.eval(|pred| tg_match!(pred, linux_musl)));
242 assert!(complex.eval(|pred| tg_match!(pred, windows_msvc)));
243 assert!(complex.eval(|pred| tg_match!(pred, mac)));
244 assert!(!complex.eval(|pred| tg_match!(pred, android)));
245
246 let complex = Expression::parse(r#"all(any(unix, target_arch="x86"), not(any(target_os="android", target_os="emscripten")))"#).unwrap();
247
248 // Should match linuxes and mac
249 assert!(complex.eval(|pred| tg_match!(pred, linux_gnu)));
250 assert!(complex.eval(|pred| tg_match!(pred, linux_musl)));
251 assert!(complex.eval(|pred| tg_match!(pred, mac)));
252
253 // Should *not* match x86_64 windows or android
254 assert!(!complex.eval(|pred| tg_match!(pred, windows_msvc)));
255 assert!(!complex.eval(|pred| tg_match!(pred, android)));
256
257 // Ensure that target_os = "none" matches against Os == None.
258 let complex = Expression::parse(r#"all(target_os="none")"#).unwrap();
259 let armebv7r_none_eabi = Target::make("armebv7r-none-eabi");
260 assert!(!complex.eval(|pred| tg_match!(pred, linux_gnu)));
261 assert!(complex.eval(|pred| tg_match!(pred, armebv7r_none_eabi)));
262 }
263
264 #[test]
265 fn unstable_target_abi() {
266 let linux_gnu = Target::make("x86_64-unknown-linux-gnu");
267 let linux_musl = Target::make("x86_64-unknown-linux-musl");
268 let windows_msvc = Target::make("x86_64-pc-windows-msvc");
269 let mac = Target::make("x86_64-apple-darwin");
270 let android = Target::make("aarch64-linux-android");
271
272 let target_with_abi_that_matches = cfg_expr::targets::TargetInfo {
273 triple: cfg_expr::targets::Triple::new_const("aarch64-apple-darwin"),
274 os: None,
275 abi: Some(cfg_expr::targets::Abi::new_const("eabihf")),
276 arch: cfg_expr::targets::Arch::aarch64,
277 env: None,
278 vendor: None,
279 families: cfg_expr::targets::Families::unix,
280 pointer_width: 64,
281 endian: cfg_expr::targets::Endian::little,
282 has_atomics: cfg_expr::targets::HasAtomics::atomic_8_16_32_64_128_ptr,
283 panic: cfg_expr::targets::Panic::unwind,
284 };
285
286 let target_with_abi_that_doesnt_match = cfg_expr::targets::TargetInfo {
287 abi: Some(cfg_expr::targets::Abi::new_const("ilp32")),
288 ..target_with_abi_that_matches.clone()
289 };
290
291 let abi_pred =
292 Expression::parse(r#"cfg(any(target_arch = "wasm32", target_abi = "eabihf"))"#).unwrap();
293
294 // Should match a specified target_abi that's the same
295 assert!(abi_pred.eval(|pred| {
296 match pred {
297 Predicate::Target(tp) => tp.matches(&target_with_abi_that_matches),
298 _ => false,
299 }
300 }));
301
302 // Should *not* match a specified target_abi that isn't the same
303 assert!(!abi_pred.eval(|pred| {
304 match pred {
305 Predicate::Target(tp) => tp.matches(&target_with_abi_that_doesnt_match),
306 _ => false,
307 }
308 }));
309
310 // Should *not* match any builtins at this point because target_abi isn't stable
311 assert!(!abi_pred.eval(|pred| tg_match!(pred, linux_gnu)));
312 assert!(!abi_pred.eval(|pred| tg_match!(pred, linux_musl)));
313 assert!(!abi_pred.eval(|pred| tg_match!(pred, mac)));
314 assert!(!abi_pred.eval(|pred| tg_match!(pred, windows_msvc)));
315 assert!(!abi_pred.eval(|pred| tg_match!(pred, android)));
316 }
317
318 #[test]
319 fn wasm_family() {
320 let wasm = Expression::parse(r#"cfg(target_family = "wasm")"#).unwrap();
321
322 let wasm32_unknown = Target::make("wasm32-unknown-unknown");
323 let wasm32_emscripten = Target::make("wasm32-unknown-emscripten");
324 let wasm32_wasip1 = Target::make("wasm32-wasip1");
325 let wasm32_wasip1_threads = Target::make("wasm32-wasip1-threads");
326 let wasm32_wasip2 = Target::make("wasm32-wasip2");
327 let wasm32v1_none = Target::make("wasm32v1-none");
328 let wasm64_unknown = Target::make("wasm64-unknown-unknown");
329
330 // All of the above targets match.
331 assert!(wasm.eval(|pred| tg_match!(pred, wasm32_unknown)));
332 assert!(wasm.eval(|pred| tg_match!(pred, wasm32_emscripten)));
333 assert!(wasm.eval(|pred| tg_match!(pred, wasm32_wasip1)));
334 assert!(wasm.eval(|pred| tg_match!(pred, wasm32_wasip1_threads)));
335 assert!(wasm.eval(|pred| tg_match!(pred, wasm32_wasip2)));
336 assert!(wasm.eval(|pred| tg_match!(pred, wasm32v1_none)));
337 assert!(wasm.eval(|pred| tg_match!(pred, wasm64_unknown)));
338 }
339
340 #[test]
341 fn features() {
342 let enabled = ["good", "bad", "ugly"];
343
344 let many_features = Expression::parse(
345 r#"all(feature = "good", feature = "bad", feature = "ugly", not(feature = "nope"))"#,
346 )
347 .unwrap();
348
349 assert!(many_features.eval(|pred| {
350 match pred {
351 Predicate::Feature(name) => enabled.contains(name),
352 _ => false,
353 }
354 }));
355
356 let feature_and_target_feature =
357 Expression::parse(r#"all(feature = "make_fast", target_feature = "sse4.2")"#).unwrap();
358
359 assert!(feature_and_target_feature.eval(|pred| {
360 match pred {
361 Predicate::Feature(name) => *name == "make_fast",
362 Predicate::TargetFeature(feat) => *feat == "sse4.2",
363 _ => false,
364 }
365 }));
366
367 assert_eq!(
368 feature_and_target_feature.eval(|pred| {
369 match pred {
370 Predicate::Feature(_) => Some(false),
371 Predicate::TargetFeature(_) => None,
372 _ => panic!("unexpected predicate"),
373 }
374 }),
375 Some(false),
376 "all() with Some(false) and None evaluates to Some(false)"
377 );
378
379 assert_eq!(
380 feature_and_target_feature.eval(|pred| {
381 match pred {
382 Predicate::Feature(_) => Some(true),
383 Predicate::TargetFeature(_) => None,
384 _ => panic!("unexpected predicate"),
385 }
386 }),
387 None,
388 "all() with Some(true) and None evaluates to None"
389 );
390 }