Skip to content

Commit 5dcfcaa

Browse files
committed
Allow boolean literals in check-cfg
1 parent 78948ac commit 5dcfcaa

File tree

5 files changed

+22
-24
lines changed

5 files changed

+22
-24
lines changed

compiler/rustc_ast/src/attr/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,14 @@ impl MetaItemInner {
570570
}
571571
}
572572

573+
/// Returns the bool if `self` is a boolean `MetaItemInner::Literal`.
574+
pub fn bool(&self) -> Option<bool> {
575+
match self {
576+
MetaItemInner::Lit(MetaItemLit { kind: LitKind::Bool(b), .. }) => Some(*b),
577+
_ => None,
578+
}
579+
}
580+
573581
/// Returns the `MetaItem` if `self` is a `MetaItemInner::MetaItem` or if it's
574582
/// `MetaItemInner::Lit(MetaItemLit { kind: LitKind::Bool(_), .. })`.
575583
pub fn meta_item_or_bool(&self) -> Option<&MetaItemInner> {

compiler/rustc_interface/src/interface.rs

+5
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ pub(crate) fn parse_check_cfg(dcx: DiagCtxtHandle<'_>, specs: Vec<String>) -> Ch
204204
error!("`cfg()` names cannot be after values");
205205
}
206206
names.push(ident);
207+
} else if let Some(boolean) = arg.bool() {
208+
names.push(rustc_span::Ident::new(
209+
if boolean { rustc_span::kw::True } else { rustc_span::kw::False },
210+
arg.span(),
211+
));
207212
} else if arg.has_name(sym::any)
208213
&& let Some(args) = arg.meta_item_list()
209214
{

tests/ui/cfg/raw-true-false.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
//@ check-pass
2-
//@ compile-flags: --cfg false --check-cfg=cfg(r#false)
3-
4-
#![deny(warnings)]
5-
6-
#[expect(unexpected_cfgs)]
7-
mod a {
8-
#[cfg(r#true)]
9-
pub fn foo() {}
10-
}
11-
12-
mod b {
13-
#[cfg(r#false)]
14-
pub fn bar() {}
15-
}
16-
2+
//@ revisions: r0x0 r0x1 r1x0 r1x1
3+
//@[r0x0] compile-flags: --cfg false --check-cfg=cfg(false)
4+
//@[r0x1] compile-flags: --cfg false --check-cfg=cfg(r#false)
5+
//@[r1x0] compile-flags: --cfg r#false --check-cfg=cfg(false)
6+
//@[r1x1] compile-flags: --cfg r#false --check-cfg=cfg(r#false)
7+
#![deny(unexpected_cfgs)]
178
fn main() {
18-
b::bar()
9+
#[cfg(not(r#false))]
10+
compile_error!("");
1911
}

tests/ui/check-cfg/invalid-arguments.boolean.stderr

-6
This file was deleted.

tests/ui/check-cfg/invalid-arguments.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
//@ check-fail
44
//@ no-auto-check-cfg
5-
//@ revisions: anything_else boolean
5+
//@ revisions: anything_else
66
//@ revisions: string_for_name_1 string_for_name_2 multiple_any multiple_values
77
//@ revisions: multiple_values_any not_empty_any not_empty_values_any
88
//@ revisions: values_any_missing_values values_any_before_ident ident_in_values_1
@@ -11,7 +11,6 @@
1111
//@ revisions: none_not_empty cfg_none unsafe_attr
1212
//
1313
//@ [anything_else]compile-flags: --check-cfg=anything_else(...)
14-
//@ [boolean]compile-flags: --check-cfg=cfg(true)
1514
//@ [string_for_name_1]compile-flags: --check-cfg=cfg("NOT_IDENT")
1615
//@ [string_for_name_2]compile-flags: --check-cfg=cfg(foo,"NOT_IDENT",bar)
1716
//@ [multiple_any]compile-flags: --check-cfg=cfg(any(),any())

0 commit comments

Comments
 (0)