Auto merge of #16037 - Veykril:proc-macro-backtrace, r=Veykril

fix: Don't print proc-macro panic backtraces in the logs

Fixes https://github.com/rust-lang/rust-analyzer/issues/9859
This commit is contained in:
bors 2023-12-06 17:02:17 +00:00
commit 7e8a3391bb
11 changed files with 41 additions and 42 deletions

View File

@ -152,6 +152,8 @@ fn line_expand(
span: SpanData,
) -> ExpandResult<tt::Subtree> {
// 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 {

View File

@ -78,6 +78,7 @@ pub enum ExpandError {
Mbe(mbe::ExpandError),
RecursionOverflowPoisoned,
Other(Box<Box<str>>),
ProcMacroPanic(Box<Box<str>>),
}
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),
}
}

View File

@ -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())),
),
},
}

View File

@ -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) {

View File

@ -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

View File

@ -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; }
</style>
<pre><code><span class="module crate_root library">proc_macros</span><span class="operator">::</span><span class="macro library">mirror</span><span class="macro_bang">!</span> <span class="brace macro">{</span>
<pre><code><span class="keyword">use</span> <span class="module crate_root library">proc_macros</span><span class="operator">::</span><span class="brace">{</span><span class="function library">mirror</span><span class="comma">,</span> <span class="function library">identity</span><span class="comma">,</span> <span class="derive library">DeriveIdentity</span><span class="brace">}</span><span class="semicolon">;</span>
<span class="macro library">mirror</span><span class="macro_bang">!</span> <span class="brace macro">{</span>
<span class="brace macro">{</span>
<span class="comma macro">,</span><span class="builtin_type macro">i32</span> <span class="colon macro">:</span><span class="field declaration macro public">x</span> <span class="keyword macro">pub</span>
<span class="comma macro">,</span><span class="builtin_type macro">i32</span> <span class="colon macro">:</span><span class="field declaration macro public">y</span> <span class="keyword macro">pub</span>

View File

@ -118,7 +118,7 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="macro">println</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="string_literal macro">"Hello </span><span class="format_specifier">{</span><span class="format_specifier">:</span><span class="format_specifier">+</span><span class="format_specifier">}</span><span class="string_literal macro">!"</span><span class="comma macro">,</span> <span class="numeric_literal macro">5</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
<span class="macro">println</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="string_literal macro">"</span><span class="format_specifier">{</span><span class="format_specifier">:</span><span class="format_specifier">#</span><span class="variable">x</span><span class="format_specifier">}</span><span class="string_literal macro">!"</span><span class="comma macro">,</span> <span class="numeric_literal macro">27</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
<span class="macro">println</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="string_literal macro">"Hello </span><span class="format_specifier">{</span><span class="format_specifier">:</span><span class="numeric_literal">0</span><span class="numeric_literal">5</span><span class="format_specifier">}</span><span class="string_literal macro">!"</span><span class="comma macro">,</span> <span class="numeric_literal macro">5</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
<span class="macro">println</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="string_literal macro">"Hello </span><span class="format_specifier">{</span><span class="format_specifier">:</span><span class="numeric_literal">0</span><span class="numeric_literal">5</span><span class="format_specifier">}</span><span class="string_literal macro">!"</span>, <span class="numeric_literal macro">-</span><span class="numeric_literal macro">5</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
<span class="macro">println</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="string_literal macro">"Hello </span><span class="format_specifier">{</span><span class="format_specifier">:</span><span class="numeric_literal">0</span><span class="numeric_literal">5</span><span class="format_specifier">}</span><span class="string_literal macro">!"</span><span class="comma macro">,</span> <span class="numeric_literal macro">-</span><span class="numeric_literal macro">5</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
<span class="macro">println</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="string_literal macro">"</span><span class="format_specifier">{</span><span class="format_specifier">:</span><span class="format_specifier">#</span><span class="numeric_literal">0</span><span class="numeric_literal">10</span><span class="variable">x</span><span class="format_specifier">}</span><span class="string_literal macro">!"</span><span class="comma macro">,</span> <span class="numeric_literal macro">27</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
<span class="macro">println</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="string_literal macro">"Hello </span><span class="format_specifier">{</span><span class="numeric_literal">0</span><span class="format_specifier">}</span><span class="string_literal macro"> is </span><span class="format_specifier">{</span><span class="numeric_literal">1</span><span class="format_specifier">:</span><span class="format_specifier">.</span><span class="numeric_literal">5</span><span class="format_specifier">}</span><span class="string_literal macro">"</span><span class="comma macro">,</span> <span class="string_literal macro">"x"</span><span class="comma macro">,</span> <span class="numeric_literal macro">0.01</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>
<span class="macro">println</span><span class="macro_bang">!</span><span class="parenthesis macro">(</span><span class="string_literal macro">"Hello </span><span class="format_specifier">{</span><span class="numeric_literal">1</span><span class="format_specifier">}</span><span class="string_literal macro"> is </span><span class="format_specifier">{</span><span class="numeric_literal">2</span><span class="format_specifier">:</span><span class="format_specifier">.</span><span class="numeric_literal">0</span><span class="format_specifier">$</span><span class="format_specifier">}</span><span class="string_literal macro">"</span><span class="comma macro">,</span> <span class="numeric_literal macro">5</span><span class="comma macro">,</span> <span class="string_literal macro">"x"</span><span class="comma macro">,</span> <span class="numeric_literal macro">0.01</span><span class="parenthesis macro">)</span><span class="semicolon">;</span>

View File

@ -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

View File

@ -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(|| "<unknown error>".to_string()))
result.map_err(|e| e.into_string().unwrap_or_default())
}
pub fn list_macros(&self) -> Vec<(String, ProcMacroKind)> {

View File

@ -160,8 +160,8 @@ pub struct PanicMessage {
}
impl PanicMessage {
pub fn as_str(&self) -> Option<String> {
self.message.clone()
pub fn into_string(self) -> Option<String> {
self.message
}
}

View File

@ -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<proc_macro::bridge::client::ProcMacro>,
exported_macros: Vec<bridge::client::ProcMacro>,
}
impl From<proc_macro::bridge::PanicMessage> for crate::PanicMessage {
fn from(p: proc_macro::bridge::PanicMessage) -> Self {
impl From<bridge::PanicMessage> 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<ProcMacros, LoadProcMacroDylibError> {
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)
}
})