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 70fc288

Browse files
committedMar 11, 2025·
Do not suggest using -Zmacro-backtrace for builtin macros
For macros that are implemented on the compiler, or that are annotated with `rustc_diagnostic_item`, which have arbitrary implementations from the point of view of the user and might as well be intrinsics, we do *not* mention the `-Zmacro-backtrace` flag. This includes `derive`s and standard macros like `panic!` and `format!`.
1 parent 385970f commit 70fc288

File tree

276 files changed

+41
-679
lines changed

Some content is hidden

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

276 files changed

+41
-679
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.builtin_macro))
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: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,11 @@ 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 either a compiler intrinsic (implied by the above field) or a macro
761+
/// provided by the standard library where the specific of its implementation are not relevant
762+
/// to the end user, like for `format!` or `println!`, which are normal macros by example, but
763+
/// that is an implementation detail.
764+
pub builtin: bool,
760765
/// Suppresses the `unsafe_code` lint for code produced by this macro.
761766
pub allow_internal_unsafe: bool,
762767
/// Enables the macro helper hack (`ident!(...)` -> `$crate::ident!(...)`) for this macro.
@@ -792,6 +797,7 @@ impl SyntaxExtension {
792797
helper_attrs: Vec::new(),
793798
edition,
794799
builtin_name: None,
800+
builtin: false,
795801
kind,
796802
allow_internal_unsafe: false,
797803
local_inner_macros: false,
@@ -888,17 +894,19 @@ impl SyntaxExtension {
888894
)
889895
})
890896
.unwrap_or_else(|| (None, helper_attrs));
897+
let builtin = builtin_name.is_some()
898+
|| ast::attr::find_by_name(attrs, sym::rustc_diagnostic_item).is_some();
891899

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

894902
// 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) {
903+
if let Some(sp) = find_attr!(attrs, AttributeKind::ConstStability { span, .. } => *span) {
896904
sess.dcx().emit_err(errors::MacroConstStability {
897905
span: sp,
898906
head_span: sess.source_map().guess_head_span(span),
899907
});
900908
}
901-
if let Some(sp) = find_attr!(attrs, AttributeKind::BodyStability{span, ..} => *span) {
909+
if let Some(sp) = find_attr!(attrs, AttributeKind::BodyStability{ span, .. } => *span) {
902910
sess.dcx().emit_err(errors::MacroBodyStability {
903911
span: sp,
904912
head_span: sess.source_map().guess_head_span(span),
@@ -912,10 +920,14 @@ impl SyntaxExtension {
912920
// FIXME(jdonszelmann): avoid the into_iter/collect?
913921
.then(|| allow_internal_unstable.iter().map(|i| i.0).collect::<Vec<_>>().into()),
914922
stability,
915-
deprecation: find_attr!(attrs, AttributeKind::Deprecation{deprecation, ..} => *deprecation),
923+
deprecation: find_attr!(
924+
attrs,
925+
AttributeKind::Deprecation { deprecation, .. } => *deprecation
926+
),
916927
helper_attrs,
917928
edition,
918929
builtin_name,
930+
builtin,
919931
allow_internal_unsafe,
920932
local_inner_macros,
921933
collapse_debuginfo,
@@ -1000,6 +1012,7 @@ impl SyntaxExtension {
10001012
self.allow_internal_unsafe,
10011013
self.local_inner_macros,
10021014
self.collapse_debuginfo,
1015+
self.builtin,
10031016
)
10041017
}
10051018
}

0 commit comments

Comments
 (0)
Please sign in to comment.