Skip to content

Commit adea7cb

Browse files
committed
Auto merge of rust-lang#138379 - estebank:macro-backtrace-note, r=petrochenkov
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!`. This PR adds a field to every `Span`'s `ExpnData` stating whether it comes from a builtin macro. This is determined by the macro being annotated with either `#[rustc_builtin_macro]` or `#[rustc_diagnostic_item]`. An alternative to using these attributes that already exist for other uses would be to introduce another attribute like `#[rustc_no_backtrace]` to have finer control on which macros are affected (for example, an error within `vec![]` now doesn't mention the backtrace, but one could make the case that it should). Ideally, instead of carrying this information in the `ExpnData` we'd instead try to query the `DefId` of the macro (that is already stored) to see if it is annotated in some way, but we do not have access to the `TyCtxt` from `rustc_errors`. r? `@petrochenkov`
2 parents 2828650 + f0b8e13 commit adea7cb

File tree

205 files changed

+27
-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

+27
-495
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 10 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,17 @@ 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!`.
316+
if let Some((macro_kind, name, _)) = has_macro_spans.first()
317+
&& let Some((_, _, false)) = has_macro_spans.last()
318+
{
313319
// Mark the actual macro this originates from
314-
let and_then = if let Some((macro_kind, last_name)) = has_macro_spans.last()
320+
let and_then = if let Some((macro_kind, last_name, _)) = has_macro_spans.last()
315321
&& last_name != name
316322
{
317323
let descr = macro_kind.descr();
318-
format!(" which comes from the expansion of the {descr} `{last_name}`",)
324+
format!(" which comes from the expansion of the {descr} `{last_name}`")
319325
} else {
320326
"".to_string()
321327
};

compiler/rustc_expand/src/base.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -889,16 +889,16 @@ impl SyntaxExtension {
889889
})
890890
.unwrap_or_else(|| (None, helper_attrs));
891891

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

894894
// 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) {
895+
if let Some(sp) = find_attr!(attrs, AttributeKind::ConstStability { span, .. } => *span) {
896896
sess.dcx().emit_err(errors::MacroConstStability {
897897
span: sp,
898898
head_span: sess.source_map().guess_head_span(span),
899899
});
900900
}
901-
if let Some(sp) = find_attr!(attrs, AttributeKind::BodyStability{span, ..} => *span) {
901+
if let Some(sp) = find_attr!(attrs, AttributeKind::BodyStability{ span, .. } => *span) {
902902
sess.dcx().emit_err(errors::MacroBodyStability {
903903
span: sp,
904904
head_span: sess.source_map().guess_head_span(span),
@@ -912,7 +912,10 @@ impl SyntaxExtension {
912912
// FIXME(jdonszelmann): avoid the into_iter/collect?
913913
.then(|| allow_internal_unstable.iter().map(|i| i.0).collect::<Vec<_>>().into()),
914914
stability,
915-
deprecation: find_attr!(attrs, AttributeKind::Deprecation{deprecation, ..} => *deprecation),
915+
deprecation: find_attr!(
916+
attrs,
917+
AttributeKind::Deprecation { deprecation, .. } => *deprecation
918+
),
916919
helper_attrs,
917920
edition,
918921
builtin_name,
@@ -1000,6 +1003,7 @@ impl SyntaxExtension {
10001003
self.allow_internal_unsafe,
10011004
self.local_inner_macros,
10021005
self.collapse_debuginfo,
1006+
self.builtin_name.is_some(),
10031007
)
10041008
}
10051009
}

compiler/rustc_span/src/hygiene.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,8 @@ pub struct ExpnData {
982982
/// Should debuginfo for the macro be collapsed to the outermost expansion site (in other
983983
/// words, was the macro definition annotated with `#[collapse_debuginfo]`)?
984984
pub(crate) collapse_debuginfo: bool,
985+
/// When true, we do not display the note telling people to use the `-Zmacro-backtrace` flag.
986+
pub hide_backtrace: bool,
985987
}
986988

987989
impl !PartialEq for ExpnData {}
@@ -1000,6 +1002,7 @@ impl ExpnData {
10001002
allow_internal_unsafe: bool,
10011003
local_inner_macros: bool,
10021004
collapse_debuginfo: bool,
1005+
hide_backtrace: bool,
10031006
) -> ExpnData {
10041007
ExpnData {
10051008
kind,
@@ -1014,6 +1017,7 @@ impl ExpnData {
10141017
allow_internal_unsafe,
10151018
local_inner_macros,
10161019
collapse_debuginfo,
1020+
hide_backtrace,
10171021
}
10181022
}
10191023

@@ -1038,6 +1042,7 @@ impl ExpnData {
10381042
allow_internal_unsafe: false,
10391043
local_inner_macros: false,
10401044
collapse_debuginfo: false,
1045+
hide_backtrace: false,
10411046
}
10421047
}
10431048

