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;

0 commit comments

Comments
 (0)