Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions compiler/rustc_trait_selection/src/error_reporting/infer/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc_errors::{
use rustc_hir::def::{DefKind, Namespace};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::Visitor;
use rustc_hir::{self as hir, ParamName};
use rustc_hir::{self as hir, GenericParamKind, ParamName};
use rustc_middle::hir::nested_filter;
use rustc_middle::middle::resolve_bound_vars as rbv;
use rustc_middle::traits::ObligationCauseCode;
Expand Down Expand Up @@ -772,9 +772,8 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}
}

let msg = "consider adding an explicit lifetime bound";
'suggestion: {
let msg = "consider adding an explicit lifetime bound";

Comment thread
bushrat011899 marked this conversation as resolved.
Resolved
if (bound_kind, sub).has_infer_regions()
|| (bound_kind, sub).has_placeholders()
|| !bound_kind.is_suggestable(self.tcx, false)
Expand Down Expand Up @@ -894,15 +893,18 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
if sub.kind() == ty::ReStatic
&& let Some(node) = self.tcx.hir_get_if_local(generic_param_scope.into())
&& let hir::Node::Item(hir::Item {
kind: hir::ItemKind::Fn { sig, body, has_body: true, .. },
kind: hir::ItemKind::Fn { sig, body, generics, has_body: true, .. },
..
})
| hir::Node::TraitItem(hir::TraitItem {
kind: hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body)),
generics,
..
})
| hir::Node::ImplItem(hir::ImplItem {
kind: hir::ImplItemKind::Fn(sig, body), ..
kind: hir::ImplItemKind::Fn(sig, body),
generics,
..
}) = node
&& let hir::Node::Expr(expr) = self.tcx.hir_node(body.hir_id)
&& let hir::ExprKind::Block(block, _) = expr.kind
Expand All @@ -925,6 +927,27 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
ty.span,
format!("this `dyn Trait` has an implicit `'static` lifetime bound"),
);
let lt_name = "'a";
if generics
.params
.iter()
.filter(|p| matches!(p.kind, GenericParamKind::Lifetime { .. }))
.count()
== 0
{
let (span, suggestion) = if let Some(span) = generics.span_for_lifetime_suggestion()
{
(span, format!("{lt_name}, "))
} else {
(generics.span, format!("<{lt_name}>"))
};

err.multipart_suggestion(
msg,
vec![(span, suggestion), (ty.span.shrink_to_hi(), format!(" + {lt_name}"))],
Applicability::MaybeIncorrect,
);
}
}

err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ help: consider adding an explicit lifetime bound
|
LL | fn bb<R: 'static>(r: R) -> Box<dyn Foo> {
| +++++++++
help: consider adding an explicit lifetime bound
|
LL | fn bb<'a, R>(r: R) -> Box<dyn Foo + 'a> {
| +++ ++++

error[E0310]: the parameter type `T` may not live long enough
--> $DIR/implicit-static-lifetime-in-dyn-trait-return-type.rs:27:9
Expand All @@ -46,6 +50,10 @@ help: consider adding an explicit lifetime bound
|
LL | impl<T: 'static> Qux<T> {
| +++++++++
help: consider adding an explicit lifetime bound
|
LL | fn baz<'a>(self) -> Box<dyn Baz + 'a> {
| ++++ ++++

error: aborting due to 3 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ help: consider adding an explicit lifetime bound
|
LL | executor: impl FnOnce(T) -> (dyn Future<Output = ()>) + 'static,
| + +++++++++++
help: consider adding an explicit lifetime bound
|
LL ~ pub fn dyn_func<'a, T>(
LL | executor: impl FnOnce(T) -> dyn Future<Output = ()>,
LL ~ ) -> Box<dyn FnOnce(T) -> dyn Future<Output = ()> + 'a> {
|

error: aborting due to 3 previous errors

Expand Down
Loading