hir/semantics: Only allow expansion of specific built in macros

This commit is contained in:
Ishan Jain 2024-06-12 11:03:04 +05:30
parent 4af21ffb02
commit 5d5f68af3f
No known key found for this signature in database
GPG key ID: 0506DB2A1CC75C27

View file

@ -19,8 +19,12 @@ use hir_def::{
AsMacroCall, DefWithBodyId, FunctionId, MacroId, TraitId, VariantId, AsMacroCall, DefWithBodyId, FunctionId, MacroId, TraitId, VariantId,
}; };
use hir_expand::{ use hir_expand::{
attrs::collect_attrs, db::ExpandDatabase, files::InRealFile, name::AsName, InMacroFile, attrs::collect_attrs,
MacroCallId, MacroFileId, MacroFileIdExt, builtin_fn_macro::{BuiltinFnLikeExpander, EagerExpander},
db::ExpandDatabase,
files::InRealFile,
name::AsName,
InMacroFile, MacroCallId, MacroFileId, MacroFileIdExt,
}; };
use itertools::Itertools; use itertools::Itertools;
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
@ -319,6 +323,30 @@ impl<'db> SemanticsImpl<'db> {
} else { } else {
sa.expand(self.db, macro_call)? sa.expand(self.db, macro_call)?
}; };
let macro_call = self.db.lookup_intern_macro_call(file_id.macro_call_id);
match macro_call.def.kind {
hir_expand::MacroDefKind::BuiltIn(
_,
BuiltinFnLikeExpander::Cfg
| BuiltinFnLikeExpander::StdPanic
| BuiltinFnLikeExpander::Stringify
| BuiltinFnLikeExpander::CorePanic,
)
| hir_expand::MacroDefKind::BuiltInEager(
_,
EagerExpander::Env
| EagerExpander::Concat
| EagerExpander::Include
| EagerExpander::OptionEnv
| EagerExpander::IncludeStr
| EagerExpander::ConcatBytes
| EagerExpander::IncludeBytes,
) => {
// Do nothing and allow matching macros to be expanded
}
_ => return None,
}
let node = self.parse_or_expand(file_id.into()); let node = self.parse_or_expand(file_id.into());
Some(node) Some(node)