Skip to content

Commit aaff81e

Browse files
committed
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 aaff81e

File tree

283 files changed

+40
-713
lines changed

Some content is hidden

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

283 files changed

+40
-713
lines changed

compiler/rustc_errors/src/emitter.rs

+11-4
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

+17-4
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
}

compiler/rustc_span/src/hygiene.rs

+8
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,11 @@ 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+
/// Whether this macro expansion came from a compiler implemented macro (`rustc_builtin_macro`)
986+
/// or a macro by example provided by the standard library (marked with
987+
/// `rustc_diagnostic_item`). If this is true, then we do not display the standard note telling
988+
/// people to use the `-Zmacro-backtrace` flag.
989+
pub builtin_macro: bool,
985990
}
986991

987992
impl !PartialEq for ExpnData {}
@@ -1000,6 +1005,7 @@ impl ExpnData {
10001005
allow_internal_unsafe: bool,
10011006
local_inner_macros: bool,
10021007
collapse_debuginfo: bool,
1008+
builtin_macro: bool,
10031009
) -> ExpnData {
10041010
ExpnData {
10051011
kind,
@@ -1014,6 +1020,7 @@ impl ExpnData {
10141020
allow_internal_unsafe,
10151021
local_inner_macros,
10161022
collapse_debuginfo,
1023+
builtin_macro,
10171024
}
10181025
}
10191026

@@ -1038,6 +1045,7 @@ impl ExpnData {
10381045
allow_internal_unsafe: false,
10391046
local_inner_macros: false,
10401047
collapse_debuginfo: false,
1048+
builtin_macro: false,
10411049
}
10421050
}
10431051

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

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

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

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

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

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

-3
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/recursive_format_impl.stderr

-18
Original file line numberDiff line numberDiff line change
@@ -12,72 +12,54 @@ error: using `self` as `Display` in `impl Display` will cause infinite recursion
1212
|
1313
LL | write!(f, "{}", self)
1414
| ^^^^^^^^^^^^^^^^^^^^^
15-
|
16-
= note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)
1715

1816
error: using `self` as `Display` in `impl Display` will cause infinite recursion
1917
--> tests/ui/recursive_format_impl.rs:86:9
2018
|
2119
LL | write!(f, "{}", &self)
2220
| ^^^^^^^^^^^^^^^^^^^^^^
23-
|
24-
= note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)
2521

2622
error: using `self` as `Debug` in `impl Debug` will cause infinite recursion
2723
--> tests/ui/recursive_format_impl.rs:93:9
2824
|
2925
LL | write!(f, "{:?}", &self)
3026
| ^^^^^^^^^^^^^^^^^^^^^^^^
31-
|
32-
= note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)
3327

3428
error: using `self` as `Display` in `impl Display` will cause infinite recursion
3529
--> tests/ui/recursive_format_impl.rs:103:9
3630
|
3731
LL | write!(f, "{}", &&&self)
3832
| ^^^^^^^^^^^^^^^^^^^^^^^^
39-
|
40-
= note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)
4133

4234
error: using `self` as `Display` in `impl Display` will cause infinite recursion
4335
--> tests/ui/recursive_format_impl.rs:178:9
4436
|
4537
LL | write!(f, "{}", &*self)
4638
| ^^^^^^^^^^^^^^^^^^^^^^^
47-
|
48-
= note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)
4939

5040
error: using `self` as `Debug` in `impl Debug` will cause infinite recursion
5141
--> tests/ui/recursive_format_impl.rs:185:9
5242
|
5343
LL | write!(f, "{:?}", &*self)
5444
| ^^^^^^^^^^^^^^^^^^^^^^^^^
55-
|
56-
= note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)
5745

5846
error: using `self` as `Display` in `impl Display` will cause infinite recursion
5947
--> tests/ui/recursive_format_impl.rs:202:9
6048
|
6149
LL | write!(f, "{}", *self)
6250
| ^^^^^^^^^^^^^^^^^^^^^^
63-
|
64-
= note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)
6551

