Invert matching on builtin macros in expand_allowed_builtins

This commit is contained in:
Lukas Wirth 2024-06-20 10:31:20 +02:00
parent a2d4e2934e
commit e5d5c7b20a
2 changed files with 25 additions and 29 deletions

View File

@ -328,6 +328,8 @@ impl<'db> SemanticsImpl<'db> {
Some(node)
}
/// Expands the macro if it isn't one of the built-in ones that expand to custom syntax or dummy
/// expansions.
pub fn expand_allowed_builtins(&self, macro_call: &ast::MacroCall) -> Option<SyntaxNode> {
let sa = self.analyze_no_infer(macro_call.syntax())?;
@ -341,33 +343,27 @@ impl<'db> SemanticsImpl<'db> {
};
let macro_call = self.db.lookup_intern_macro_call(file_id.macro_call_id);
match macro_call.def.kind {
let skip = matches!(
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
}
hir_expand::MacroDefKind::BuiltIn(_, _)
| hir_expand::MacroDefKind::BuiltInAttr(_, _)
| hir_expand::MacroDefKind::BuiltInEager(_, _)
| hir_expand::MacroDefKind::BuiltInDerive(_, _) => return None,
_ => (),
BuiltinFnLikeExpander::Column
| BuiltinFnLikeExpander::File
| BuiltinFnLikeExpander::ModulePath
| BuiltinFnLikeExpander::Asm
| BuiltinFnLikeExpander::LlvmAsm
| BuiltinFnLikeExpander::GlobalAsm
| BuiltinFnLikeExpander::LogSyntax
| BuiltinFnLikeExpander::TraceMacros
| BuiltinFnLikeExpander::FormatArgs
| BuiltinFnLikeExpander::FormatArgsNl
| BuiltinFnLikeExpander::ConstFormatArgs,
) | hir_expand::MacroDefKind::BuiltInEager(_, EagerExpander::CompileError)
);
if skip {
// these macros expand to custom builtin syntax and/or dummy things, no point in
// showing these to the user
return None;
}
let node = self.parse_or_expand(file_id.into());

View File

@ -233,8 +233,8 @@ mod tests {
fn expand_allowed_builtin_macro() {
check(
r#"
//- minicore: concat
$0concat!("test", 10, 'b', true);"#,
//- minicore: concat
$0concat!("test", 10, 'b', true);"#,
expect![[r#"
concat!
"test10btrue""#]],
@ -245,8 +245,8 @@ mod tests {
fn do_not_expand_disallowed_macro() {
let (analysis, pos) = fixture::position(
r#"
//- minicore: asm
$0asm!("0x300, x0");"#,
//- minicore: asm
$0asm!("0x300, x0");"#,
);
let expansion = analysis.expand_macro(pos).unwrap();
assert!(expansion.is_none());