Skip to content

Commit 5cccb36

Browse files
higher-ranked lifetime message
1 parent 984eab5 commit 5cccb36

7 files changed

+57
-6
lines changed

compiler/rustc_resolve/src/late.rs

+21-6
Original file line numberDiff line numberDiff line change
@@ -1512,7 +1512,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
15121512
count: 1,
15131513
};
15141514
let elision_candidate = LifetimeElisionCandidate::Missing(missing_lifetime);
1515-
for rib in self.lifetime_ribs.iter().rev() {
1515+
for (i, rib) in self.lifetime_ribs.iter().enumerate().rev() {
15161516
debug!(?rib.kind);
15171517
match rib.kind {
15181518
LifetimeRibKind::AnonymousCreateParameter { binder, .. } => {
@@ -1529,16 +1529,31 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
15291529
} else {
15301530
("`'_` cannot be used here", "`'_` is a reserved lifetime name")
15311531
};
1532-
rustc_errors::struct_span_err!(
1532+
let mut diag = rustc_errors::struct_span_err!(
15331533
self.r.session,
15341534
lifetime.ident.span,
15351535
E0637,
15361536
"{}",
15371537
msg,
1538-
)
1539-
.span_label(lifetime.ident.span, note)
1540-
.emit();
1541-
1538+
);
1539+
diag.span_label(lifetime.ident.span, note);
1540+
if elided {
1541+
for rib in self.lifetime_ribs[i..].iter().rev() {
1542+
if let LifetimeRibKind::Generics {
1543+
span,
1544+
kind: LifetimeBinderKind::PolyTrait | LifetimeBinderKind::WhereBound,
1545+
..
1546+
} = &rib.kind
1547+
{
1548+
diag.span_help(
1549+
*span,
1550+
"consider introducing a higher-ranked lifetime here with `for<'a>`",
1551+
);
1552+
break;
1553+
}
1554+
}
1555+
}
1556+
diag.emit();
15421557
self.record_lifetime_res(lifetime.id, LifetimeRes::Error, elision_candidate);
15431558
return;
15441559
}

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)