src/tools/clippy/tests/ui/derive_ord_xor_partial_ord.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ LL | impl PartialOrd for DeriveOrd {
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212
= note: `-D clippy::derive-ord-xor-partial-ord` implied by `-D warnings`
1313
= help: to override `-D warnings` add `#[allow(clippy::derive_ord_xor_partial_ord)]`
14-
= note: this error originates in the derive macro `Ord` (in Nightly builds, run with -Z macro-backtrace for more info)
1514

1615
error: you are deriving `Ord` but have implemented `PartialOrd` explicitly
1716
--> tests/ui/derive_ord_xor_partial_ord.rs:33:10
@@ -24,7 +23,6 @@ note: `PartialOrd` implemented here
2423
|
2524
LL | impl PartialOrd<DeriveOrdWithExplicitTypeVariable> for DeriveOrdWithExplicitTypeVariable {
2625
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
27-
= note: this error originates in the derive macro `Ord` (in Nightly builds, run with -Z macro-backtrace for more info)
2826

2927
error: you are implementing `Ord` explicitly but have derived `PartialOrd`
3028
--> tests/ui/derive_ord_xor_partial_ord.rs:47:1
@@ -42,7 +40,6 @@ note: `PartialOrd` implemented here
4240
|
4341
LL | #[derive(PartialOrd, PartialEq, Eq)]
4442
| ^^^^^^^^^^
45-
= note: this error originates in the derive macro `PartialOrd` (in Nightly builds, run with -Z macro-backtrace for more info)
4643

4744
error: you are implementing `Ord` explicitly but have derived `PartialOrd`
4845
--> tests/ui/derive_ord_xor_partial_ord.rs:69:5
@@ -60,7 +57,6 @@ note: `PartialOrd` implemented here
6057
|
6158
LL | #[derive(PartialOrd, PartialEq, Eq)]
6259
| ^^^^^^^^^^
63-
= note: this error originates in the derive macro `PartialOrd` (in Nightly builds, run with -Z macro-backtrace for more info)
6460

6561
error: aborting due to 4 previous errors
6662

src/tools/clippy/tests/ui/derived_hash_with_manual_eq.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ note: `PartialEq` implemented here
1010
LL | impl PartialEq for Bar {
1111
| ^^^^^^^^^^^^^^^^^^^^^^
1212
= note: `#[deny(clippy::derived_hash_with_manual_eq)]` on by default
13-
= note: this error originates in the derive macro `Hash` (in Nightly builds, run with -Z macro-backtrace for more info)
1413

1514
error: you are deriving `Hash` but have implemented `PartialEq` explicitly
1615
--> tests/ui/derived_hash_with_manual_eq.rs:23:10
@@ -23,7 +22,6 @@ note: `PartialEq` implemented here
2322
|
2423
LL | impl PartialEq<Baz> for Baz {
2524
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
26-
= note: this error originates in the derive macro `Hash` (in Nightly builds, run with -Z macro-backtrace for more info)
2725

2826
error: aborting due to 2 previous errors
2927

src/tools/clippy/tests/ui/diverging_sub_expression.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ error: sub-expression diverges
3636
|
3737
LL | _ => true || panic!("boo"),
3838
| ^^^^^^^^^^^^^
39-
|
40-
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
4139

4240
error: sub-expression diverges
4341
--> tests/ui/diverging_sub_expression.rs:52:29

src/tools/clippy/tests/ui/fallible_impl_from.stderr

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ note: potential failure(s)
3838
|
3939
LL | panic!();
4040
| ^^^^^^^^
41-
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
4241

4342
error: consider implementing `TryFrom` instead
4443
--> tests/ui/fallible_impl_from.rs:40:1
@@ -64,7 +63,6 @@ LL | } else if s.parse::<u32>().unwrap() != 42 {
6463
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6564
LL | panic!("{:?}", s);
6665
| ^^^^^^^^^^^^^^^^^
67-
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
6866

6967
error: consider implementing `TryFrom` instead
7068
--> tests/ui/fallible_impl_from.rs:60:1
@@ -85,7 +83,6 @@ LL | if s.parse::<u32>().ok().unwrap() != 42 {
8583
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8684
LL | panic!("{:?}", s);
8785
| ^^^^^^^^^^^^^^^^^
88-
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
8986

9087
error: aborting due to 4 previous errors
9188

src/tools/clippy/tests/ui/impl_hash_with_borrow_str_and_bytes.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ LL | #[derive(Hash)]
2323
= note: ... as (`hash("abc") != hash("abc".as_bytes())`
2424
= help: consider either removing one of the `Borrow` implementations (`Borrow<str>` or `Borrow<[u8]>`) ...
2525
= help: ... or not implementing `Hash` for this type
26-
= note: this error originates in the derive macro `Hash` (in Nightly builds, run with -Z macro-backtrace for more info)
2726

2827
error: the semantics of `Borrow<T>` around `Hash` can't be satisfied when both `Borrow<str>` and `Borrow<[u8]>` are implemented
2928
--> tests/ui/impl_hash_with_borrow_str_and_bytes.rs:117:6

src/tools/clippy/tests/ui/issue-7447.stderr

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ LL | byte_view(panic!());
66
|
77
= note: `-D clippy::diverging-sub-expression` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::diverging_sub_expression)]`
9-
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
109

1110
error: sub-expression diverges
1211
--> tests/ui/issue-7447.rs:29:19
1312
|
1413
LL | group_entries(panic!());
1514
| ^^^^^^^^
16-
|
17-
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)
1815

1916
error: aborting due to 2 previous errors
2017

src/tools/clippy/tests/ui/same_name_method.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ note: existing `clone` defined here
2323
|
2424
LL | #[derive(Clone)]
2525
| ^^^^^
26-
= note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
2726

2827
error: method's name is the same as an existing method in a trait
2928
--> tests/ui/same_name_method.rs:46:13

0 commit comments

Comments
 (0)