Skip to content

Commit ab107e3

Browse files
committed
Reduce verbosity of errors involving builtin macros
Do not show note "this error originates in" when it is caused by one in an explicit block list of builtin macros. Most notably, `println!` and friends will stop doing so.
1 parent b85f57d commit ab107e3

File tree

74 files changed

+33
-157
lines changed

Some content is hidden

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

74 files changed

+33
-157
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,9 @@ pub trait Emitter: Translate {
333333
// are some which do actually involve macros.
334334
ExpnKind::Inlined | ExpnKind::Desugaring(..) | ExpnKind::AstPass(..) => None,
335335

336-
ExpnKind::Macro(macro_kind, name) => Some((macro_kind, name)),
336+
ExpnKind::Macro(macro_kind, name) => {
337+
Some((macro_kind, name, expn_data.builtin_name))
338+
}
337339
}
338340
})
339341
.collect();
@@ -345,9 +347,9 @@ pub trait Emitter: Translate {
345347
self.render_multispans_macro_backtrace(span, children, backtrace);
346348

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

367-
children.push(SubDiagnostic {
368-
level: Level::Note,
369-
message: vec![(DiagnosticMessage::Str(msg), Style::NoStyle)],
370-
span: MultiSpan::new(),
371-
render_span: None,
372-
});
369+
if let Some(
370+
"assert" | "bench" | "cfg" | "concat" | "concat_idents" | "env" | "format_args"
371+
| "format_args_nl" | "global_allocator" | "include_str" | "test",
372+
) = builtin_name.as_ref().map(|n| n.as_str())
373+
{
374+
// We explicitly ignore these builtin macros and not show the line, as end
375+
// users will never care about these, the macros should be providing good
376+
// diagnostics always.
377+
// FIXME: we could include the builtin `derive` macros here, but we need to
378+
// audit every one of them before doing so to verify that they always produce
379+
// reasonable diagnostics that point at the macro use.
380+
} else {
381+
children.push(SubDiagnostic {
382+
level: Level::Note,
383+
message: vec![(DiagnosticMessage::Str(msg), Style::NoStyle)],
384+
span: MultiSpan::new(),
385+
render_span: None,
386+
});
387+
}
373388
}
374389
}
375390
}

compiler/rustc_expand/src/base.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,7 @@ impl SyntaxExtension {
858858
descr: Symbol,
859859
macro_def_id: Option<DefId>,
860860
parent_module: Option<DefId>,
861+
builtin_name: Option<Symbol>,
861862
) -> ExpnData {
862863
ExpnData::new(
863864
ExpnKind::Macro(self.macro_kind(), descr),
@@ -871,6 +872,7 @@ impl SyntaxExtension {
871872
self.allow_internal_unsafe,
872873
self.local_inner_macros,
873874
self.collapse_debuginfo,
875+
builtin_name,
874876
)
875877
}
876878
}

compiler/rustc_resolve/src/macros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ impl<'a> ResolverExpand for Resolver<'a> {
292292
fast_print_path(path),
293293
def_id,
294294
def_id.map(|def_id| self.macro_def_scope(def_id).nearest_parent_mod()),
295+
ext.builtin_name,
295296
),
296297
self.create_stable_hashing_context(),
297298
);

compiler/rustc_span/src/hygiene.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,9 @@ pub struct ExpnData {
961961
/// Should debuginfo for the macro be collapsed to the outermost expansion site (in other
962962
/// words, was the macro definition annotated with `#[collapse_debuginfo]`)?
963963
pub collapse_debuginfo: bool,
964+
/// If this expansion comes from a builtin macro, this is the macro's name. Used when emitting
965+
/// diagnostics to hide the line "this error originates in...".
966+
pub builtin_name: Option<Symbol>,
964967
}
965968

966969
impl !PartialEq for ExpnData {}
@@ -979,6 +982,7 @@ impl ExpnData {
979982
allow_internal_unsafe: bool,
980983
local_inner_macros: bool,
981984
collapse_debuginfo: bool,
985+
builtin_name: Option<Symbol>,
982986
) -> ExpnData {
983987
ExpnData {
984988
kind,
@@ -993,6 +997,7 @@ impl ExpnData {
993997
allow_internal_unsafe,
994998
local_inner_macros,
995999
collapse_debuginfo,
1000+
builtin_name,
9961001
}
9971002
}
9981003

@@ -1017,6 +1022,7 @@ impl ExpnData {
10171022
allow_internal_unsafe: false,
10181023
local_inner_macros: false,
10191024
collapse_debuginfo: false,
1025+
builtin_name: None,
10201026
}
10211027
}
10221028

