Allow boolean literals in check-cfg
This commit is contained in:
parent
78948ac259
commit
3df2acd31b
6 changed files with 31 additions and 24 deletions
|
@ -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
|
/// Returns the `MetaItem` if `self` is a `MetaItemInner::MetaItem` or if it's
|
||||||
/// `MetaItemInner::Lit(MetaItemLit { kind: LitKind::Bool(_), .. })`.
|
/// `MetaItemInner::Lit(MetaItemLit { kind: LitKind::Bool(_), .. })`.
|
||||||
pub fn meta_item_or_bool(&self) -> Option<&MetaItemInner> {
|
pub fn meta_item_or_bool(&self) -> Option<&MetaItemInner> {
|
||||||
|
|
|
@ -204,6 +204,14 @@ pub(crate) fn parse_check_cfg(dcx: DiagCtxtHandle<'_>, specs: Vec<String>) -> Ch
|
||||||
error!("`cfg()` names cannot be after values");
|
error!("`cfg()` names cannot be after values");
|
||||||
}
|
}
|
||||||
names.push(ident);
|
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)
|
} else if arg.has_name(sym::any)
|
||||||
&& let Some(args) = arg.meta_item_list()
|
&& let Some(args) = arg.meta_item_list()
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,19 +1,11 @@
|
||||||
//@ check-pass
|
//@ check-pass
|
||||||
//@ compile-flags: --cfg false --check-cfg=cfg(r#false)
|
//@ revisions: r0x0 r0x1 r1x0 r1x1
|
||||||
|
//@[r0x0] compile-flags: --cfg false --check-cfg=cfg(false)
|
||||||
#![deny(warnings)]
|
//@[r0x1] compile-flags: --cfg false --check-cfg=cfg(r#false)
|
||||||
|
//@[r1x0] compile-flags: --cfg r#false --check-cfg=cfg(false)
|
||||||
#[expect(unexpected_cfgs)]
|
//@[r1x1] compile-flags: --cfg r#false --check-cfg=cfg(r#false)
|
||||||
mod a {
|
#![deny(unexpected_cfgs)]
|
||||||
#[cfg(r#true)]
|
|
||||||
pub fn foo() {}
|
|
||||||
}
|
|
||||||
|
|
||||||
mod b {
|
|
||||||
#[cfg(r#false)]
|
|
||||||
pub fn bar() {}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
b::bar()
|
#[cfg(not(r#false))]
|
||||||
|
compile_error!("");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
error: invalid `--check-cfg` argument: `cfg(true)`
|
|
||||||
|
|
|
||||||
= note: `true` is a boolean literal
|
|
||||||
= note: `cfg()` arguments must be simple identifiers, `any()` or `values(...)`
|
|
||||||
= note: visit <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more details
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
//
|
//
|
||||||
//@ check-fail
|
//@ check-fail
|
||||||
//@ no-auto-check-cfg
|
//@ 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: string_for_name_1 string_for_name_2 multiple_any multiple_values
|
||||||
//@ revisions: multiple_values_any not_empty_any not_empty_values_any
|
//@ revisions: multiple_values_any not_empty_any not_empty_values_any
|
||||||
//@ revisions: values_any_missing_values values_any_before_ident ident_in_values_1
|
//@ revisions: values_any_missing_values values_any_before_ident ident_in_values_1
|
||||||
|
@ -11,7 +11,7 @@
|
||||||
//@ revisions: none_not_empty cfg_none unsafe_attr
|
//@ revisions: none_not_empty cfg_none unsafe_attr
|
||||||
//
|
//
|
||||||
//@ [anything_else]compile-flags: --check-cfg=anything_else(...)
|
//@ [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_1]compile-flags: --check-cfg=cfg("NOT_IDENT")
|
||||||
//@ [string_for_name_2]compile-flags: --check-cfg=cfg(foo,"NOT_IDENT",bar)
|
//@ [string_for_name_2]compile-flags: --check-cfg=cfg(foo,"NOT_IDENT",bar)
|
||||||
//@ [multiple_any]compile-flags: --check-cfg=cfg(any(),any())
|
//@ [multiple_any]compile-flags: --check-cfg=cfg(any(),any())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue