Skip to content

diagnostics: hide expansion of builtin-like macros #141314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use rustc_lexer;
use rustc_lint_defs::pluralize;
use rustc_span::hygiene::{ExpnKind, MacroKind};
use rustc_span::source_map::SourceMap;
use rustc_span::{FileLines, FileName, SourceFile, Span, char_width, str_width};
use rustc_span::{FileLines, FileName, SourceFile, Span, char_width, str_width, sym};
use termcolor::{Buffer, BufferWriter, Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
use tracing::{debug, instrument, trace, warn};

Expand Down Expand Up @@ -298,7 +298,17 @@ pub trait Emitter: Translate {
ExpnKind::Desugaring(..) | ExpnKind::AstPass(..) => None,

ExpnKind::Macro(macro_kind, name) => {
Some((macro_kind, name, expn_data.hide_backtrace))
// Don't recommend using `-Zmacro-backtrace` if
// - hide_backtrace is true, which it is for builtin macros
// - if the macro uses internal features so their expansion is unlikely to be useful
let hide_macro_backtrace_suggestion = expn_data.hide_backtrace
|| expn_data.allow_internal_unstable.is_some_and(|aiu| {
aiu.contains(&sym::fmt_internals)
|| aiu.contains(&sym::print_internals)
|| aiu.contains(&sym::liballoc_internals)
});

Some((macro_kind, name, hide_macro_backtrace_suggestion))
}
}
})
Expand All @@ -311,8 +321,9 @@ pub trait Emitter: Translate {
self.render_multispans_macro_backtrace(span, children, backtrace);

if !backtrace {
// Skip builtin macros, as their expansion isn't relevant to the end user. This includes
// actual intrinsics, like `asm!`.
// Skip some special macros, as their expansion isn't relevant to the end user.
// This includes actual intrinsics and builtins like `asm!`, as well as macros
// that contain implementation details like the formatting macros
if let Some((macro_kind, name, _)) = has_macro_spans.first()
&& let Some((_, _, false)) = has_macro_spans.last()
{
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,7 @@ symbols! {
fmaf64,
fmt,
fmt_debug,
fmt_internals,
fmul_algebraic,
fmul_fast,
fmuladdf128,
Expand Down Expand Up @@ -1240,6 +1241,7 @@ symbols! {
let_else,
lhs,
lib,
liballoc_internals,
libc,
lifetime,
lifetime_capture_rules_2024,
Expand Down Expand Up @@ -1623,6 +1625,7 @@ symbols! {
prelude_import,
preserves_flags,
prfchw_target_feature,
print_internals,
print_macro,
println_macro,
proc_dash_macro: "proc-macro",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ LL | let v: Vec<u16> = vec![1, 2];
| ^^^^^^^^^^
= note: BACKTRACE (of the first span):
= note: inside `main` at tests/fail/dangling_pointers/out_of_bounds_read.rs:LL:CC
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ LL | let mut v: Vec<u16> = vec![1, 2];
| ^^^^^^^^^^
= note: BACKTRACE (of the first span):
= note: inside `main` at tests/fail/dangling_pointers/out_of_bounds_write.rs:LL:CC
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

Expand Down
2 changes: 0 additions & 2 deletions src/tools/miri/tests/fail/erroneous_const2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ note: erroneous constant encountered
|
LL | println!("{}", FOO);
| ^^^
|
= note: this note originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

Expand Down
1 change: 0 additions & 1 deletion src/tools/miri/tests/fail/intrinsics/simd-scatter.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ LL | let mut vec: Vec<i8> = vec![10, 11, 12, 13, 14, 15, 16, 17, 18];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: BACKTRACE (of the first span):
= note: inside `main` at tests/fail/intrinsics/simd-scatter.rs:LL:CC
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ LL | }
= help: this transition corresponds to a loss of read and write permissions
= note: BACKTRACE (of the first span):
= note: inside `main` at tests/fail/tree_borrows/protector-write-lazy.rs:LL:CC
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ error: macros that expand to items must be delimited with braces or followed by
|
LL | println!();
| ^^^^^^^^^^
|
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error: macro expansion ignores `{` and any tokens following
--> $SRC_DIR/std/src/macros.rs:LL:COL
Expand All @@ -35,8 +33,6 @@ error: macros that expand to items must be delimited with braces or followed by
|
LL | println!();
| ^^^^^^^^^^
|
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error: macro expansion ignores `{` and any tokens following
--> $SRC_DIR/std/src/macros.rs:LL:COL
Expand Down
8 changes: 0 additions & 8 deletions tests/ui/asm/parse-error.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,12 @@ error: asm template must be a string literal
|
LL | asm!(format!("{{{}}}", 0), in(reg) foo);
| ^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does -Zmacro-backtrace still work for builtin-like macros with this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I've added two tests for it.


error: asm template must be a string literal
--> $DIR/parse-error.rs:86:21
|
LL | asm!("{1}", format!("{{{}}}", 0), in(reg) foo, out(reg) bar);
| ^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)

error: _ cannot be used for input operands
--> $DIR/parse-error.rs:88:28
Expand Down Expand Up @@ -357,16 +353,12 @@ error: asm template must be a string literal
|
LL | global_asm!(format!("{{{}}}", 0), const FOO);
| ^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)

error: asm template must be a string literal
--> $DIR/parse-error.rs:143:20
|
LL | global_asm!("{1}", format!("{{{}}}", 0), const FOO, const BAR);
| ^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)

error: the `in` operand cannot be used with `global_asm!`
--> $DIR/parse-error.rs:146:19
Expand Down
1 change: 0 additions & 1 deletion tests/ui/asm/x86_64/type-check-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ LL | asm!("{}", in(reg) vec![0]);
| ^^^^^^^
|
= note: only integers, floats, SIMD vectors, pointers and function pointers can be used as arguments for inline assembly
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error: cannot use value of type `(i32, i32, i32)` for inline assembly
--> $DIR/type-check-2.rs:52:28
Expand Down
1 change: 0 additions & 1 deletion tests/ui/async-await/unreachable-lint-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ note: the lint level is defined here
|
LL | #![deny(unreachable_code)]
| ^^^^^^^^^^^^^^^^
= note: this error originates in the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ LL | test(&vec![])
| argument requires that borrow lasts for `'static`
LL | }
| - temporary value is freed at the end of this statement
|
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

Expand Down
2 changes: 0 additions & 2 deletions tests/ui/borrowck/borrowck-and-init.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ LL | println!("{}", false && { i = 5; true });
| ----- binding initialized here in some conditions
LL | println!("{}", i);
| ^ `i` used here but it is possibly-uninitialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

Expand Down
1 change: 0 additions & 1 deletion tests/ui/borrowck/borrowck-borrowed-uniq-rvalue-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ LL | let x = defer(&vec!["Goodbye", "world!"]);
LL | x.x[0];
| ------ borrow later used here
|
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider using a `let` binding to create a longer lived value
|
LL ~ let binding = vec!["Goodbye", "world!"];
Expand Down
1 change: 0 additions & 1 deletion tests/ui/borrowck/borrowck-break-uninit-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ LL | let x: isize;
LL | println!("{}", x);
| ^ `x` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let x: isize = 42;
Expand Down
1 change: 0 additions & 1 deletion tests/ui/borrowck/borrowck-break-uninit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ LL | let x: isize;
LL | println!("{}", x);
| ^ `x` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let x: isize = 42;
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/borrowck/borrowck-or-init.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ LL | println!("{}", false || { i = 5; true });
| ----- binding initialized here in some conditions
LL | println!("{}", i);
| ^ `i` used here but it is possibly-uninitialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

Expand Down
2 changes: 0 additions & 2 deletions tests/ui/borrowck/borrowck-while-break.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ LL | while cond {
...
LL | println!("{}", v);
| ^ `v` used here but it is possibly-uninitialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

Expand Down
2 changes: 0 additions & 2 deletions tests/ui/borrowck/issue-24267-flow-exit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ LL | loop { x = break; }
LL | println!("{}", x);
| ^ `x` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let x: i32 = 42;
Expand All @@ -22,7 +21,6 @@ LL | for _ in 0..10 { x = continue; }
LL | println!("{}", x);
| ^ `x` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let x: i32 = 42;
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/borrowck/issue-47646.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ LL | println!("{:?}", heap);
...
LL | };
| - ... and the mutable borrow might be used here, when that temporary is dropped and runs the destructor for type `(Option<PeekMut<'_, i32>>, ())`
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

Expand Down
1 change: 0 additions & 1 deletion tests/ui/borrowck/issue-64453.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ LL | static settings_dir: String = format!("");
|
= note: calls in statics are limited to constant functions, tuple structs and tuple variants
= note: consider wrapping this expression in `std::sync::LazyLock::new(|| ...)`
= note: this error originates in the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0507]: cannot move out of static item `settings_dir`
--> $DIR/issue-64453.rs:13:37
Expand Down
10 changes: 0 additions & 10 deletions tests/ui/borrowck/suggest-assign-rvalue.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ LL | let my_float: f32;
LL | println!("my_float: {}", my_float);
| ^^^^^^^^ `my_float` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let my_float: f32 = 3.14159;
Expand All @@ -33,7 +32,6 @@ LL | let demo: Demo;
LL | println!("demo: {:?}", demo);
| ^^^^ `demo` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let demo: Demo = Default::default();
Expand All @@ -47,7 +45,6 @@ LL | let demo_no: DemoNoDef;
LL | println!("demo_no: {:?}", demo_no);
| ^^^^^^^ `demo_no` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let demo_no: DemoNoDef = /* value */;
Expand All @@ -61,7 +58,6 @@ LL | let arr: [i32; 5];
LL | println!("arr: {:?}", arr);
| ^^^ `arr` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let arr: [i32; 5] = [42; 5];
Expand All @@ -75,7 +71,6 @@ LL | let foo: Vec<&str>;
LL | println!("foo: {:?}", foo);
| ^^^ `foo` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let foo: Vec<&str> = vec![];
Expand All @@ -89,7 +84,6 @@ LL | let my_string: String;
LL | println!("my_string: {}", my_string);
| ^^^^^^^^^ `my_string` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let my_string: String = Default::default();
Expand All @@ -103,7 +97,6 @@ LL | let my_int: &i32;
LL | println!("my_int: {}", *my_int);
| ^^^^^^^ `*my_int` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let my_int: &i32 = &42;
Expand All @@ -117,7 +110,6 @@ LL | let hello: &str;
LL | println!("hello: {}", hello);
| ^^^^^ `hello` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider assigning a value
|
LL | let hello: &str = "";
Expand All @@ -130,8 +122,6 @@ LL | let never: !;
| ----- binding declared here but left uninitialized
LL | println!("never: {}", never);
| ^^^^^ `never` used here but it isn't initialized
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 10 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ LL | println!("{}", arr[3]);
...
LL | c();
| - mutable borrow later used here
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0502]: cannot borrow `arr` as immutable because it is also borrowed as mutable
--> $DIR/arrays.rs:71:24
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ LL | println!("{}", e.0.0.m.x);
LL |
LL | c();
| - mutable borrow later used here
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0506]: cannot assign to `e.0.0.m.x` because it is borrowed
--> $DIR/box.rs:55:5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ LL | println!("{}", foo.x);
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses
= note: creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
= help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ LL | println!("{:?}", p);
LL |
LL | c();
| - mutable borrow later used here
|
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

Expand Down
1 change: 0 additions & 1 deletion tests/ui/closures/issue-111932.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ LL | println!("{:?}", foo);
= help: the trait `Sized` is not implemented for `dyn Foo`
note: required by an implicit `Sized` bound in `core::fmt::rt::Argument::<'_>::new_debug`
--> $SRC_DIR/core/src/fmt/rt.rs:LL:COL
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors

Expand Down
2 changes: 0 additions & 2 deletions tests/ui/codemap_tests/bad-format-args.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ error: requires at least a format string argument
|
LL | format!();
| ^^^^^^^^^
|
= note: this error originates in the macro `$crate::__export::format_args` which comes from the expansion of the macro `format` (in Nightly builds, run with -Z macro-backtrace for more info)

error: expected `,`, found `1`
--> $DIR/bad-format-args.rs:3:16
Expand Down
1 change: 0 additions & 1 deletion tests/ui/codemap_tests/tab_3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ LL | println!("{:?}", some_vec);
|
note: `into_iter` takes ownership of the receiver `self`, which moves `some_vec`
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you can `clone` the value and consume it, but this might not be your desired behavior
|
LL | some_vec.clone().into_iter();
Expand Down
Loading
Loading