Skip to content

Allow boolean literals in check-cfg #138767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions compiler/rustc_ast/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,14 @@ impl MetaItemInner {
}
}

/// Returns the bool if `self` is a boolean `MetaItemInner::Literal`.
pub fn boolean_literal(&self) -> Option<bool> {
match self {
MetaItemInner::Lit(MetaItemLit { kind: LitKind::Bool(b), .. }) => Some(*b),
_ => None,
}
}

/// Returns the `MetaItem` if `self` is a `MetaItemInner::MetaItem` or if it's
/// `MetaItemInner::Lit(MetaItemLit { kind: LitKind::Bool(_), .. })`.
pub fn meta_item_or_bool(&self) -> Option<&MetaItemInner> {
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_interface/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,14 @@ pub(crate) fn parse_check_cfg(dcx: DiagCtxtHandle<'_>, specs: Vec<String>) -> Ch
error!("`cfg()` names cannot be after values");
}
names.push(ident);
} else if let Some(boolean) = arg.boolean_literal() {
if values_specified {
error!("`cfg()` names cannot be after values");
}
names.push(rustc_span::Ident::new(
if boolean { rustc_span::kw::True } else { rustc_span::kw::False },
arg.span(),
));
} else if arg.has_name(sym::any)
&& let Some(args) = arg.meta_item_list()
{
Expand Down
24 changes: 8 additions & 16 deletions tests/ui/cfg/raw-true-false.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
//@ check-pass
//@ compile-flags: --cfg false --check-cfg=cfg(r#false)

#![deny(warnings)]

#[expect(unexpected_cfgs)]
mod a {
#[cfg(r#true)]
pub fn foo() {}
}

mod b {
#[cfg(r#false)]
pub fn bar() {}
}

//@ revisions: r0x0 r0x1 r1x0 r1x1
//@[r0x0] compile-flags: --cfg false --check-cfg=cfg(false)
//@[r0x1] compile-flags: --cfg false --check-cfg=cfg(r#false)
//@[r1x0] compile-flags: --cfg r#false --check-cfg=cfg(false)
//@[r1x1] compile-flags: --cfg r#false --check-cfg=cfg(r#false)
#![deny(unexpected_cfgs)]
fn main() {
b::bar()
#[cfg(not(r#false))]
compile_error!("");
}
6 changes: 0 additions & 6 deletions tests/ui/check-cfg/invalid-arguments.boolean.stderr

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: invalid `--check-cfg` argument: `cfg(values(),true)`
|
= note: `values()` cannot be specified before the names
= note: visit <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details

4 changes: 2 additions & 2 deletions tests/ui/check-cfg/invalid-arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
//@ check-fail
//@ no-auto-check-cfg
//@ revisions: anything_else boolean
//@ revisions: anything_else boolean_after_values
//@ revisions: string_for_name_1 string_for_name_2 multiple_any multiple_values
//@ revisions: multiple_values_any not_empty_any not_empty_values_any
//@ revisions: values_any_missing_values values_any_before_ident ident_in_values_1
Expand All @@ -11,7 +11,7 @@
//@ revisions: none_not_empty cfg_none unsafe_attr
//
//@ [anything_else]compile-flags: --check-cfg=anything_else(...)
//@ [boolean]compile-flags: --check-cfg=cfg(true)
//@ [boolean_after_values]compile-flags: --check-cfg=cfg(values(),true)
//@ [string_for_name_1]compile-flags: --check-cfg=cfg("NOT_IDENT")
//@ [string_for_name_2]compile-flags: --check-cfg=cfg(foo,"NOT_IDENT",bar)
//@ [multiple_any]compile-flags: --check-cfg=cfg(any(),any())
Expand Down
Loading