5 use rustc_span::create_default_session_globals_then;
6 use rustc_span::symbol::{Ident, Symbol};
7 use rustc_span::DUMMY_SP;
9 fn word_cfg(s: &str) -> Cfg {
10 Cfg::Cfg(Symbol::intern(s), None)
13 fn name_value_cfg(name: &str, value: &str) -> Cfg {
14 Cfg::Cfg(Symbol::intern(name), Some(Symbol::intern(value)))
17 fn dummy_meta_item_word(name: &str) -> MetaItem {
19 path: Path::from_ident(Ident::from_str(name)),
20 kind: MetaItemKind::Word,
25 macro_rules! dummy_meta_item_list {
26 ($name:ident, [$($list:ident),* $(,)?]) => {
28 path: Path::from_ident(Ident::from_str(stringify!($name))),
29 kind: MetaItemKind::List(vec![
31 NestedMetaItem::MetaItem(
32 dummy_meta_item_word(stringify!($list)),
40 ($name:ident, [$($list:expr),* $(,)?]) => {
42 path: Path::from_ident(Ident::from_str(stringify!($name))),
43 kind: MetaItemKind::List(vec![
45 NestedMetaItem::MetaItem($list),
55 create_default_session_globals_then(|| {
56 assert_eq!(!Cfg::False, Cfg::True);
57 assert_eq!(!Cfg::True, Cfg::False);
58 assert_eq!(!word_cfg("test"), Cfg::Not(Box::new(word_cfg("test"))));
60 !Cfg::All(vec![word_cfg("a"), word_cfg("b")]),
61 Cfg::Not(Box::new(Cfg::All(vec![word_cfg("a"), word_cfg("b")])))
64 !Cfg::Any(vec![word_cfg("a"), word_cfg("b")]),
65 Cfg::Not(Box::new(Cfg::Any(vec![word_cfg("a"), word_cfg("b")])))
67 assert_eq!(!Cfg::Not(Box::new(word_cfg("test"))), word_cfg("test"));
73 create_default_session_globals_then(|| {
74 let mut x = Cfg::False;
76 assert_eq!(x, Cfg::False);
80 assert_eq!(x, Cfg::False);
82 x = word_cfg("test2");
84 assert_eq!(x, word_cfg("test2"));
87 x &= word_cfg("test3");
88 assert_eq!(x, word_cfg("test3"));
90 x &= word_cfg("test3");
91 assert_eq!(x, word_cfg("test3"));
93 x &= word_cfg("test4");
94 assert_eq!(x, Cfg::All(vec![word_cfg("test3"), word_cfg("test4")]));
96 x &= word_cfg("test4");
97 assert_eq!(x, Cfg::All(vec![word_cfg("test3"), word_cfg("test4")]));
99 x &= word_cfg("test5");
100 assert_eq!(x, Cfg::All(vec![word_cfg("test3"), word_cfg("test4"), word_cfg("test5")]));
102 x &= Cfg::All(vec![word_cfg("test6"), word_cfg("test7")]);
114 x &= Cfg::All(vec![word_cfg("test6"), word_cfg("test7")]);
126 let mut y = Cfg::Any(vec![word_cfg("a"), word_cfg("b")]);
136 Cfg::Any(vec![word_cfg("a"), word_cfg("b")]),
140 let mut z = word_cfg("test8");
141 z &= Cfg::All(vec![word_cfg("test9"), word_cfg("test10")]);
142 assert_eq!(z, Cfg::All(vec![word_cfg("test9"), word_cfg("test10"), word_cfg("test8")]));
144 let mut z = word_cfg("test11");
145 z &= Cfg::All(vec![word_cfg("test11"), word_cfg("test12")]);
146 assert_eq!(z, Cfg::All(vec![word_cfg("test11"), word_cfg("test12")]));
149 word_cfg("a") & word_cfg("b") & word_cfg("c"),
150 Cfg::All(vec![word_cfg("a"), word_cfg("b"), word_cfg("c")])
157 create_default_session_globals_then(|| {
158 let mut x = Cfg::True;
160 assert_eq!(x, Cfg::True);
162 x = word_cfg("test");
164 assert_eq!(x, Cfg::True);
166 x = word_cfg("test2");
168 assert_eq!(x, word_cfg("test2"));
171 x |= word_cfg("test3");
172 assert_eq!(x, word_cfg("test3"));
174 x |= word_cfg("test3");
175 assert_eq!(x, word_cfg("test3"));
177 x |= word_cfg("test4");
178 assert_eq!(x, Cfg::Any(vec![word_cfg("test3"), word_cfg("test4")]));
180 x |= word_cfg("test4");
181 assert_eq!(x, Cfg::Any(vec![word_cfg("test3"), word_cfg("test4")]));
183 x |= word_cfg("test5");
184 assert_eq!(x, Cfg::Any(vec![word_cfg("test3"), word_cfg("test4"), word_cfg("test5")]));
186 x |= Cfg::Any(vec![word_cfg("test6"), word_cfg("test7")]);
198 x |= Cfg::Any(vec![word_cfg("test6"), word_cfg("test7")]);
210 let mut y = Cfg::All(vec![word_cfg("a"), word_cfg("b")]);
220 Cfg::All(vec![word_cfg("a"), word_cfg("b")]),
224 let mut z = word_cfg("test8");
225 z |= Cfg::Any(vec![word_cfg("test9"), word_cfg("test10")]);
226 assert_eq!(z, Cfg::Any(vec![word_cfg("test9"), word_cfg("test10"), word_cfg("test8")]));
228 let mut z = word_cfg("test11");
229 z |= Cfg::Any(vec![word_cfg("test11"), word_cfg("test12")]);
230 assert_eq!(z, Cfg::Any(vec![word_cfg("test11"), word_cfg("test12")]));
233 word_cfg("a") | word_cfg("b") | word_cfg("c"),
234 Cfg::Any(vec![word_cfg("a"), word_cfg("b"), word_cfg("c")])
241 create_default_session_globals_then(|| {
242 let mi = dummy_meta_item_word("all");
243 assert_eq!(Cfg::parse(&mi), Ok(word_cfg("all")));
246 attr::mk_name_value_item_str(Ident::from_str("all"), Symbol::intern("done"), DUMMY_SP);
247 assert_eq!(Cfg::parse(&mi), Ok(name_value_cfg("all", "done")));
249 let mi = dummy_meta_item_list!(all, [a, b]);
250 assert_eq!(Cfg::parse(&mi), Ok(word_cfg("a") & word_cfg("b")));
252 let mi = dummy_meta_item_list!(any, [a, b]);
253 assert_eq!(Cfg::parse(&mi), Ok(word_cfg("a") | word_cfg("b")));
255 let mi = dummy_meta_item_list!(not, [a]);
256 assert_eq!(Cfg::parse(&mi), Ok(!word_cfg("a")));
258 let mi = dummy_meta_item_list!(
260 [dummy_meta_item_list!(
262 [dummy_meta_item_word("a"), dummy_meta_item_list!(all, [b, c]),]
265 assert_eq!(Cfg::parse(&mi), Ok(!(word_cfg("a") | (word_cfg("b") & word_cfg("c")))));
267 let mi = dummy_meta_item_list!(all, [a, b, c]);
268 assert_eq!(Cfg::parse(&mi), Ok(word_cfg("a") & word_cfg("b") & word_cfg("c")));
273 fn test_parse_err() {
274 create_default_session_globals_then(|| {
275 let mi = attr::mk_name_value_item(Ident::from_str("foo"), LitKind::Bool(false), DUMMY_SP);
276 assert!(Cfg::parse(&mi).is_err());
278 let mi = dummy_meta_item_list!(not, [a, b]);
279 assert!(Cfg::parse(&mi).is_err());
281 let mi = dummy_meta_item_list!(not, []);
282 assert!(Cfg::parse(&mi).is_err());
284 let mi = dummy_meta_item_list!(foo, []);
285 assert!(Cfg::parse(&mi).is_err());
287 let mi = dummy_meta_item_list!(
289 [dummy_meta_item_list!(foo, []), dummy_meta_item_word("b"),]
291 assert!(Cfg::parse(&mi).is_err());
293 let mi = dummy_meta_item_list!(
295 [dummy_meta_item_word("a"), dummy_meta_item_list!(foo, []),]
297 assert!(Cfg::parse(&mi).is_err());
299 let mi = dummy_meta_item_list!(not, [dummy_meta_item_list!(foo, []),]);
300 assert!(Cfg::parse(&mi).is_err());
305 fn test_render_short_html() {
306 create_default_session_globals_then(|| {
307 assert_eq!(word_cfg("unix").render_short_html(), "Unix");
308 assert_eq!(name_value_cfg("target_os", "macos").render_short_html(), "macOS");
309 assert_eq!(name_value_cfg("target_pointer_width", "16").render_short_html(), "16-bit");
310 assert_eq!(name_value_cfg("target_endian", "little").render_short_html(), "Little-endian");
311 assert_eq!((!word_cfg("windows")).render_short_html(), "Non-Windows");
313 (word_cfg("unix") & word_cfg("windows")).render_short_html(),
316 assert_eq!((word_cfg("unix") | word_cfg("windows")).render_short_html(), "Unix or Windows");
318 (word_cfg("unix") & word_cfg("windows") & word_cfg("debug_assertions"))
319 .render_short_html(),
320 "Unix and Windows and debug-assertions enabled"
323 (word_cfg("unix") | word_cfg("windows") | word_cfg("debug_assertions"))
324 .render_short_html(),
325 "Unix or Windows or debug-assertions enabled"
328 (!(word_cfg("unix") | word_cfg("windows") | word_cfg("debug_assertions")))
329 .render_short_html(),
330 "Neither Unix nor Windows nor debug-assertions enabled"
333 ((word_cfg("unix") & name_value_cfg("target_arch", "x86_64"))
334 | (word_cfg("windows") & name_value_cfg("target_pointer_width", "64")))
335 .render_short_html(),
336 "Unix and x86-64, or Windows and 64-bit"
339 (!(word_cfg("unix") & word_cfg("windows"))).render_short_html(),
340 "Not (Unix and Windows)"
343 ((word_cfg("debug_assertions") | word_cfg("windows")) & word_cfg("unix"))
344 .render_short_html(),
345 "(Debug-assertions enabled or Windows) and Unix"
348 name_value_cfg("target_feature", "sse2").render_short_html(),
352 (name_value_cfg("target_arch", "x86_64") & name_value_cfg("target_feature", "sse2"))
353 .render_short_html(),
354 "x86-64 and <code>sse2</code>"
360 fn test_render_long_html() {
361 create_default_session_globals_then(|| {
362 assert_eq!(word_cfg("unix").render_long_html(), "Available on <strong>Unix</strong> only.");
364 name_value_cfg("target_os", "macos").render_long_html(),
365 "Available on <strong>macOS</strong> only."
368 name_value_cfg("target_os", "wasi").render_long_html(),
369 "Available on <strong>WASI</strong> only."
372 name_value_cfg("target_pointer_width", "16").render_long_html(),
373 "Available on <strong>16-bit</strong> only."
376 name_value_cfg("target_endian", "little").render_long_html(),
377 "Available on <strong>little-endian</strong> only."
380 (!word_cfg("windows")).render_long_html(),
381 "Available on <strong>non-Windows</strong> only."
384 (word_cfg("unix") & word_cfg("windows")).render_long_html(),
385 "Available on <strong>Unix and Windows</strong> only."
388 (word_cfg("unix") | word_cfg("windows")).render_long_html(),
389 "Available on <strong>Unix or Windows</strong> only."
392 (word_cfg("unix") & word_cfg("windows") & word_cfg("debug_assertions"))
394 "Available on <strong>Unix and Windows and debug-assertions enabled</strong> only."
397 (word_cfg("unix") | word_cfg("windows") | word_cfg("debug_assertions"))
399 "Available on <strong>Unix or Windows or debug-assertions enabled</strong> only."
402 (!(word_cfg("unix") | word_cfg("windows") | word_cfg("debug_assertions")))
404 "Available on <strong>neither Unix nor Windows nor debug-assertions enabled</strong>."
407 ((word_cfg("unix") & name_value_cfg("target_arch", "x86_64"))
408 | (word_cfg("windows") & name_value_cfg("target_pointer_width", "64")))
410 "Available on <strong>Unix and x86-64, or Windows and 64-bit</strong> only."
413 (!(word_cfg("unix") & word_cfg("windows"))).render_long_html(),
414 "Available on <strong>not (Unix and Windows)</strong>."
417 ((word_cfg("debug_assertions") | word_cfg("windows")) & word_cfg("unix"))
419 "Available on <strong>(debug-assertions enabled or Windows) and Unix</strong> only."
422 name_value_cfg("target_feature", "sse2").render_long_html(),
423 "Available with <strong>target feature <code>sse2</code></strong> only."
426 (name_value_cfg("target_arch", "x86_64") & name_value_cfg("target_feature", "sse2"))
428 "Available on <strong>x86-64 and target feature <code>sse2</code></strong> only."
434 fn test_simplify_with() {
435 // This is a tiny subset of things that could be simplified, but it likely covers 90% of
436 // real world usecases well.
437 create_default_session_globals_then(|| {
438 let foo = word_cfg("foo");
439 let bar = word_cfg("bar");
440 let baz = word_cfg("baz");
441 let quux = word_cfg("quux");
443 let foobar = Cfg::All(vec![foo.clone(), bar.clone()]);
444 let barbaz = Cfg::All(vec![bar.clone(), baz.clone()]);
445 let foobarbaz = Cfg::All(vec![foo.clone(), bar.clone(), baz.clone()]);
446 let bazquux = Cfg::All(vec![baz.clone(), quux.clone()]);
448 // Unrelated cfgs don't affect each other
449 assert_eq!(foo.simplify_with(&bar).as_ref(), Some(&foo));
450 assert_eq!(foobar.simplify_with(&bazquux).as_ref(), Some(&foobar));
452 // Identical cfgs are eliminated
453 assert_eq!(foo.simplify_with(&foo), None);
454 assert_eq!(foobar.simplify_with(&foobar), None);
456 // Multiple cfgs eliminate a single assumed cfg
457 assert_eq!(foobar.simplify_with(&foo).as_ref(), Some(&bar));
458 assert_eq!(foobar.simplify_with(&bar).as_ref(), Some(&foo));
460 // A single cfg is eliminated by multiple assumed cfg containing it
461 assert_eq!(foo.simplify_with(&foobar), None);
463 // Multiple cfgs eliminate the matching subset of multiple assumed cfg
464 assert_eq!(foobar.simplify_with(&barbaz).as_ref(), Some(&foo));
465 assert_eq!(foobar.simplify_with(&foobarbaz), None);