2020-03-08 21:32:25 +00:00
|
|
|
//! Conditional compilation stripping.
|
2019-11-20 02:35:11 +00:00
|
|
|
|
2025-03-14 17:34:43 +00:00
|
|
|
use std::iter;
|
|
|
|
|
2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::ptr::P;
|
2022-04-26 12:40:14 +00:00
|
|
|
use rustc_ast::token::{Delimiter, Token, TokenKind};
|
2024-07-24 10:29:28 +00:00
|
|
|
use rustc_ast::tokenstream::{
|
|
|
|
AttrTokenStream, AttrTokenTree, LazyAttrTokenStream, Spacing, TokenTree,
|
2022-09-09 07:15:53 +00:00
|
|
|
};
|
2024-09-18 15:44:32 +00:00
|
|
|
use rustc_ast::{
|
2025-03-14 17:34:43 +00:00
|
|
|
self as ast, AttrKind, AttrStyle, Attribute, HasAttrs, HasTokens, MetaItem, MetaItemInner,
|
|
|
|
NodeId, NormalAttr,
|
2024-09-18 15:44:32 +00:00
|
|
|
};
|
2024-12-13 13:47:11 +00:00
|
|
|
use rustc_attr_parsing as attr;
|
2023-03-08 04:53:56 +00:00
|
|
|
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
|
2024-07-10 00:06:49 +00:00
|
|
|
use rustc_feature::{
|
2024-10-24 15:53:08 +00:00
|
|
|
ACCEPTED_LANG_FEATURES, AttributeSafety, EnabledLangFeature, EnabledLibFeature, Features,
|
|
|
|
REMOVED_LANG_FEATURES, UNSTABLE_LANG_FEATURES,
|
2024-07-10 00:06:49 +00:00
|
|
|
};
|
2024-04-14 20:11:14 +00:00
|
|
|
use rustc_lint_defs::BuiltinLintDiag;
|
2021-07-29 17:00:41 +00:00
|
|
|
use rustc_parse::validate_attr;
|
2020-07-30 01:27:50 +00:00
|
|
|
use rustc_session::Session;
|
2020-01-02 11:33:56 +00:00
|
|
|
use rustc_session::parse::feature_err;
|
2025-01-08 10:26:24 +00:00
|
|
|
use rustc_span::{STDLIB_STABLE_CRATES, Span, Symbol, sym};
|
2023-10-05 00:15:14 +00:00
|
|
|
use thin_vec::ThinVec;
|
2024-04-29 06:24:06 +00:00
|
|
|
use tracing::instrument;
|
2019-02-06 17:33:01 +00:00
|
|
|
|
2022-11-15 13:24:33 +00:00
|
|
|
use crate::errors::{
|
2024-08-27 22:00:08 +00:00
|
|
|
CrateNameInCfgAttr, CrateTypeInCfgAttr, FeatureNotAllowed, FeatureRemoved,
|
|
|
|
FeatureRemovedReason, InvalidCfg, MalformedFeatureAttribute, MalformedFeatureAttributeHelp,
|
|
|
|
RemoveExprNotSupported,
|
2024-07-28 22:13:50 +00:00
|
|
|
};
|
|
|
|
|
2016-06-01 02:13:45 +00:00
|
|
|
/// A folder that strips out items that do not belong in the current configuration.
|
2016-05-15 09:22:58 +00:00
|
|
|
pub struct StripUnconfigured<'a> {
|
2020-07-30 01:27:50 +00:00
|
|
|
pub sess: &'a Session,
|
2016-06-11 01:37:24 +00:00
|
|
|
pub features: Option<&'a Features>,
|
2020-11-28 23:33:17 +00:00
|
|
|
/// If `true`, perform cfg-stripping on attached tokens.
|
|
|
|
/// This is only used for the input to derive macros,
|
|
|
|
/// which needs eager expansion of `cfg` and `cfg_attr`
|
|
|
|
pub config_tokens: bool,
|
2022-02-27 21:26:24 +00:00
|
|
|
pub lint_node_id: NodeId,
|
2013-02-19 07:40:42 +00:00
|
|
|
}
|
2011-06-30 05:32:08 +00:00
|
|
|
|
2023-10-16 20:11:57 +00:00
|
|
|
pub fn features(sess: &Session, krate_attrs: &[Attribute], crate_name: Symbol) -> Features {
|
2024-10-04 12:59:04 +00:00
|
|
|
fn feature_list(attr: &Attribute) -> ThinVec<ast::MetaItemInner> {
|
2023-10-05 00:15:14 +00:00
|
|
|
if attr.has_name(sym::feature)
|
|
|
|
&& let Some(list) = attr.meta_item_list()
|
|
|
|
{
|
|
|
|
list
|
|
|
|
} else {
|
|
|
|
ThinVec::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-02 11:33:56 +00:00
|
|
|
let mut features = Features::default();
|
|
|
|
|
2024-10-08 12:06:56 +00:00
|
|
|
// Process all features enabled in the code.
|
2020-01-02 11:33:56 +00:00
|
|
|
for attr in krate_attrs {
|
2023-10-05 00:15:14 +00:00
|
|
|
for mi in feature_list(attr) {
|
2020-01-02 11:33:56 +00:00
|
|
|
let name = match mi.ident() {
|
|
|
|
Some(ident) if mi.is_word() => ident.name,
|
|
|
|
Some(ident) => {
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_err(MalformedFeatureAttribute {
|
2022-11-15 13:24:33 +00:00
|
|
|
span: mi.span(),
|
|
|
|
help: MalformedFeatureAttributeHelp::Suggestion {
|
|
|
|
span: mi.span(),
|
|
|
|
suggestion: ident.name,
|
|
|
|
},
|
|
|
|
});
|
2020-01-02 11:33:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
None => {
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_err(MalformedFeatureAttribute {
|
2022-11-15 13:24:33 +00:00
|
|
|
span: mi.span(),
|
|
|
|
help: MalformedFeatureAttributeHelp::Label { span: mi.span() },
|
|
|
|
});
|
2020-01-02 11:33:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-10-08 12:06:56 +00:00
|
|
|
// If the enabled feature has been removed, issue an error.
|
2024-10-23 07:20:02 +00:00
|
|
|
if let Some(f) = REMOVED_LANG_FEATURES.iter().find(|f| name == f.feature.name) {
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_err(FeatureRemoved {
|
2023-10-05 07:59:01 +00:00
|
|
|
span: mi.span(),
|
|
|
|
reason: f.reason.map(|reason| FeatureRemovedReason { reason }),
|
|
|
|
});
|
|
|
|
continue;
|
2020-01-02 11:33:56 +00:00
|
|
|
}
|
|
|
|
|
2024-10-08 12:06:56 +00:00
|
|
|
// If the enabled feature is stable, record it.
|
2024-10-23 07:20:02 +00:00
|
|
|
if let Some(f) = ACCEPTED_LANG_FEATURES.iter().find(|f| name == f.name) {
|
2024-10-24 15:53:08 +00:00
|
|
|
features.set_enabled_lang_feature(EnabledLangFeature {
|
|
|
|
gate_name: name,
|
|
|
|
attr_sp: mi.span(),
|
|
|
|
stable_since: Some(Symbol::intern(f.since)),
|
|
|
|
});
|
2020-01-02 11:33:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-10-08 12:06:56 +00:00
|
|
|
// If `-Z allow-features` is used and the enabled feature is
|
2023-10-04 23:17:42 +00:00
|
|
|
// unstable and not also listed as one of the allowed features,
|
|
|
|
// issue an error.
|
2022-07-06 12:44:47 +00:00
|
|
|
if let Some(allowed) = sess.opts.unstable_opts.allow_features.as_ref() {
|
2021-12-15 03:39:23 +00:00
|
|
|
if allowed.iter().all(|f| name.as_str() != f) {
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_err(FeatureNotAllowed { span: mi.span(), name });
|
2020-01-02 11:33:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-08 12:06:56 +00:00
|
|
|
// If the enabled feature is unstable, record it.
|
2024-10-23 07:20:02 +00:00
|
|
|
if UNSTABLE_LANG_FEATURES.iter().find(|f| name == f.name).is_some() {
|
2025-01-08 10:26:24 +00:00
|
|
|
// When the ICE comes a standard library crate, there's a chance that the person
|
|
|
|
// hitting the ICE may be using -Zbuild-std or similar with an untested target.
|
|
|
|
// The bug is probably in the standard library and not the compiler in that case,
|
|
|
|
// but that doesn't really matter - we want a bug report.
|
|
|
|
if features.internal(name) && !STDLIB_STABLE_CRATES.contains(&crate_name) {
|
2023-10-16 20:11:57 +00:00
|
|
|
sess.using_internal_features.store(true, std::sync::atomic::Ordering::Relaxed);
|
|
|
|
}
|
2024-10-24 15:53:08 +00:00
|
|
|
|
|
|
|
features.set_enabled_lang_feature(EnabledLangFeature {
|
|
|
|
gate_name: name,
|
|
|
|
attr_sp: mi.span(),
|
|
|
|
stable_since: None,
|
|
|
|
});
|
2020-01-02 11:33:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-10-08 12:06:56 +00:00
|
|
|
// Otherwise, the feature is unknown. Enable it as a lib feature.
|
|
|
|
// It will be checked later whether the feature really exists.
|
2024-10-24 15:53:08 +00:00
|
|
|
features
|
|
|
|
.set_enabled_lib_feature(EnabledLibFeature { gate_name: name, attr_sp: mi.span() });
|
2024-07-25 19:13:39 +00:00
|
|
|
|
|
|
|
// Similar to above, detect internal lib features to suppress
|
|
|
|
// the ICE message that asks for a report.
|
2025-01-08 10:26:24 +00:00
|
|
|
if features.internal(name) && !STDLIB_STABLE_CRATES.contains(&crate_name) {
|
2024-07-25 19:13:39 +00:00
|
|
|
sess.using_internal_features.store(true, std::sync::atomic::Ordering::Relaxed);
|
|
|
|
}
|
2020-01-02 11:33:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
features
|
|
|
|
}
|
|
|
|
|
2023-03-14 12:53:04 +00:00
|
|
|
pub fn pre_configure_attrs(sess: &Session, attrs: &[Attribute]) -> ast::AttrVec {
|
|
|
|
let strip_unconfigured = StripUnconfigured {
|
|
|
|
sess,
|
|
|
|
features: None,
|
|
|
|
config_tokens: false,
|
|
|
|
lint_node_id: ast::CRATE_NODE_ID,
|
|
|
|
};
|
2023-04-10 11:20:38 +00:00
|
|
|
attrs
|
|
|
|
.iter()
|
|
|
|
.flat_map(|attr| strip_unconfigured.process_cfg_attr(attr))
|
|
|
|
.take_while(|attr| !is_cfg(attr) || strip_unconfigured.cfg_true(attr).0)
|
|
|
|
.collect()
|
2016-08-31 23:39:16 +00:00
|
|
|
}
|
|
|
|
|
2025-03-22 18:42:34 +00:00
|
|
|
pub(crate) fn attr_into_trace(mut attr: Attribute, trace_name: Symbol) -> Attribute {
|
|
|
|
match &mut attr.kind {
|
|
|
|
AttrKind::Normal(normal) => {
|
|
|
|
let NormalAttr { item, tokens } = &mut **normal;
|
|
|
|
item.path.segments[0].ident.name = trace_name;
|
|
|
|
// This makes the trace attributes unobservable to token-based proc macros.
|
2025-04-29 01:57:27 +00:00
|
|
|
*tokens = Some(LazyAttrTokenStream::new_direct(AttrTokenStream::default()));
|
2025-03-22 18:42:34 +00:00
|
|
|
}
|
|
|
|
AttrKind::DocComment(..) => unreachable!(),
|
|
|
|
}
|
|
|
|
attr
|
|
|
|
}
|
|
|
|
|
2019-10-16 08:59:30 +00:00
|
|
|
#[macro_export]
|
2016-09-02 01:44:23 +00:00
|
|
|
macro_rules! configure {
|
|
|
|
($this:ident, $node:ident) => {
|
|
|
|
match $this.configure($node) {
|
|
|
|
Some(node) => node,
|
|
|
|
None => return Default::default(),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-05-16 04:42:57 +00:00
|
|
|
impl<'a> StripUnconfigured<'a> {
|
2022-05-01 17:58:24 +00:00
|
|
|
pub fn configure<T: HasAttrs + HasTokens>(&self, mut node: T) -> Option<T> {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
self.process_cfg_attrs(&mut node);
|
2023-02-15 11:43:41 +00:00
|
|
|
self.in_cfg(node.attrs()).then(|| {
|
2020-11-28 23:33:17 +00:00
|
|
|
self.try_configure_tokens(&mut node);
|
2023-02-15 11:43:41 +00:00
|
|
|
node
|
|
|
|
})
|
2016-06-01 02:13:45 +00:00
|
|
|
}
|
|
|
|
|
2022-05-01 17:58:24 +00:00
|
|
|
fn try_configure_tokens<T: HasTokens>(&self, node: &mut T) {
|
2020-11-28 23:33:17 +00:00
|
|
|
if self.config_tokens {
|
|
|
|
if let Some(Some(tokens)) = node.tokens_mut() {
|
2022-09-09 07:15:53 +00:00
|
|
|
let attr_stream = tokens.to_attr_token_stream();
|
2025-04-29 01:57:27 +00:00
|
|
|
*tokens = LazyAttrTokenStream::new_direct(self.configure_tokens(&attr_stream));
|
2020-11-28 23:33:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-09 02:44:05 +00:00
|
|
|
/// Performs cfg-expansion on `stream`, producing a new `AttrTokenStream`.
|
2020-11-28 23:33:17 +00:00
|
|
|
/// This is only used during the invocation of `derive` proc-macros,
|
|
|
|
/// which require that we cfg-expand their entire input.
|
|
|
|
/// Normal cfg-expansion operates on parsed AST nodes via the `configure` method
|
2022-09-09 02:44:05 +00:00
|
|
|
fn configure_tokens(&self, stream: &AttrTokenStream) -> AttrTokenStream {
|
|
|
|
fn can_skip(stream: &AttrTokenStream) -> bool {
|
2022-09-09 01:51:23 +00:00
|
|
|
stream.0.iter().all(|tree| match tree {
|
2024-07-07 06:14:30 +00:00
|
|
|
AttrTokenTree::AttrsTarget(_) => false,
|
2022-09-09 02:44:05 +00:00
|
|
|
AttrTokenTree::Token(..) => true,
|
2023-10-12 04:36:14 +00:00
|
|
|
AttrTokenTree::Delimited(.., inner) => can_skip(inner),
|
2020-11-28 23:33:17 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if can_skip(stream) {
|
|
|
|
return stream.clone();
|
2021-02-23 15:21:20 +00:00
|
|
|
}
|
2020-11-28 23:33:17 +00:00
|
|
|
|
|
|
|
let trees: Vec<_> = stream
|
|
|
|
.0
|
|
|
|
.iter()
|
2024-07-07 06:29:07 +00:00
|
|
|
.filter_map(|tree| match tree.clone() {
|
2024-07-07 06:14:30 +00:00
|
|
|
AttrTokenTree::AttrsTarget(mut target) => {
|
2024-07-10 06:29:48 +00:00
|
|
|
// Expand any `cfg_attr` attributes.
|
2024-07-07 06:14:30 +00:00
|
|
|
target.attrs.flat_map_in_place(|attr| self.process_cfg_attr(&attr));
|
2020-11-28 23:33:17 +00:00
|
|
|
|
2024-07-07 06:14:30 +00:00
|
|
|
if self.in_cfg(&target.attrs) {
|
2025-04-29 01:57:27 +00:00
|
|
|
target.tokens = LazyAttrTokenStream::new_direct(
|
2024-07-07 06:14:30 +00:00
|
|
|
self.configure_tokens(&target.tokens.to_attr_token_stream()),
|
2020-11-28 23:33:17 +00:00
|
|
|
);
|
2024-07-07 06:29:07 +00:00
|
|
|
Some(AttrTokenTree::AttrsTarget(target))
|
2020-11-28 23:33:17 +00:00
|
|
|
} else {
|
2024-07-10 06:29:48 +00:00
|
|
|
// Remove the target if there's a `cfg` attribute and
|
|
|
|
// the condition isn't satisfied.
|
2024-07-07 06:29:07 +00:00
|
|
|
None
|
2020-11-28 23:33:17 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-12 04:36:14 +00:00
|
|
|
AttrTokenTree::Delimited(sp, spacing, delim, mut inner) => {
|
2020-11-28 23:33:17 +00:00
|
|
|
inner = self.configure_tokens(&inner);
|
2024-07-07 06:29:07 +00:00
|
|
|
Some(AttrTokenTree::Delimited(sp, spacing, delim, inner))
|
2020-11-28 23:33:17 +00:00
|
|
|
}
|
Remove `token::{Open,Close}Delim`.
By replacing them with `{Open,Close}{Param,Brace,Bracket,Invisible}`.
PR #137902 made `ast::TokenKind` more like `lexer::TokenKind` by
replacing the compound `BinOp{,Eq}(BinOpToken)` variants with fieldless
variants `Plus`, `Minus`, `Star`, etc. This commit does a similar thing
with delimiters. It also makes `ast::TokenKind` more similar to
`parser::TokenType`.
This requires a few new methods:
- `TokenKind::is_{,open_,close_}delim()` replace various kinds of
pattern matches.
- `Delimiter::as_{open,close}_token_kind` are used to convert
`Delimiter` values to `TokenKind`.
Despite these additions, it's a net reduction in lines of code. This is
because e.g. `token::OpenParen` is so much shorter than
`token::OpenDelim(Delimiter::Parenthesis)` that many multi-line forms
reduce to single line forms. And many places where the number of lines
doesn't change are still easier to read, just because the names are
shorter, e.g.:
```
- } else if self.token != token::CloseDelim(Delimiter::Brace) {
+ } else if self.token != token::CloseBrace {
```
2025-04-16 06:13:50 +00:00
|
|
|
AttrTokenTree::Token(Token { kind, .. }, _) if kind.is_delim() => {
|
2024-05-14 23:29:11 +00:00
|
|
|
panic!("Should be `AttrTokenTree::Delimited`, not delim tokens: {:?}", tree);
|
|
|
|
}
|
2024-07-07 06:29:07 +00:00
|
|
|
AttrTokenTree::Token(token, spacing) => Some(AttrTokenTree::Token(token, spacing)),
|
2020-11-28 23:33:17 +00:00
|
|
|
})
|
|
|
|
.collect();
|
2022-09-09 02:44:05 +00:00
|
|
|
AttrTokenStream::new(trees)
|
2021-02-23 15:21:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 22:06:49 +00:00
|
|
|
/// Parse and expand all `cfg_attr` attributes into a list of attributes
|
|
|
|
/// that are within each `cfg_attr` that has a true configuration predicate.
|
|
|
|
///
|
2020-03-06 11:13:55 +00:00
|
|
|
/// Gives compiler warnings if any `cfg_attr` does not contain any
|
2018-09-10 22:06:49 +00:00
|
|
|
/// attributes and is in the original source code. Gives compiler errors if
|
|
|
|
/// the syntax of any `cfg_attr` is incorrect.
|
2022-05-01 17:58:24 +00:00
|
|
|
fn process_cfg_attrs<T: HasAttrs>(&self, node: &mut T) {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
node.visit_attrs(|attrs| {
|
2023-02-18 12:23:57 +00:00
|
|
|
attrs.flat_map_in_place(|attr| self.process_cfg_attr(&attr));
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
});
|
2016-06-01 02:13:45 +00:00
|
|
|
}
|
|
|
|
|
2023-02-18 12:23:57 +00:00
|
|
|
fn process_cfg_attr(&self, attr: &Attribute) -> Vec<Attribute> {
|
|
|
|
if attr.has_name(sym::cfg_attr) {
|
|
|
|
self.expand_cfg_attr(attr, true)
|
|
|
|
} else {
|
|
|
|
vec![attr.clone()]
|
|
|
|
}
|
2021-12-29 10:47:19 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 22:06:49 +00:00
|
|
|
/// Parse and expand a single `cfg_attr` attribute into a list of attributes
|
|
|
|
/// when the configuration predicate is true, or otherwise expand into an
|
|
|
|
/// empty list of attributes.
|
|
|
|
///
|
2018-10-22 16:21:55 +00:00
|
|
|
/// Gives a compiler warning when the `cfg_attr` contains no attributes and
|
2018-09-10 22:06:49 +00:00
|
|
|
/// is in the original source file. Gives a compiler error if the syntax of
|
2019-02-08 13:53:55 +00:00
|
|
|
/// the attribute is incorrect.
|
2024-07-10 05:11:57 +00:00
|
|
|
pub(crate) fn expand_cfg_attr(&self, cfg_attr: &Attribute, recursive: bool) -> Vec<Attribute> {
|
2025-05-03 13:19:08 +00:00
|
|
|
validate_attr::check_attribute_safety(
|
|
|
|
&self.sess.psess,
|
2025-05-03 13:57:19 +00:00
|
|
|
Some(AttributeSafety::Normal),
|
2025-05-03 13:19:08 +00:00
|
|
|
&cfg_attr,
|
|
|
|
ast::CRATE_NODE_ID,
|
|
|
|
);
|
2024-07-03 04:52:16 +00:00
|
|
|
|
2025-03-14 17:34:43 +00:00
|
|
|
// A trace attribute left in AST in place of the original `cfg_attr` attribute.
|
|
|
|
// It can later be used by lints or other diagnostics.
|
2025-03-22 18:42:34 +00:00
|
|
|
let trace_attr = attr_into_trace(cfg_attr.clone(), sym::cfg_attr_trace);
|
2025-03-14 17:34:43 +00:00
|
|
|
|
2022-02-18 23:48:49 +00:00
|
|
|
let Some((cfg_predicate, expanded_attrs)) =
|
2024-07-10 05:11:57 +00:00
|
|
|
rustc_parse::parse_cfg_attr(cfg_attr, &self.sess.psess)
|
2023-02-18 12:23:57 +00:00
|
|
|
else {
|
2025-03-14 17:34:43 +00:00
|
|
|
return vec![trace_attr];
|
2021-07-29 17:00:41 +00:00
|
|
|
};
|
2016-08-20 01:58:14 +00:00
|
|
|
|
2019-06-22 10:11:01 +00:00
|
|
|
// Lint on zero attributes in source.
|
|
|
|
if expanded_attrs.is_empty() {
|
2024-05-20 17:47:54 +00:00
|
|
|
self.sess.psess.buffer_lint(
|
2021-12-29 10:47:19 +00:00
|
|
|
rustc_lint_defs::builtin::UNUSED_ATTRIBUTES,
|
2024-07-10 05:11:57 +00:00
|
|
|
cfg_attr.span,
|
2021-12-29 10:47:19 +00:00
|
|
|
ast::CRATE_NODE_ID,
|
2024-04-14 20:11:14 +00:00
|
|
|
BuiltinLintDiag::CfgAttrNoAttributes,
|
2021-12-29 10:47:19 +00:00
|
|
|
);
|
2018-10-04 11:55:47 +00:00
|
|
|
}
|
|
|
|
|
2024-01-10 05:37:30 +00:00
|
|
|
if !attr::cfg_matches(&cfg_predicate, &self.sess, self.lint_node_id, self.features) {
|
2025-03-14 17:34:43 +00:00
|
|
|
return vec![trace_attr];
|
2019-12-05 05:45:50 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 10:47:19 +00:00
|
|
|
if recursive {
|
|
|
|
// We call `process_cfg_attr` recursively in case there's a
|
|
|
|
// `cfg_attr` inside of another `cfg_attr`. E.g.
|
|
|
|
// `#[cfg_attr(false, cfg_attr(true, some_attr))]`.
|
2025-03-14 17:34:43 +00:00
|
|
|
let expanded_attrs = expanded_attrs
|
2021-12-29 10:47:19 +00:00
|
|
|
.into_iter()
|
2025-03-14 17:34:43 +00:00
|
|
|
.flat_map(|item| self.process_cfg_attr(&self.expand_cfg_attr_item(cfg_attr, item)));
|
|
|
|
iter::once(trace_attr).chain(expanded_attrs).collect()
|
2021-12-29 10:47:19 +00:00
|
|
|
} else {
|
2025-03-14 17:34:43 +00:00
|
|
|
let expanded_attrs =
|
|
|
|
expanded_attrs.into_iter().map(|item| self.expand_cfg_attr_item(cfg_attr, item));
|
|
|
|
iter::once(trace_attr).chain(expanded_attrs).collect()
|
2021-12-29 10:47:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_cfg_attr_item(
|
|
|
|
&self,
|
2024-07-10 05:11:57 +00:00
|
|
|
cfg_attr: &Attribute,
|
2021-12-29 10:47:19 +00:00
|
|
|
(item, item_span): (ast::AttrItem, Span),
|
|
|
|
) -> Attribute {
|
2024-07-24 10:29:28 +00:00
|
|
|
// Convert `#[cfg_attr(pred, attr)]` to `#[attr]`.
|
2021-12-29 10:47:19 +00:00
|
|
|
|
2024-07-24 10:29:28 +00:00
|
|
|
// Use the `#` from `#[cfg_attr(pred, attr)]` in the result `#[attr]`.
|
2024-07-10 05:11:57 +00:00
|
|
|
let mut orig_trees = cfg_attr.token_trees().into_iter();
|
2024-07-24 10:29:28 +00:00
|
|
|
let Some(TokenTree::Token(pound_token @ Token { kind: TokenKind::Pound, .. }, _)) =
|
|
|
|
orig_trees.next()
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 00:31:04 +00:00
|
|
|
else {
|
2024-07-10 05:11:57 +00:00
|
|
|
panic!("Bad tokens for attribute {cfg_attr:?}");
|
2021-12-29 10:47:19 +00:00
|
|
|
};
|
|
|
|
|
2024-07-24 10:29:28 +00:00
|
|
|
// For inner attributes, we do the same thing for the `!` in `#![attr]`.
|
|
|
|
let mut trees = if cfg_attr.style == AttrStyle::Inner {
|
2024-12-20 03:04:25 +00:00
|
|
|
let Some(TokenTree::Token(bang_token @ Token { kind: TokenKind::Bang, .. }, _)) =
|
2024-07-24 10:29:28 +00:00
|
|
|
orig_trees.next()
|
2023-08-08 01:43:44 +00:00
|
|
|
else {
|
2024-07-10 05:11:57 +00:00
|
|
|
panic!("Bad tokens for attribute {cfg_attr:?}");
|
2023-08-08 01:43:44 +00:00
|
|
|
};
|
|
|
|
vec![
|
|
|
|
AttrTokenTree::Token(pound_token, Spacing::Joint),
|
|
|
|
AttrTokenTree::Token(bang_token, Spacing::JointHidden),
|
|
|
|
]
|
|
|
|
} else {
|
2024-07-24 10:29:28 +00:00
|
|
|
vec![AttrTokenTree::Token(pound_token, Spacing::JointHidden)]
|
|
|
|
};
|
|
|
|
|
|
|
|
// And the same thing for the `[`/`]` delimiters in `#[attr]`.
|
|
|
|
let Some(TokenTree::Delimited(delim_span, delim_spacing, Delimiter::Bracket, _)) =
|
|
|
|
orig_trees.next()
|
|
|
|
else {
|
|
|
|
panic!("Bad tokens for attribute {cfg_attr:?}");
|
2023-08-08 01:43:44 +00:00
|
|
|
};
|
2024-07-24 10:29:28 +00:00
|
|
|
trees.push(AttrTokenTree::Delimited(
|
|
|
|
delim_span,
|
|
|
|
delim_spacing,
|
|
|
|
Delimiter::Bracket,
|
|
|
|
item.tokens
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or_else(|| panic!("Missing tokens for {item:?}"))
|
|
|
|
.to_attr_token_stream(),
|
|
|
|
));
|
|
|
|
|
2025-04-29 01:57:27 +00:00
|
|
|
let tokens = Some(LazyAttrTokenStream::new_direct(AttrTokenStream::new(trees)));
|
2024-12-07 14:27:17 +00:00
|
|
|
let attr = ast::attr::mk_attr_from_item(
|
2024-03-04 05:31:49 +00:00
|
|
|
&self.sess.psess.attr_id_generator,
|
2022-09-02 08:29:40 +00:00
|
|
|
item,
|
|
|
|
tokens,
|
2024-07-10 05:11:57 +00:00
|
|
|
cfg_attr.style,
|
2022-09-02 08:29:40 +00:00
|
|
|
item_span,
|
|
|
|
);
|
2021-12-29 10:47:19 +00:00
|
|
|
if attr.has_name(sym::crate_type) {
|
2024-08-27 22:00:08 +00:00
|
|
|
self.sess.dcx().emit_err(CrateTypeInCfgAttr { span: attr.span });
|
2021-12-29 10:47:19 +00:00
|
|
|
}
|
|
|
|
if attr.has_name(sym::crate_name) {
|
2024-08-27 22:00:08 +00:00
|
|
|
self.sess.dcx().emit_err(CrateNameInCfgAttr { span: attr.span });
|
2021-12-29 10:47:19 +00:00
|
|
|
}
|
|
|
|
attr
|
2019-12-05 05:45:50 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Determines if a node with the given attributes should be included in this configuration.
|
2021-03-06 20:37:59 +00:00
|
|
|
fn in_cfg(&self, attrs: &[Attribute]) -> bool {
|
2023-03-10 21:39:14 +00:00
|
|
|
attrs.iter().all(|attr| !is_cfg(attr) || self.cfg_true(attr).0)
|
2021-12-29 10:47:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-10 21:39:14 +00:00
|
|
|
pub(crate) fn cfg_true(&self, attr: &Attribute) -> (bool, Option<MetaItem>) {
|
2024-03-04 05:31:49 +00:00
|
|
|
let meta_item = match validate_attr::parse_meta(&self.sess.psess, attr) {
|
2021-12-29 10:47:19 +00:00
|
|
|
Ok(meta_item) => meta_item,
|
Make `DiagnosticBuilder::emit` consuming.
This works for most of its call sites. This is nice, because `emit` very
much makes sense as a consuming operation -- indeed,
`DiagnosticBuilderState` exists to ensure no diagnostic is emitted
twice, but it uses runtime checks.
For the small number of call sites where a consuming emit doesn't work,
the commit adds `DiagnosticBuilder::emit_without_consuming`. (This will
be removed in subsequent commits.)
Likewise, `emit_unless` becomes consuming. And `delay_as_bug` becomes
consuming, while `delay_as_bug_without_consuming` is added (which will
also be removed in subsequent commits.)
All this requires significant changes to `DiagnosticBuilder`'s chaining
methods. Currently `DiagnosticBuilder` method chaining uses a
non-consuming `&mut self -> &mut Self` style, which allows chaining to
be used when the chain ends in `emit()`, like so:
```
struct_err(msg).span(span).emit();
```
But it doesn't work when producing a `DiagnosticBuilder` value,
requiring this:
```
let mut err = self.struct_err(msg);
err.span(span);
err
```
This style of chaining won't work with consuming `emit` though. For
that, we need to use to a `self -> Self` style. That also would allow
`DiagnosticBuilder` production to be chained, e.g.:
```
self.struct_err(msg).span(span)
```
However, removing the `&mut self -> &mut Self` style would require that
individual modifications of a `DiagnosticBuilder` go from this:
```
err.span(span);
```
to this:
```
err = err.span(span);
```
There are *many* such places. I have a high tolerance for tedious
refactorings, but even I gave up after a long time trying to convert
them all.
Instead, this commit has it both ways: the existing `&mut self -> Self`
chaining methods are kept, and new `self -> Self` chaining methods are
added, all of which have a `_mv` suffix (short for "move"). Changes to
the existing `forward!` macro lets this happen with very little
additional boilerplate code. I chose to add the suffix to the new
chaining methods rather than the existing ones, because the number of
changes required is much smaller that way.
This doubled chainging is a bit clumsy, but I think it is worthwhile
because it allows a *lot* of good things to subsequently happen. In this
commit, there are many `mut` qualifiers removed in places where
diagnostics are emitted without being modified. In subsequent commits:
- chaining can be used more, making the code more concise;
- more use of chaining also permits the removal of redundant diagnostic
APIs like `struct_err_with_code`, which can be replaced easily with
`struct_err` + `code_mv`;
- `emit_without_diagnostic` can be removed, which simplifies a lot of
machinery, removing the need for `DiagnosticBuilderState`.
2024-01-03 01:17:35 +00:00
|
|
|
Err(err) => {
|
2021-12-29 10:47:19 +00:00
|
|
|
err.emit();
|
2023-03-10 21:39:14 +00:00
|
|
|
return (true, None);
|
2018-09-01 21:13:22 +00:00
|
|
|
}
|
2021-12-29 10:47:19 +00:00
|
|
|
};
|
2024-07-03 04:52:16 +00:00
|
|
|
|
2024-08-07 06:36:28 +00:00
|
|
|
validate_attr::deny_builtin_meta_unsafety(&self.sess.psess, &meta_item);
|
2024-07-03 04:52:16 +00:00
|
|
|
|
2023-03-10 21:39:14 +00:00
|
|
|
(
|
2025-01-19 19:15:00 +00:00
|
|
|
parse_cfg(&meta_item, self.sess).is_none_or(|meta_item| {
|
2024-01-10 05:37:30 +00:00
|
|
|
attr::cfg_matches(meta_item, &self.sess, self.lint_node_id, self.features)
|
2023-03-10 21:39:14 +00:00
|
|
|
}),
|
|
|
|
Some(meta_item),
|
|
|
|
)
|
2016-05-26 23:56:25 +00:00
|
|
|
}
|
|
|
|
|
2018-03-16 06:20:56 +00:00
|
|
|
/// If attributes are not allowed on expressions, emit an error for `attr`
|
2022-10-23 09:22:19 +00:00
|
|
|
#[instrument(level = "trace", skip(self))]
|
2022-05-20 23:51:09 +00:00
|
|
|
pub(crate) fn maybe_emit_expr_attr_err(&self, attr: &Attribute) {
|
2024-10-09 07:01:57 +00:00
|
|
|
if self.features.is_some_and(|features| !features.stmt_expr_attributes())
|
2023-10-31 01:16:23 +00:00
|
|
|
&& !attr.span.allows_unstable(sym::stmt_expr_attributes)
|
|
|
|
{
|
2018-03-16 06:20:56 +00:00
|
|
|
let mut err = feature_err(
|
2024-01-10 05:37:30 +00:00
|
|
|
&self.sess,
|
2019-05-08 03:21:18 +00:00
|
|
|
sym::stmt_expr_attributes,
|
2018-03-16 06:20:56 +00:00
|
|
|
attr.span,
|
2024-04-09 20:43:46 +00:00
|
|
|
crate::fluent_generated::expand_attributes_on_expressions_experimental,
|
2019-11-30 01:40:28 +00:00
|
|
|
);
|
2018-03-16 06:20:56 +00:00
|
|
|
|
2019-10-23 19:33:12 +00:00
|
|
|
if attr.is_doc_comment() {
|
2024-04-09 20:45:53 +00:00
|
|
|
err.help(if attr.style == AttrStyle::Outer {
|
2024-04-09 20:43:46 +00:00
|
|
|
crate::fluent_generated::expand_help_outer_doc
|
2024-04-09 20:45:53 +00:00
|
|
|
} else {
|
2024-04-09 20:43:46 +00:00
|
|
|
crate::fluent_generated::expand_help_inner_doc
|
2024-04-09 20:45:53 +00:00
|
|
|
});
|
2016-06-11 01:37:24 +00:00
|
|
|
}
|
2018-03-16 06:20:56 +00:00
|
|
|
|
|
|
|
err.emit();
|
2016-05-15 09:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 09:22:19 +00:00
|
|
|
#[instrument(level = "trace", skip(self))]
|
|
|
|
pub fn configure_expr(&self, expr: &mut P<ast::Expr>, method_receiver: bool) {
|
|
|
|
if !method_receiver {
|
|
|
|
for attr in expr.attrs.iter() {
|
|
|
|
self.maybe_emit_expr_attr_err(attr);
|
|
|
|
}
|
2021-03-06 20:37:59 +00:00
|
|
|
}
|
2016-06-11 01:37:24 +00:00
|
|
|
|
2015-11-03 16:39:51 +00:00
|
|
|
// If an expr is valid to cfg away it will have been removed by the
|
|
|
|
// outer stmt or expression folder before descending in here.
|
|
|
|
// Anything else is always required, and thus has to error out
|
|
|
|
// in case of a cfg attr.
|
|
|
|
//
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
// N.B., this is intentionally not part of the visit_expr() function
|
|
|
|
// in order for filter_map_expr() to be able to avoid this check
|
2023-04-09 21:07:18 +00:00
|
|
|
if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(a)) {
|
2023-12-18 11:21:37 +00:00
|
|
|
self.sess.dcx().emit_err(RemoveExprNotSupported { span: attr.span });
|
2016-06-11 01:37:24 +00:00
|
|
|
}
|
|
|
|
|
2020-11-28 23:33:17 +00:00
|
|
|
self.process_cfg_attrs(expr);
|
|
|
|
self.try_configure_tokens(&mut *expr);
|
2014-04-23 04:54:48 +00:00
|
|
|
}
|
2013-08-29 19:10:02 +00:00
|
|
|
}
|
2011-06-30 05:32:08 +00:00
|
|
|
|
2024-10-04 12:59:04 +00:00
|
|
|
pub fn parse_cfg<'a>(meta_item: &'a MetaItem, sess: &Session) -> Option<&'a MetaItemInner> {
|
2021-04-22 17:28:43 +00:00
|
|
|
let span = meta_item.span;
|
|
|
|
match meta_item.meta_item_list() {
|
2022-11-15 13:24:33 +00:00
|
|
|
None => {
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_err(InvalidCfg::NotFollowedByParens { span });
|
2022-11-15 13:24:33 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
Some([]) => {
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_err(InvalidCfg::NoPredicate { span });
|
2022-11-15 13:24:33 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
Some([_, .., l]) => {
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_err(InvalidCfg::MultiplePredicates { span: l.span() });
|
2022-11-15 13:24:33 +00:00
|
|
|
None
|
|
|
|
}
|
2024-09-19 08:13:14 +00:00
|
|
|
Some([single]) => match single.meta_item_or_bool() {
|
|
|
|
Some(meta_item) => Some(meta_item),
|
|
|
|
None => {
|
2023-12-18 11:21:37 +00:00
|
|
|
sess.dcx().emit_err(InvalidCfg::PredicateLiteral { span: single.span() });
|
2022-11-15 13:24:33 +00:00
|
|
|
None
|
|
|
|
}
|
2021-04-22 17:28:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 17:00:41 +00:00
|
|
|
fn is_cfg(attr: &Attribute) -> bool {
|
|
|
|
attr.has_name(sym::cfg)
|
2015-11-03 16:39:51 +00:00
|
|
|
}
|