mirror of
https://github.com/rust-lang/rust.git
synced 2024-10-31 22:41:50 +00:00
Rollup merge of #101445 - TaKO8Ki:suggest-introducing-explicit-lifetime, r=oli-obk
Suggest introducing an explicit lifetime if it does not exist Fixes #101027
This commit is contained in:
commit
7c7548cd37
@ -2395,19 +2395,23 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
|||||||
type_param_span: Option<(Span, bool)>,
|
type_param_span: Option<(Span, bool)>,
|
||||||
bound_kind: GenericKind<'tcx>,
|
bound_kind: GenericKind<'tcx>,
|
||||||
sub: S,
|
sub: S,
|
||||||
|
add_lt_sugg: Option<(Span, String)>,
|
||||||
) {
|
) {
|
||||||
let msg = "consider adding an explicit lifetime bound";
|
let msg = "consider adding an explicit lifetime bound";
|
||||||
if let Some((sp, has_lifetimes)) = type_param_span {
|
if let Some((sp, has_lifetimes)) = type_param_span {
|
||||||
let suggestion =
|
let suggestion =
|
||||||
if has_lifetimes { format!(" + {}", sub) } else { format!(": {}", sub) };
|
if has_lifetimes { format!(" + {}", sub) } else { format!(": {}", sub) };
|
||||||
err.span_suggestion_verbose(
|
let mut suggestions = vec![(sp, suggestion)];
|
||||||
sp,
|
if let Some(add_lt_sugg) = add_lt_sugg {
|
||||||
&format!("{}...", msg),
|
suggestions.push(add_lt_sugg);
|
||||||
suggestion,
|
}
|
||||||
|
err.multipart_suggestion_verbose(
|
||||||
|
format!("{msg}..."),
|
||||||
|
suggestions,
|
||||||
Applicability::MaybeIncorrect, // Issue #41966
|
Applicability::MaybeIncorrect, // Issue #41966
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
let consider = format!("{} `{}: {}`...", msg, bound_kind, sub,);
|
let consider = format!("{} `{}: {}`...", msg, bound_kind, sub);
|
||||||
err.help(&consider);
|
err.help(&consider);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2423,7 +2427,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
|||||||
};
|
};
|
||||||
let mut sugg =
|
let mut sugg =
|
||||||
vec![(sp, suggestion), (span.shrink_to_hi(), format!(" + {}", new_lt))];
|
vec![(sp, suggestion), (span.shrink_to_hi(), format!(" + {}", new_lt))];
|
||||||
if let Some(lt) = add_lt_sugg {
|
if let Some(lt) = add_lt_sugg.clone() {
|
||||||
sugg.push(lt);
|
sugg.push(lt);
|
||||||
sugg.rotate_right(1);
|
sugg.rotate_right(1);
|
||||||
}
|
}
|
||||||
@ -2529,7 +2533,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
|||||||
// for the bound is not suitable for suggestions when `-Zverbose` is set because it
|
// for the bound is not suitable for suggestions when `-Zverbose` is set because it
|
||||||
// uses `Debug` output, so we handle it specially here so that suggestions are
|
// uses `Debug` output, so we handle it specially here so that suggestions are
|
||||||
// always correct.
|
// always correct.
|
||||||
binding_suggestion(&mut err, type_param_span, bound_kind, name);
|
binding_suggestion(&mut err, type_param_span, bound_kind, name, None);
|
||||||
err
|
err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2542,7 +2546,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
|||||||
"{} may not live long enough",
|
"{} may not live long enough",
|
||||||
labeled_user_string
|
labeled_user_string
|
||||||
);
|
);
|
||||||
binding_suggestion(&mut err, type_param_span, bound_kind, "'static");
|
binding_suggestion(&mut err, type_param_span, bound_kind, "'static", None);
|
||||||
err
|
err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2576,7 +2580,13 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
|||||||
new_binding_suggestion(&mut err, type_param_span);
|
new_binding_suggestion(&mut err, type_param_span);
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
binding_suggestion(&mut err, type_param_span, bound_kind, new_lt);
|
binding_suggestion(
|
||||||
|
&mut err,
|
||||||
|
type_param_span,
|
||||||
|
bound_kind,
|
||||||
|
new_lt,
|
||||||
|
add_lt_sugg,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
fn no_restriction<T>(x: &()) -> &() {
|
||||||
|
with_restriction::<T>(x) //~ ERROR the parameter type `T` may not live long enough
|
||||||
|
}
|
||||||
|
|
||||||
|
fn with_restriction<'b, T: 'b>(x: &'b ()) -> &'b () {
|
||||||
|
x
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
@ -0,0 +1,23 @@
|
|||||||
|
error[E0311]: the parameter type `T` may not live long enough
|
||||||
|
--> $DIR/suggest-introducing-and-adding-missing-lifetime.rs:2:5
|
||||||
|
|
|
||||||
|
LL | with_restriction::<T>(x)
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
|
||||||
|
note: the parameter type `T` must be valid for the anonymous lifetime defined here...
|
||||||
|
--> $DIR/suggest-introducing-and-adding-missing-lifetime.rs:1:25
|
||||||
|
|
|
||||||
|
LL | fn no_restriction<T>(x: &()) -> &() {
|
||||||
|
| ^^^
|
||||||
|
note: ...so that the type `T` will meet its required lifetime bounds
|
||||||
|
--> $DIR/suggest-introducing-and-adding-missing-lifetime.rs:2:5
|
||||||
|
|
|
||||||
|
LL | with_restriction::<T>(x)
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
help: consider adding an explicit lifetime bound...
|
||||||
|
|
|
||||||
|
LL | fn no_restriction<'a, T: 'a>(x: &()) -> &() {
|
||||||
|
| +++ ++++
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
@ -22,8 +22,8 @@ LL | | });
|
|||||||
| |______^
|
| |______^
|
||||||
help: consider adding an explicit lifetime bound...
|
help: consider adding an explicit lifetime bound...
|
||||||
|
|
|
|
||||||
LL | fn func<T: Test + 'a>(foo: &Foo, t: T) {
|
LL | fn func<'a, T: Test + 'a>(foo: &Foo, t: T) {
|
||||||
| ++++
|
| +++ ++++
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to previous error
|
||||||
|
|
||||||
|
@ -47,8 +47,10 @@ LL | | }
|
|||||||
| |_____^
|
| |_____^
|
||||||
help: consider adding an explicit lifetime bound...
|
help: consider adding an explicit lifetime bound...
|
||||||
|
|
|
|
||||||
LL | G: Get<T> + 'a,
|
LL ~ fn bar<'a, G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
|
||||||
| ++++
|
LL | where
|
||||||
|
LL ~ G: Get<T> + 'a,
|
||||||
|
|
|
||||||
|
|
||||||
error[E0311]: the parameter type `G` may not live long enough
|
error[E0311]: the parameter type `G` may not live long enough
|
||||||
--> $DIR/missing-lifetimes-in-signature.rs:52:5
|
--> $DIR/missing-lifetimes-in-signature.rs:52:5
|
||||||
@ -74,8 +76,8 @@ LL | | }
|
|||||||
| |_____^
|
| |_____^
|
||||||
help: consider adding an explicit lifetime bound...
|
help: consider adding an explicit lifetime bound...
|
||||||
|
|
|
|
||||||
LL | fn qux<'a, G: 'a + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
|
LL | fn qux<'b, 'a, G: 'a + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_
|
||||||
| ++++
|
| +++ ++++
|
||||||
|
|
||||||
error[E0311]: the parameter type `G` may not live long enough
|
error[E0311]: the parameter type `G` may not live long enough
|
||||||
--> $DIR/missing-lifetimes-in-signature.rs:61:9
|
--> $DIR/missing-lifetimes-in-signature.rs:61:9
|
||||||
@ -101,8 +103,8 @@ LL | | }
|
|||||||
| |_________^
|
| |_________^
|
||||||
help: consider adding an explicit lifetime bound...
|
help: consider adding an explicit lifetime bound...
|
||||||
|
|
|
|
||||||
LL | fn qux<'b, G: Get<T> + 'b + 'c, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ {
|
LL | fn qux<'c, 'b, G: Get<T> + 'b + 'c, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ {
|
||||||
| ++++
|
| +++ ++++
|
||||||
|
|
||||||
error[E0311]: the parameter type `G` may not live long enough
|
error[E0311]: the parameter type `G` may not live long enough
|
||||||
--> $DIR/missing-lifetimes-in-signature.rs:73:5
|
--> $DIR/missing-lifetimes-in-signature.rs:73:5
|
||||||
@ -130,8 +132,8 @@ LL | | }
|
|||||||
| |_____^
|
| |_____^
|
||||||
help: consider adding an explicit lifetime bound...
|
help: consider adding an explicit lifetime bound...
|
||||||
|
|
|
|
||||||
LL | fn bat<'a, G: 'a + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
|
LL | fn bat<'b, 'a, G: 'a + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a
|
||||||
| ++++
|
| +++ ++++
|
||||||
|
|
||||||
error[E0621]: explicit lifetime required in the type of `dest`
|
error[E0621]: explicit lifetime required in the type of `dest`
|
||||||
--> $DIR/missing-lifetimes-in-signature.rs:73:5
|
--> $DIR/missing-lifetimes-in-signature.rs:73:5
|
||||||
|
Loading…
Reference in New Issue
Block a user