From ddf144051ae223cebc2a78f579af47e004b13aa8 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 20 Mar 2022 19:27:46 +0100 Subject: [PATCH] internal: Treat {global_}asm macros as unsafe --- crates/hir/src/source_analyzer.rs | 22 +++++++++++++++++-- .../test_data/highlight_keywords.html | 2 +- .../test_data/highlight_strings.html | 2 +- crates/ide/src/syntax_highlighting/tests.rs | 2 +- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/crates/hir/src/source_analyzer.rs b/crates/hir/src/source_analyzer.rs index 6b2120b253..2b818b8eba 100644 --- a/crates/hir/src/source_analyzer.rs +++ b/crates/hir/src/source_analyzer.rs @@ -21,9 +21,11 @@ use hir_def::{ path::{ModPath, Path, PathKind}, resolver::{resolver_for_scope, Resolver, TypeNs, ValueNs}, type_ref::Mutability, - AsMacroCall, DefWithBodyId, FieldId, FunctionId, LocalFieldId, ModuleDefId, VariantId, + AsMacroCall, DefWithBodyId, FieldId, FunctionId, LocalFieldId, Lookup, ModuleDefId, VariantId, +}; +use hir_expand::{ + builtin_fn_macro::BuiltinFnLikeExpander, hygiene::Hygiene, name::AsName, HirFileId, InFile, }; -use hir_expand::{hygiene::Hygiene, name::AsName, HirFileId, InFile}; use hir_ty::{ diagnostics::{ record_literal_missing_fields, record_pattern_missing_fields, unsafe_expressions, @@ -490,6 +492,22 @@ impl SourceAnalyzer { db: &dyn HirDatabase, macro_call: InFile<&ast::MacroCall>, ) -> bool { + // check for asm/global_asm + if let Some(mac) = self.resolve_macro_call(db, macro_call) { + let ex = match mac.id { + hir_def::MacroId::Macro2Id(it) => it.lookup(db.upcast()).expander, + hir_def::MacroId::MacroRulesId(it) => it.lookup(db.upcast()).expander, + _ => hir_def::MacroExpander::Declarative, + }; + match ex { + hir_def::MacroExpander::BuiltIn(e) + if e == BuiltinFnLikeExpander::Asm || e == BuiltinFnLikeExpander::GlobalAsm => + { + return true + } + _ => (), + } + } if let (Some((def, body, sm)), Some(infer)) = (&self.def, &self.infer) { if let Some(expr_ids) = sm.macro_expansion_expr(macro_call) { let mut is_unsafe = false; diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_keywords.html b/crates/ide/src/syntax_highlighting/test_data/highlight_keywords.html index 278e0d84ee..7cd6a5a72d 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_keywords.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_keywords.html @@ -51,7 +51,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd } macro_rules! void { - ($($tt:tt)) => {} + ($($tt:tt)*) => {} } void!(Self); struct __ where Self:; diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html index e26c017f95..29a0a9f066 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html @@ -149,6 +149,6 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd assert!(true, "{}", 1); assert!(true, "{} asdasd", 1); toho!("{}fmt", 0); - asm!("mov eax, {0}"); + asm!("mov eax, {0}"); format_args!(concat!("{}"), "{}"); } \ No newline at end of file diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs index c74bced637..f8ca5c269c 100644 --- a/crates/ide/src/syntax_highlighting/tests.rs +++ b/crates/ide/src/syntax_highlighting/tests.rs @@ -347,7 +347,7 @@ mod __ { } macro_rules! void { - ($($tt:tt)) => {} + ($($tt:tt)*) => {} } void!(Self); struct __ where Self:;