Skip to content

Commit

Permalink
Account for macros when suggesting adding lifetime
Browse files Browse the repository at this point in the history
Fix #70152.
  • Loading branch information
estebank committed Mar 3, 2021
1 parent 795a934 commit 5824917
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
25 changes: 17 additions & 8 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Expand Up @@ -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) => {
Expand All @@ -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(
Expand All @@ -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",
);
}
_ => {}
Expand All @@ -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();
Expand Down
@@ -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() {}
@@ -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`.

0 comments on commit 5824917

Please sign in to comment.