Skip to content

Commit 6b45e4f

Browse files
committed
Implement boolean lit support in cfg predicates
1 parent 3894313 commit 6b45e4f

File tree

5 files changed

+45
-5
lines changed

5 files changed

+45
-5
lines changed

compiler/rustc_ast/src/attr/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,16 @@ impl NestedMetaItem {
527527
}
528528
}
529529

530+
/// Returns the `MetaItem` if `self` is a `NestedMetaItem::MetaItem` or if it's
531+
/// `NestedMetaItem::Lit(MetaItemLit { kind: LitKind::Bool(_), .. })`.
532+
pub fn meta_item_or_bool(&self) -> Option<&NestedMetaItem> {
533+
match self {
534+
NestedMetaItem::MetaItem(_item) => Some(self),
535+
NestedMetaItem::Lit(MetaItemLit { kind: LitKind::Bool(_), .. }) => Some(self),
536+
_ => None,
537+
}
538+
}
539+
530540
/// Returns the `MetaItem` if `self` is a `NestedMetaItem::MetaItem`.
531541
pub fn meta_item(&self) -> Option<&MetaItem> {
532542
match self {

compiler/rustc_attr/src/builtin.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ pub fn eval_condition(
603603

604604
let cfg = match cfg {
605605
ast::NestedMetaItem::MetaItem(meta_item) => meta_item,
606+
ast::NestedMetaItem::Lit(MetaItemLit { kind: LitKind::Bool(b), .. }) => return *b,
606607
_ => {
607608
dcx.emit_err(session_diagnostics::UnsupportedLiteral {
608609
span: cfg.span(),
@@ -649,7 +650,7 @@ pub fn eval_condition(
649650
}
650651
ast::MetaItemKind::List(mis) => {
651652
for mi in mis.iter() {
652-
if !mi.is_meta_item() {
653+
if mi.meta_item_or_bool().is_none() {
653654
dcx.emit_err(session_diagnostics::UnsupportedLiteral {
654655
span: mi.span(),
655656
reason: UnsupportedLiteralReason::Generic,

compiler/rustc_expand/src/config.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,9 @@ pub fn parse_cfg<'a>(meta_item: &'a MetaItem, sess: &Session) -> Option<&'a Nest
466466
sess.dcx().emit_err(InvalidCfg::MultiplePredicates { span: l.span() });
467467
None
468468
}
469-
Some([single]) => match single.is_meta_item() {
470-
true => Some(single),
471-
false => {
469+
Some([single]) => match single.meta_item_or_bool() {
470+
Some(meta_item) => Some(meta_item),
471+
None => {
472472
sess.dcx().emit_err(InvalidCfg::PredicateLiteral { span: single.span() });
473473
None
474474
}

compiler/rustc_metadata/src/native_libs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ impl<'tcx> Collector<'tcx> {
309309
.emit_err(errors::LinkCfgSinglePredicate { span: item.span() });
310310
continue;
311311
};
312-
if !link_cfg.is_meta_item() {
312+
let Some(link_cfg) = link_cfg.meta_item_or_bool() else {
313313
sess.dcx()
314314
.emit_err(errors::LinkCfgSinglePredicate { span: item.span() });
315315
continue;

tests/ui/cfg/true-false.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//@ run-pass
2+
3+
#![feature(link_cfg)]
4+
5+
#[cfg(true)]
6+
fn foo() -> bool {
7+
cfg!(true)
8+
}
9+
10+
#[cfg(false)]
11+
fn foo() -> bool {
12+
cfg!(false)
13+
}
14+
15+
#[cfg_attr(true, cfg(false))]
16+
fn foo() {}
17+
18+
#[link(name = "foo", cfg(false))]
19+
extern "C" {}
20+
21+
fn main() {
22+
assert!(foo());
23+
assert!(!cfg!(false));
24+
assert!(cfg!(not(false)));
25+
assert!(cfg!(all(true)));
26+
assert!(cfg!(any(true)));
27+
assert!(cfg!(any(false, true)));
28+
assert_eq!(cfg!(true), cfg!(not(false)));
29+
}

0 commit comments

Comments
 (0)