From 5824917dbb895a0fec2135d7c448b64317dd0aad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Tue, 2 Mar 2021 18:24:13 -0800 Subject: [PATCH 1/2] Account for macros when suggesting adding lifetime Fix #70152. --- .../rustc_resolve/src/late/diagnostics.rs | 25 ++++++++---- ...ifetime-used-in-debug-macro-issue-70152.rs | 16 ++++++++ ...ime-used-in-debug-macro-issue-70152.stderr | 38 +++++++++++++++++++ 3 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 src/test/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.rs create mode 100644 src/test/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index d60f929cb8f3f..36707b5938f26 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -1645,6 +1645,7 @@ impl<'tcx> LifetimeContext<'_, 'tcx> { ); err.span_label(lifetime_ref.span, "undeclared lifetime"); let mut suggests_in_band = false; + let mut suggest_note = true; for missing in &self.missing_named_lifetime_spots { match missing { MissingLifetimeSpot::Generics(generics) => { @@ -1664,12 +1665,20 @@ impl<'tcx> LifetimeContext<'_, 'tcx> { suggests_in_band = true; (generics.span, format!("<{}>", lifetime_ref)) }; - err.span_suggestion( - span, - &format!("consider introducing lifetime `{}` here", lifetime_ref), - sugg, - Applicability::MaybeIncorrect, - ); + if !span.from_expansion() { + err.span_suggestion( + span, + &format!("consider introducing lifetime `{}` here", lifetime_ref), + sugg, + Applicability::MaybeIncorrect, + ); + } else if suggest_note { + suggest_note = false; // Avoid displaying the same help multiple times. + err.help(&format!( + "consider introducing lifetime `{}` to the item's generics", + lifetime_ref, + )); + } } MissingLifetimeSpot::HigherRanked { span, span_type } => { err.span_suggestion( @@ -1684,7 +1693,7 @@ impl<'tcx> LifetimeContext<'_, 'tcx> { ); err.note( "for more information on higher-ranked polymorphism, visit \ - https://doc.rust-lang.org/nomicon/hrtb.html", + https://doc.rust-lang.org/nomicon/hrtb.html", ); } _ => {} @@ -1696,7 +1705,7 @@ impl<'tcx> LifetimeContext<'_, 'tcx> { { err.help( "if you want to experiment with in-band lifetime bindings, \ - add `#![feature(in_band_lifetimes)]` to the crate attributes", + add `#![feature(in_band_lifetimes)]` to the crate attributes", ); } err.emit(); diff --git a/src/test/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.rs b/src/test/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.rs new file mode 100644 index 0000000000000..cc29f9de7fb4a --- /dev/null +++ b/src/test/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.rs @@ -0,0 +1,16 @@ +#[derive(Eq, PartialEq)] +struct Test { + a: &'b str, + //~^ ERROR use of undeclared lifetime name `'b` + //~| ERROR use of undeclared lifetime name `'b` +} + +trait T { + fn foo(&'static self) {} +} + +impl T for Test { + fn foo(&'b self) {} //~ ERROR use of undeclared lifetime name `'b` +} + +fn main() {} diff --git a/src/test/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr b/src/test/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr new file mode 100644 index 0000000000000..01dc0edc61eec --- /dev/null +++ b/src/test/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr @@ -0,0 +1,38 @@ +error[E0261]: use of undeclared lifetime name `'b` + --> $DIR/undeclared-lifetime-used-in-debug-macro-issue-70152.rs:13:13 + | +LL | fn foo(&'b self) {} + | ^^ undeclared lifetime + | + = help: if you want to experiment with in-band lifetime bindings, add `#![feature(in_band_lifetimes)]` to the crate attributes +help: consider introducing lifetime `'b` here + | +LL | impl<'b> T for Test { + | ^^^^ +help: consider introducing lifetime `'b` here + | +LL | fn foo<'b>(&'b self) {} + | ^^^^ + +error[E0261]: use of undeclared lifetime name `'b` + --> $DIR/undeclared-lifetime-used-in-debug-macro-issue-70152.rs:3:9 + | +LL | struct Test { + | - help: consider introducing lifetime `'b` here: `<'b>` +LL | a: &'b str, + | ^^ undeclared lifetime + | + = help: if you want to experiment with in-band lifetime bindings, add `#![feature(in_band_lifetimes)]` to the crate attributes + +error[E0261]: use of undeclared lifetime name `'b` + --> $DIR/undeclared-lifetime-used-in-debug-macro-issue-70152.rs:3:9 + | +LL | a: &'b str, + | ^^ undeclared lifetime + | + = help: consider introducing lifetime `'b` to the item's generics + = help: if you want to experiment with in-band lifetime bindings, add `#![feature(in_band_lifetimes)]` to the crate attributes + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0261`. From 5d0697b1be074a0774f521baf97492260e2a0737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 3 Mar 2021 12:15:26 -0800 Subject: [PATCH 2/2] reworded message --- compiler/rustc_resolve/src/late/diagnostics.rs | 12 ++++++++---- ...d-lifetime-used-in-debug-macro-issue-70152.stderr | 4 +++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 36707b5938f26..87bf79d722bde 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -1674,10 +1674,14 @@ impl<'tcx> LifetimeContext<'_, 'tcx> { ); } else if suggest_note { suggest_note = false; // Avoid displaying the same help multiple times. - err.help(&format!( - "consider introducing lifetime `{}` to the item's generics", - lifetime_ref, - )); + err.span_label( + span, + &format!( + "lifetime `{}` is missing in item created through this procedural \ + macro", + lifetime_ref, + ), + ); } } MissingLifetimeSpot::HigherRanked { span, span_type } => { diff --git a/src/test/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr b/src/test/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr index 01dc0edc61eec..30142e24cd5f1 100644 --- a/src/test/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr +++ b/src/test/ui/lifetimes/undeclared-lifetime-used-in-debug-macro-issue-70152.stderr @@ -27,10 +27,12 @@ LL | a: &'b str, error[E0261]: use of undeclared lifetime name `'b` --> $DIR/undeclared-lifetime-used-in-debug-macro-issue-70152.rs:3:9 | +LL | #[derive(Eq, PartialEq)] + | -- lifetime `'b` is missing in item created through this procedural macro +LL | struct Test { LL | a: &'b str, | ^^ undeclared lifetime | - = help: consider introducing lifetime `'b` to the item's generics = help: if you want to experiment with in-band lifetime bindings, add `#![feature(in_band_lifetimes)]` to the crate attributes error: aborting due to 3 previous errors