Skip to content
Merged
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
69 changes: 49 additions & 20 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ pub trait Emitter {
let has_macro_spans: Vec<_> = iter::once(&*span)
.chain(children.iter().map(|child| &child.span))
.flat_map(|span| span.primary_spans())
.flat_map(|sp| sp.macro_backtrace())
.filter_map(|expn_data| {
.flat_map(|sp| sp.macro_backtrace().map(move |expn_data| (sp, expn_data)))
.filter_map(|(sp, expn_data)| {
match expn_data.kind {
ExpnKind::Root => None,

Expand All @@ -176,7 +176,16 @@ pub trait Emitter {
ExpnKind::Desugaring(..) | ExpnKind::AstPass(..) => None,

ExpnKind::Macro(macro_kind, name) => {
Some((macro_kind, name, expn_data.diagnostic_opaque))
let same_line = sp
.overlaps(expn_data.def_site)
.then(|| expn_data.def_site.with_hi(sp.lo()));
Some((
macro_kind,
name,
expn_data.diagnostic_opaque,
expn_data.def_site,
same_line,
))
}
}
})
Expand All @@ -188,27 +197,47 @@ pub trait Emitter {

self.render_multispans_macro_backtrace(span, children, backtrace);

if !backtrace {
if !backtrace
// Skip macros annotated with `#[diagnostic::opaque]`. Builtin macros are "opaque" too.
if let Some((macro_kind, name, _)) = has_macro_spans.first()
&& let Some((_, _, false)) = has_macro_spans.last()
&& let Some((macro_kind, name, _, sp, same_line_span)) = has_macro_spans.first()
&& let Some((_, _, false, _, _)) = has_macro_spans.last()
{
// Mark the actual macro this originates from
let and_then = if let Some((macro_kind, last_name, _, _, _)) = has_macro_spans.last()
&& last_name != name
{
// Mark the actual macro this originates from
let and_then = if let Some((macro_kind, last_name, _)) = has_macro_spans.last()
&& last_name != name
let descr = macro_kind.descr();
format!(" which comes from the expansion of the {descr} `{last_name}`")
} else {
"".to_string()
};

let descr = macro_kind.descr();
let msg = format!("this {level} originates in the {descr} `{name}`{and_then}");

if let Some(source_map) = self.source_map() {
if source_map.is_imported(*sp) || same_line_span.is_none() {
let msg = format!(
"{msg} (in Nightly builds, run with -Z macro-backtrace for more info)"
);
children.push(Subdiag {
level: Sublevel::Note,
messages: vec![(DiagMessage::from(msg), Style::NoStyle)],
span: MultiSpan::new(),
});
} else if let Some(def) = same_line_span
&& source_map.is_multiline(*def)
&& !and_then.is_empty()
{
let descr = macro_kind.descr();
format!(" which comes from the expansion of the {descr} `{last_name}`")
// We only point at the macro definition when it is not on the same line as the
// error produced and there are more levels of macro nesting.
span.push_span_label(*sp, msg);
} else {
"".to_string()
};

let descr = macro_kind.descr();
let msg = format!(
"this {level} originates in the {descr} `{name}`{and_then} \
(in Nightly builds, run with -Z macro-backtrace for more info)",
);

// We point at the macro def line, but without an underline or message, we
// leave it implied.
span.push_span_context(sp.shrink_to_lo());
}
} else {
children.push(Subdiag {
level: Sublevel::Note,
messages: vec![(DiagMessage::from(msg), Style::NoStyle)],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
error: unsafe block missing a safety comment
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:244:22
|
LL | macro_rules! t {
LL | () => {
LL | let _x = unsafe { 1 };
| ^^^^^^^^^^^^
...
Expand All @@ -10,19 +12,19 @@ LL | t!();
= help: consider adding a safety comment on the preceding line
= note: `-D clippy::undocumented-unsafe-blocks` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::undocumented_unsafe_blocks)]`
= note: this error originates in the macro `t` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unsafe block missing a safety comment
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:256:13
|
LL | macro_rules! t {
...
LL | unsafe { 1 };
| ^^^^^^^^^^^^
...
LL | t!();
| ---- in this macro invocation
|
= help: consider adding a safety comment on the preceding line
= note: this error originates in the macro `t` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unsafe block missing a safety comment
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:337:19
Expand Down Expand Up @@ -131,14 +133,15 @@ LL | t!(unsafe {});
error: unsafe block missing a safety comment
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:401:13
|
LL | macro_rules! t {
LL | () => {
LL | unsafe {}
| ^^^^^^^^^
...
LL | t!();
| ---- in this macro invocation
|
= help: consider adding a safety comment on the preceding line
= note: this error originates in the macro `t` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unsafe block missing a safety comment
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:410:5
Expand Down Expand Up @@ -191,26 +194,28 @@ LL | unsafe impl B for (u32) {}
error: unsafe impl missing a safety comment
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:470:13
|
LL | macro_rules! no_safety_comment {
LL | ($t:ty) => {
LL | unsafe impl T for $t {}
| ^^^^^^^^^^^^^^^^^^^^^^^
...
LL | no_safety_comment!(());
| ---------------------- in this macro invocation
|
= help: consider adding a safety comment on the preceding line
= note: this error originates in the macro `no_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unsafe impl missing a safety comment
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:496:13
|
LL | macro_rules! no_safety_comment {
LL | ($t:ty) => {
LL | unsafe impl T for $t {}
| ^^^^^^^^^^^^^^^^^^^^^^^
...
LL | no_safety_comment!(());
| ---------------------- in this macro invocation
|
= help: consider adding a safety comment on the preceding line
= note: this error originates in the macro `no_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unsafe impl missing a safety comment
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:506:5
Expand All @@ -223,14 +228,15 @@ LL | unsafe impl T for (i32) {}
error: unsafe impl missing a safety comment
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:496:13
|
LL | macro_rules! no_safety_comment {
LL | ($t:ty) => {
LL | unsafe impl T for $t {}
| ^^^^^^^^^^^^^^^^^^^^^^^
...
LL | no_safety_comment!(u32);
| ----------------------- in this macro invocation
|
= help: consider adding a safety comment on the preceding line
= note: this error originates in the macro `no_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unsafe impl missing a safety comment
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:513:5
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
error: unsafe block missing a safety comment
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:244:22
|
LL | macro_rules! t {
LL | () => {
LL | let _x = unsafe { 1 };
| ^^^^^^^^^^^^
...
Expand All @@ -10,19 +12,19 @@ LL | t!();
= help: consider adding a safety comment on the preceding line
= note: `-D clippy::undocumented-unsafe-blocks` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::undocumented_unsafe_blocks)]`
= note: this error originates in the macro `t` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unsafe block missing a safety comment
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:256:13
|
LL | macro_rules! t {
...
LL | unsafe { 1 };
| ^^^^^^^^^^^^
...
LL | t!();
| ---- in this macro invocation
|
= help: consider adding a safety comment on the preceding line
= note: this error originates in the macro `t` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unsafe block missing a safety comment
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:337:19
Expand Down Expand Up @@ -131,14 +133,15 @@ LL | t!(unsafe {});
error: unsafe block missing a safety comment
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:401:13
|
LL | macro_rules! t {
LL | () => {
LL | unsafe {}
| ^^^^^^^^^
...
LL | t!();
| ---- in this macro invocation
|
= help: consider adding a safety comment on the preceding line
= note: this error originates in the macro `t` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unsafe block missing a safety comment
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:410:5
Expand Down Expand Up @@ -191,26 +194,28 @@ LL | unsafe impl B for (u32) {}
error: unsafe impl missing a safety comment
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:470:13
|
LL | macro_rules! no_safety_comment {
LL | ($t:ty) => {
LL | unsafe impl T for $t {}
| ^^^^^^^^^^^^^^^^^^^^^^^
...
LL | no_safety_comment!(());
| ---------------------- in this macro invocation
|
= help: consider adding a safety comment on the preceding line
= note: this error originates in the macro `no_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unsafe impl missing a safety comment
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:496:13
|
LL | macro_rules! no_safety_comment {
LL | ($t:ty) => {
LL | unsafe impl T for $t {}
| ^^^^^^^^^^^^^^^^^^^^^^^
...
LL | no_safety_comment!(());
| ---------------------- in this macro invocation
|
= help: consider adding a safety comment on the preceding line
= note: this error originates in the macro `no_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unsafe impl missing a safety comment
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:506:5
Expand All @@ -223,14 +228,15 @@ LL | unsafe impl T for (i32) {}
error: unsafe impl missing a safety comment
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:496:13
|
LL | macro_rules! no_safety_comment {
LL | ($t:ty) => {
LL | unsafe impl T for $t {}
| ^^^^^^^^^^^^^^^^^^^^^^^
...
LL | no_safety_comment!(u32);
| ----------------------- in this macro invocation
|
= help: consider adding a safety comment on the preceding line
= note: this error originates in the macro `no_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info)

error: unsafe impl missing a safety comment
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:513:5
Expand Down
4 changes: 2 additions & 2 deletions src/tools/clippy/tests/ui/async_yields_async.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ LL | let y = async { call_bar!() };
error: an async construct yields a type which is itself awaitable
--> tests/ui/async_yields_async.rs:90:21
|
LL | macro_rules! call_bar {
LL | () => {
LL | async { bar(5) }
| --^^^^^^--
| | |
Expand All @@ -121,8 +123,6 @@ LL | async { bar(5) }
...
LL | let y = async { call_bar!() };
| ----------- in this macro invocation
|
= note: this error originates in the macro `call_bar` (in Nightly builds, run with -Z macro-backtrace for more info)

error: an async construct yields a type which is itself awaitable
--> tests/ui/async_yields_async.rs:119:21
Expand Down
10 changes: 6 additions & 4 deletions src/tools/clippy/tests/ui/cast.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -670,25 +670,26 @@ LL | (a.abs() * b.pow(2) / c.abs()) as u32
error: casting `i32` to `u32` may lose the sign of the value
--> tests/ui/cast.rs:532:21
|
LL | macro_rules! m {
LL | () => {
LL | let _ = i32::MIN as u32; // cast_sign_loss
| ^^^^^^^^^^^^^^^ help: if this is intentional, use `cast_unsigned()` instead: `i32::MIN.cast_unsigned()`
...
LL | m!();
| ---- in this macro invocation
|
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)

error: casting `u32` to `u8` may truncate the value
--> tests/ui/cast.rs:535:21
|
LL | macro_rules! m {
...
LL | let _ = u32::MAX as u8; // cast_possible_truncation
| ^^^^^^^^^^^^^^
...
LL | m!();
| ---- in this macro invocation
|
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
help: ... or use `try_from` and handle the error accordingly
|
LL - let _ = u32::MAX as u8; // cast_possible_truncation
Expand All @@ -698,14 +699,15 @@ LL + let _ = u8::try_from(u32::MAX); // cast_possible_truncation
error: casting `f64` to `f32` may truncate the value
--> tests/ui/cast.rs:538:21
|
LL | macro_rules! m {
...
LL | let _ = std::f64::consts::PI as f32; // cast_possible_truncation
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
LL | m!();
| ---- in this macro invocation
|
= help: if this is intentional allow the lint with `#[allow(clippy::cast_possible_truncation)]` ...
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)

error: casting `i64` to `usize` may truncate the value on targets with 32-bit wide pointers
--> tests/ui/cast.rs:549:5
Expand Down
3 changes: 2 additions & 1 deletion src/tools/clippy/tests/ui/cast_lossless_integer.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -497,14 +497,15 @@ LL + let _ = i32::from(sign_cast!(x, u8, i8) + 1);
error: casts from `u8` to `u32` can be expressed infallibly using `From`
--> tests/ui/cast_lossless_integer.rs:159:13
|
LL | macro_rules! in_macro {
LL | () => {
LL | 1u8 as u32
| ^^^^^^^^^^
...
LL | let _ = in_macro!();
| ----------- in this macro invocation
|
= help: an `as` cast can become silently lossy if the types change in the future
= note: this error originates in the macro `in_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
help: use `u32::from` instead
|
LL - 1u8 as u32
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ LL | x.unwrap();
error: called `unwrap` on `x` after checking its variant with `is_some`
--> tests/ui/checked_unwrap/simple_conditionals.rs:13:13
|
LL | macro_rules! m {
LL | ($a:expr) => {
LL | if $a.is_some() {
| --------------- help: try: `if let Some(<item>) = x`
LL | $a.unwrap();
| ^^^^^^^^^^^
...
LL | m!(x);
| ----- in this macro invocation
|
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)

error: called `unwrap` on `x` after checking its variant with `is_ok`
--> tests/ui/checked_unwrap/simple_conditionals.rs:71:9
Expand Down
Loading
Loading