1 //! Error Reporting for static impl Traits.
3 use crate::infer
::error_reporting
::nice_region_error
::NiceRegionError
;
4 use crate::infer
::lexical_region_resolve
::RegionResolutionError
;
5 use crate::infer
::{SubregionOrigin, TypeTrace}
;
6 use crate::traits
::{ObligationCauseCode, UnifyReceiverContext}
;
7 use rustc_errors
::{struct_span_err, Applicability, DiagnosticBuilder, ErrorReported}
;
8 use rustc_hir
::def_id
::DefId
;
9 use rustc_hir
::intravisit
::{walk_ty, ErasedMap, NestedVisitorMap, Visitor}
;
10 use rustc_hir
::{self as hir, GenericBound, Item, ItemKind, Lifetime, LifetimeName, Node, TyKind}
;
11 use rustc_middle
::ty
::{self, AssocItemContainer, RegionKind, Ty, TypeFoldable, TypeVisitor}
;
12 use rustc_span
::symbol
::Ident
;
13 use rustc_span
::{MultiSpan, Span}
;
15 use std
::ops
::ControlFlow
;
17 impl<'a
, 'tcx
> NiceRegionError
<'a
, 'tcx
> {
18 /// Print the error message for lifetime errors when the return type is a static `impl Trait`,
19 /// `dyn Trait` or if a method call on a trait object introduces a static requirement.
20 pub(super) fn try_report_static_impl_trait(&self) -> Option
<ErrorReported
> {
21 debug
!("try_report_static_impl_trait(error={:?})", self.error
);
23 let (var_origin
, sub_origin
, sub_r
, sup_origin
, sup_r
) = match self.error
.as_ref()?
{
24 RegionResolutionError
::SubSupConflict(
31 ) if **sub_r
== RegionKind
::ReStatic
=> {
32 (var_origin
, sub_origin
, sub_r
, sup_origin
, sup_r
)
34 RegionResolutionError
::ConcreteFailure(
35 SubregionOrigin
::Subtype(box TypeTrace { cause, .. }
),
38 ) if **sub_r
== RegionKind
::ReStatic
=> {
39 // This is for an implicit `'static` requirement coming from `impl dyn Trait {}`.
40 if let ObligationCauseCode
::UnifyReceiver(ctxt
) = &cause
.code
{
41 // This may have a closure and it would cause ICE
42 // through `find_param_with_region` (#78262).
43 let anon_reg_sup
= tcx
.is_suitable_region(sup_r
)?
;
44 let fn_returns
= tcx
.return_type_impl_or_dyn_traits(anon_reg_sup
.def_id
);
45 if fn_returns
.is_empty() {
49 let param
= self.find_param_with_region(sup_r
, sub_r
)?
;
50 let lifetime
= if sup_r
.has_name() {
51 format
!("lifetime `{}`", sup_r
)
53 "an anonymous lifetime `'_`".to_string()
55 let mut err
= struct_span_err
!(
59 "{} has {} but calling `{}` introduces an implicit `'static` lifetime \
65 .map(|s
| format
!("`{}`", s
))
66 .unwrap_or_else(|| "`fn` parameter".to_string()),
68 ctxt
.assoc_item
.ident
,
70 err
.span_label(param
.param_ty_span
, &format
!("this data with {}...", lifetime
));
74 "...is captured and required to live as long as `'static` here \
75 because of an implicit lifetime bound on the {}",
76 match ctxt
.assoc_item
.container
{
77 AssocItemContainer
::TraitContainer(id
) =>
78 format
!("`impl` of `{}`", tcx
.def_path_str(id
)),
79 AssocItemContainer
::ImplContainer(_
) =>
80 "inherent `impl`".to_string(),
84 if self.find_impl_on_dyn_trait(&mut err
, param
.param_ty
, &ctxt
) {
86 return Some(ErrorReported
);
96 "try_report_static_impl_trait(var={:?}, sub={:?} {:?} sup={:?} {:?})",
97 var_origin
, sub_origin
, sub_r
, sup_origin
, sup_r
99 let anon_reg_sup
= tcx
.is_suitable_region(sup_r
)?
;
100 debug
!("try_report_static_impl_trait: anon_reg_sup={:?}", anon_reg_sup
);
101 let sp
= var_origin
.span();
102 let return_sp
= sub_origin
.span();
103 let param
= self.find_param_with_region(sup_r
, sub_r
)?
;
104 let (lifetime_name
, lifetime
) = if sup_r
.has_name() {
105 (sup_r
.to_string(), format
!("lifetime `{}`", sup_r
))
107 ("'_".to_owned(), "an anonymous lifetime `'_`".to_string())
109 let param_name
= param
113 .map(|s
| format
!("`{}`", s
))
114 .unwrap_or_else(|| "`fn` parameter".to_string());
115 let mut err
= struct_span_err
!(
119 "{} has {} but it needs to satisfy a `'static` lifetime requirement",
123 err
.span_label(param
.param_ty_span
, &format
!("this data with {}...", lifetime
));
124 debug
!("try_report_static_impl_trait: param_info={:?}", param
);
126 // We try to make the output have fewer overlapping spans if possible.
127 if (sp
== sup_origin
.span() || !return_sp
.overlaps(sup_origin
.span()))
128 && sup_origin
.span() != return_sp
130 // FIXME: account for `async fn` like in `async-await/issues/issue-62097.rs`
132 // Customize the spans and labels depending on their relative order so
133 // that split sentences flow correctly.
134 if sup_origin
.span().overlaps(return_sp
) && sp
== sup_origin
.span() {
135 // Avoid the following:
137 // error: cannot infer an appropriate lifetime
138 // --> $DIR/must_outlive_least_region_or_bound.rs:18:50
140 // LL | fn foo(x: &i32) -> Box<dyn Debug> { Box::new(x) }
141 // | ---- ---------^-
145 // error: cannot infer an appropriate lifetime
146 // --> $DIR/must_outlive_least_region_or_bound.rs:18:50
148 // LL | fn foo(x: &i32) -> Box<dyn Debug> { Box::new(x) }
152 "...is captured here, requiring it to live as long as `'static`",
155 err
.span_label(sup_origin
.span(), "...is captured here...");
156 if return_sp
< sup_origin
.span() {
159 "...and is required to live as long as `'static` here",
164 "...and is required to live as long as `'static` here",
171 "...is captured and required to live as long as `'static` here",
175 let fn_returns
= tcx
.return_type_impl_or_dyn_traits(anon_reg_sup
.def_id
);
177 let mut override_error_code
= None
;
178 if let SubregionOrigin
::Subtype(box TypeTrace { cause, .. }
) = &sup_origin
{
179 if let ObligationCauseCode
::UnifyReceiver(ctxt
) = &cause
.code
{
180 // Handle case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a
181 // `'static` lifetime when called as a method on a binding: `bar.qux()`.
182 if self.find_impl_on_dyn_trait(&mut err
, param
.param_ty
, &ctxt
) {
183 override_error_code
= Some(ctxt
.assoc_item
.ident
);
187 if let SubregionOrigin
::Subtype(box TypeTrace { cause, .. }
) = &sub_origin
{
188 if let ObligationCauseCode
::ItemObligation(item_def_id
) = cause
.code
{
189 // Same case of `impl Foo for dyn Bar { fn qux(&self) {} }` introducing a `'static`
190 // lifetime as above, but called using a fully-qualified path to the method:
192 let mut v
= TraitObjectVisitor(vec
![]);
193 v
.visit_ty(param
.param_ty
);
194 if let Some((ident
, self_ty
)) =
195 self.get_impl_ident_and_self_ty_from_trait(item_def_id
, &v
.0[..])
197 if self.suggest_constrain_dyn_trait_in_impl(&mut err
, &v
.0[..], ident
, self_ty
)
199 override_error_code
= Some(ident
);
204 if let (Some(ident
), true) = (override_error_code
, fn_returns
.is_empty()) {
205 // Provide a more targeted error code and description.
206 err
.code(rustc_errors
::error_code
!(E0772
));
207 err
.set_primary_message(&format
!(
208 "{} has {} but calling `{}` introduces an implicit `'static` lifetime \
210 param_name
, lifetime
, ident
,
214 debug
!("try_report_static_impl_trait: fn_return={:?}", fn_returns
);
215 // FIXME: account for the need of parens in `&(dyn Trait + '_)`
216 let consider
= "consider changing the";
217 let declare
= "to declare that the";
218 let arg
= match param
.param
.pat
.simple_ident() {
219 Some(simple_ident
) => format
!("argument `{}`", simple_ident
),
220 None
=> "the argument".to_string(),
222 let explicit
= format
!("you can add an explicit `{}` lifetime bound", lifetime_name
);
223 let explicit_static
= format
!("explicit `'static` bound to the lifetime of {}", arg
);
224 let captures
= format
!("captures data from {}", arg
);
225 let add_static_bound
= "alternatively, add an explicit `'static` bound to this reference";
226 let plus_lt
= format
!(" + {}", lifetime_name
);
227 for fn_return
in fn_returns
{
228 if fn_return
.span
.desugaring_kind().is_some() {
229 // Skip `async` desugaring `impl Future`.
232 match fn_return
.kind
{
233 TyKind
::OpaqueDef(item_id
, _
) => {
234 let item
= tcx
.hir().item(item_id
);
235 let opaque
= if let ItemKind
::OpaqueTy(opaque
) = &item
.kind
{
239 return Some(ErrorReported
);
242 if let Some(span
) = opaque
245 .filter_map(|arg
| match arg
{
246 GenericBound
::Outlives(Lifetime
{
247 name
: LifetimeName
::Static
,
255 err
.span_suggestion_verbose(
257 &format
!("{} `impl Trait`'s {}", consider
, explicit_static
),
258 lifetime_name
.clone(),
259 Applicability
::MaybeIncorrect
,
261 err
.span_suggestion_verbose(
264 param
.param_ty
.to_string(),
265 Applicability
::MaybeIncorrect
,
270 .filter_map(|arg
| match arg
{
271 GenericBound
::Outlives(Lifetime { name, span, .. }
)
272 if name
.ident().to_string() == lifetime_name
=>
282 err
.span_suggestion_verbose(
283 fn_return
.span
.shrink_to_hi(),
285 "{declare} `impl Trait` {captures}, {explicit}",
291 Applicability
::MaybeIncorrect
,
295 TyKind
::TraitObject(_
, lt
, _
) => match lt
.name
{
296 LifetimeName
::ImplicitObjectLifetimeDefault
=> {
297 err
.span_suggestion_verbose(
298 fn_return
.span
.shrink_to_hi(),
300 "{declare} trait object {captures}, {explicit}",
306 Applicability
::MaybeIncorrect
,
309 name
if name
.ident().to_string() != lifetime_name
=> {
310 // With this check we avoid suggesting redundant bounds. This
311 // would happen if there are nested impl/dyn traits and only
312 // one of them has the bound we'd suggest already there, like
313 // in `impl Foo<X = dyn Bar> + '_`.
314 err
.span_suggestion_verbose(
316 &format
!("{} trait object's {}", consider
, explicit_static
),
317 lifetime_name
.clone(),
318 Applicability
::MaybeIncorrect
,
320 err
.span_suggestion_verbose(
323 param
.param_ty
.to_string(),
324 Applicability
::MaybeIncorrect
,
336 fn get_impl_ident_and_self_ty_from_trait(
339 trait_objects
: &[DefId
],
340 ) -> Option
<(Ident
, &'tcx hir
::Ty
<'tcx
>)> {
341 let tcx
= self.tcx();
342 match tcx
.hir().get_if_local(def_id
) {
343 Some(Node
::ImplItem(impl_item
)) => {
344 match tcx
.hir().find(tcx
.hir().get_parent_item(impl_item
.hir_id())) {
345 Some(Node
::Item(Item
{
346 kind
: ItemKind
::Impl(hir
::Impl { self_ty, .. }
),
348 })) => Some((impl_item
.ident
, self_ty
)),
352 Some(Node
::TraitItem(trait_item
)) => {
353 let parent_id
= tcx
.hir().get_parent_item(trait_item
.hir_id());
354 match tcx
.hir().find(parent_id
) {
355 Some(Node
::Item(Item { kind: ItemKind::Trait(..), .. }
)) => {
356 // The method being called is defined in the `trait`, but the `'static`
357 // obligation comes from the `impl`. Find that `impl` so that we can point
358 // at it in the suggestion.
359 let trait_did
= tcx
.hir().local_def_id(parent_id
).to_def_id();
362 .trait_impls(trait_did
)
364 .filter_map(|&impl_did
| {
365 match tcx
.hir().get_if_local(impl_did
.to_def_id()) {
366 Some(Node
::Item(Item
{
367 kind
: ItemKind
::Impl(hir
::Impl { self_ty, .. }
),
369 })) if trait_objects
.iter().all(|did
| {
370 // FIXME: we should check `self_ty` against the receiver
371 // type in the `UnifyReceiver` context, but for now, use
372 // this imperfect proxy. This will fail if there are
373 // multiple `impl`s for the same trait like
374 // `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
375 // In that case, only the first one will get suggestions.
376 let mut hir_v
= HirTraitObjectVisitor(vec
![], *did
);
377 hir_v
.visit_ty(self_ty
);
388 Some(self_ty
) => Some((trait_item
.ident
, self_ty
)),
399 /// When we call a method coming from an `impl Foo for dyn Bar`, `dyn Bar` introduces a default
400 /// `'static` obligation. Suggest relaxing that implicit bound.
401 fn find_impl_on_dyn_trait(
403 err
: &mut DiagnosticBuilder
<'_
>,
405 ctxt
: &UnifyReceiverContext
<'tcx
>,
407 let tcx
= self.tcx();
409 // Find the method being called.
410 let instance
= match ty
::Instance
::resolve(
413 ctxt
.assoc_item
.def_id
,
414 self.infcx
.resolve_vars_if_possible(ctxt
.substs
),
416 Ok(Some(instance
)) => instance
,
420 let mut v
= TraitObjectVisitor(vec
![]);
423 // Get the `Ident` of the method being called and the corresponding `impl` (to point at
424 // `Bar` in `impl Foo for dyn Bar {}` and the definition of the method being called).
425 let (ident
, self_ty
) =
426 match self.get_impl_ident_and_self_ty_from_trait(instance
.def_id(), &v
.0[..]) {
427 Some((ident
, self_ty
)) => (ident
, self_ty
),
428 None
=> return false,
431 // Find the trait object types in the argument, so we point at *only* the trait object.
432 self.suggest_constrain_dyn_trait_in_impl(err
, &v
.0[..], ident
, self_ty
)
435 fn suggest_constrain_dyn_trait_in_impl(
437 err
: &mut DiagnosticBuilder
<'_
>,
438 found_dids
: &[DefId
],
440 self_ty
: &hir
::Ty
<'_
>,
442 let mut suggested
= false;
443 for found_did
in found_dids
{
444 let mut hir_v
= HirTraitObjectVisitor(vec
![], *found_did
);
445 hir_v
.visit_ty(&self_ty
);
446 for span
in &hir_v
.0 {
447 let mut multi_span
: MultiSpan
= vec
![*span
].into();
448 multi_span
.push_span_label(
450 "this has an implicit `'static` lifetime requirement".to_string(),
452 multi_span
.push_span_label(
454 "calling this method introduces the `impl`'s 'static` requirement".to_string(),
456 err
.span_note(multi_span
, "the used `impl` has a `'static` requirement");
457 err
.span_suggestion_verbose(
459 "consider relaxing the implicit `'static` requirement",
461 Applicability
::MaybeIncorrect
,
470 /// Collect all the trait objects in a type that could have received an implicit `'static` lifetime.
471 struct TraitObjectVisitor(Vec
<DefId
>);
473 impl TypeVisitor
<'_
> for TraitObjectVisitor
{
474 fn visit_ty(&mut self, t
: Ty
<'_
>) -> ControlFlow
<Self::BreakTy
> {
476 ty
::Dynamic(preds
, RegionKind
::ReStatic
) => {
477 if let Some(def_id
) = preds
.principal_def_id() {
480 ControlFlow
::CONTINUE
482 _
=> t
.super_visit_with(self),
487 /// Collect all `hir::Ty<'_>` `Span`s for trait objects with an implicit lifetime.
488 struct HirTraitObjectVisitor(Vec
<Span
>, DefId
);
490 impl<'tcx
> Visitor
<'tcx
> for HirTraitObjectVisitor
{
491 type Map
= ErasedMap
<'tcx
>;
493 fn nested_visit_map(&mut self) -> NestedVisitorMap
<Self::Map
> {
494 NestedVisitorMap
::None
497 fn visit_ty(&mut self, t
: &'tcx hir
::Ty
<'tcx
>) {
498 if let TyKind
::TraitObject(
500 Lifetime { name: LifetimeName::ImplicitObjectLifetimeDefault, .. }
,
504 for ptr
in poly_trait_refs
{
505 if Some(self.1) == ptr
.trait_ref
.trait_def_id() {
506 self.0.push(ptr
.span
);