src/test/ui/allocator/not-an-allocator.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LL | static A: usize = 0;
77
| ^^^^^ the trait `GlobalAlloc` is not implemented for `usize`
88
|
99
= help: the trait `GlobalAlloc` is implemented for `System`
10-
= note: this error originates in the attribute macro `global_allocator` (in Nightly builds, run with -Z macro-backtrace for more info)
1110

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

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

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

4541
error: aborting due to 4 previous errors
4642

src/test/ui/allocator/two-allocators.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ LL | #[global_allocator]
77
| ------------------- in this procedural macro expansion
88
LL | static B: System = System;
99
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot define a new global allocator
10-
|
11-
= note: this error originates in the attribute macro `global_allocator` (in Nightly builds, run with -Z macro-backtrace for more info)
1210

1311
error: aborting due to previous error
1412

src/test/ui/attributes/extented-attribute-macro-error.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ error: couldn't read the file
33
|
44
LL | #![doc = include_str!("../not_existing_file.md")]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
= note: this error originates in the macro `include_str` (in Nightly builds, run with -Z macro-backtrace for more info)
86

97
error: aborting due to previous error
108

src/test/ui/borrowck/borrowck-and-init.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ LL | println!("{}", false && { i = 5; true });
88
| ----- binding initialized here in some conditions
99
LL | println!("{}", i);
1010
| ^ `i` used here but it is possibly-uninitialized
11-
|
12-
= 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)
1311

1412
error: aborting due to previous error
1513

src/test/ui/borrowck/borrowck-break-uninit-2.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LL | let x: isize;
77
LL | println!("{}", x);
88
| ^ `x` used here but it isn't initialized
99
|
10-
= 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)
1110
help: consider assigning a value
1211
|
1312
LL | let x: isize = 0;

src/test/ui/borrowck/borrowck-break-uninit.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LL | let x: isize;
77
LL | println!("{}", x);
88
| ^ `x` used here but it isn't initialized
99
|
10-
= 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)
1110
help: consider assigning a value
1211
|
1312
LL | let x: isize = 0;

src/test/ui/borrowck/borrowck-or-init.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ LL | println!("{}", false || { i = 5; true });
88
| ----- binding initialized here in some conditions
99
LL | println!("{}", i);
1010
| ^ `i` used here but it is possibly-uninitialized
11-
|
12-
= 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)
1311

1412
error: aborting due to previous error
1513

src/test/ui/borrowck/borrowck-while-break.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ LL | while cond {
88
...
99
LL | println!("{}", v);
1010
| ^ `v` used here but it is possibly-uninitialized
11-
|
12-
= 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)
1311

1412
error: aborting due to previous error
1513

src/test/ui/borrowck/issue-24267-flow-exit.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ LL | loop { x = break; }
77
LL | println!("{}", x);
88
| ^ `x` used here but it isn't initialized
99
|
10-
= 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)
1110
help: consider assigning a value
1211
|
1312
LL | let x: i32 = 0;
@@ -22,7 +21,6 @@ LL | for _ in 0..10 { x = continue; }
2221
LL | println!("{}", x);
2322
| ^ `x` used here but it isn't initialized
2423
|
25-
= 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)
2624
help: consider assigning a value
2725
|
2826
LL | let x: i32 = 0;

src/test/ui/borrowck/issue-64453.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ LL | static settings_dir: String = format!("");
55
| ^^^^^^^^^^^
66
|
77
= help: add `#![feature(const_fmt_arguments_new)]` to the crate attributes to enable
8-
= 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)
98

109
error[E0015]: cannot call non-const fn `format` in statics
1110
--> $DIR/issue-64453.rs:4:31

