Skip to content

Commit 016626a

Browse files
committed
suggest introducing an explicit lifetime if it does not exist
1 parent c2d140b commit 016626a

File tree

5 files changed

+63
-19
lines changed

5 files changed

+63
-19
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -2395,19 +2395,23 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
23952395
type_param_span: Option<(Span, bool)>,
23962396
bound_kind: GenericKind<'tcx>,
23972397
sub: S,
2398+
add_lt_sugg: Option<(Span, String)>,
23982399
) {
23992400
let msg = "consider adding an explicit lifetime bound";
24002401
if let Some((sp, has_lifetimes)) = type_param_span {
24012402
let suggestion =
24022403
if has_lifetimes { format!(" + {}", sub) } else { format!(": {}", sub) };
2403-
err.span_suggestion_verbose(
2404-
sp,
2405-
&format!("{}...", msg),
2406-
suggestion,
2404+
let mut suggestions = vec![(sp, suggestion)];
2405+
if let Some(add_lt_sugg) = add_lt_sugg {
2406+
suggestions.push(add_lt_sugg);
2407+
}
2408+
err.multipart_suggestion_verbose(
2409+
format!("{msg}..."),
2410+
suggestions,
24072411
Applicability::MaybeIncorrect, // Issue #41966
24082412
);
24092413
} else {
2410-
let consider = format!("{} `{}: {}`...", msg, bound_kind, sub,);
2414+
let consider = format!("{} `{}: {}`...", msg, bound_kind, sub);
24112415
err.help(&consider);
24122416
}
24132417
}
@@ -2423,7 +2427,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
24232427
};
24242428
let mut sugg =
24252429
vec![(sp, suggestion), (span.shrink_to_hi(), format!(" + {}", new_lt))];
2426-
if let Some(lt) = add_lt_sugg {
2430+
if let Some(lt) = add_lt_sugg.clone() {
24272431
sugg.push(lt);
24282432
sugg.rotate_right(1);
24292433
}
@@ -2529,7 +2533,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
25292533
// for the bound is not suitable for suggestions when `-Zverbose` is set because it
25302534
// uses `Debug` output, so we handle it specially here so that suggestions are
25312535
// always correct.
2532-
binding_suggestion(&mut err, type_param_span, bound_kind, name);
2536+
binding_suggestion(&mut err, type_param_span, bound_kind, name, None);
25332537
err
25342538
}
25352539

@@ -2542,7 +2546,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
25422546
"{} may not live long enough",
25432547
labeled_user_string
25442548
);
2545-
binding_suggestion(&mut err, type_param_span, bound_kind, "'static");
2549+
binding_suggestion(&mut err, type_param_span, bound_kind, "'static", None);
25462550
err
25472551
}
25482552

@@ -2576,7 +2580,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
25762580
new_binding_suggestion(&mut err, type_param_span);
25772581
}
25782582
_ => {
2579-
binding_suggestion(&mut err, type_param_span, bound_kind, new_lt);
2583+
binding_suggestion(
2584+
&mut err,
2585+
type_param_span,
2586+
bound_kind,
2587+
new_lt,
2588+
add_lt_sugg,
2589+
);
25802590
}
25812591
}
25822592
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
fn no_restriction<T>(x: &()) -> &() {
2+
with_restriction::<T>(x) //~ ERROR the parameter type `T` may not live long enough
3+
}
4+
5+
fn with_restriction<'b, T: 'b>(x: &'b ()) -> &'b () {
6+
x
7+
}
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error[E0311]: the parameter type `T` may not live long enough
2+
--> $DIR/suggest-introducing-and-adding-missing-lifetime.rs:2:5
3+
|
4+
LL | with_restriction::<T>(x)
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: the parameter type `T` must be valid for the anonymous lifetime defined here...
8+
--> $DIR/suggest-introducing-and-adding-missing-lifetime.rs:1:25
9+
|
10+
LL | fn no_restriction<T>(x: &()) -> &() {
11+
| ^^^
12+
note: ...so that the type `T` will meet its required lifetime bounds
13+
--> $DIR/suggest-introducing-and-adding-missing-lifetime.rs:2:5
14+
|
15+
LL | with_restriction::<T>(x)
16+
| ^^^^^^^^^^^^^^^^^^^^^
17+
help: consider adding an explicit lifetime bound...
18+
|
19+
LL | fn no_restriction<'a, T: 'a>(x: &()) -> &() {
20+
| +++ ++++
21+
22+
error: aborting due to previous error
23+

src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature-2.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ LL | | });
2222
| |______^
2323
help: consider adding an explicit lifetime bound...
2424
|
25-
LL | fn func<T: Test + 'a>(foo: &Foo, t: T) {
26-
| ++++
25+
LL | fn func<'a, T: Test + 'a>(foo: &Foo, t: T) {
26+
| +++ ++++
2727

2828
error: aborting due to previous error
2929

src/test/ui/suggestions/lifetimes/missing-lifetimes-in-signature.stderr

+10-8
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ LL | | }
4747
| |_____^
4848
help: consider adding an explicit lifetime bound...
4949
|
50-
LL | G: Get<T> + 'a,
51-
| ++++
50+
LL ~ fn bar<'a, G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
51+
LL | where
52+
LL ~ G: Get<T> + 'a,
53+
|
5254

5355
error[E0311]: the parameter type `G` may not live long enough
5456
--> $DIR/missing-lifetimes-in-signature.rs:52:5
@@ -74,8 +76,8 @@ LL | | }
7476
| |_____^
7577
help: consider adding an explicit lifetime bound...
7678
|
77-
LL | fn qux<'a, G: 'a + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
78-
| ++++
79+
LL | fn qux<'b, 'a, G: 'a + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
80+
| +++ ++++
7981

8082
error[E0311]: the parameter type `G` may not live long enough
8183
--> $DIR/missing-lifetimes-in-signature.rs:61:9
@@ -101,8 +103,8 @@ LL | | }
101103
| |_________^
102104
help: consider adding an explicit lifetime bound...
103105
|
104-
LL | fn qux<'b, G: Get<T> + 'b + 'c, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ {
105-
| ++++
106+
LL | fn qux<'c, 'b, G: Get<T> + 'b + 'c, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ {
107+
| +++ ++++
106108

107109
error[E0311]: the parameter type `G` may not live long enough
108110
--> $DIR/missing-lifetimes-in-signature.rs:73:5
@@ -130,8 +132,8 @@ LL | | }
130132
| |_____^
131133
help: consider adding an explicit lifetime bound...
132134
|
133-
LL | fn bat<'a, G: 'a + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
134-
| ++++
135+
LL | fn bat<'b, 'a, G: 'a + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
136+
| +++ ++++
135137

136138
error[E0621]: explicit lifetime required in the type of `dest`
137139
--> $DIR/missing-lifetimes-in-signature.rs:73:5

0 commit comments

Comments
 (0)