Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #19125 from ChayimFriedman2/cfg-attr-fast
fix: Censor cfg_attr for attribute macros
Lukas Wirth 2025-02-12
parent 622ef64 · parent 9338320 · commit b636cf9
-rw-r--r--crates/hir-def/src/macro_expansion_tests/proc_macros.rs20
-rw-r--r--crates/hir-expand/src/cfg_process.rs56
-rw-r--r--crates/test-fixture/src/lib.rs44
3 files changed, 96 insertions, 24 deletions
diff --git a/crates/hir-def/src/macro_expansion_tests/proc_macros.rs b/crates/hir-def/src/macro_expansion_tests/proc_macros.rs
index 70e3e1ed4e..a43c0eb9d7 100644
--- a/crates/hir-def/src/macro_expansion_tests/proc_macros.rs
+++ b/crates/hir-def/src/macro_expansion_tests/proc_macros.rs
@@ -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#""#]],
+ );
+}
diff --git a/crates/hir-expand/src/cfg_process.rs b/crates/hir-expand/src/cfg_process.rs
index 01a3103af8..626a82ae08 100644
--- a/crates/hir-expand/src/cfg_process.rs
+++ b/crates/hir-expand/src/cfg_process.rs
@@ -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)?;
- }
- 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
+ _ => {}
}
- // 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)
}
diff --git a/crates/test-fixture/src/lib.rs b/crates/test-fixture/src/lib.rs
index ca59658359..613f27c795 100644
--- a/crates/test-fixture/src/lib.rs
+++ b/crates/test-fixture/src/lib.rs
@@ -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())
+ }
+}