src/test/ui/borrowck/suggest-assign-rvalue.stderr

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ LL | let my_float: f32;
1919
LL | println!("my_float: {}", my_float);
2020
| ^^^^^^^^ `my_float` used here but it isn't initialized
2121
|
22-
= 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)
2322
help: consider assigning a value
2423
|
2524
LL | let my_float: f32 = 0.0;
@@ -33,7 +32,6 @@ LL | let demo: Demo;
3332
LL | println!("demo: {:?}", demo);
3433
| ^^^^ `demo` used here but it isn't initialized
3534
|
36-
= 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)
3735
help: consider assigning a value
3836
|
3937
LL | let demo: Demo = Default::default();
@@ -47,7 +45,6 @@ LL | let demo_no: DemoNoDef;
4745
LL | println!("demo_no: {:?}", demo_no);
4846
| ^^^^^^^ `demo_no` used here but it isn't initialized
4947
|
50-
= 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)
5148
help: consider assigning a value
5249
|
5350
LL | let demo_no: DemoNoDef = todo!();
@@ -61,7 +58,6 @@ LL | let arr: [i32; 5];
6158
LL | println!("arr: {:?}", arr);
6259
| ^^^ `arr` used here but it isn't initialized
6360
|
64-
= 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)
6561
help: consider assigning a value
6662
|
6763
LL | let arr: [i32; 5] = todo!();
@@ -75,7 +71,6 @@ LL | let foo: Vec<&str>;
7571
LL | println!("foo: {:?}", foo);
7672
| ^^^ `foo` used here but it isn't initialized
7773
|
78-
= 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)
7974
help: consider assigning a value
8075
|
8176
LL | let foo: Vec<&str> = vec![];
@@ -89,7 +84,6 @@ LL | let my_string: String;
8984
LL | println!("my_string: {}", my_string);
9085
| ^^^^^^^^^ `my_string` used here but it isn't initialized
9186
|
92-
= 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)
9387
help: consider assigning a value
9488
|
9589
LL | let my_string: String = Default::default();
@@ -103,7 +97,6 @@ LL | let my_int: &i32;
10397
LL | println!("my_int: {}", *my_int);
10498
| ^^^^^^^ `*my_int` used here but it isn't initialized
10599
|
106-
= 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)
107100
help: consider assigning a value
108101
|
109102
LL | let my_int: &i32 = todo!();
@@ -117,7 +110,6 @@ LL | let hello: &str;
117110
LL | println!("hello: {}", hello);
118111
| ^^^^^ `hello` used here but it isn't initialized
119112
|
120-
= 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)
121113
help: consider assigning a value
122114
|
123115
LL | let hello: &str = todo!();
@@ -130,8 +122,6 @@ LL | let never: !;
130122
| ----- binding declared here but left uninitialized
131123
LL | println!("never: {}", never);
132124
| ^^^^^ `never` used here but it isn't initialized
133-
|
134-
= 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)
135125

136126
error: aborting due to 10 previous errors
137127

src/test/ui/closures/2229_closure_analysis/diagnostics/arrays.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ LL | println!("{}", arr[3]);
8181
...
8282
LL | c();
8383
| - mutable borrow later used here
84-
|
85-
= 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)
8684

8785
error[E0502]: cannot borrow `arr` as immutable because it is also borrowed as mutable
8886
--> $DIR/arrays.rs:73:24

src/test/ui/closures/2229_closure_analysis/diagnostics/box.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ LL | println!("{}", e.0.0.m.x);
2525
LL |
2626
LL | c();
2727
| - mutable borrow later used here
28-
|
29-
= 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)
3028

3129
error[E0506]: cannot assign to `e.0.0.m.x` because it is borrowed
3230
--> $DIR/box.rs:55:5

src/test/ui/closures/2229_closure_analysis/diagnostics/repr_packed.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ LL | println!("{}", foo.x);
99
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
1010
= 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)
1111
= note: `#[deny(unaligned_references)]` on by default
12-
= 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)
1312

1413
error: aborting due to previous error
1514

@@ -25,5 +24,4 @@ LL | println!("{}", foo.x);
2524
= note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
2625
= 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)
2726
= note: `#[deny(unaligned_references)]` on by default
28-
= 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)
2927

src/test/ui/closures/2229_closure_analysis/diagnostics/simple-struct-min-capture.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ LL | println!("{:?}", p);
1313
LL |
1414
LL | c();
1515
| - mutable borrow later used here
16-
|
17-
= 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)
1816

1917
error: aborting due to previous error
2018

src/test/ui/codemap_tests/bad-format-args.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ error: requires at least a format string argument
33
|
44
LL | format!();
55
| ^^^^^^^^^
6-
|
7-
= 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)
86

97
error: expected `,`, found `1`
108
--> $DIR/bad-format-args.rs:3:16

src/test/ui/codemap_tests/issue-28308.stderr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ error[E0600]: cannot apply unary operator `!` to type `&'static str`
33
|
44
LL | assert!("foo");
55
| ^^^^^^^^^^^^^^ cannot apply unary operator `!`
6-
|
7-
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
86

97
error: aborting due to previous error
108

src/test/ui/codemap_tests/tab_3.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ LL | println!("{:?}", some_vec);
1111
|
1212
note: `into_iter` takes ownership of the receiver `self`, which moves `some_vec`
1313
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
14-
= 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)
1514
help: you can `clone` the value and consume it, but this might not be your desired behavior
1615
|
1716
LL | some_vec.clone().into_iter();

src/test/ui/const-ptr/forbidden_slices.32bit.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ note: inside `R1`
124124
|
125125
LL | pub static R1: &[()] = unsafe { from_ptr_range(ptr::null()..ptr::null()) };
126126
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
127-
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
128127

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

src/test/ui/const-ptr/forbidden_slices.64bit.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ note: inside `R1`
124124
|
125125
LL | pub static R1: &[()] = unsafe { from_ptr_range(ptr::null()..ptr::null()) };
126126
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
127-
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
128127

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

0 commit comments

Comments
 (0)