Skip to content

Commit 0c8d11b

Browse files
authored
Rollup merge of #105859 - compiler-errors:hr-lifetime-add, r=davidtwco
Point out span where we could introduce higher-ranked lifetime Somewhat addresses #105422, but not really. We don't have that much useful information here since we're still in resolution :^( Maybe this suggestion isn't worth it. If the reviewer has an idea how we can get a more succinct binder information for a structured suggestion, it would be appreciated.
2 parents 3d18c4d + 5cccb36 commit 0c8d11b

7 files changed

+57
-6
lines changed

compiler/rustc_resolve/src/late.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -1515,7 +1515,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
15151515
count: 1,
15161516
};
15171517
let elision_candidate = LifetimeElisionCandidate::Missing(missing_lifetime);
1518-
for rib in self.lifetime_ribs.iter().rev() {
1518+
for (i, rib) in self.lifetime_ribs.iter().enumerate().rev() {
15191519
debug!(?rib.kind);
15201520
match rib.kind {
15211521
LifetimeRibKind::AnonymousCreateParameter { binder, .. } => {
@@ -1532,16 +1532,31 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
15321532
} else {
15331533
("`'_` cannot be used here", "`'_` is a reserved lifetime name")
15341534
};
1535-
rustc_errors::struct_span_err!(
1535+
let mut diag = rustc_errors::struct_span_err!(
15361536
self.r.session,
15371537
lifetime.ident.span,
15381538
E0637,
15391539
"{}",
15401540
msg,
1541-
)
1542-
.span_label(lifetime.ident.span, note)
1543-
.emit();
1544-
1541+
);
1542+
diag.span_label(lifetime.ident.span, note);
1543+
if elided {
1544+
for rib in self.lifetime_ribs[i..].iter().rev() {
1545+
if let LifetimeRibKind::Generics {
1546+
span,
1547+
kind: LifetimeBinderKind::PolyTrait | LifetimeBinderKind::WhereBound,
1548+
..
1549+
} = &rib.kind
1550+
{
1551+
diag.span_help(
1552+
*span,
1553+
"consider introducing a higher-ranked lifetime here with `for<'a>`",
1554+
);
1555+
break;
1556+
}
1557+
}
1558+
}
1559+
diag.emit();
15451560
self.record_lifetime_res(lifetime.id, LifetimeRes::Error, elision_candidate);
15461561
return;
15471562
}

src/test/ui/error-codes/E0637.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ error[E0637]: `&` without an explicit lifetime name cannot be used here
2121
|
2222
LL | T: Into<&u32>,
2323
| ^ explicit lifetime name needed here
24+
|
25+
help: consider introducing a higher-ranked lifetime here with `for<'a>`
26+
--> $DIR/E0637.rs:13:8
27+
|
28+
LL | T: Into<&u32>,
29+
| ^
2430

2531
error: aborting due to 3 previous errors
2632

src/test/ui/generics/issue-65285-incorrect-explicit-lifetime-name-needed.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error[E0637]: `&` without an explicit lifetime name cannot be used here
33
|
44
LL | fn should_error<T>() where T : Into<&u32> {}
55
| ^ explicit lifetime name needed here
6+
|
7+
help: consider introducing a higher-ranked lifetime here with `for<'a>`
8+
--> $DIR/issue-65285-incorrect-explicit-lifetime-name-needed.rs:5:32
9+
|
10+
LL | fn should_error<T>() where T : Into<&u32> {}
11+
| ^
612

713
error[E0106]: missing lifetime specifier
814
--> $DIR/issue-65285-incorrect-explicit-lifetime-name-needed.rs:9:20

src/test/ui/underscore-lifetime/where-clause-inherent-impl-ampersand.rust2015.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error[E0637]: `&` without an explicit lifetime name cannot be used here
33
|
44
LL | T: WithType<&u32>
55
| ^ explicit lifetime name needed here
6+
|
7+
help: consider introducing a higher-ranked lifetime here with `for<'a>`
8+
--> $DIR/where-clause-inherent-impl-ampersand.rs:13:8
9+
|
10+
LL | T: WithType<&u32>
11+
| ^
612

713
error: aborting due to previous error
814

src/test/ui/underscore-lifetime/where-clause-inherent-impl-ampersand.rust2018.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error[E0637]: `&` without an explicit lifetime name cannot be used here
33
|
44
LL | T: WithType<&u32>
55
| ^ explicit lifetime name needed here
6+
|
7+
help: consider introducing a higher-ranked lifetime here with `for<'a>`
8+
--> $DIR/where-clause-inherent-impl-ampersand.rs:13:8
9+
|
10+
LL | T: WithType<&u32>
11+
| ^
612

713
error: aborting due to previous error
814

src/test/ui/underscore-lifetime/where-clause-trait-impl-region.rust2015.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error[E0637]: `&` without an explicit lifetime name cannot be used here
33
|
44
LL | T: WithType<&u32>
55
| ^ explicit lifetime name needed here
6+
|
7+
help: consider introducing a higher-ranked lifetime here with `for<'a>`
8+
--> $DIR/where-clause-trait-impl-region.rs:11:8
9+
|
10+
LL | T: WithType<&u32>
11+
| ^
612

713
error: aborting due to previous error
814

src/test/ui/underscore-lifetime/where-clause-trait-impl-region.rust2018.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error[E0637]: `&` without an explicit lifetime name cannot be used here
33
|
44
LL | T: WithType<&u32>
55
| ^ explicit lifetime name needed here
6+
|
7+
help: consider introducing a higher-ranked lifetime here with `for<'a>`
8+
--> $DIR/where-clause-trait-impl-region.rs:11:8
9+
|
10+
LL | T: WithType<&u32>
11+
| ^
612

713
error: aborting due to previous error
814

0 commit comments

Comments
 (0)