about summary refs log tree commit diff
diff options
context:
space:
mode:
authorLukas Wirth <lukastw97@gmail.com>2023-08-05 20:00:33 +0200
committerLukas Wirth <lukastw97@gmail.com>2023-08-05 20:00:37 +0200
commit042be329a7f09ba6b5e7901c59dee044723b9670 (patch)
tree548eab9abfaca36b85124751d33bb4c31c725199
parentc59bd2dc3f05692f92b5b1c76f3d08d116e63422 (diff)
downloadrust-042be329a7f09ba6b5e7901c59dee044723b9670.tar.gz
rust-042be329a7f09ba6b5e7901c59dee044723b9670.zip
Turn unresolved proc macro expansions into missing expressions
-rw-r--r--crates/hir-def/src/body/lower.rs5
-rw-r--r--crates/hir-def/src/expander.rs32
-rw-r--r--crates/ide-assists/src/handlers/inline_macro.rs3
-rw-r--r--crates/parser/src/grammar/items.rs5
4 files changed, 25 insertions, 20 deletions
diff --git a/crates/hir-def/src/body/lower.rs b/crates/hir-def/src/body/lower.rs
index 3df43576482..3853a6ab3a5 100644
--- a/crates/hir-def/src/body/lower.rs
+++ b/crates/hir-def/src/body/lower.rs
@@ -913,15 +913,14 @@ impl ExprCollector<'_> {
         self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr)
     }
 
-    fn collect_macro_call<F, T, U>(
+    fn collect_macro_call<T, U>(
         &mut self,
         mcall: ast::MacroCall,
         syntax_ptr: AstPtr<ast::MacroCall>,
         record_diagnostics: bool,
-        collector: F,
+        collector: impl FnOnce(&mut Self, Option<T>) -> U,
     ) -> U
     where
-        F: FnOnce(&mut Self, Option<T>) -> U,
         T: ast::AstNode,
     {
         // File containing the macro call. Expansion errors will be attached here.
diff --git a/crates/hir-def/src/expander.rs b/crates/hir-def/src/expander.rs
index cc85bd893ac..6db8398bc98 100644
--- a/crates/hir-def/src/expander.rs
+++ b/crates/hir-def/src/expander.rs
@@ -164,18 +164,26 @@ impl Expander {
             return ExpandResult { value: None, err };
         };
 
-        Self::enter_expand_inner(db, call_id, err).map(|value| {
-            value.and_then(|InFile { file_id, value }| {
-                let parse = value.cast::<T>()?;
-
-                self.recursion_depth += 1;
-                self.hygiene = Hygiene::new(db.upcast(), file_id);
-                let old_file_id = std::mem::replace(&mut self.current_file_id, file_id);
-                let mark =
-                    Mark { file_id: old_file_id, bomb: DropBomb::new("expansion mark dropped") };
-                Some((mark, parse))
-            })
-        })
+        let res = Self::enter_expand_inner(db, call_id, err);
+        match res.err {
+            // If proc-macro is disabled or unresolved, we want to expand to a missing expression
+            // instead of an empty tree which might end up in an empty block.
+            Some(ExpandError::UnresolvedProcMacro(_)) => res.map(|_| None),
+            _ => res.map(|value| {
+                value.and_then(|InFile { file_id, value }| {
+                    let parse = value.cast::<T>()?;
+
+                    self.recursion_depth += 1;
+                    self.hygiene = Hygiene::new(db.upcast(), file_id);
+                    let old_file_id = std::mem::replace(&mut self.current_file_id, file_id);
+                    let mark = Mark {
+                        file_id: old_file_id,
+                        bomb: DropBomb::new("expansion mark dropped"),
+                    };
+                    Some((mark, parse))
+                })
+            }),
+        }
     }
 }
 
diff --git a/crates/ide-assists/src/handlers/inline_macro.rs b/crates/ide-assists/src/handlers/inline_macro.rs
index 5aa8e56f562..5d956b1a5e8 100644
--- a/crates/ide-assists/src/handlers/inline_macro.rs
+++ b/crates/ide-assists/src/handlers/inline_macro.rs
@@ -37,11 +37,10 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
 pub(crate) fn inline_macro(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
     let unexpanded = ctx.find_node_at_offset::<ast::MacroCall>()?;
     let expanded = insert_ws_into(ctx.sema.expand(&unexpanded)?.clone_for_update());
-
     let text_range = unexpanded.syntax().text_range();
 
     acc.add(
-        AssistId("inline_macro", AssistKind::RefactorRewrite),
+        AssistId("inline_macro", AssistKind::RefactorInline),
         format!("Inline macro"),
         text_range,
         |builder| builder.replace(text_range, expanded.to_string()),
diff --git a/crates/parser/src/grammar/items.rs b/crates/parser/src/grammar/items.rs
index 1c056819f4b..4e850b1f74d 100644
--- a/crates/parser/src/grammar/items.rs
+++ b/crates/parser/src/grammar/items.rs
@@ -328,9 +328,6 @@ fn macro_rules(p: &mut Parser<'_>, m: Marker) {
     p.bump_remap(T![macro_rules]);
     p.expect(T![!]);
 
-    if p.at(IDENT) {
-        name(p);
-    }
     // Special-case `macro_rules! try`.
     // This is a hack until we do proper edition support
 
@@ -340,6 +337,8 @@ fn macro_rules(p: &mut Parser<'_>, m: Marker) {
         let m = p.start();
         p.bump_remap(IDENT);
         m.complete(p, NAME);
+    } else {
+        name(p);
     }
 
     match p.current() {