Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c37a10c

Browse files
committedMar 14, 2025·
Do not suggest using -Zmacro-backtrace for builtin macros
For macros that are implemented on the compiler, we do *not* mention the `-Zmacro-backtrace` flag. This includes `derive`s and standard macros.
1 parent f7b4354 commit c37a10c

File tree

205 files changed

+37
-495
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

205 files changed

+37
-495
lines changed
 

‎compiler/rustc_errors/src/emitter.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,9 @@ pub trait Emitter: Translate {
297297
// are some which do actually involve macros.
298298
ExpnKind::Desugaring(..) | ExpnKind::AstPass(..) => None,
299299

300-
ExpnKind::Macro(macro_kind, name) => Some((macro_kind, name)),
300+
ExpnKind::Macro(macro_kind, name) => {
301+
Some((macro_kind, name, expn_data.hide_backtrace))
302+
}
301303
}
302304
})
303305
.collect();
@@ -309,13 +311,18 @@ pub trait Emitter: Translate {
309311
self.render_multispans_macro_backtrace(span, children, backtrace);
310312

311313
if !backtrace {
312-
if let Some((macro_kind, name)) = has_macro_spans.first() {
314+
// Skip builtin macros, as their expansion isn't relevant to the end user. This includes
315+
// actual intrinsics, like `asm!`, as well as macros by example, like `println!`, which
316+
// might as well be an intrinsic as far as the user is concerned.
317+
if let Some((macro_kind, name, _)) = has_macro_spans.first()
318+
&& let Some((_, _, false)) = has_macro_spans.last()
319+
{
313320
// Mark the actual macro this originates from
314-
let and_then = if let Some((macro_kind, last_name)) = has_macro_spans.last()
321+
let and_then = if let Some((macro_kind, last_name, _)) = has_macro_spans.last()
315322
&& last_name != name
316323
{
317324
let descr = macro_kind.descr();
318-
format!(" which comes from the expansion of the {descr} `{last_name}`",)
325+
format!(" which comes from the expansion of the {descr} `{last_name}`")
319326
} else {
320327
"".to_string()
321328
};

‎compiler/rustc_expand/src/base.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,10 @@ pub struct SyntaxExtension {
757757
/// Built-in macros have a couple of special properties like availability
758758
/// in `#[no_implicit_prelude]` modules, so we have to keep this flag.
759759
pub builtin_name: Option<Symbol>,
760+
/// Whether this macro is a compiler intrinsic (implied by the above field) or a macro
761+
/// explicitly annotated to not display the `-Zmacro-backtrace` note when errors come from a
762+
/// macro.
763+
pub hide_backtrace: bool,
760764
/// Suppresses the `unsafe_code` lint for code produced by this macro.
761765
pub allow_internal_unsafe: bool,
762766
/// Enables the macro helper hack (`ident!(...)` -> `$crate::ident!(...)`) for this macro.
@@ -792,6 +796,7 @@ impl SyntaxExtension {
792796
helper_attrs: Vec::new(),
793797
edition,
794798
builtin_name: None,
799+
hide_backtrace: false,
795800
kind,
796801
allow_internal_unsafe: false,
797802
local_inner_macros: false,
@@ -888,17 +893,18 @@ impl SyntaxExtension {
888893
)
889894
})
890895
.unwrap_or_else(|| (None, helper_attrs));
896+
let hide_backtrace = builtin_name.is_some();
891897

892-
let stability = find_attr!(attrs, AttributeKind::Stability{stability, ..} => *stability);
898+
let stability = find_attr!(attrs, AttributeKind::Stability { stability, .. } => *stability);
893899

894900
// FIXME(jdonszelmann): make it impossible to miss the or_else in the typesystem
895-
if let Some(sp) = find_attr!(attrs, AttributeKind::ConstStability{span, ..} => *span) {
901+
if let Some(sp) = find_attr!(attrs, AttributeKind::ConstStability { span, .. } => *span) {
896902
sess.dcx().emit_err(errors::MacroConstStability {
897903
span: sp,
898904
head_span: sess.source_map().guess_head_span(span),
899905
});
900906
}
901-
if let Some(sp) = find_attr!(attrs, AttributeKind::BodyStability{span, ..} => *span) {
907+
if let Some(sp) = find_attr!(attrs, AttributeKind::BodyStability{ span, .. } => *span) {
902908
sess.dcx().emit_err(errors::MacroBodyStability {
903909
span: sp,
904910
head_span: sess.source_map().guess_head_span(span),
@@ -912,10 +918,14 @@ impl SyntaxExtension {
912918
// FIXME(jdonszelmann): avoid the into_iter/collect?
913919
.then(|| allow_internal_unstable.iter().map(|i| i.0).collect::<Vec<_>>().into()),
914920
stability,
915-
deprecation: find_attr!(attrs, AttributeKind::Deprecation{deprecation, ..} => *deprecation),
921+
deprecation: find_attr!(
922+
attrs,
923+
AttributeKind::Deprecation { deprecation, .. } => *deprecation
924+
),
916925
helper_attrs,
917926
edition,
918927
builtin_name,
928+
hide_backtrace,
919929
allow_internal_unsafe,
920930
local_inner_macros,
921931
collapse_debuginfo,
@@ -1000,6 +1010,7 @@ impl SyntaxExtension {
10001010
self.allow_internal_unsafe,
10011011
self.local_inner_macros,
10021012
self.collapse_debuginfo,
1013+
self.hide_backtrace,
10031014
)
10041015
}
10051016
}

0 commit comments

Comments
 (0)
Please sign in to comment.