Skip to content

Reduce verbosity of errors involving builtin macros #106526

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

Closed
wants to merge 1 commit into from
Closed
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
33 changes: 24 additions & 9 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,9 @@ pub trait Emitter: Translate {
// are some which do actually involve macros.
ExpnKind::Inlined | ExpnKind::Desugaring(..) | ExpnKind::AstPass(..) => None,

ExpnKind::Macro(macro_kind, name) => Some((macro_kind, name)),
ExpnKind::Macro(macro_kind, name) => {
Some((macro_kind, name, expn_data.builtin_name))
}
}
})
.collect();
Expand All @@ -345,9 +347,9 @@ pub trait Emitter: Translate {
self.render_multispans_macro_backtrace(span, children, backtrace);

if !backtrace {
if let Some((macro_kind, name)) = has_macro_spans.first() {
if let Some((macro_kind, name, builtin_name)) = has_macro_spans.first() {
// Mark the actual macro this originates from
let and_then = if let Some((macro_kind, last_name)) = has_macro_spans.last()
let and_then = if let Some((macro_kind, last_name, _)) = has_macro_spans.last()
&& last_name != name
{
let descr = macro_kind.descr();
Expand All @@ -364,12 +366,25 @@ pub trait Emitter: Translate {
(in Nightly builds, run with -Z macro-backtrace for more info)",
);

children.push(SubDiagnostic {
level: Level::Note,
message: vec![(DiagnosticMessage::Str(msg), Style::NoStyle)],
span: MultiSpan::new(),
render_span: None,
});
if let Some(
"assert" | "bench" | "cfg" | "concat" | "concat_idents" | "env" | "format_args"
| "format_args_nl" | "global_allocator" | "include_str" | "test",
Comment on lines +370 to +371
Copy link
Member

Choose a reason for hiding this comment

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

I also don't love the hardcoded list of macro names. It feels to me like what these have in common, and why we might want to suppress the subdiagnostic, is that these act more like a regular function call that returns a value than a macro that expands into multiple statements which you would potentially want a macro-backtrace expansion for.

Perhaps we should have an internal rustc attribute that controls this behavior which can be applied to the appropriate macros in the std? That feels like it could be a cleaner solution than hardcoding the macro names at this place.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also don't love the hardcoded list of macro names.

That's fair, I would like to hide the note for all builtin macros at some point, just audit that the derive macros in particular don't have bad corner cases (didn't see any in the test suite, but they have occurred in the past). The idea behind the blocklist was to test this out in a controlled manner, and see if we have to back off from hiding this information.

Initially I tried using a solution based on DefIds, but doing any kind of querying in the Emitter was a non-starter. That being said, maybe we could inject a trait object callable to allow Emitter to have queries. If that were to work, then we can accomplish the same behavior with annotations on the macros. I do not know at this point how feasible it would be to do so.

) = builtin_name.as_ref().map(|n| n.as_str())
{
// We explicitly ignore these builtin macros and not show the line, as end
// users will never care about these, the macros should be providing good
// diagnostics always.
// FIXME: we could include the builtin `derive` macros here, but we need to
// audit every one of them before doing so to verify that they always produce
// reasonable diagnostics that point at the macro use.
} else {
children.push(SubDiagnostic {
level: Level::Note,
message: vec![(DiagnosticMessage::Str(msg), Style::NoStyle)],
span: MultiSpan::new(),
render_span: None,
});
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,7 @@ impl SyntaxExtension {
descr: Symbol,
macro_def_id: Option<DefId>,
parent_module: Option<DefId>,
builtin_name: Option<Symbol>,
) -> ExpnData {
ExpnData::new(
ExpnKind::Macro(self.macro_kind(), descr),
Expand All @@ -871,6 +872,7 @@ impl SyntaxExtension {
self.allow_internal_unsafe,
self.local_inner_macros,
self.collapse_debuginfo,
builtin_name,
)
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ impl<'a> ResolverExpand for Resolver<'a> {
fast_print_path(path),
def_id,
def_id.map(|def_id| self.macro_def_scope(def_id).nearest_parent_mod()),
ext.builtin_name,
),
self.create_stable_hashing_context(),
);
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_span/src/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,9 @@ pub struct ExpnData {
/// Should debuginfo for the macro be collapsed to the outermost expansion site (in other
/// words, was the macro definition annotated with `#[collapse_debuginfo]`)?
pub collapse_debuginfo: bool,
/// If this expansion comes from a builtin macro, this is the macro's name. Used when emitting
/// diagnostics to hide the line "this error originates in...".
pub builtin_name: Option<Symbol>,
}

impl !PartialEq for ExpnData {}
Expand All @@ -979,6 +982,7 @@ impl ExpnData {
allow_internal_unsafe: bool,
local_inner_macros: bool,
collapse_debuginfo: bool,
builtin_name: Option<Symbol>,
) -> ExpnData {
ExpnData {
kind,
Expand All @@ -993,6 +997,7 @@ impl ExpnData {
allow_internal_unsafe,
local_inner_macros,
collapse_debuginfo,
builtin_name,
}
}

Expand All @@ -1017,6 +1022,7 @@ impl ExpnData {
allow_internal_unsafe: false,
local_inner_macros: false,
collapse_debuginfo: false,
builtin_name: None,
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/test/ui/allocator/not-an-allocator.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ LL | static A: usize = 0;
| ^^^^^ the trait `GlobalAlloc` is not implemented for `usize`
|
= help: the trait `GlobalAlloc` is implemented for `System`
= note: this error originates in the attribute macro `global_allocator` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `usize: GlobalAlloc` is not satisfied
--> $DIR/not-an-allocator.rs:2:11
Expand All @@ -18,7 +17,6 @@ LL | static A: usize = 0;
| ^^^^^ the trait `GlobalAlloc` is not implemented for `usize`
|
= help: the trait `GlobalAlloc` is implemented for `System`
= note: this error originates in the attribute macro `global_allocator` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `usize: GlobalAlloc` is not satisfied
--> $DIR/not-an-allocator.rs:2:11
Expand All @@ -29,7 +27,6 @@ LL | static A: usize = 0;
| ^^^^^ the trait `GlobalAlloc` is not implemented for `usize`
|
= help: the trait `GlobalAlloc` is implemented for `System`
= note: this error originates in the attribute macro `global_allocator` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `usize: GlobalAlloc` is not satisfied
--> $DIR/not-an-allocator.rs:2:11
Expand All @@ -40,7 +37,6 @@ LL | static A: usize = 0;
| ^^^^^ the trait `GlobalAlloc` is not implemented for `usize`
|
= help: the trait `GlobalAlloc` is implemented for `System`
= note: this error originates in the attribute macro `global_allocator` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 4 previous errors

Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/allocator/two-allocators.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ LL | #[global_allocator]
| ------------------- in this procedural macro expansion
LL | static B: System = System;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot define a new global allocator
|
= note: this error originates in the attribute macro `global_allocator` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

2 changes: 0 additions & 2 deletions src/test/ui/attributes/extented-attribute-macro-error.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ error: couldn't read the file
|
LL | #![doc = include_str!("../not_existing_file.md")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in the macro `include_str` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

2 changes: 0 additions & 2 deletions src/test/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 previous error

Expand Down
1 change: 0 additions & 1 deletion src/test/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 = 0;
Expand Down
1 change: 0 additions & 1 deletion src/test/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 = 0;
Expand Down
2 changes: 0 additions & 2 deletions src/test/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 previous error

Expand Down
2 changes: 0 additions & 2 deletions src/test/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 previous error

Expand Down
2 changes: 0 additions & 2 deletions src/test/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 = 0;
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 = 0;
Expand Down
1 change: 0 additions & 1 deletion src/test/ui/borrowck/issue-64453.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ LL | static settings_dir: String = format!("");
| ^^^^^^^^^^^
|
= help: add `#![feature(const_fmt_arguments_new)]` to the crate attributes to enable
= 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[E0015]: cannot call non-const fn `format` in statics
--> $DIR/issue-64453.rs:4:31
Expand Down
10 changes: 0 additions & 10 deletions src/test/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 = 0.0;
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 = todo!();
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] = todo!();
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 = todo!();
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 = todo!();
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 @@ -81,8 +81,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:73: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 @@ -9,7 +9,6 @@ LL | println!("{}", foo.x);
= note: fields of packed structs are not properly aligned, and 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: `#[deny(unaligned_references)]` on by default
= 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 previous error

Expand All @@ -25,5 +24,4 @@ LL | println!("{}", foo.x);
= note: fields of packed structs are not properly aligned, and 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: `#[deny(unaligned_references)]` on by default
= 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)

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 previous error

Expand Down
2 changes: 0 additions & 2 deletions src/test/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
2 changes: 0 additions & 2 deletions src/test/ui/codemap_tests/issue-28308.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ error[E0600]: cannot apply unary operator `!` to type `&'static str`
|
LL | assert!("foo");
| ^^^^^^^^^^^^^^ cannot apply unary operator `!`
|
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to previous error

Expand Down
1 change: 0 additions & 1 deletion src/test/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
1 change: 0 additions & 1 deletion src/test/ui/const-ptr/forbidden_slices.32bit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ note: inside `R1`
|
LL | pub static R1: &[()] = unsafe { from_ptr_range(ptr::null()..ptr::null()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0080]: could not evaluate static initializer
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
Expand Down
1 change: 0 additions & 1 deletion src/test/ui/const-ptr/forbidden_slices.64bit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ note: inside `R1`
|
LL | pub static R1: &[()] = unsafe { from_ptr_range(ptr::null()..ptr::null()) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0080]: could not evaluate static initializer
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
Expand Down
Loading