From 80dc20f7d8b42e16fc68385ba3fab52402893ddf Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 6 Dec 2023 17:39:41 +0100 Subject: [PATCH] fix: Don't print proc-macro panic backtraces in the logs --- crates/hir-expand/src/builtin_fn_macro.rs | 2 + crates/hir-expand/src/lib.rs | 5 ++ crates/hir-expand/src/proc_macro.rs | 2 +- crates/ide/src/navigation_target.rs | 2 - crates/ide/src/syntax_highlighting.rs | 8 +--- .../test_data/highlight_macros.html | 4 +- .../test_data/highlight_strings.html | 2 +- crates/ide/src/syntax_highlighting/tests.rs | 6 ++- crates/proc-macro-srv/src/dylib.rs | 2 +- crates/proc-macro-srv/src/lib.rs | 4 +- crates/proc-macro-srv/src/proc_macros.rs | 46 +++++++++---------- 11 files changed, 41 insertions(+), 42 deletions(-) diff --git a/crates/hir-expand/src/builtin_fn_macro.rs b/crates/hir-expand/src/builtin_fn_macro.rs index 4b2f27bd465..903c21c84ee 100644 --- a/crates/hir-expand/src/builtin_fn_macro.rs +++ b/crates/hir-expand/src/builtin_fn_macro.rs @@ -152,6 +152,8 @@ fn line_expand( span: SpanData, ) -> ExpandResult { // dummy implementation for type-checking purposes + // Note that `line!` and `column!` will never be implemented properly, as they are by definition + // not incremental ExpandResult::ok(tt::Subtree { delimiter: tt::Delimiter::dummy_invisible(), token_trees: vec![tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal { diff --git a/crates/hir-expand/src/lib.rs b/crates/hir-expand/src/lib.rs index fe336aa1421..a159cf92a7a 100644 --- a/crates/hir-expand/src/lib.rs +++ b/crates/hir-expand/src/lib.rs @@ -78,6 +78,7 @@ pub enum ExpandError { Mbe(mbe::ExpandError), RecursionOverflowPoisoned, Other(Box>), + ProcMacroPanic(Box>), } impl ExpandError { @@ -100,6 +101,10 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { ExpandError::RecursionOverflowPoisoned => { f.write_str("overflow expanding the original macro") } + ExpandError::ProcMacroPanic(it) => { + f.write_str("proc-macro panicked: ")?; + f.write_str(it) + } ExpandError::Other(it) => f.write_str(it), } } diff --git a/crates/hir-expand/src/proc_macro.rs b/crates/hir-expand/src/proc_macro.rs index ccae4c288e6..de577796831 100644 --- a/crates/hir-expand/src/proc_macro.rs +++ b/crates/hir-expand/src/proc_macro.rs @@ -85,7 +85,7 @@ pub fn expand( ProcMacroExpansionError::System(text) | ProcMacroExpansionError::Panic(text) => ExpandResult::new( tt::Subtree::empty(tt::DelimSpan { open: call_site, close: call_site }), - ExpandError::other(text), + ExpandError::ProcMacroPanic(Box::new(text.into_boxed_str())), ), }, } diff --git a/crates/ide/src/navigation_target.rs b/crates/ide/src/navigation_target.rs index 31f4aad41e5..6cb7d7724d5 100644 --- a/crates/ide/src/navigation_target.rs +++ b/crates/ide/src/navigation_target.rs @@ -736,8 +736,6 @@ fn orig_range_with_focus( .definition_range(db) }; - // FIXME What about include!d things - let value_range = InFile::new(hir_file, value).original_file_range_opt(db); let ((call_site_range, call_site_focus), def_site) = match InFile::new(hir_file, name.syntax()).original_file_range_opt(db) { diff --git a/crates/ide/src/syntax_highlighting.rs b/crates/ide/src/syntax_highlighting.rs index 366a3c969f9..307812156e9 100644 --- a/crates/ide/src/syntax_highlighting.rs +++ b/crates/ide/src/syntax_highlighting.rs @@ -404,13 +404,7 @@ fn item(&self) -> &ast::Item { }) .unwrap() } else { - sema.descend_into_macros_single( - match attr_or_derive_item { - Some(AttrOrDerive::Attr(_)) => DescendPreference::SameKind, - Some(AttrOrDerive::Derive(_)) | None => DescendPreference::None, - }, - token, - ) + sema.descend_into_macros_single(DescendPreference::SameKind, token) }; match token.parent().and_then(ast::NameLike::cast) { // Remap the token into the wrapping single token nodes diff --git a/crates/ide/src/syntax_highlighting/test_data/highlight_macros.html b/crates/ide/src/syntax_highlighting/test_data/highlight_macros.html index b8d38a60fc0..e8b3a38c9e0 100644 --- a/crates/ide/src/syntax_highlighting/test_data/highlight_macros.html +++ b/crates/ide/src/syntax_highlighting/test_data/highlight_macros.html @@ -43,7 +43,9 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd .invalid_escape_sequence { color: #FC5555; text-decoration: wavy underline; } .unresolved_reference { color: #FC5555; text-decoration: wavy underline; } -
proc_macros::mirror! {
+
use proc_macros::{mirror, identity, DeriveIdentity};
+
+mirror! {
     {
         ,i32 :x pub
         ,i32 :y pub
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 75cb6223e0e..84a823363f6 100644
--- a/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html
+++ b/crates/ide/src/syntax_highlighting/test_data/highlight_strings.html
@@ -118,7 +118,7 @@ pre                 { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
     println!("Hello {:+}!", 5);
     println!("{:#x}!", 27);
     println!("Hello {:05}!", 5);
-    println!("Hello {:05}!", -5);
+    println!("Hello {:05}!", -5);
     println!("{:#010x}!", 27);
     println!("Hello {0} is {1:.5}", "x", 0.01);
     println!("Hello {1} is {2:.0$}", 5, "x", 0.01);
diff --git a/crates/ide/src/syntax_highlighting/tests.rs b/crates/ide/src/syntax_highlighting/tests.rs
index fcfd3c92571..afb6c555b4a 100644
--- a/crates/ide/src/syntax_highlighting/tests.rs
+++ b/crates/ide/src/syntax_highlighting/tests.rs
@@ -47,10 +47,12 @@ fn attributes() {
 fn macros() {
     check_highlighting(
         r#"
-//- proc_macros: mirror
+//- proc_macros: mirror, identity, derive_identity
 //- minicore: fmt, include, concat
 //- /lib.rs crate:lib
-proc_macros::mirror! {
+use proc_macros::{mirror, identity, DeriveIdentity};
+
+mirror! {
     {
         ,i32 :x pub
         ,i32 :y pub
diff --git a/crates/proc-macro-srv/src/dylib.rs b/crates/proc-macro-srv/src/dylib.rs
index 80bce3af1a0..f20e6832f6e 100644
--- a/crates/proc-macro-srv/src/dylib.rs
+++ b/crates/proc-macro-srv/src/dylib.rs
@@ -160,7 +160,7 @@ pub fn expand(
             .inner
             .proc_macros
             .expand(macro_name, macro_body, attributes, def_site, call_site, mixed_site);
-        result.map_err(|e| e.as_str().unwrap_or_else(|| "".to_string()))
+        result.map_err(|e| e.into_string().unwrap_or_default())
     }
 
     pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> {
diff --git a/crates/proc-macro-srv/src/lib.rs b/crates/proc-macro-srv/src/lib.rs
index 790e7936cdc..5b03813b19a 100644
--- a/crates/proc-macro-srv/src/lib.rs
+++ b/crates/proc-macro-srv/src/lib.rs
@@ -160,8 +160,8 @@ pub struct PanicMessage {
 }
 
 impl PanicMessage {
-    pub fn as_str(&self) -> Option {
-        self.message.clone()
+    pub fn into_string(self) -> Option {
+        self.message
     }
 }
 
diff --git a/crates/proc-macro-srv/src/proc_macros.rs b/crates/proc-macro-srv/src/proc_macros.rs
index 4f87fa281b7..716b85d096d 100644
--- a/crates/proc-macro-srv/src/proc_macros.rs
+++ b/crates/proc-macro-srv/src/proc_macros.rs
@@ -1,16 +1,17 @@
 //! Proc macro ABI
 
 use libloading::Library;
+use proc_macro::bridge;
 use proc_macro_api::{msg::TokenId, ProcMacroKind, RustCInfo};
 
 use crate::{dylib::LoadProcMacroDylibError, server::SYMBOL_INTERNER, tt};
 
 pub(crate) struct ProcMacros {
-    exported_macros: Vec,
+    exported_macros: Vec,
 }
 
-impl From for crate::PanicMessage {
-    fn from(p: proc_macro::bridge::PanicMessage) -> Self {
+impl From for crate::PanicMessage {
+    fn from(p: bridge::PanicMessage) -> Self {
         Self { message: p.as_str().map(|s| s.to_string()) }
     }
 }
@@ -31,9 +32,8 @@ pub(crate) fn from_lib(
         info: RustCInfo,
     ) -> Result {
         if info.version_string == crate::RUSTC_VERSION_STRING {
-            let macros = unsafe {
-                lib.get::<&&[proc_macro::bridge::client::ProcMacro]>(symbol_name.as_bytes())
-            }?;
+            let macros =
+                unsafe { lib.get::<&&[bridge::client::ProcMacro]>(symbol_name.as_bytes()) }?;
 
             return Ok(Self { exported_macros: macros.to_vec() });
         }
@@ -57,11 +57,11 @@ pub(crate) fn expand(
 
         for proc_macro in &self.exported_macros {
             match proc_macro {
-                proc_macro::bridge::client::ProcMacro::CustomDerive {
-                    trait_name, client, ..
-                } if *trait_name == macro_name => {
+                bridge::client::ProcMacro::CustomDerive { trait_name, client, .. }
+                    if *trait_name == macro_name =>
+                {
                     let res = client.run(
-                        &proc_macro::bridge::server::SameThread,
+                        &bridge::server::SameThread,
                         crate::server::RustAnalyzer {
                             interner: &SYMBOL_INTERNER,
                             call_site,
@@ -69,17 +69,15 @@ pub(crate) fn expand(
                             mixed_site,
                         },
                         parsed_body,
-                        true,
+                        false,
                     );
                     return res
                         .map(|it| it.into_subtree(call_site))
                         .map_err(crate::PanicMessage::from);
                 }
-                proc_macro::bridge::client::ProcMacro::Bang { name, client }
-                    if *name == macro_name =>
-                {
+                bridge::client::ProcMacro::Bang { name, client } if *name == macro_name => {
                     let res = client.run(
-                        &proc_macro::bridge::server::SameThread,
+                        &bridge::server::SameThread,
                         crate::server::RustAnalyzer {
                             interner: &SYMBOL_INTERNER,
                             call_site,
@@ -87,17 +85,15 @@ pub(crate) fn expand(
                             mixed_site,
                         },
                         parsed_body,
-                        true,
+                        false,
                     );
                     return res
                         .map(|it| it.into_subtree(call_site))
                         .map_err(crate::PanicMessage::from);
                 }
-                proc_macro::bridge::client::ProcMacro::Attr { name, client }
-                    if *name == macro_name =>
-                {
+                bridge::client::ProcMacro::Attr { name, client } if *name == macro_name => {
                     let res = client.run(
-                        &proc_macro::bridge::server::SameThread,
+                        &bridge::server::SameThread,
                         crate::server::RustAnalyzer {
                             interner: &SYMBOL_INTERNER,
 
@@ -107,7 +103,7 @@ pub(crate) fn expand(
                         },
                         parsed_attributes,
                         parsed_body,
-                        true,
+                        false,
                     );
                     return res
                         .map(|it| it.into_subtree(call_site))
@@ -117,20 +113,20 @@ pub(crate) fn expand(
             }
         }
 
-        Err(proc_macro::bridge::PanicMessage::String("Nothing to expand".to_string()).into())
+        Err(bridge::PanicMessage::String("Nothing to expand".to_string()).into())
     }
 
     pub(crate) fn list_macros(&self) -> Vec<(String, ProcMacroKind)> {
         self.exported_macros
             .iter()
             .map(|proc_macro| match proc_macro {
-                proc_macro::bridge::client::ProcMacro::CustomDerive { trait_name, .. } => {
+                bridge::client::ProcMacro::CustomDerive { trait_name, .. } => {
                     (trait_name.to_string(), ProcMacroKind::CustomDerive)
                 }
-                proc_macro::bridge::client::ProcMacro::Bang { name, .. } => {
+                bridge::client::ProcMacro::Bang { name, .. } => {
                     (name.to_string(), ProcMacroKind::FuncLike)
                 }
-                proc_macro::bridge::client::ProcMacro::Attr { name, .. } => {
+                bridge::client::ProcMacro::Attr { name, .. } => {
                     (name.to_string(), ProcMacroKind::Attr)
                 }
             })