2020-03-08 22:32:25 +01:00
|
|
|
//! Conditional compilation stripping.
|
2019-11-19 21:35:11 -05:00
|
|
|
|
2020-02-29 20:37:32 +03:00
|
|
|
use rustc_ast::ptr::P;
|
2022-04-26 15:40:14 +03:00
|
|
|
use rustc_ast::token::{Delimiter, Token, TokenKind};
|
2020-11-28 18:33:17 -05:00
|
|
|
use rustc_ast::tokenstream::{AttrAnnotatedTokenStream, AttrAnnotatedTokenTree};
|
|
|
|
use rustc_ast::tokenstream::{DelimSpan, Spacing};
|
|
|
|
use rustc_ast::tokenstream::{LazyTokenStream, TokenTree};
|
2022-02-27 22:26:24 +01:00
|
|
|
use rustc_ast::NodeId;
|
2022-05-01 20:58:24 +03:00
|
|
|
use rustc_ast::{self as ast, AttrStyle, Attribute, HasAttrs, HasTokens, MetaItem};
|
2020-01-11 13:15:20 +01:00
|
|
|
use rustc_attr as attr;
|
2020-01-02 12:33:56 +01:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-04-18 13:01:54 +09:00
|
|
|
use rustc_data_structures::map_in_place::MapInPlace;
|
2020-01-02 12:33:56 +01:00
|
|
|
use rustc_errors::{error_code, struct_span_err, Applicability, Handler};
|
|
|
|
use rustc_feature::{Feature, Features, State as FeatureState};
|
|
|
|
use rustc_feature::{
|
|
|
|
ACCEPTED_FEATURES, ACTIVE_FEATURES, REMOVED_FEATURES, STABLE_REMOVED_FEATURES,
|
|
|
|
};
|
2021-07-29 12:00:41 -05:00
|
|
|
use rustc_parse::validate_attr;
|
2020-07-30 11:27:50 +10:00
|
|
|
use rustc_session::parse::feature_err;
|
|
|
|
use rustc_session::Session;
|
2020-01-02 12:33:56 +01:00
|
|
|
use rustc_span::edition::{Edition, ALL_EDITIONS};
|
|
|
|
use rustc_span::symbol::{sym, Symbol};
|
|
|
|
use rustc_span::{Span, DUMMY_SP};
|
2019-02-07 02:33:01 +09: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 11:27:50 +10:00
|
|
|
pub sess: &'a Session,
|
2016-06-11 01:37:24 +00:00
|
|
|
pub features: Option<&'a Features>,
|
2020-11-28 18:33:17 -05: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 22:26:24 +01:00
|
|
|
pub lint_node_id: NodeId,
|
2013-02-19 02:40:42 -05:00
|
|
|
}
|
2011-06-29 22:32:08 -07:00
|
|
|
|
2020-01-02 12:33:56 +01:00
|
|
|
fn get_features(
|
2020-07-30 11:27:50 +10:00
|
|
|
sess: &Session,
|
2020-01-02 12:33:56 +01:00
|
|
|
span_handler: &Handler,
|
|
|
|
krate_attrs: &[ast::Attribute],
|
|
|
|
) -> Features {
|
|
|
|
fn feature_removed(span_handler: &Handler, span: Span, reason: Option<&str>) {
|
|
|
|
let mut err = struct_span_err!(span_handler, span, E0557, "feature has been removed");
|
|
|
|
err.span_label(span, "feature has been removed");
|
|
|
|
if let Some(reason) = reason {
|
|
|
|
err.note(reason);
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn active_features_up_to(edition: Edition) -> impl Iterator<Item = &'static Feature> {
|
|
|
|
ACTIVE_FEATURES.iter().filter(move |feature| {
|
|
|
|
if let Some(feature_edition) = feature.edition {
|
|
|
|
feature_edition <= edition
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut features = Features::default();
|
|
|
|
let mut edition_enabled_features = FxHashMap::default();
|
2020-07-30 11:27:50 +10:00
|
|
|
let crate_edition = sess.edition();
|
2020-01-02 12:33:56 +01:00
|
|
|
|
|
|
|
for &edition in ALL_EDITIONS {
|
|
|
|
if edition <= crate_edition {
|
|
|
|
// The `crate_edition` implies its respective umbrella feature-gate
|
|
|
|
// (i.e., `#![feature(rust_20XX_preview)]` isn't needed on edition 20XX).
|
|
|
|
edition_enabled_features.insert(edition.feature_name(), edition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for feature in active_features_up_to(crate_edition) {
|
|
|
|
feature.set(&mut features, DUMMY_SP);
|
|
|
|
edition_enabled_features.insert(feature.name, crate_edition);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process the edition umbrella feature-gates first, to ensure
|
|
|
|
// `edition_enabled_features` is completed before it's queried.
|
|
|
|
for attr in krate_attrs {
|
2021-07-29 12:00:41 -05:00
|
|
|
if !attr.has_name(sym::feature) {
|
2020-01-02 12:33:56 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-02-19 00:48:49 +01:00
|
|
|
let Some(list) = attr.meta_item_list() else {
|
|
|
|
continue;
|
2020-01-02 12:33:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
for mi in list {
|
|
|
|
if !mi.is_word() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let name = mi.name_or_empty();
|
|
|
|
|
|
|
|
let edition = ALL_EDITIONS.iter().find(|e| name == e.feature_name()).copied();
|
|
|
|
if let Some(edition) = edition {
|
|
|
|
if edition <= crate_edition {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for feature in active_features_up_to(edition) {
|
|
|
|
// FIXME(Manishearth) there is currently no way to set
|
|
|
|
// lib features by edition
|
|
|
|
feature.set(&mut features, DUMMY_SP);
|
|
|
|
edition_enabled_features.insert(feature.name, edition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for attr in krate_attrs {
|
2021-07-29 12:00:41 -05:00
|
|
|
if !attr.has_name(sym::feature) {
|
2020-01-02 12:33:56 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-02-19 00:48:49 +01:00
|
|
|
let Some(list) = attr.meta_item_list() else {
|
|
|
|
continue;
|
2020-01-02 12:33:56 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let bad_input = |span| {
|
|
|
|
struct_span_err!(span_handler, span, E0556, "malformed `feature` attribute input")
|
|
|
|
};
|
|
|
|
|
|
|
|
for mi in list {
|
|
|
|
let name = match mi.ident() {
|
|
|
|
Some(ident) if mi.is_word() => ident.name,
|
|
|
|
Some(ident) => {
|
|
|
|
bad_input(mi.span())
|
|
|
|
.span_suggestion(
|
|
|
|
mi.span(),
|
|
|
|
"expected just one word",
|
2022-07-28 10:20:55 +09:00
|
|
|
ident.name,
|
2020-01-02 12:33:56 +01:00
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
bad_input(mi.span()).span_label(mi.span(), "expected just one word").emit();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(edition) = edition_enabled_features.get(&name) {
|
|
|
|
let msg =
|
|
|
|
&format!("the feature `{}` is included in the Rust {} edition", name, edition);
|
|
|
|
span_handler.struct_span_warn_with_code(mi.span(), msg, error_code!(E0705)).emit();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ALL_EDITIONS.iter().any(|e| name == e.feature_name()) {
|
|
|
|
// Handled in the separate loop above.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let removed = REMOVED_FEATURES.iter().find(|f| name == f.name);
|
|
|
|
let stable_removed = STABLE_REMOVED_FEATURES.iter().find(|f| name == f.name);
|
|
|
|
if let Some(Feature { state, .. }) = removed.or(stable_removed) {
|
|
|
|
if let FeatureState::Removed { reason } | FeatureState::Stabilized { reason } =
|
|
|
|
state
|
|
|
|
{
|
|
|
|
feature_removed(span_handler, mi.span(), *reason);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(Feature { since, .. }) = ACCEPTED_FEATURES.iter().find(|f| name == f.name) {
|
|
|
|
let since = Some(Symbol::intern(since));
|
|
|
|
features.declared_lang_features.push((name, mi.span(), since));
|
2022-01-16 16:25:47 +01:00
|
|
|
features.active_features.insert(name);
|
2020-01-02 12:33:56 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-07-06 07:44:47 -05:00
|
|
|
if let Some(allowed) = sess.opts.unstable_opts.allow_features.as_ref() {
|
2021-12-15 14:39:23 +11:00
|
|
|
if allowed.iter().all(|f| name.as_str() != f) {
|
2020-01-02 12:33:56 +01:00
|
|
|
struct_span_err!(
|
|
|
|
span_handler,
|
|
|
|
mi.span(),
|
|
|
|
E0725,
|
|
|
|
"the feature `{}` is not in the list of allowed features",
|
|
|
|
name
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(f) = ACTIVE_FEATURES.iter().find(|f| name == f.name) {
|
|
|
|
f.set(&mut features, mi.span());
|
|
|
|
features.declared_lang_features.push((name, mi.span(), None));
|
2022-01-16 16:25:47 +01:00
|
|
|
features.active_features.insert(name);
|
2020-01-02 12:33:56 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
features.declared_lib_features.push((name, mi.span()));
|
2022-01-16 16:25:47 +01:00
|
|
|
features.active_features.insert(name);
|
2020-01-02 12:33:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
features
|
|
|
|
}
|
|
|
|
|
2016-08-31 23:39:16 +00:00
|
|
|
// `cfg_attr`-process the crate's attributes and compute the crate's features.
|
2022-02-27 22:26:24 +01:00
|
|
|
pub fn features(
|
|
|
|
sess: &Session,
|
|
|
|
mut krate: ast::Crate,
|
|
|
|
lint_node_id: NodeId,
|
|
|
|
) -> (ast::Crate, Features) {
|
|
|
|
let mut strip_unconfigured =
|
|
|
|
StripUnconfigured { sess, features: None, config_tokens: false, lint_node_id };
|
2019-12-31 07:42:19 +01:00
|
|
|
|
|
|
|
let unconfigured_attrs = krate.attrs.clone();
|
2020-07-30 11:27:50 +10:00
|
|
|
let diag = &sess.parse_sess.span_diagnostic;
|
2019-12-31 07:42:19 +01:00
|
|
|
let err_count = diag.err_count();
|
2021-02-23 10:21:20 -05:00
|
|
|
let features = match strip_unconfigured.configure_krate_attrs(krate.attrs) {
|
2019-12-31 07:42:19 +01:00
|
|
|
None => {
|
|
|
|
// The entire crate is unconfigured.
|
2022-08-17 12:34:33 +10:00
|
|
|
krate.attrs = ast::AttrVec::new();
|
2021-02-14 21:14:12 +03:00
|
|
|
krate.items = Vec::new();
|
2019-12-31 07:42:19 +01:00
|
|
|
Features::default()
|
2016-08-31 23:39:16 +00:00
|
|
|
}
|
2019-12-31 07:42:19 +01:00
|
|
|
Some(attrs) => {
|
|
|
|
krate.attrs = attrs;
|
2020-07-30 11:27:50 +10:00
|
|
|
let features = get_features(sess, diag, &krate.attrs);
|
2019-12-31 07:42:19 +01:00
|
|
|
if err_count == diag.err_count() {
|
|
|
|
// Avoid reconfiguring malformed `cfg_attr`s.
|
|
|
|
strip_unconfigured.features = Some(&features);
|
2021-02-23 10:21:20 -05:00
|
|
|
// Run configuration again, this time with features available
|
|
|
|
// so that we can perform feature-gating.
|
|
|
|
strip_unconfigured.configure_krate_attrs(unconfigured_attrs);
|
2019-12-31 07:42:19 +01:00
|
|
|
}
|
|
|
|
features
|
2016-08-31 23:39:16 +00:00
|
|
|
}
|
2019-12-31 07:42:19 +01:00
|
|
|
};
|
2016-08-31 23:39:16 +00:00
|
|
|
(krate, features)
|
|
|
|
}
|
|
|
|
|
2019-10-16 10:59:30 +02: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 20:58:24 +03: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 15:20:55 +11:00
|
|
|
self.process_cfg_attrs(&mut node);
|
2020-11-23 01:43:55 -05:00
|
|
|
if self.in_cfg(node.attrs()) {
|
2020-11-28 18:33:17 -05:00
|
|
|
self.try_configure_tokens(&mut node);
|
2020-11-23 01:43:55 -05:00
|
|
|
Some(node)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2016-06-01 02:13:45 +00:00
|
|
|
}
|
|
|
|
|
2022-05-01 20:58:24 +03:00
|
|
|
fn try_configure_tokens<T: HasTokens>(&self, node: &mut T) {
|
2020-11-28 18:33:17 -05:00
|
|
|
if self.config_tokens {
|
|
|
|
if let Some(Some(tokens)) = node.tokens_mut() {
|
|
|
|
let attr_annotated_tokens = tokens.create_token_stream();
|
|
|
|
*tokens = LazyTokenStream::new(self.configure_tokens(&attr_annotated_tokens));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 12:34:33 +10:00
|
|
|
fn configure_krate_attrs(&self, mut attrs: ast::AttrVec) -> Option<ast::AttrVec> {
|
2021-02-23 10:21:20 -05:00
|
|
|
attrs.flat_map_in_place(|attr| self.process_cfg_attr(attr));
|
2020-11-28 18:33:17 -05:00
|
|
|
if self.in_cfg(&attrs) { Some(attrs) } else { None }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Performs cfg-expansion on `stream`, producing a new `AttrAnnotatedTokenStream`.
|
|
|
|
/// 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-01-05 14:10:07 +08:00
|
|
|
fn configure_tokens(&self, stream: &AttrAnnotatedTokenStream) -> AttrAnnotatedTokenStream {
|
2020-11-28 18:33:17 -05:00
|
|
|
fn can_skip(stream: &AttrAnnotatedTokenStream) -> bool {
|
|
|
|
stream.0.iter().all(|(tree, _spacing)| match tree {
|
|
|
|
AttrAnnotatedTokenTree::Attributes(_) => false,
|
|
|
|
AttrAnnotatedTokenTree::Token(_) => true,
|
|
|
|
AttrAnnotatedTokenTree::Delimited(_, _, inner) => can_skip(inner),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if can_skip(stream) {
|
|
|
|
return stream.clone();
|
2021-02-23 10:21:20 -05:00
|
|
|
}
|
2020-11-28 18:33:17 -05:00
|
|
|
|
|
|
|
let trees: Vec<_> = stream
|
|
|
|
.0
|
|
|
|
.iter()
|
|
|
|
.flat_map(|(tree, spacing)| match tree.clone() {
|
|
|
|
AttrAnnotatedTokenTree::Attributes(mut data) => {
|
2022-08-17 12:34:33 +10:00
|
|
|
data.attrs.flat_map_in_place(|attr| self.process_cfg_attr(attr));
|
2020-11-28 18:33:17 -05:00
|
|
|
|
|
|
|
if self.in_cfg(&data.attrs) {
|
|
|
|
data.tokens = LazyTokenStream::new(
|
|
|
|
self.configure_tokens(&data.tokens.create_token_stream()),
|
|
|
|
);
|
|
|
|
Some((AttrAnnotatedTokenTree::Attributes(data), *spacing)).into_iter()
|
|
|
|
} else {
|
|
|
|
None.into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AttrAnnotatedTokenTree::Delimited(sp, delim, mut inner) => {
|
|
|
|
inner = self.configure_tokens(&inner);
|
|
|
|
Some((AttrAnnotatedTokenTree::Delimited(sp, delim, inner), *spacing))
|
|
|
|
.into_iter()
|
|
|
|
}
|
2021-08-16 17:29:49 +02:00
|
|
|
AttrAnnotatedTokenTree::Token(ref token) if let TokenKind::Interpolated(ref nt) = token.kind => {
|
|
|
|
panic!(
|
|
|
|
"Nonterminal should have been flattened at {:?}: {:?}",
|
|
|
|
token.span, nt
|
|
|
|
);
|
|
|
|
}
|
2020-11-28 18:33:17 -05:00
|
|
|
AttrAnnotatedTokenTree::Token(token) => {
|
2021-08-16 17:29:49 +02:00
|
|
|
Some((AttrAnnotatedTokenTree::Token(token), *spacing)).into_iter()
|
2020-11-28 18:33:17 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
AttrAnnotatedTokenStream::new(trees)
|
2021-02-23 10:21:20 -05:00
|
|
|
}
|
|
|
|
|
2018-09-10 15:06:49 -07: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 12:13:55 +01:00
|
|
|
/// Gives compiler warnings if any `cfg_attr` does not contain any
|
2018-09-10 15:06:49 -07:00
|
|
|
/// attributes and is in the original source code. Gives compiler errors if
|
|
|
|
/// the syntax of any `cfg_attr` is incorrect.
|
2022-05-01 20:58:24 +03: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 15:20:55 +11:00
|
|
|
node.visit_attrs(|attrs| {
|
|
|
|
attrs.flat_map_in_place(|attr| self.process_cfg_attr(attr));
|
|
|
|
});
|
2016-06-01 02:13:45 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 18:47:19 +08: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] }
|
|
|
|
}
|
|
|
|
|
2018-09-10 15:06:49 -07: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 18:21:55 +02:00
|
|
|
/// Gives a compiler warning when the `cfg_attr` contains no attributes and
|
2018-09-10 15:06:49 -07:00
|
|
|
/// is in the original source file. Gives a compiler error if the syntax of
|
2019-02-08 14:53:55 +01:00
|
|
|
/// the attribute is incorrect.
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn expand_cfg_attr(&self, attr: Attribute, recursive: bool) -> Vec<Attribute> {
|
2022-02-19 00:48:49 +01:00
|
|
|
let Some((cfg_predicate, expanded_attrs)) =
|
|
|
|
rustc_parse::parse_cfg_attr(&attr, &self.sess.parse_sess) else {
|
|
|
|
return vec![];
|
2021-07-29 12:00:41 -05:00
|
|
|
};
|
2016-08-19 18:58:14 -07:00
|
|
|
|
2019-06-22 12:11:01 +02:00
|
|
|
// Lint on zero attributes in source.
|
|
|
|
if expanded_attrs.is_empty() {
|
2021-12-29 18:47:19 +08:00
|
|
|
self.sess.parse_sess.buffer_lint(
|
|
|
|
rustc_lint_defs::builtin::UNUSED_ATTRIBUTES,
|
|
|
|
attr.span,
|
|
|
|
ast::CRATE_NODE_ID,
|
|
|
|
"`#[cfg_attr]` does not expand to any attributes",
|
|
|
|
);
|
2018-10-04 04:55:47 -07:00
|
|
|
}
|
|
|
|
|
2022-02-27 22:26:24 +01:00
|
|
|
if !attr::cfg_matches(
|
|
|
|
&cfg_predicate,
|
|
|
|
&self.sess.parse_sess,
|
|
|
|
self.lint_node_id,
|
|
|
|
self.features,
|
|
|
|
) {
|
2019-12-05 06:45:50 +01:00
|
|
|
return vec![];
|
|
|
|
}
|
|
|
|
|
2021-12-29 18:47:19 +08: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))]`.
|
|
|
|
expanded_attrs
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|item| self.process_cfg_attr(self.expand_cfg_attr_item(&attr, item)))
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
expanded_attrs.into_iter().map(|item| self.expand_cfg_attr_item(&attr, item)).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_cfg_attr_item(
|
|
|
|
&self,
|
|
|
|
attr: &Attribute,
|
|
|
|
(item, item_span): (ast::AttrItem, Span),
|
|
|
|
) -> Attribute {
|
|
|
|
let orig_tokens = attr.tokens().to_tokenstream();
|
|
|
|
|
|
|
|
// We are taking an attribute of the form `#[cfg_attr(pred, attr)]`
|
|
|
|
// and producing an attribute of the form `#[attr]`. We
|
|
|
|
// have captured tokens for `attr` itself, but we need to
|
|
|
|
// synthesize tokens for the wrapper `#` and `[]`, which
|
|
|
|
// we do below.
|
|
|
|
|
|
|
|
// Use the `#` in `#[cfg_attr(pred, attr)]` as the `#` token
|
|
|
|
// for `attr` when we expand it to `#[attr]`
|
2022-05-16 18:58:15 +03:00
|
|
|
let mut orig_trees = orig_tokens.into_trees();
|
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 10:31:04 +10:00
|
|
|
let TokenTree::Token(pound_token @ Token { kind: TokenKind::Pound, .. }, _) = orig_trees.next().unwrap() else {
|
2022-02-19 00:48:49 +01:00
|
|
|
panic!("Bad tokens for attribute {:?}", attr);
|
2021-12-29 18:47:19 +08:00
|
|
|
};
|
|
|
|
let pound_span = pound_token.span;
|
|
|
|
|
|
|
|
let mut trees = vec![(AttrAnnotatedTokenTree::Token(pound_token), Spacing::Alone)];
|
|
|
|
if attr.style == AttrStyle::Inner {
|
|
|
|
// For inner attributes, we do the same thing for the `!` in `#![some_attr]`
|
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 10:31:04 +10:00
|
|
|
let TokenTree::Token(bang_token @ Token { kind: TokenKind::Not, .. }, _) = orig_trees.next().unwrap() else {
|
2022-02-19 00:48:49 +01:00
|
|
|
panic!("Bad tokens for attribute {:?}", attr);
|
2021-12-29 18:47:19 +08:00
|
|
|
};
|
|
|
|
trees.push((AttrAnnotatedTokenTree::Token(bang_token), Spacing::Alone));
|
|
|
|
}
|
2022-03-30 01:39:38 -04:00
|
|
|
// We don't really have a good span to use for the synthesized `[]`
|
2021-12-29 18:47:19 +08:00
|
|
|
// in `#[attr]`, so just use the span of the `#` token.
|
|
|
|
let bracket_group = AttrAnnotatedTokenTree::Delimited(
|
|
|
|
DelimSpan::from_single(pound_span),
|
2022-04-26 15:40:14 +03:00
|
|
|
Delimiter::Bracket,
|
2021-12-29 18:47:19 +08:00
|
|
|
item.tokens
|
|
|
|
.as_ref()
|
|
|
|
.unwrap_or_else(|| panic!("Missing tokens for {:?}", item))
|
|
|
|
.create_token_stream(),
|
|
|
|
);
|
|
|
|
trees.push((bracket_group, Spacing::Alone));
|
|
|
|
let tokens = Some(LazyTokenStream::new(AttrAnnotatedTokenStream::new(trees)));
|
|
|
|
let attr = attr::mk_attr_from_item(item, tokens, attr.style, item_span);
|
|
|
|
if attr.has_name(sym::crate_type) {
|
|
|
|
self.sess.parse_sess.buffer_lint(
|
|
|
|
rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
|
|
|
|
attr.span,
|
|
|
|
ast::CRATE_NODE_ID,
|
|
|
|
"`crate_type` within an `#![cfg_attr] attribute is deprecated`",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if attr.has_name(sym::crate_name) {
|
|
|
|
self.sess.parse_sess.buffer_lint(
|
|
|
|
rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
|
|
|
|
attr.span,
|
|
|
|
ast::CRATE_NODE_ID,
|
|
|
|
"`crate_name` within an `#![cfg_attr] attribute is deprecated`",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
attr
|
2019-12-05 06:45:50 +01:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Determines if a node with the given attributes should be included in this configuration.
|
2021-03-06 23:37:59 +03:00
|
|
|
fn in_cfg(&self, attrs: &[Attribute]) -> bool {
|
2021-12-29 18:47:19 +08:00
|
|
|
attrs.iter().all(|attr| !is_cfg(attr) || self.cfg_true(attr))
|
|
|
|
}
|
|
|
|
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn cfg_true(&self, attr: &Attribute) -> bool {
|
2021-12-29 18:47:19 +08:00
|
|
|
let meta_item = match validate_attr::parse_meta(&self.sess.parse_sess, attr) {
|
|
|
|
Ok(meta_item) => meta_item,
|
|
|
|
Err(mut err) => {
|
|
|
|
err.emit();
|
2017-03-08 23:13:35 +00:00
|
|
|
return true;
|
2018-09-02 00:13:22 +03:00
|
|
|
}
|
2021-12-29 18:47:19 +08:00
|
|
|
};
|
|
|
|
parse_cfg(&meta_item, &self.sess).map_or(true, |meta_item| {
|
2022-02-27 22:26:24 +01:00
|
|
|
attr::cfg_matches(&meta_item, &self.sess.parse_sess, self.lint_node_id, self.features)
|
2016-05-26 23:56:25 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-15 23:20:56 -07:00
|
|
|
/// If attributes are not allowed on expressions, emit an error for `attr`
|
2022-05-20 19:51:09 -04:00
|
|
|
pub(crate) fn maybe_emit_expr_attr_err(&self, attr: &Attribute) {
|
2021-01-11 20:45:33 +01:00
|
|
|
if !self.features.map_or(true, |features| features.stmt_expr_attributes) {
|
2018-03-15 23:20:56 -07:00
|
|
|
let mut err = feature_err(
|
2020-07-30 11:27:50 +10:00
|
|
|
&self.sess.parse_sess,
|
2019-05-08 13:21:18 +10:00
|
|
|
sym::stmt_expr_attributes,
|
2018-03-15 23:20:56 -07:00
|
|
|
attr.span,
|
2019-11-30 02:40:28 +01:00
|
|
|
"attributes on expressions are experimental",
|
|
|
|
);
|
2018-03-15 23:20:56 -07:00
|
|
|
|
2019-10-24 06:33:12 +11:00
|
|
|
if attr.is_doc_comment() {
|
2018-03-15 23:20:56 -07:00
|
|
|
err.help("`///` is for documentation comments. For a plain comment, use `//`.");
|
2016-06-11 01:37:24 +00:00
|
|
|
}
|
2018-03-15 23:20:56 -07:00
|
|
|
|
|
|
|
err.emit();
|
2016-05-15 09:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 14:10:07 +08:00
|
|
|
pub fn configure_expr(&self, expr: &mut P<ast::Expr>) {
|
2021-03-06 23:37:59 +03:00
|
|
|
for attr in expr.attrs.iter() {
|
|
|
|
self.maybe_emit_expr_attr_err(attr);
|
|
|
|
}
|
2016-06-11 01:37:24 +00:00
|
|
|
|
2015-11-03 17:39:51 +01: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 15:20:55 +11: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
|
2021-07-29 12:00:41 -05:00
|
|
|
if let Some(attr) = expr.attrs().iter().find(|a| is_cfg(*a)) {
|
2016-06-11 01:37:24 +00:00
|
|
|
let msg = "removing an expression is not supported in this position";
|
2020-07-30 11:27:50 +10:00
|
|
|
self.sess.parse_sess.span_diagnostic.span_err(attr.span, msg);
|
2016-06-11 01:37:24 +00:00
|
|
|
}
|
|
|
|
|
2020-11-28 18:33:17 -05:00
|
|
|
self.process_cfg_attrs(expr);
|
|
|
|
self.try_configure_tokens(&mut *expr);
|
2014-04-22 21:54:48 -07:00
|
|
|
}
|
2013-08-29 12:10:02 -07:00
|
|
|
}
|
2011-06-29 22:32:08 -07:00
|
|
|
|
2021-04-22 13:28:43 -04:00
|
|
|
pub fn parse_cfg<'a>(meta_item: &'a MetaItem, sess: &Session) -> Option<&'a MetaItem> {
|
|
|
|
let error = |span, msg, suggestion: &str| {
|
|
|
|
let mut err = sess.parse_sess.span_diagnostic.struct_span_err(span, msg);
|
|
|
|
if !suggestion.is_empty() {
|
|
|
|
err.span_suggestion(
|
|
|
|
span,
|
|
|
|
"expected syntax is",
|
2022-04-26 06:17:33 +01:00
|
|
|
suggestion,
|
2021-04-22 13:28:43 -04:00
|
|
|
Applicability::HasPlaceholders,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
None
|
|
|
|
};
|
|
|
|
let span = meta_item.span;
|
|
|
|
match meta_item.meta_item_list() {
|
|
|
|
None => error(span, "`cfg` is not followed by parentheses", "cfg(/* predicate */)"),
|
|
|
|
Some([]) => error(span, "`cfg` predicate is not specified", ""),
|
|
|
|
Some([_, .., l]) => error(l.span(), "multiple `cfg` predicates are specified", ""),
|
|
|
|
Some([single]) => match single.meta_item() {
|
|
|
|
Some(meta_item) => Some(meta_item),
|
|
|
|
None => error(single.span(), "`cfg` predicate key cannot be a literal", ""),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 12:00:41 -05:00
|
|
|
fn is_cfg(attr: &Attribute) -> bool {
|
|
|
|
attr.has_name(sym::cfg)
|
2015-11-03 17:39:51 +01:00
|
|
|
}
|