6652
error: using `self` as `Display` in `impl Display` will cause infinite recursion
6753
--> tests/ui/recursive_format_impl.rs:219:9
6854
|
6955
LL | write!(f, "{}", **&&*self)
7056
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
71-
|
72-
= note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)
7357

7458
error: using `self` as `Display` in `impl Display` will cause infinite recursion
7559
--> tests/ui/recursive_format_impl.rs:236:9
7660
|
7761
LL | write!(f, "{}", &&**&&*self)
7862
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
79-
|
80-
= note: this error originates in the macro `write` (in Nightly builds, run with -Z macro-backtrace for more info)
8163

8264
error: aborting due to 10 previous errors
8365

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

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

tests/run-make/non-unicode-env/non_unicode_env.stderr

-4
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@ error: environment variable `NON_UNICODE_VAR` is not a valid Unicode string
33
|
44
2 | let _ = env!("NON_UNICODE_VAR");
55
| ^^^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
= note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)
86

97
error: environment variable `NON_UNICODE_VAR` is not a valid Unicode string
108
--> non_unicode_env.rs:3:13
119
|
1210
3 | let _ = option_env!("NON_UNICODE_VAR");
1311
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14-
|
15-
= note: this error originates in the macro `option_env` (in Nightly builds, run with -Z macro-backtrace for more info)
1612

1713
error: aborting due to 2 previous errors
1814

tests/rustdoc-ui/doctest/relative-path-include-bytes-132203.edition2015.stdout

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ error: couldn't read `$DIR/relative-dir-empty-file`: $FILE_NOT_FOUND_MSG (os err
1010
|
1111
LL | let x = include_bytes!("relative-dir-empty-file");
1212
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13-
|
14-
= note: this error originates in the macro `include_bytes` (in Nightly builds, run with -Z macro-backtrace for more info)
1513

1614
error: aborting due to 1 previous error
1715

tests/ui/alloc-error/alloc-error-handler-bad-signature-1.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ LL | fn oom(
1818
| ^^^
1919
LL | info: &Layout,
2020
| -------------
21-
= note: this error originates in the attribute macro `alloc_error_handler` (in Nightly builds, run with -Z macro-backtrace for more info)
2221

2322
error[E0308]: mismatched types
2423
--> $DIR/alloc-error-handler-bad-signature-1.rs:10:1
@@ -35,7 +34,6 @@ LL | | }
3534
|
3635
= note: expected type `!`
3736
found unit type `()`
38-
= note: this error originates in the attribute macro `alloc_error_handler` (in Nightly builds, run with -Z macro-backtrace for more info)
3937

4038
error: aborting due to 2 previous errors
4139

tests/ui/alloc-error/alloc-error-handler-bad-signature-2.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ LL | fn oom(
2626
| ^^^
2727
LL | info: Layout,
2828
| ------------
29-
= note: this error originates in the attribute macro `alloc_error_handler` (in Nightly builds, run with -Z macro-backtrace for more info)
3029

3130
error[E0308]: mismatched types
3231
--> $DIR/alloc-error-handler-bad-signature-2.rs:10:1
@@ -43,7 +42,6 @@ LL | | }
4342
|
4443
= note: expected type `!`
4544
found unit type `()`
46-
= note: this error originates in the attribute macro `alloc_error_handler` (in Nightly builds, run with -Z macro-backtrace for more info)
4745

4846
error: aborting due to 2 previous errors
4947

tests/ui/alloc-error/alloc-error-handler-bad-signature-3.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ note: function defined here
1414
|
1515
LL | fn oom() -> ! {
1616
| ^^^
17-
= note: this error originates in the attribute macro `alloc_error_handler` (in Nightly builds, run with -Z macro-backtrace for more info)
1817

1918
error: aborting due to 1 previous error
2019

0 commit comments

Comments
 (0)