Skip to content

Commit ccbb37c

Browse files
authored
Rollup merge of rust-lang#82717 - estebank:issue-70152, r=lcnr
Account for macros when suggesting adding lifetime Fix rust-lang#70152.
2 parents d892d31 + 5d0697b commit ccbb37c

File tree

3 files changed

+77
-8
lines changed

3 files changed

+77
-8
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

+21-8
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,7 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
16451645
);
16461646
err.span_label(lifetime_ref.span, "undeclared lifetime");
16471647
let mut suggests_in_band = false;
1648+
let mut suggest_note = true;
16481649
for missing in &self.missing_named_lifetime_spots {
16491650
match missing {
16501651
MissingLifetimeSpot::Generics(generics) => {
@@ -1664,12 +1665,24 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
16641665
suggests_in_band = true;
16651666
(generics.span, format!("<{}>", lifetime_ref))
16661667
};
1667-
err.span_suggestion(
1668-
span,
1669-
&format!("consider introducing lifetime `{}` here", lifetime_ref),
1670-
sugg,
1671-
Applicability::MaybeIncorrect,
1672-
);
1668+
if !span.from_expansion() {
1669+
err.span_suggestion(
1670+
span,
1671+
&format!("consider introducing lifetime `{}` here", lifetime_ref),
1672+
sugg,
1673+
Applicability::MaybeIncorrect,
1674+
);
1675+
} else if suggest_note {
1676+
suggest_note = false; // Avoid displaying the same help multiple times.
1677+
err.span_label(
1678+
span,
1679+
&format!(
1680+
"lifetime `{}` is missing in item created through this procedural \
1681+
macro",
1682+
lifetime_ref,
1683+
),
1684+
);
1685+
}
16731686
}
16741687
MissingLifetimeSpot::HigherRanked { span, span_type } => {
16751688
err.span_suggestion(
@@ -1684,7 +1697,7 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
16841697
);
16851698
err.note(
16861699
"for more information on higher-ranked polymorphism, visit \
1687-
https://doc.rust-lang.org/nomicon/hrtb.html",
1700+
https://doc.rust-lang.org/nomicon/hrtb.html",
16881701
);
16891702
}
16901703
_ => {}
@@ -1696,7 +1709,7 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
16961709
{
16971710
err.help(
16981711
"if you want to experiment with in-band lifetime bindings, \
1699-
add `#![feature(in_band_lifetimes)]` to the crate attributes",
1712+
add `#![feature(in_band_lifetimes)]` to the crate attributes",
17001713
);
17011714
}
17021715
err.emit();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#[derive(Eq, PartialEq)]
2+
struct Test {
3+
a: &'b str,
4+
//~^ ERROR use of undeclared lifetime name `'b`
5+
//~| ERROR use of undeclared lifetime name `'b`
6+
}
7+
8+
trait T {
9+
fn foo(&'static self) {}
10+
}
11+
12+
impl T for Test {
13+
fn foo(&'b self) {} //~ ERROR use of undeclared lifetime name `'b`
14+
}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error[E0261]: use of undeclared lifetime name `'b`
2+
--> $DIR/undeclared-lifetime-used-in-debug-macro-issue-70152.rs:13:13
3+
|
4+
LL | fn foo(&'b self) {}
5+
| ^^ undeclared lifetime
6+
|
7+
= help: if you want to experiment with in-band lifetime bindings, add `#![feature(in_band_lifetimes)]` to the crate attributes
8+
help: consider introducing lifetime `'b` here
9+
|
10+
LL | impl<'b> T for Test {
11+
| ^^^^
12+
help: consider introducing lifetime `'b` here
13+
|
14+
LL | fn foo<'b>(&'b self) {}
15+
| ^^^^
16+
17+
error[E0261]: use of undeclared lifetime name `'b`
18+
--> $DIR/undeclared-lifetime-used-in-debug-macro-issue-70152.rs:3:9
19+
|
20+
LL | struct Test {
21+
| - help: consider introducing lifetime `'b` here: `<'b>`
22+
LL | a: &'b str,
23+
| ^^ undeclared lifetime
24+
|
25+
= help: if you want to experiment with in-band lifetime bindings, add `#![feature(in_band_lifetimes)]` to the crate attributes
26+
27+
error[E0261]: use of undeclared lifetime name `'b`
28+
--> $DIR/undeclared-lifetime-used-in-debug-macro-issue-70152.rs:3:9
29+
|
30+
LL | #[derive(Eq, PartialEq)]
31+
| -- lifetime `'b` is missing in item created through this procedural macro
32+
LL | struct Test {
33+
LL | a: &'b str,
34+
| ^^ undeclared lifetime
35+
|
36+
= help: if you want to experiment with in-band lifetime bindings, add `#![feature(in_band_lifetimes)]` to the crate attributes
37+
38+
error: aborting due to 3 previous errors
39+
40+
For more information about this error, try `rustc --explain E0261`.

0 commit comments

Comments
 (0)