mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-25 11:17:13 +00:00
Censor cfg_attr for attribute macros
This is not documented (and I discovered that from experimenting and looking at the compiler's source code), but cfg_attrs *on the same level* as the attribute macro should be processed before it is expanded. cfg_attrs *below* should not (and this is contrary to what happens with derive macros, where both should be processed).
This commit is contained in:
parent
fc726ced6b
commit
933832008b
@ -5,7 +5,7 @@
|
||||
//! in-memory macros.
|
||||
use expect_test::expect;
|
||||
|
||||
use crate::macro_expansion_tests::check;
|
||||
use crate::macro_expansion_tests::{check, check_errors};
|
||||
|
||||
#[test]
|
||||
fn attribute_macro_attr_censoring() {
|
||||
@ -216,3 +216,21 @@ struct S;
|
||||
#[doc = "doc attr"] struct S;"##]],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cfg_evaluated_before_attr_macros() {
|
||||
check_errors(
|
||||
r#"
|
||||
//- proc_macros: disallow_cfg
|
||||
|
||||
use proc_macros::disallow_cfg;
|
||||
|
||||
#[disallow_cfg] #[cfg(false)] fn foo() {}
|
||||
// True cfg are kept.
|
||||
// #[disallow_cfg] #[cfg(true)] fn bar() {}
|
||||
#[disallow_cfg] #[cfg_attr(false, inline)] fn baz() {}
|
||||
#[disallow_cfg] #[cfg_attr(true, inline)] fn qux() {}
|
||||
"#,
|
||||
expect![[r#""#]],
|
||||
);
|
||||
}
|
||||
|
@ -201,9 +201,6 @@ pub(crate) fn process_cfg_attrs(
|
||||
MacroDefKind::BuiltInAttr(_, expander) => expander.is_derive(),
|
||||
_ => false,
|
||||
};
|
||||
if !is_derive {
|
||||
return None;
|
||||
}
|
||||
let mut remove = FxHashSet::default();
|
||||
|
||||
let item = ast::Item::cast(node.clone())?;
|
||||
@ -220,28 +217,43 @@ pub(crate) fn process_cfg_attrs(
|
||||
}
|
||||
}
|
||||
}
|
||||
match item {
|
||||
ast::Item::Struct(it) => match it.field_list()? {
|
||||
ast::FieldList::RecordFieldList(fields) => {
|
||||
process_has_attrs_with_possible_comma(db, fields.fields(), loc.krate, &mut remove)?;
|
||||
|
||||
if is_derive {
|
||||
// Only derives get their code cfg-clean, normal attribute macros process only the cfg at their level
|
||||
// (cfg_attr is handled above, cfg is handled in the def map).
|
||||
match item {
|
||||
ast::Item::Struct(it) => match it.field_list()? {
|
||||
ast::FieldList::RecordFieldList(fields) => {
|
||||
process_has_attrs_with_possible_comma(
|
||||
db,
|
||||
fields.fields(),
|
||||
loc.krate,
|
||||
&mut remove,
|
||||
)?;
|
||||
}
|
||||
ast::FieldList::TupleFieldList(fields) => {
|
||||
process_has_attrs_with_possible_comma(
|
||||
db,
|
||||
fields.fields(),
|
||||
loc.krate,
|
||||
&mut remove,
|
||||
)?;
|
||||
}
|
||||
},
|
||||
ast::Item::Enum(it) => {
|
||||
process_enum(db, it.variant_list()?, loc.krate, &mut remove)?;
|
||||
}
|
||||
ast::FieldList::TupleFieldList(fields) => {
|
||||
process_has_attrs_with_possible_comma(db, fields.fields(), loc.krate, &mut remove)?;
|
||||
ast::Item::Union(it) => {
|
||||
process_has_attrs_with_possible_comma(
|
||||
db,
|
||||
it.record_field_list()?.fields(),
|
||||
loc.krate,
|
||||
&mut remove,
|
||||
)?;
|
||||
}
|
||||
},
|
||||
ast::Item::Enum(it) => {
|
||||
process_enum(db, it.variant_list()?, loc.krate, &mut remove)?;
|
||||
// FIXME: Implement for other items if necessary. As we do not support #[cfg_eval] yet, we do not need to implement it for now
|
||||
_ => {}
|
||||
}
|
||||
ast::Item::Union(it) => {
|
||||
process_has_attrs_with_possible_comma(
|
||||
db,
|
||||
it.record_field_list()?.fields(),
|
||||
loc.krate,
|
||||
&mut remove,
|
||||
)?;
|
||||
}
|
||||
// FIXME: Implement for other items if necessary. As we do not support #[cfg_eval] yet, we do not need to implement it for now
|
||||
_ => {}
|
||||
}
|
||||
Some(remove)
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ use hir_expand::{
|
||||
tt::{Leaf, TokenTree, TopSubtree, TopSubtreeBuilder, TtElement, TtIter},
|
||||
FileRange,
|
||||
};
|
||||
use intern::Symbol;
|
||||
use intern::{sym, Symbol};
|
||||
use rustc_hash::FxHashMap;
|
||||
use span::{Edition, EditionedFileId, FileId, Span};
|
||||
use stdx::itertools::Itertools;
|
||||
@ -511,6 +511,21 @@ pub fn issue_18898(_attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||
disabled: false,
|
||||
},
|
||||
),
|
||||
(
|
||||
r#"
|
||||
#[proc_macro_attribute]
|
||||
pub fn disallow_cfg(_attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||
input
|
||||
}
|
||||
"#
|
||||
.into(),
|
||||
ProcMacro {
|
||||
name: Symbol::intern("disallow_cfg"),
|
||||
kind: ProcMacroKind::Attr,
|
||||
expander: sync::Arc::new(DisallowCfgProcMacroExpander),
|
||||
disabled: false,
|
||||
},
|
||||
),
|
||||
])
|
||||
}
|
||||
|
||||
@ -865,3 +880,30 @@ impl ProcMacroExpander for Issue18898ProcMacroExpander {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Reads ident type within string quotes, for issue #17479.
|
||||
#[derive(Debug)]
|
||||
struct DisallowCfgProcMacroExpander;
|
||||
impl ProcMacroExpander for DisallowCfgProcMacroExpander {
|
||||
fn expand(
|
||||
&self,
|
||||
subtree: &TopSubtree,
|
||||
_: Option<&TopSubtree>,
|
||||
_: &Env,
|
||||
_: Span,
|
||||
_: Span,
|
||||
_: Span,
|
||||
_: Option<String>,
|
||||
) -> Result<TopSubtree, ProcMacroExpansionError> {
|
||||
for tt in subtree.token_trees().flat_tokens() {
|
||||
if let tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) = tt {
|
||||
if ident.sym == sym::cfg || ident.sym == sym::cfg_attr {
|
||||
return Err(ProcMacroExpansionError::Panic(
|
||||
"cfg or cfg_attr found in DisallowCfgProcMacroExpander".to_owned(),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(subtree.clone())
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user