1use rustc_data_structures::fx::FxIndexSet;
4use rustc_errors::{Applicability, Diag, ErrorGuaranteed, MultiSpan};
5use rustc_hir as hir;
6use rustc_hir::GenericBound::Trait;
7use rustc_hir::QPath::Resolved;
8use rustc_hir::WherePredicateKind::BoundPredicate;
9use rustc_hir::def::Res::Def;
10use rustc_hir::def_id::DefId;
11use rustc_hir::intravisit::VisitorExt;
12use rustc_hir::{PolyTraitRef, TyKind, WhereBoundPredicate};
13use rustc_infer::infer::{NllRegionVariableOrigin, SubregionOrigin};
14use rustc_middle::bug;
15use rustc_middle::hir::place::PlaceBase;
16use rustc_middle::mir::{AnnotationSource, ConstraintCategory, ReturnConstraint};
17use rustc_middle::ty::{
18 self, GenericArgs, Region, RegionVid, Ty, TyCtxt, TypeFoldable, TypeVisitor, fold_regions,
19};
20use rustc_span::{Ident, Span, kw};
21use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
22use rustc_trait_selection::error_reporting::infer::nice_region_error::{
23 self, HirTraitObjectVisitor, NiceRegionError, TraitObjectVisitor, find_anon_type,
24 find_param_with_region, suggest_adding_lifetime_params,
25};
26use rustc_trait_selection::error_reporting::infer::region::unexpected_hidden_region_diagnostic;
27use rustc_trait_selection::infer::InferCtxtExt;
28use rustc_trait_selection::traits::{Obligation, ObligationCtxt};
29use tracing::{debug, instrument, trace};
30
31use super::{OutlivesSuggestionBuilder, RegionName, RegionNameSource};
32use crate::nll::ConstraintDescription;
33use crate::region_infer::values::RegionElement;
34use crate::region_infer::{BlameConstraint, TypeTest};
35use crate::session_diagnostics::{
36 FnMutError, FnMutReturnTypeErr, GenericDoesNotLiveLongEnough, LifetimeOutliveErr,
37 LifetimeReturnCategoryErr, RequireStaticErr, VarHereDenote,
38};
39use crate::universal_regions::DefiningTy;
40use crate::{MirBorrowckCtxt, borrowck_errors, fluent_generated as fluent};
41
42impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> {
43 fn description(&self) -> &'static str {
44 match self {
46 ConstraintCategory::Assignment => "assignment ",
47 ConstraintCategory::Return(_) => "returning this value ",
48 ConstraintCategory::Yield => "yielding this value ",
49 ConstraintCategory::UseAsConst => "using this value as a constant ",
50 ConstraintCategory::UseAsStatic => "using this value as a static ",
51 ConstraintCategory::Cast { is_implicit_coercion: false, .. } => "cast ",
52 ConstraintCategory::Cast { is_implicit_coercion: true, .. } => "coercion ",
53 ConstraintCategory::CallArgument(_) => "argument ",
54 ConstraintCategory::TypeAnnotation(AnnotationSource::GenericArg) => "generic argument ",
55 ConstraintCategory::TypeAnnotation(_) => "type annotation ",
56 ConstraintCategory::SizedBound => "proving this value is `Sized` ",
57 ConstraintCategory::CopyBound => "copying this value ",
58 ConstraintCategory::OpaqueType => "opaque type ",
59 ConstraintCategory::ClosureUpvar(_) => "closure capture ",
60 ConstraintCategory::Usage => "this usage ",
61 ConstraintCategory::Predicate(_)
62 | ConstraintCategory::Boring
63 | ConstraintCategory::BoringNoLocation
64 | ConstraintCategory::Internal
65 | ConstraintCategory::IllegalUniverse => "",
66 }
67 }
68}
69
70pub(crate) struct RegionErrors<'tcx>(Vec<(RegionErrorKind<'tcx>, ErrorGuaranteed)>, TyCtxt<'tcx>);
76
77impl<'tcx> RegionErrors<'tcx> {
78 pub(crate) fn new(tcx: TyCtxt<'tcx>) -> Self {
79 Self(vec![], tcx)
80 }
81 #[track_caller]
82 pub(crate) fn push(&mut self, val: impl Into<RegionErrorKind<'tcx>>) {
83 let val = val.into();
84 let guar = self.1.sess.dcx().delayed_bug(format!("{val:?}"));
85 self.0.push((val, guar));
86 }
87 pub(crate) fn is_empty(&self) -> bool {
88 self.0.is_empty()
89 }
90 pub(crate) fn into_iter(
91 self,
92 ) -> impl Iterator<Item = (RegionErrorKind<'tcx>, ErrorGuaranteed)> {
93 self.0.into_iter()
94 }
95 pub(crate) fn has_errors(&self) -> Option<ErrorGuaranteed> {
96 self.0.get(0).map(|x| x.1)
97 }
98}
99
100impl std::fmt::Debug for RegionErrors<'_> {
101 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102 f.debug_tuple("RegionErrors").field(&self.0).finish()
103 }
104}
105
106#[derive(Clone, Debug)]
107pub(crate) enum RegionErrorKind<'tcx> {
108 TypeTestError { type_test: TypeTest<'tcx> },
110
111 UnexpectedHiddenRegion {
113 span: Span,
115 hidden_ty: Ty<'tcx>,
117 key: ty::OpaqueTypeKey<'tcx>,
119 member_region: ty::Region<'tcx>,
121 },
122
123 BoundUniversalRegionError {
125 longer_fr: RegionVid,
127 error_element: RegionElement,
129 placeholder: ty::PlaceholderRegion,
131 },
132
133 RegionError {
135 fr_origin: NllRegionVariableOrigin,
137 longer_fr: RegionVid,
139 shorter_fr: RegionVid,
141 is_reported: bool,
144 },
145}
146
147#[derive(Clone, Debug)]
149pub(crate) struct ErrorConstraintInfo<'tcx> {
150 pub(super) fr: RegionVid,
152 pub(super) outlived_fr: RegionVid,
153
154 pub(super) category: ConstraintCategory<'tcx>,
156 pub(super) span: Span,
157}
158
159impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
160 pub(super) fn to_error_region(&self, r: RegionVid) -> Option<ty::Region<'tcx>> {
167 self.to_error_region_vid(r).and_then(|r| self.regioncx.region_definition(r).external_name)
168 }
169
170 pub(super) fn to_error_region_vid(&self, r: RegionVid) -> Option<RegionVid> {
173 if self.regioncx.universal_regions().is_universal_region(r) {
174 Some(r)
175 } else {
176 let upper_bound = self.regioncx.approx_universal_upper_bound(r);
179
180 if self.regioncx.upper_bound_in_region_scc(r, upper_bound) {
181 self.to_error_region_vid(upper_bound)
182 } else {
183 None
184 }
185 }
186 }
187
188 fn name_regions<T>(&self, tcx: TyCtxt<'tcx>, ty: T) -> T
190 where
191 T: TypeFoldable<TyCtxt<'tcx>>,
192 {
193 fold_regions(tcx, ty, |region, _| match region.kind() {
194 ty::ReVar(vid) => self.to_error_region(vid).unwrap_or(region),
195 _ => region,
196 })
197 }
198
199 fn is_closure_fn_mut(&self, fr: RegionVid) -> bool {
201 if let Some(r) = self.to_error_region(fr)
202 && let ty::ReLateParam(late_param) = r.kind()
203 && let ty::LateParamRegionKind::ClosureEnv = late_param.kind
204 && let DefiningTy::Closure(_, args) = self.regioncx.universal_regions().defining_ty
205 {
206 return args.as_closure().kind() == ty::ClosureKind::FnMut;
207 }
208
209 false
210 }
211
212 #[allow(rustc::diagnostic_outside_of_impl)]
216 fn suggest_static_lifetime_for_gat_from_hrtb(
217 &self,
218 diag: &mut Diag<'_>,
219 lower_bound: RegionVid,
220 ) {
221 let mut suggestions = vec![];
222 let tcx = self.infcx.tcx;
223
224 let gat_id_and_generics = self
226 .regioncx
227 .placeholders_contained_in(lower_bound)
228 .map(|placeholder| {
229 if let Some(id) = placeholder.bound.kind.get_id()
230 && let Some(placeholder_id) = id.as_local()
231 && let gat_hir_id = tcx.local_def_id_to_hir_id(placeholder_id)
232 && let Some(generics_impl) =
233 tcx.parent_hir_node(tcx.parent_hir_id(gat_hir_id)).generics()
234 {
235 Some((gat_hir_id, generics_impl))
236 } else {
237 None
238 }
239 })
240 .collect::<Vec<_>>();
241 debug!(?gat_id_and_generics);
242
243 let mut hrtb_bounds = vec![];
245 gat_id_and_generics.iter().flatten().for_each(|(gat_hir_id, generics)| {
246 for pred in generics.predicates {
247 let BoundPredicate(WhereBoundPredicate { bound_generic_params, bounds, .. }) =
248 pred.kind
249 else {
250 continue;
251 };
252 if bound_generic_params
253 .iter()
254 .rfind(|bgp| tcx.local_def_id_to_hir_id(bgp.def_id) == *gat_hir_id)
255 .is_some()
256 {
257 for bound in *bounds {
258 hrtb_bounds.push(bound);
259 }
260 }
261 }
262 });
263 debug!(?hrtb_bounds);
264
265 hrtb_bounds.iter().for_each(|bound| {
266 let Trait(PolyTraitRef { trait_ref, span: trait_span, .. }) = bound else {
267 return;
268 };
269 diag.span_note(*trait_span, fluent::borrowck_limitations_implies_static);
270 let Some(generics_fn) = tcx.hir_get_generics(self.body.source.def_id().expect_local())
271 else {
272 return;
273 };
274 let Def(_, trait_res_defid) = trait_ref.path.res else {
275 return;
276 };
277 debug!(?generics_fn);
278 generics_fn.predicates.iter().for_each(|predicate| {
279 let BoundPredicate(WhereBoundPredicate { bounded_ty, bounds, .. }) = predicate.kind
280 else {
281 return;
282 };
283 bounds.iter().for_each(|bd| {
284 if let Trait(PolyTraitRef { trait_ref: tr_ref, .. }) = bd
285 && let Def(_, res_defid) = tr_ref.path.res
286 && res_defid == trait_res_defid && let TyKind::Path(Resolved(_, path)) = bounded_ty.kind
288 && let Def(_, defid) = path.res
289 && generics_fn.params
290 .iter()
291 .rfind(|param| param.def_id.to_def_id() == defid)
292 .is_some()
293 {
294 suggestions.push((predicate.span.shrink_to_hi(), " + 'static".to_string()));
295 }
296 });
297 });
298 });
299 if suggestions.len() > 0 {
300 suggestions.dedup();
301 diag.multipart_suggestion_verbose(
302 fluent::borrowck_restrict_to_static,
303 suggestions,
304 Applicability::MaybeIncorrect,
305 );
306 }
307 }
308
309 pub(crate) fn report_region_errors(&mut self, nll_errors: RegionErrors<'tcx>) {
311 let mut outlives_suggestion = OutlivesSuggestionBuilder::default();
315 let mut last_unexpected_hidden_region: Option<(Span, Ty<'_>, ty::OpaqueTypeKey<'tcx>)> =
316 None;
317
318 for (nll_error, _) in nll_errors.into_iter() {
319 match nll_error {
320 RegionErrorKind::TypeTestError { type_test } => {
321 let lower_bound_region = self.to_error_region(type_test.lower_bound);
324
325 let type_test_span = type_test.span;
326
327 if let Some(lower_bound_region) = lower_bound_region {
328 let generic_ty = self.name_regions(
329 self.infcx.tcx,
330 type_test.generic_kind.to_ty(self.infcx.tcx),
331 );
332 let origin =
333 SubregionOrigin::RelateParamBound(type_test_span, generic_ty, None);
334 self.buffer_error(self.infcx.err_ctxt().construct_generic_bound_failure(
335 self.body.source.def_id().expect_local(),
336 type_test_span,
337 Some(origin),
338 self.name_regions(self.infcx.tcx, type_test.generic_kind),
339 lower_bound_region,
340 ));
341 } else {
342 let mut diag = self.dcx().create_err(GenericDoesNotLiveLongEnough {
352 kind: type_test.generic_kind.to_string(),
353 span: type_test_span,
354 });
355
356 self.suggest_static_lifetime_for_gat_from_hrtb(
360 &mut diag,
361 type_test.lower_bound,
362 );
363
364 self.buffer_error(diag);
365 }
366 }
367
368 RegionErrorKind::UnexpectedHiddenRegion { span, hidden_ty, key, member_region } => {
369 let named_ty =
370 self.regioncx.name_regions_for_member_constraint(self.infcx.tcx, hidden_ty);
371 let named_key =
372 self.regioncx.name_regions_for_member_constraint(self.infcx.tcx, key);
373 let named_region = self
374 .regioncx
375 .name_regions_for_member_constraint(self.infcx.tcx, member_region);
376 let diag = unexpected_hidden_region_diagnostic(
377 self.infcx,
378 self.mir_def_id(),
379 span,
380 named_ty,
381 named_region,
382 named_key,
383 );
384 if last_unexpected_hidden_region != Some((span, named_ty, named_key)) {
385 self.buffer_error(diag);
386 last_unexpected_hidden_region = Some((span, named_ty, named_key));
387 } else {
388 diag.delay_as_bug();
389 }
390 }
391
392 RegionErrorKind::BoundUniversalRegionError {
393 longer_fr,
394 placeholder,
395 error_element,
396 } => {
397 let error_vid = self.regioncx.region_from_element(longer_fr, &error_element);
398
399 let (_, cause) = self.regioncx.find_outlives_blame_span(
401 longer_fr,
402 NllRegionVariableOrigin::Placeholder(placeholder),
403 error_vid,
404 );
405
406 let universe = placeholder.universe;
407 let universe_info = self.regioncx.universe_info(universe);
408
409 universe_info.report_erroneous_element(self, placeholder, error_element, cause);
410 }
411
412 RegionErrorKind::RegionError { fr_origin, longer_fr, shorter_fr, is_reported } => {
413 if is_reported {
414 self.report_region_error(
415 longer_fr,
416 fr_origin,
417 shorter_fr,
418 &mut outlives_suggestion,
419 );
420 } else {
421 debug!(
428 "Unreported region error: can't prove that {:?}: {:?}",
429 longer_fr, shorter_fr
430 );
431 }
432 }
433 }
434 }
435
436 outlives_suggestion.add_suggestion(self);
438 }
439
440 #[allow(rustc::diagnostic_outside_of_impl)]
450 #[allow(rustc::untranslatable_diagnostic)]
451 pub(crate) fn report_region_error(
452 &mut self,
453 fr: RegionVid,
454 fr_origin: NllRegionVariableOrigin,
455 outlived_fr: RegionVid,
456 outlives_suggestion: &mut OutlivesSuggestionBuilder,
457 ) {
458 debug!("report_region_error(fr={:?}, outlived_fr={:?})", fr, outlived_fr);
459
460 let (blame_constraint, path) = self.regioncx.best_blame_constraint(fr, fr_origin, |r| {
461 self.regioncx.provides_universal_region(r, fr, outlived_fr)
462 });
463 let BlameConstraint { category, cause, variance_info, .. } = blame_constraint;
464
465 debug!("report_region_error: category={:?} {:?} {:?}", category, cause, variance_info);
466
467 if let (Some(f), Some(o)) = (self.to_error_region(fr), self.to_error_region(outlived_fr)) {
469 let infer_err = self.infcx.err_ctxt();
470 let nice =
471 NiceRegionError::new_from_span(&infer_err, self.mir_def_id(), cause.span, o, f);
472 if let Some(diag) = nice.try_report_from_nll() {
473 self.buffer_error(diag);
474 return;
475 }
476 }
477
478 let (fr_is_local, outlived_fr_is_local): (bool, bool) = (
479 self.regioncx.universal_regions().is_local_free_region(fr),
480 self.regioncx.universal_regions().is_local_free_region(outlived_fr),
481 );
482
483 debug!(
484 "report_region_error: fr_is_local={:?} outlived_fr_is_local={:?} category={:?}",
485 fr_is_local, outlived_fr_is_local, category
486 );
487
488 let errci = ErrorConstraintInfo { fr, outlived_fr, category, span: cause.span };
489
490 let mut diag = match (category, fr_is_local, outlived_fr_is_local) {
491 (ConstraintCategory::Return(kind), true, false) if self.is_closure_fn_mut(fr) => {
492 self.report_fnmut_error(&errci, kind)
493 }
494 (ConstraintCategory::Assignment, true, false)
495 | (ConstraintCategory::CallArgument(_), true, false) => {
496 let mut db = self.report_escaping_data_error(&errci);
497
498 outlives_suggestion.intermediate_suggestion(self, &errci, &mut db);
499 outlives_suggestion.collect_constraint(fr, outlived_fr);
500
501 db
502 }
503 _ => {
504 let mut db = self.report_general_error(&errci);
505
506 outlives_suggestion.intermediate_suggestion(self, &errci, &mut db);
507 outlives_suggestion.collect_constraint(fr, outlived_fr);
508
509 db
510 }
511 };
512
513 match variance_info {
514 ty::VarianceDiagInfo::None => {}
515 ty::VarianceDiagInfo::Invariant { ty, param_index } => {
516 let (desc, note) = match ty.kind() {
517 ty::RawPtr(ty, mutbl) => {
518 assert_eq!(*mutbl, hir::Mutability::Mut);
519 (
520 format!("a mutable pointer to `{}`", ty),
521 "mutable pointers are invariant over their type parameter".to_string(),
522 )
523 }
524 ty::Ref(_, inner_ty, mutbl) => {
525 assert_eq!(*mutbl, hir::Mutability::Mut);
526 (
527 format!("a mutable reference to `{inner_ty}`"),
528 "mutable references are invariant over their type parameter"
529 .to_string(),
530 )
531 }
532 ty::Adt(adt, args) => {
533 let generic_arg = args[param_index as usize];
534 let identity_args =
535 GenericArgs::identity_for_item(self.infcx.tcx, adt.did());
536 let base_ty = Ty::new_adt(self.infcx.tcx, *adt, identity_args);
537 let base_generic_arg = identity_args[param_index as usize];
538 let adt_desc = adt.descr();
539
540 let desc = format!(
541 "the type `{ty}`, which makes the generic argument `{generic_arg}` invariant"
542 );
543 let note = format!(
544 "the {adt_desc} `{base_ty}` is invariant over the parameter `{base_generic_arg}`"
545 );
546 (desc, note)
547 }
548 ty::FnDef(def_id, _) => {
549 let name = self.infcx.tcx.item_name(*def_id);
550 let identity_args = GenericArgs::identity_for_item(self.infcx.tcx, *def_id);
551 let desc = format!("a function pointer to `{name}`");
552 let note = format!(
553 "the function `{name}` is invariant over the parameter `{}`",
554 identity_args[param_index as usize]
555 );
556 (desc, note)
557 }
558 _ => panic!("Unexpected type {ty:?}"),
559 };
560 diag.note(format!("requirement occurs because of {desc}",));
561 diag.note(note);
562 diag.help("see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance");
563 }
564 }
565
566 self.add_placeholder_from_predicate_note(&mut diag, &path);
567 self.add_sized_or_copy_bound_info(&mut diag, category, &path);
568
569 self.buffer_error(diag);
570 }
571
572 #[allow(rustc::diagnostic_outside_of_impl)] fn report_fnmut_error(
590 &self,
591 errci: &ErrorConstraintInfo<'tcx>,
592 kind: ReturnConstraint,
593 ) -> Diag<'infcx> {
594 let ErrorConstraintInfo { outlived_fr, span, .. } = errci;
595
596 let mut output_ty = self.regioncx.universal_regions().unnormalized_output_ty;
597 if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) = *output_ty.kind() {
598 output_ty = self.infcx.tcx.type_of(def_id).instantiate_identity()
599 };
600
601 debug!("report_fnmut_error: output_ty={:?}", output_ty);
602
603 let err = FnMutError {
604 span: *span,
605 ty_err: match output_ty.kind() {
606 ty::Coroutine(def, ..) if self.infcx.tcx.coroutine_is_async(*def) => {
607 FnMutReturnTypeErr::ReturnAsyncBlock { span: *span }
608 }
609 _ if output_ty.contains_closure() => {
610 FnMutReturnTypeErr::ReturnClosure { span: *span }
611 }
612 _ => FnMutReturnTypeErr::ReturnRef { span: *span },
613 },
614 };
615
616 let mut diag = self.dcx().create_err(err);
617
618 if let ReturnConstraint::ClosureUpvar(upvar_field) = kind {
619 let def_id = match self.regioncx.universal_regions().defining_ty {
620 DefiningTy::Closure(def_id, _) => def_id,
621 ty => bug!("unexpected DefiningTy {:?}", ty),
622 };
623
624 let captured_place = &self.upvars[upvar_field.index()].place;
625 let defined_hir = match captured_place.base {
626 PlaceBase::Local(hirid) => Some(hirid),
627 PlaceBase::Upvar(upvar) => Some(upvar.var_path.hir_id),
628 _ => None,
629 };
630
631 if let Some(def_hir) = defined_hir {
632 let upvars_map = self.infcx.tcx.upvars_mentioned(def_id).unwrap();
633 let upvar_def_span = self.infcx.tcx.hir_span(def_hir);
634 let upvar_span = upvars_map.get(&def_hir).unwrap().span;
635 diag.subdiagnostic(VarHereDenote::Defined { span: upvar_def_span });
636 diag.subdiagnostic(VarHereDenote::Captured { span: upvar_span });
637 }
638 }
639
640 if let Some(fr_span) = self.give_region_a_name(*outlived_fr).unwrap().span() {
641 diag.subdiagnostic(VarHereDenote::FnMutInferred { span: fr_span });
642 }
643
644 self.suggest_move_on_borrowing_closure(&mut diag);
645
646 diag
647 }
648
649 #[instrument(level = "debug", skip(self))]
662 fn report_escaping_data_error(&self, errci: &ErrorConstraintInfo<'tcx>) -> Diag<'infcx> {
663 let ErrorConstraintInfo { span, category, .. } = errci;
664
665 let fr_name_and_span = self.regioncx.get_var_name_and_span_for_region(
666 self.infcx.tcx,
667 self.body,
668 &self.local_names(),
669 &self.upvars,
670 errci.fr,
671 );
672 let outlived_fr_name_and_span = self.regioncx.get_var_name_and_span_for_region(
673 self.infcx.tcx,
674 self.body,
675 &self.local_names(),
676 &self.upvars,
677 errci.outlived_fr,
678 );
679
680 let escapes_from =
681 self.infcx.tcx.def_descr(self.regioncx.universal_regions().defining_ty.def_id());
682
683 if (fr_name_and_span.is_none() && outlived_fr_name_and_span.is_none())
686 || (*category == ConstraintCategory::Assignment
687 && self.regioncx.universal_regions().defining_ty.is_fn_def())
688 || self.regioncx.universal_regions().defining_ty.is_const()
689 {
690 return self.report_general_error(errci);
691 }
692
693 let mut diag =
694 borrowck_errors::borrowed_data_escapes_closure(self.infcx.tcx, *span, escapes_from);
695
696 if let Some((Some(outlived_fr_name), outlived_fr_span)) = outlived_fr_name_and_span {
697 #[allow(rustc::diagnostic_outside_of_impl)]
699 #[allow(rustc::untranslatable_diagnostic)]
700 diag.span_label(
701 outlived_fr_span,
702 format!("`{outlived_fr_name}` declared here, outside of the {escapes_from} body",),
703 );
704 }
705
706 #[allow(rustc::diagnostic_outside_of_impl)]
708 #[allow(rustc::untranslatable_diagnostic)]
709 if let Some((Some(fr_name), fr_span)) = fr_name_and_span {
710 diag.span_label(
711 fr_span,
712 format!(
713 "`{fr_name}` is a reference that is only valid in the {escapes_from} body",
714 ),
715 );
716
717 diag.span_label(*span, format!("`{fr_name}` escapes the {escapes_from} body here"));
718 }
719
720 match (self.to_error_region(errci.fr), self.to_error_region(errci.outlived_fr)) {
724 (Some(f), Some(o)) => {
725 self.maybe_suggest_constrain_dyn_trait_impl(&mut diag, f, o, category);
726
727 let fr_region_name = self.give_region_a_name(errci.fr).unwrap();
728 fr_region_name.highlight_region_name(&mut diag);
729 let outlived_fr_region_name = self.give_region_a_name(errci.outlived_fr).unwrap();
730 outlived_fr_region_name.highlight_region_name(&mut diag);
731
732 #[allow(rustc::diagnostic_outside_of_impl)]
734 #[allow(rustc::untranslatable_diagnostic)]
735 diag.span_label(
736 *span,
737 format!(
738 "{}requires that `{}` must outlive `{}`",
739 category.description(),
740 fr_region_name,
741 outlived_fr_region_name,
742 ),
743 );
744 }
745 _ => {}
746 }
747
748 diag
749 }
750
751 #[allow(rustc::diagnostic_outside_of_impl)] fn report_general_error(&self, errci: &ErrorConstraintInfo<'tcx>) -> Diag<'infcx> {
768 let ErrorConstraintInfo { fr, outlived_fr, span, category, .. } = errci;
769
770 let mir_def_name = self.infcx.tcx.def_descr(self.mir_def_id().to_def_id());
771
772 let err = LifetimeOutliveErr { span: *span };
773 let mut diag = self.dcx().create_err(err);
774
775 let fr_name = self.give_region_a_name(*fr).unwrap_or(RegionName {
780 name: kw::UnderscoreLifetime,
781 source: RegionNameSource::Static,
782 });
783 fr_name.highlight_region_name(&mut diag);
784 let outlived_fr_name = self.give_region_a_name(*outlived_fr).unwrap();
785 outlived_fr_name.highlight_region_name(&mut diag);
786
787 let err_category = if matches!(category, ConstraintCategory::Return(_))
788 && self.regioncx.universal_regions().is_local_free_region(*outlived_fr)
789 {
790 LifetimeReturnCategoryErr::WrongReturn {
791 span: *span,
792 mir_def_name,
793 outlived_fr_name,
794 fr_name: &fr_name,
795 }
796 } else {
797 LifetimeReturnCategoryErr::ShortReturn {
798 span: *span,
799 category_desc: category.description(),
800 free_region_name: &fr_name,
801 outlived_fr_name,
802 }
803 };
804
805 diag.subdiagnostic(err_category);
806
807 self.add_static_impl_trait_suggestion(&mut diag, *fr, fr_name, *outlived_fr);
808 self.suggest_adding_lifetime_params(&mut diag, *fr, *outlived_fr);
809 self.suggest_move_on_borrowing_closure(&mut diag);
810 self.suggest_deref_closure_return(&mut diag);
811
812 diag
813 }
814
815 #[allow(rustc::diagnostic_outside_of_impl)]
825 #[allow(rustc::untranslatable_diagnostic)] fn add_static_impl_trait_suggestion(
827 &self,
828 diag: &mut Diag<'_>,
829 fr: RegionVid,
830 fr_name: RegionName,
832 outlived_fr: RegionVid,
833 ) {
834 if let (Some(f), Some(outlived_f)) =
835 (self.to_error_region(fr), self.to_error_region(outlived_fr))
836 {
837 if outlived_f.kind() != ty::ReStatic {
838 return;
839 }
840 let suitable_region = self.infcx.tcx.is_suitable_region(self.mir_def_id(), f);
841 let Some(suitable_region) = suitable_region else {
842 return;
843 };
844
845 let fn_returns = self.infcx.tcx.return_type_impl_or_dyn_traits(suitable_region.scope);
846
847 let param = if let Some(param) =
848 find_param_with_region(self.infcx.tcx, self.mir_def_id(), f, outlived_f)
849 {
850 param
851 } else {
852 return;
853 };
854
855 let lifetime =
856 if f.is_named(self.infcx.tcx) { fr_name.name } else { kw::UnderscoreLifetime };
857
858 let arg = match param.param.pat.simple_ident() {
859 Some(simple_ident) => format!("argument `{simple_ident}`"),
860 None => "the argument".to_string(),
861 };
862 let captures = format!("captures data from {arg}");
863
864 if !fn_returns.is_empty() {
865 nice_region_error::suggest_new_region_bound(
866 self.infcx.tcx,
867 diag,
868 fn_returns,
869 lifetime.to_string(),
870 Some(arg),
871 captures,
872 Some((param.param_ty_span, param.param_ty.to_string())),
873 Some(suitable_region.scope),
874 );
875 return;
876 }
877
878 let Some((alias_tys, alias_span, lt_addition_span)) = self
879 .infcx
880 .tcx
881 .return_type_impl_or_dyn_traits_with_type_alias(suitable_region.scope)
882 else {
883 return;
884 };
885
886 let mut spans_suggs: Vec<_> = Vec::new();
888 for alias_ty in alias_tys {
889 if alias_ty.span.desugaring_kind().is_some() {
890 }
892 if let TyKind::TraitObject(_, lt) = alias_ty.kind {
893 if lt.kind == hir::LifetimeKind::ImplicitObjectLifetimeDefault {
894 spans_suggs.push((lt.ident.span.shrink_to_hi(), " + 'a".to_string()));
895 } else {
896 spans_suggs.push((lt.ident.span, "'a".to_string()));
897 }
898 }
899 }
900
901 if let Some(lt_addition_span) = lt_addition_span {
902 spans_suggs.push((lt_addition_span, "'a, ".to_string()));
903 } else {
904 spans_suggs.push((alias_span.shrink_to_hi(), "<'a>".to_string()));
905 }
906
907 diag.multipart_suggestion_verbose(
908 format!(
909 "to declare that the trait object {captures}, you can add a lifetime parameter `'a` in the type alias"
910 ),
911 spans_suggs,
912 Applicability::MaybeIncorrect,
913 );
914 }
915 }
916
917 fn maybe_suggest_constrain_dyn_trait_impl(
918 &self,
919 diag: &mut Diag<'_>,
920 f: Region<'tcx>,
921 o: Region<'tcx>,
922 category: &ConstraintCategory<'tcx>,
923 ) {
924 if !o.is_static() {
925 return;
926 }
927
928 let tcx = self.infcx.tcx;
929
930 let instance = if let ConstraintCategory::CallArgument(Some(func_ty)) = category {
931 let (fn_did, args) = match func_ty.kind() {
932 ty::FnDef(fn_did, args) => (fn_did, args),
933 _ => return,
934 };
935 debug!(?fn_did, ?args);
936
937 let ty = tcx.type_of(fn_did).instantiate_identity();
939 debug!("ty: {:?}, ty.kind: {:?}", ty, ty.kind());
940 if let ty::Closure(_, _) = ty.kind() {
941 return;
942 }
943
944 if let Ok(Some(instance)) = ty::Instance::try_resolve(
945 tcx,
946 self.infcx.typing_env(self.infcx.param_env),
947 *fn_did,
948 self.infcx.resolve_vars_if_possible(args),
949 ) {
950 instance
951 } else {
952 return;
953 }
954 } else {
955 return;
956 };
957
958 let param = match find_param_with_region(tcx, self.mir_def_id(), f, o) {
959 Some(param) => param,
960 None => return,
961 };
962 debug!(?param);
963
964 let mut visitor = TraitObjectVisitor(FxIndexSet::default());
965 visitor.visit_ty(param.param_ty);
966
967 let Some((ident, self_ty)) = NiceRegionError::get_impl_ident_and_self_ty_from_trait(
968 tcx,
969 instance.def_id(),
970 &visitor.0,
971 ) else {
972 return;
973 };
974
975 self.suggest_constrain_dyn_trait_in_impl(diag, &visitor.0, ident, self_ty);
976 }
977
978 #[allow(rustc::diagnostic_outside_of_impl)]
979 #[instrument(skip(self, err), level = "debug")]
980 fn suggest_constrain_dyn_trait_in_impl(
981 &self,
982 err: &mut Diag<'_>,
983 found_dids: &FxIndexSet<DefId>,
984 ident: Ident,
985 self_ty: &hir::Ty<'_>,
986 ) -> bool {
987 debug!("err: {:#?}", err);
988 let mut suggested = false;
989 for found_did in found_dids {
990 let mut traits = vec![];
991 let mut hir_v = HirTraitObjectVisitor(&mut traits, *found_did);
992 hir_v.visit_ty_unambig(self_ty);
993 debug!("trait spans found: {:?}", traits);
994 for span in &traits {
995 let mut multi_span: MultiSpan = vec![*span].into();
996 multi_span.push_span_label(*span, fluent::borrowck_implicit_static);
997 multi_span.push_span_label(ident.span, fluent::borrowck_implicit_static_introduced);
998 err.subdiagnostic(RequireStaticErr::UsedImpl { multi_span });
999 err.span_suggestion_verbose(
1000 span.shrink_to_hi(),
1001 fluent::borrowck_implicit_static_relax,
1002 " + '_",
1003 Applicability::MaybeIncorrect,
1004 );
1005 suggested = true;
1006 }
1007 }
1008 suggested
1009 }
1010
1011 fn suggest_adding_lifetime_params(&self, diag: &mut Diag<'_>, sub: RegionVid, sup: RegionVid) {
1012 let (Some(sub), Some(sup)) = (self.to_error_region(sub), self.to_error_region(sup)) else {
1013 return;
1014 };
1015
1016 let Some((ty_sub, _)) = self
1017 .infcx
1018 .tcx
1019 .is_suitable_region(self.mir_def_id(), sub)
1020 .and_then(|_| find_anon_type(self.infcx.tcx, self.mir_def_id(), sub))
1021 else {
1022 return;
1023 };
1024
1025 let Some((ty_sup, _)) = self
1026 .infcx
1027 .tcx
1028 .is_suitable_region(self.mir_def_id(), sup)
1029 .and_then(|_| find_anon_type(self.infcx.tcx, self.mir_def_id(), sup))
1030 else {
1031 return;
1032 };
1033
1034 suggest_adding_lifetime_params(
1035 self.infcx.tcx,
1036 diag,
1037 self.mir_def_id(),
1038 sub,
1039 ty_sup,
1040 ty_sub,
1041 );
1042 }
1043
1044 #[allow(rustc::diagnostic_outside_of_impl)]
1045 fn suggest_deref_closure_return(&self, diag: &mut Diag<'_>) {
1049 let tcx = self.infcx.tcx;
1050
1051 let closure_def_id = self.mir_def_id();
1053 let hir::Node::Expr(
1054 closure_expr @ hir::Expr {
1055 kind: hir::ExprKind::Closure(hir::Closure { body, .. }), ..
1056 },
1057 ) = tcx.hir_node_by_def_id(closure_def_id)
1058 else {
1059 return;
1060 };
1061 let ty::Closure(_, args) = *tcx.type_of(closure_def_id).instantiate_identity().kind()
1062 else {
1063 return;
1064 };
1065 let args = args.as_closure();
1066
1067 let parent_expr_id = tcx.parent_hir_id(self.mir_hir_id());
1069 let hir::Node::Expr(
1070 parent_expr @ hir::Expr {
1071 kind: hir::ExprKind::MethodCall(_, rcvr, call_args, _), ..
1072 },
1073 ) = tcx.hir_node(parent_expr_id)
1074 else {
1075 return;
1076 };
1077 let typeck_results = tcx.typeck(self.mir_def_id());
1078
1079 let liberated_sig = tcx.liberate_late_bound_regions(closure_def_id.to_def_id(), args.sig());
1081 let mut peeled_ty = liberated_sig.output();
1082 let mut count = 0;
1083 while let ty::Ref(_, ref_ty, _) = *peeled_ty.kind() {
1084 peeled_ty = ref_ty;
1085 count += 1;
1086 }
1087 if !self.infcx.type_is_copy_modulo_regions(self.infcx.param_env, peeled_ty) {
1088 return;
1089 }
1090
1091 let closure_sig_as_fn_ptr_ty = Ty::new_fn_ptr(
1093 tcx,
1094 ty::Binder::dummy(tcx.mk_fn_sig(
1095 liberated_sig.inputs().iter().copied(),
1096 peeled_ty,
1097 liberated_sig.c_variadic,
1098 hir::Safety::Safe,
1099 rustc_abi::ExternAbi::Rust,
1100 )),
1101 );
1102 let closure_ty = Ty::new_closure(
1103 tcx,
1104 closure_def_id.to_def_id(),
1105 ty::ClosureArgs::new(
1106 tcx,
1107 ty::ClosureArgsParts {
1108 parent_args: args.parent_args(),
1109 closure_kind_ty: args.kind_ty(),
1110 tupled_upvars_ty: args.tupled_upvars_ty(),
1111 closure_sig_as_fn_ptr_ty,
1112 },
1113 )
1114 .args,
1115 );
1116
1117 let Some((closure_arg_pos, _)) =
1118 call_args.iter().enumerate().find(|(_, arg)| arg.hir_id == closure_expr.hir_id)
1119 else {
1120 return;
1121 };
1122 let Some(method_def_id) = typeck_results.type_dependent_def_id(parent_expr.hir_id) else {
1125 return;
1126 };
1127 let Some(input_arg) = tcx
1128 .fn_sig(method_def_id)
1129 .skip_binder()
1130 .inputs()
1131 .skip_binder()
1132 .get(closure_arg_pos + 1)
1134 else {
1135 return;
1136 };
1137 let ty::Param(closure_param) = input_arg.kind() else { return };
1139
1140 let Some(possible_rcvr_ty) = typeck_results.node_type_opt(rcvr.hir_id) else { return };
1142 let args = GenericArgs::for_item(tcx, method_def_id, |param, _| {
1143 if let ty::GenericParamDefKind::Lifetime = param.kind {
1144 tcx.lifetimes.re_erased.into()
1145 } else if param.index == 0 && param.name == kw::SelfUpper {
1146 possible_rcvr_ty.into()
1147 } else if param.index == closure_param.index {
1148 closure_ty.into()
1149 } else {
1150 self.infcx.var_for_def(parent_expr.span, param)
1151 }
1152 });
1153
1154 let preds = tcx.predicates_of(method_def_id).instantiate(tcx, args);
1155
1156 let ocx = ObligationCtxt::new(&self.infcx);
1157 ocx.register_obligations(preds.iter().map(|(pred, span)| {
1158 trace!(?pred);
1159 Obligation::misc(tcx, span, self.mir_def_id(), self.infcx.param_env, pred)
1160 }));
1161
1162 if ocx.select_all_or_error().is_empty() && count > 0 {
1163 diag.span_suggestion_verbose(
1164 tcx.hir_body(*body).value.peel_blocks().span.shrink_to_lo(),
1165 fluent::borrowck_dereference_suggestion,
1166 "*".repeat(count),
1167 Applicability::MachineApplicable,
1168 );
1169 }
1170 }
1171
1172 #[allow(rustc::diagnostic_outside_of_impl)]
1173 fn suggest_move_on_borrowing_closure(&self, diag: &mut Diag<'_>) {
1174 let body = self.infcx.tcx.hir_body_owned_by(self.mir_def_id());
1175 let expr = &body.value.peel_blocks();
1176 let mut closure_span = None::<rustc_span::Span>;
1177 match expr.kind {
1178 hir::ExprKind::MethodCall(.., args, _) => {
1179 for arg in args {
1180 if let hir::ExprKind::Closure(hir::Closure {
1181 capture_clause: hir::CaptureBy::Ref,
1182 ..
1183 }) = arg.kind
1184 {
1185 closure_span = Some(arg.span.shrink_to_lo());
1186 break;
1187 }
1188 }
1189 }
1190 hir::ExprKind::Closure(hir::Closure {
1191 capture_clause: hir::CaptureBy::Ref,
1192 kind,
1193 ..
1194 }) => {
1195 if !matches!(
1196 kind,
1197 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
1198 hir::CoroutineDesugaring::Async,
1199 _
1200 ),)
1201 ) {
1202 closure_span = Some(expr.span.shrink_to_lo());
1203 }
1204 }
1205 _ => {}
1206 }
1207 if let Some(closure_span) = closure_span {
1208 diag.span_suggestion_verbose(
1209 closure_span,
1210 fluent::borrowck_move_closure_suggestion,
1211 "move ",
1212 Applicability::MaybeIncorrect,
1213 );
1214 }
1215 }
1216}