Skip to content

Commit 97fdee6

Browse files
Even more cfg cleanup
1 parent efd7436 commit 97fdee6

2 files changed

Lines changed: 64 additions & 26 deletions

File tree

src/librustdoc/clean/cfg.rs

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -235,14 +235,11 @@ impl Cfg {
235235
CfgEntry::Any(a, _) | CfgEntry::All(a, _) => {
236236
if a.is_empty() {
237237
false
238-
} else if let [a] = a.as_slice() {
239-
should_append_only_to_description(&a)
240238
} else {
241-
true
239+
a.iter().any(|sub| should_append_only_to_description(sub))
242240
}
243241
}
244-
CfgEntry::Not(a, _) => !should_append_only_to_description(a),
245-
CfgEntry::Bool(..) => false,
242+
CfgEntry::Not(..) | CfgEntry::Bool(..) => false,
246243
}
247244
}
248245
should_append_only_to_description(&self.0)
@@ -534,20 +531,45 @@ impl Display<'_> {
534531

535532
impl fmt::Display for Display<'_> {
536533
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
534+
fn display_bool(fmt: &mut fmt::Formatter<'_>, value: bool) -> fmt::Result {
535+
if value { fmt.write_str("everywhere") } else { fmt.write_str("nowhere") }
536+
}
537+
537538
match &self.0 {
538-
CfgEntry::Not(CfgEntry::Any(sub_cfgs, _), _) => {
539-
let separator = if sub_cfgs.iter().all(is_simple_cfg) { " nor " } else { ", nor " };
540-
fmt.write_str("neither ")?;
541-
542-
sub_cfgs
543-
.iter()
544-
.map(|sub_cfg| {
545-
Wrapped::with_parens()
546-
.when(is_any_cfg(sub_cfg))
547-
.wrap(Display(sub_cfg, self.1))
548-
})
549-
.joined(separator, fmt)
550-
}
539+
CfgEntry::Not(CfgEntry::Not(sub_cfg, _), _) => Display(sub_cfg, self.1).fmt(fmt),
540+
CfgEntry::Not(CfgEntry::Any(sub_cfgs, _), _) => match sub_cfgs.as_slice() {
541+
// `not(any())` is `true` because `any()` is `false`.
542+
[] => display_bool(fmt, true),
543+
[CfgEntry::Bool(value, _)] => display_bool(fmt, !*value),
544+
sub_cfgs => {
545+
let separator =
546+
if sub_cfgs.iter().all(is_simple_cfg) { " nor " } else { ", nor " };
547+
if sub_cfgs.len() > 1 {
548+
fmt.write_str("neither ")?;
549+
} else {
550+
fmt.write_str("not(")?;
551+
}
552+
553+
sub_cfgs
554+
.iter()
555+
.map(|sub_cfg| {
556+
Wrapped::with_parens()
557+
.when(is_any_cfg(sub_cfg))
558+
.wrap(Display(sub_cfg, self.1))
559+
})
560+
.joined(separator, fmt)?;
561+
if sub_cfgs.len() == 1 {
562+
fmt.write_str(")")?;
563+
}
564+
Ok(())
565+
}
566+
},
567+
CfgEntry::Not(s @ CfgEntry::All(sub_cfgs, _), _) => match sub_cfgs.as_slice() {
568+
// `not(all())` is `false` because `all()` is `true`.
569+
[] => display_bool(fmt, false),
570+
[CfgEntry::Bool(value, _)] => display_bool(fmt, !*value),
571+
_ => write!(fmt, "not ({})", Display(s, self.1)),
572+
},
551573
CfgEntry::Not(simple @ CfgEntry::NameValue { .. }, _) => {
552574
write!(fmt, "non-{}", Display(simple, self.1))
553575
}
@@ -559,13 +581,7 @@ impl fmt::Display for Display<'_> {
559581
}
560582
CfgEntry::All(sub_cfgs, _) => self.display_sub_cfgs(fmt, sub_cfgs.as_slice(), " and "),
561583

562-
CfgEntry::Bool(v, _) => {
563-
if *v {
564-
fmt.write_str("everywhere")
565-
} else {
566-
fmt.write_str("nowhere")
567-
}
568-
}
584+
CfgEntry::Bool(v, _) => display_bool(fmt, *v),
569585

570586
&CfgEntry::NameValue { name, value, .. } => {
571587
let human_readable = match (*name, value) {

src/librustdoc/clean/cfg/tests.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ fn cfg_any_e(v: ThinVec<CfgEntry>) -> CfgEntry {
4242
}
4343

4444
fn cfg_not(v: CfgEntry) -> Cfg {
45-
Cfg(CfgEntry::Not(Box::new(v), DUMMY_SP))
45+
Cfg(cfg_not_e(v))
46+
}
47+
48+
fn cfg_not_e(v: CfgEntry) -> CfgEntry {
49+
CfgEntry::Not(Box::new(v), DUMMY_SP)
4650
}
4751

4852
fn cfg_true() -> Cfg {
@@ -381,14 +385,32 @@ fn test_render_long_html() {
381385
.render_long_html(),
382386
"Available on <strong>x86-64 and target feature <code>sse2</code></strong> only."
383387
);
388+
// `any(true)`
384389
assert_eq!(
385390
cfg_any(thin_vec![cfg_true_e()]).render_long_html(),
386391
"Available <strong>everywhere</strong>.",
387392
);
393+
// `not(any(true))`
394+
assert_eq!(
395+
cfg_not(cfg_any_e(thin_vec![cfg_true_e()])).render_long_html(),
396+
"Available <strong>nowhere</strong>.",
397+
);
398+
// `any(all(true))`
388399
assert_eq!(
389400
cfg_any(thin_vec![cfg_all_e(thin_vec![cfg_true_e()])]).render_long_html(),
390401
"Available <strong>everywhere</strong>."
391402
);
403+
// `not(any(all(true)))`
404+
assert_eq!(
405+
cfg_not(cfg_any_e(thin_vec![cfg_all_e(thin_vec![cfg_true_e()])])).render_long_html(),
406+
"Available <strong>not(everywhere)</strong>.",
407+
);
408+
// `not(not(any(all(true))))`
409+
assert_eq!(
410+
cfg_not(cfg_not_e(cfg_any_e(thin_vec![cfg_all_e(thin_vec![cfg_true_e()])])))
411+
.render_long_html(),
412+
"Available <strong>everywhere</strong>.",
413+
);
392414
})
393415
}
394416

0 commit comments

Comments
 (0)