]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_borrowck/src/diagnostics/region_errors.rs
New upstream version 1.64.0+dfsg1
[rustc.git] / compiler / rustc_borrowck / src / diagnostics / region_errors.rs
CommitLineData
60c5eb7d
XL
1//! Error reporting machinery for lifetime errors.
2
064997fb 3use rustc_data_structures::fx::FxHashSet;
923072b8
FG
4use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan};
5use rustc_hir::def_id::DefId;
6use rustc_hir::intravisit::Visitor;
7use rustc_hir::{self as hir, Item, ItemKind, Node};
74b04a01 8use rustc_infer::infer::{
04454e1e
FG
9 error_reporting::nice_region_error::{
10 self, find_anon_type, find_param_with_region, suggest_adding_lifetime_params,
923072b8 11 HirTraitObjectVisitor, NiceRegionError, TraitObjectVisitor,
04454e1e
FG
12 },
13 error_reporting::unexpected_hidden_region_diagnostic,
14 NllRegionVariableOrigin, RelateParamBound,
74b04a01 15};
5099ac24 16use rustc_middle::hir::place::PlaceBase;
f035d41b 17use rustc_middle::mir::{ConstraintCategory, ReturnConstraint};
04454e1e 18use rustc_middle::ty::subst::InternalSubsts;
923072b8
FG
19use rustc_middle::ty::Region;
20use rustc_middle::ty::TypeVisitor;
ba9703b0 21use rustc_middle::ty::{self, RegionVid, Ty};
064997fb 22use rustc_span::symbol::{kw, sym, Ident};
04454e1e 23use rustc_span::Span;
60c5eb7d 24
c295e0f8 25use crate::borrowck_errors;
064997fb 26use crate::session_diagnostics::GenericDoesNotLiveLongEnough;
8faf50e0 27
c295e0f8
XL
28use super::{OutlivesSuggestionBuilder, RegionName};
29use crate::region_infer::BlameConstraint;
30use crate::{
60c5eb7d 31 nll::ConstraintDescription,
dfeec247
XL
32 region_infer::{values::RegionElement, TypeTest},
33 universal_regions::DefiningTy,
34 MirBorrowckCtxt,
60c5eb7d 35};
8faf50e0 36
923072b8 37impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> {
0bf4aa26 38 fn description(&self) -> &'static str {
8faf50e0
XL
39 // Must end with a space. Allows for empty names to be provided.
40 match self {
0bf4aa26 41 ConstraintCategory::Assignment => "assignment ",
f035d41b 42 ConstraintCategory::Return(_) => "returning this value ",
0731742a 43 ConstraintCategory::Yield => "yielding this value ",
0bf4aa26
XL
44 ConstraintCategory::UseAsConst => "using this value as a constant ",
45 ConstraintCategory::UseAsStatic => "using this value as a static ",
46 ConstraintCategory::Cast => "cast ",
923072b8 47 ConstraintCategory::CallArgument(_) => "argument ",
0bf4aa26
XL
48 ConstraintCategory::TypeAnnotation => "type annotation ",
49 ConstraintCategory::ClosureBounds => "closure body ",
50 ConstraintCategory::SizedBound => "proving this value is `Sized` ",
51 ConstraintCategory::CopyBound => "copying this value ",
52 ConstraintCategory::OpaqueType => "opaque type ",
f035d41b 53 ConstraintCategory::ClosureUpvar(_) => "closure capture ",
c295e0f8
XL
54 ConstraintCategory::Usage => "this usage ",
55 ConstraintCategory::Predicate(_)
56 | ConstraintCategory::Boring
0bf4aa26
XL
57 | ConstraintCategory::BoringNoLocation
58 | ConstraintCategory::Internal => "",
8faf50e0
XL
59 }
60 }
61}
62
dfeec247
XL
63/// A collection of errors encountered during region inference. This is needed to efficiently
64/// report errors after borrow checking.
65///
66/// Usually we expect this to either be empty or contain a small number of items, so we can avoid
67/// allocation most of the time.
923072b8 68pub(crate) type RegionErrors<'tcx> = Vec<RegionErrorKind<'tcx>>;
60c5eb7d 69
dfeec247 70#[derive(Clone, Debug)]
923072b8 71pub(crate) enum RegionErrorKind<'tcx> {
dfeec247
XL
72 /// A generic bound failure for a type test (`T: 'a`).
73 TypeTestError { type_test: TypeTest<'tcx> },
74
75 /// An unexpected hidden region for an opaque type.
76 UnexpectedHiddenRegion {
74b04a01
XL
77 /// The span for the member constraint.
78 span: Span,
dfeec247
XL
79 /// The hidden type.
80 hidden_ty: Ty<'tcx>,
064997fb
FG
81 /// The opaque type.
82 key: ty::OpaqueTypeKey<'tcx>,
dfeec247
XL
83 /// The unexpected region.
84 member_region: ty::Region<'tcx>,
85 },
86
87 /// Higher-ranked subtyping error.
88 BoundUniversalRegionError {
89 /// The placeholder free region.
90 longer_fr: RegionVid,
91 /// The region element that erroneously must be outlived by `longer_fr`.
92 error_element: RegionElement,
94222f64
XL
93 /// The placeholder region.
94 placeholder: ty::PlaceholderRegion,
dfeec247 95 },
e1599b0c 96
dfeec247
XL
97 /// Any other lifetime error.
98 RegionError {
99 /// The origin of the region.
5869c6ff 100 fr_origin: NllRegionVariableOrigin,
dfeec247
XL
101 /// The region that should outlive `shorter_fr`.
102 longer_fr: RegionVid,
103 /// The region that should be shorter, but we can't prove it.
104 shorter_fr: RegionVid,
105 /// Indicates whether this is a reported error. We currently only report the first error
106 /// encountered and leave the rest unreported so as not to overwhelm the user.
107 is_reported: bool,
108 },
e1599b0c
XL
109}
110
111/// Information about the various region constraints involved in a borrow checker error.
112#[derive(Clone, Debug)]
923072b8 113pub struct ErrorConstraintInfo<'tcx> {
e1599b0c 114 // fr: outlived_fr
60c5eb7d
XL
115 pub(super) fr: RegionVid,
116 pub(super) fr_is_local: bool,
117 pub(super) outlived_fr: RegionVid,
118 pub(super) outlived_fr_is_local: bool,
e1599b0c
XL
119
120 // Category and span for best blame constraint
923072b8 121 pub(super) category: ConstraintCategory<'tcx>,
60c5eb7d 122 pub(super) span: Span,
e1599b0c
XL
123}
124
dfeec247
XL
125impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
126 /// Converts a region inference variable into a `ty::Region` that
127 /// we can use for error reporting. If `r` is universally bound,
128 /// then we use the name that we have on record for it. If `r` is
129 /// existentially bound, then we check its inferred value and try
130 /// to find a good name from that. Returns `None` if we can't find
131 /// one (e.g., this is just some random part of the CFG).
132 pub(super) fn to_error_region(&self, r: RegionVid) -> Option<ty::Region<'tcx>> {
133 self.to_error_region_vid(r).and_then(|r| self.regioncx.region_definition(r).external_name)
134 }
8faf50e0 135
dfeec247
XL
136 /// Returns the `RegionVid` corresponding to the region returned by
137 /// `to_error_region`.
138 pub(super) fn to_error_region_vid(&self, r: RegionVid) -> Option<RegionVid> {
139 if self.regioncx.universal_regions().is_universal_region(r) {
140 Some(r)
141 } else {
f035d41b
XL
142 // We just want something nameable, even if it's not
143 // actually an upper bound.
144 let upper_bound = self.regioncx.approx_universal_upper_bound(r);
8faf50e0 145
dfeec247
XL
146 if self.regioncx.upper_bound_in_region_scc(r, upper_bound) {
147 self.to_error_region_vid(upper_bound)
e74abb32 148 } else {
dfeec247 149 None
8faf50e0 150 }
dfeec247
XL
151 }
152 }
e74abb32 153
dfeec247
XL
154 /// Returns `true` if a closure is inferred to be an `FnMut` closure.
155 fn is_closure_fn_mut(&self, fr: RegionVid) -> bool {
5e7ed085
FG
156 if let Some(ty::ReFree(free_region)) = self.to_error_region(fr).as_deref()
157 && let ty::BoundRegionKind::BrEnv = free_region.bound_region
158 && let DefiningTy::Closure(_, substs) = self.regioncx.universal_regions().defining_ty
159 {
160 return substs.as_closure().kind() == ty::ClosureKind::FnMut;
8faf50e0
XL
161 }
162
dfeec247 163 false
8faf50e0
XL
164 }
165
dfeec247 166 /// Produces nice borrowck error diagnostics for all the errors collected in `nll_errors`.
c295e0f8 167 pub(crate) fn report_region_errors(&mut self, nll_errors: RegionErrors<'tcx>) {
dfeec247
XL
168 // Iterate through all the errors, producing a diagnostic for each one. The diagnostics are
169 // buffered in the `MirBorrowckCtxt`.
170
171 let mut outlives_suggestion = OutlivesSuggestionBuilder::default();
172
173 for nll_error in nll_errors.into_iter() {
174 match nll_error {
175 RegionErrorKind::TypeTestError { type_test } => {
176 // Try to convert the lower-bound region into something named we can print for the user.
177 let lower_bound_region = self.to_error_region(type_test.lower_bound);
178
179 let type_test_span = type_test.locations.span(&self.body);
180
181 if let Some(lower_bound_region) = lower_bound_region {
04454e1e
FG
182 let generic_ty = type_test.generic_kind.to_ty(self.infcx.tcx);
183 let origin = RelateParamBound(type_test_span, generic_ty, None);
5099ac24 184 self.buffer_error(self.infcx.construct_generic_bound_failure(
064997fb 185 self.body.source.def_id().expect_local(),
5099ac24 186 type_test_span,
04454e1e 187 Some(origin),
5099ac24
FG
188 type_test.generic_kind,
189 lower_bound_region,
190 ));
dfeec247
XL
191 } else {
192 // FIXME. We should handle this case better. It
193 // indicates that we have e.g., some region variable
194 // whose value is like `'a+'b` where `'a` and `'b` are
5e7ed085 195 // distinct unrelated universal regions that are not
dfeec247
XL
196 // known to outlive one another. It'd be nice to have
197 // some examples where this arises to decide how best
198 // to report it; we could probably handle it by
199 // iterating over the universal regions and reporting
200 // an error that multiple bounds are required.
064997fb
FG
201 self.buffer_error(self.infcx.tcx.sess.create_err(
202 GenericDoesNotLiveLongEnough {
203 kind: type_test.generic_kind.to_string(),
204 span: type_test_span,
205 },
5099ac24 206 ));
8faf50e0
XL
207 }
208 }
8faf50e0 209
064997fb 210 RegionErrorKind::UnexpectedHiddenRegion { span, hidden_ty, key, member_region } => {
74b04a01 211 let named_ty = self.regioncx.name_regions(self.infcx.tcx, hidden_ty);
064997fb 212 let named_key = self.regioncx.name_regions(self.infcx.tcx, key);
74b04a01 213 let named_region = self.regioncx.name_regions(self.infcx.tcx, member_region);
5099ac24 214 self.buffer_error(unexpected_hidden_region_diagnostic(
dfeec247 215 self.infcx.tcx,
74b04a01
XL
216 span,
217 named_ty,
218 named_region,
064997fb 219 named_key,
5099ac24 220 ));
8faf50e0 221 }
e74abb32 222
dfeec247
XL
223 RegionErrorKind::BoundUniversalRegionError {
224 longer_fr,
94222f64 225 placeholder,
dfeec247
XL
226 error_element,
227 } => {
94222f64 228 let error_vid = self.regioncx.region_from_element(longer_fr, &error_element);
dfeec247
XL
229
230 // Find the code to blame for the fact that `longer_fr` outlives `error_fr`.
c295e0f8 231 let (_, cause) = self.regioncx.find_outlives_blame_span(
dfeec247
XL
232 &self.body,
233 longer_fr,
94222f64
XL
234 NllRegionVariableOrigin::Placeholder(placeholder),
235 error_vid,
dfeec247
XL
236 );
237
94222f64
XL
238 let universe = placeholder.universe;
239 let universe_info = self.regioncx.universe_info(universe);
240
c295e0f8 241 universe_info.report_error(self, placeholder, error_element, cause);
dfeec247 242 }
e74abb32 243
dfeec247
XL
244 RegionErrorKind::RegionError { fr_origin, longer_fr, shorter_fr, is_reported } => {
245 if is_reported {
246 self.report_region_error(
247 longer_fr,
248 fr_origin,
249 shorter_fr,
250 &mut outlives_suggestion,
251 );
252 } else {
253 // We only report the first error, so as not to overwhelm the user. See
254 // `RegRegionErrorKind` docs.
255 //
256 // FIXME: currently we do nothing with these, but perhaps we can do better?
257 // FIXME: try collecting these constraints on the outlives suggestion
258 // builder. Does it make the suggestions any better?
259 debug!(
260 "Unreported region error: can't prove that {:?}: {:?}",
261 longer_fr, shorter_fr
262 );
263 }
264 }
8faf50e0
XL
265 }
266 }
267
dfeec247
XL
268 // Emit one outlives suggestions for each MIR def we borrowck
269 outlives_suggestion.add_suggestion(self);
8faf50e0
XL
270 }
271
923072b8
FG
272 fn get_impl_ident_and_self_ty_from_trait(
273 &self,
274 def_id: DefId,
275 trait_objects: &FxHashSet<DefId>,
276 ) -> Option<(Ident, &'tcx hir::Ty<'tcx>)> {
277 let tcx = self.infcx.tcx;
278 match tcx.hir().get_if_local(def_id) {
279 Some(Node::ImplItem(impl_item)) => {
280 match tcx.hir().find_by_def_id(tcx.hir().get_parent_item(impl_item.hir_id())) {
281 Some(Node::Item(Item {
282 kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
283 ..
284 })) => Some((impl_item.ident, self_ty)),
285 _ => None,
286 }
287 }
288 Some(Node::TraitItem(trait_item)) => {
289 let trait_did = tcx.hir().get_parent_item(trait_item.hir_id());
290 match tcx.hir().find_by_def_id(trait_did) {
291 Some(Node::Item(Item { kind: ItemKind::Trait(..), .. })) => {
292 // The method being called is defined in the `trait`, but the `'static`
293 // obligation comes from the `impl`. Find that `impl` so that we can point
294 // at it in the suggestion.
295 let trait_did = trait_did.to_def_id();
296 match tcx
297 .hir()
298 .trait_impls(trait_did)
299 .iter()
300 .filter_map(|&impl_did| {
301 match tcx.hir().get_if_local(impl_did.to_def_id()) {
302 Some(Node::Item(Item {
303 kind: ItemKind::Impl(hir::Impl { self_ty, .. }),
304 ..
305 })) if trait_objects.iter().all(|did| {
306 // FIXME: we should check `self_ty` against the receiver
307 // type in the `UnifyReceiver` context, but for now, use
308 // this imperfect proxy. This will fail if there are
309 // multiple `impl`s for the same trait like
310 // `impl Foo for Box<dyn Bar>` and `impl Foo for dyn Bar`.
311 // In that case, only the first one will get suggestions.
312 let mut traits = vec![];
313 let mut hir_v = HirTraitObjectVisitor(&mut traits, *did);
314 hir_v.visit_ty(self_ty);
315 !traits.is_empty()
316 }) =>
317 {
318 Some(self_ty)
319 }
320 _ => None,
321 }
322 })
323 .next()
324 {
325 Some(self_ty) => Some((trait_item.ident, self_ty)),
326 _ => None,
327 }
328 }
329 _ => None,
330 }
331 }
332 _ => None,
333 }
334 }
335
8faf50e0
XL
336 /// Report an error because the universal region `fr` was required to outlive
337 /// `outlived_fr` but it is not known to do so. For example:
338 ///
04454e1e 339 /// ```compile_fail,E0312
8faf50e0
XL
340 /// fn foo<'a, 'b>(x: &'a u32) -> &'b u32 { x }
341 /// ```
342 ///
343 /// Here we would be invoked with `fr = 'a` and `outlived_fr = `'b`.
c295e0f8 344 pub(crate) fn report_region_error(
dfeec247 345 &mut self,
8faf50e0 346 fr: RegionVid,
5869c6ff 347 fr_origin: NllRegionVariableOrigin,
8faf50e0 348 outlived_fr: RegionVid,
dfeec247
XL
349 outlives_suggestion: &mut OutlivesSuggestionBuilder,
350 ) {
351 debug!("report_region_error(fr={:?}, outlived_fr={:?})", fr, outlived_fr);
8faf50e0 352
c295e0f8 353 let BlameConstraint { category, cause, variance_info, from_closure: _ } =
dfeec247
XL
354 self.regioncx.best_blame_constraint(&self.body, fr, fr_origin, |r| {
355 self.regioncx.provides_universal_region(r, fr, outlived_fr)
356 });
8faf50e0 357
c295e0f8 358 debug!("report_region_error: category={:?} {:?} {:?}", category, cause, variance_info);
923072b8 359
8faf50e0
XL
360 // Check if we can use one of the "nice region errors".
361 if let (Some(f), Some(o)) = (self.to_error_region(fr), self.to_error_region(outlived_fr)) {
c295e0f8 362 let nice = NiceRegionError::new_from_span(self.infcx, cause.span, o, f);
9fa01778 363 if let Some(diag) = nice.try_report_from_nll() {
5099ac24 364 self.buffer_error(diag);
dfeec247 365 return;
8faf50e0
XL
366 }
367 }
368
369 let (fr_is_local, outlived_fr_is_local): (bool, bool) = (
dfeec247
XL
370 self.regioncx.universal_regions().is_local_free_region(fr),
371 self.regioncx.universal_regions().is_local_free_region(outlived_fr),
8faf50e0 372 );
0bf4aa26 373
a1dfa0c6 374 debug!(
dfeec247 375 "report_region_error: fr_is_local={:?} outlived_fr_is_local={:?} category={:?}",
a1dfa0c6
XL
376 fr_is_local, outlived_fr_is_local, category
377 );
e1599b0c 378
e1599b0c 379 let errci = ErrorConstraintInfo {
dfeec247
XL
380 fr,
381 outlived_fr,
382 fr_is_local,
383 outlived_fr_is_local,
384 category,
c295e0f8 385 span: cause.span,
e1599b0c
XL
386 };
387
17df50a5 388 let mut diag = match (category, fr_is_local, outlived_fr_is_local) {
f035d41b
XL
389 (ConstraintCategory::Return(kind), true, false) if self.is_closure_fn_mut(fr) => {
390 self.report_fnmut_error(&errci, kind)
a1dfa0c6
XL
391 }
392 (ConstraintCategory::Assignment, true, false)
923072b8 393 | (ConstraintCategory::CallArgument(_), true, false) => {
dfeec247 394 let mut db = self.report_escaping_data_error(&errci);
60c5eb7d 395
dfeec247 396 outlives_suggestion.intermediate_suggestion(self, &errci, &mut db);
60c5eb7d
XL
397 outlives_suggestion.collect_constraint(fr, outlived_fr);
398
399 db
400 }
401 _ => {
dfeec247 402 let mut db = self.report_general_error(&errci);
60c5eb7d 403
dfeec247 404 outlives_suggestion.intermediate_suggestion(self, &errci, &mut db);
60c5eb7d
XL
405 outlives_suggestion.collect_constraint(fr, outlived_fr);
406
407 db
408 }
a1dfa0c6 409 };
dfeec247 410
17df50a5
XL
411 match variance_info {
412 ty::VarianceDiagInfo::None => {}
a2a8927a
XL
413 ty::VarianceDiagInfo::Invariant { ty, param_index } => {
414 let (desc, note) = match ty.kind() {
415 ty::RawPtr(ty_mut) => {
416 assert_eq!(ty_mut.mutbl, rustc_hir::Mutability::Mut);
417 (
04454e1e 418 format!("a mutable pointer to `{}`", ty_mut.ty),
a2a8927a
XL
419 "mutable pointers are invariant over their type parameter".to_string(),
420 )
421 }
422 ty::Ref(_, inner_ty, mutbl) => {
423 assert_eq!(*mutbl, rustc_hir::Mutability::Mut);
424 (
04454e1e 425 format!("a mutable reference to `{inner_ty}`"),
a2a8927a
XL
426 "mutable references are invariant over their type parameter"
427 .to_string(),
428 )
429 }
430 ty::Adt(adt, substs) => {
431 let generic_arg = substs[param_index as usize];
432 let identity_substs =
5e7ed085
FG
433 InternalSubsts::identity_for_item(self.infcx.tcx, adt.did());
434 let base_ty = self.infcx.tcx.mk_adt(*adt, identity_substs);
a2a8927a
XL
435 let base_generic_arg = identity_substs[param_index as usize];
436 let adt_desc = adt.descr();
437
438 let desc = format!(
04454e1e 439 "the type `{ty}`, which makes the generic argument `{generic_arg}` invariant"
a2a8927a
XL
440 );
441 let note = format!(
04454e1e
FG
442 "the {adt_desc} `{base_ty}` is invariant over the parameter `{base_generic_arg}`"
443 );
444 (desc, note)
445 }
446 ty::FnDef(def_id, _) => {
447 let name = self.infcx.tcx.item_name(*def_id);
448 let identity_substs =
449 InternalSubsts::identity_for_item(self.infcx.tcx, *def_id);
450 let desc = format!("a function pointer to `{name}`");
451 let note = format!(
452 "the function `{name}` is invariant over the parameter `{}`",
453 identity_substs[param_index as usize]
a2a8927a
XL
454 );
455 (desc, note)
456 }
457 _ => panic!("Unexpected type {:?}", ty),
17df50a5 458 };
a2a8927a
XL
459 diag.note(&format!("requirement occurs because of {desc}",));
460 diag.note(&note);
17df50a5
XL
461 diag.help("see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance");
462 }
463 }
464
5099ac24 465 self.buffer_error(diag);
a1dfa0c6
XL
466 }
467
0bf4aa26
XL
468 /// Report a specialized error when `FnMut` closures return a reference to a captured variable.
469 /// This function expects `fr` to be local and `outlived_fr` to not be local.
470 ///
471 /// ```text
472 /// error: captured variable cannot escape `FnMut` closure body
473 /// --> $DIR/issue-53040.rs:15:8
474 /// |
475 /// LL | || &mut v;
476 /// | -- ^^^^^^ creates a reference to a captured variable which escapes the closure body
477 /// | |
478 /// | inferred to be a `FnMut` closure
479 /// |
480 /// = note: `FnMut` closures only have access to their captured variables while they are
481 /// executing...
482 /// = note: ...therefore, returned references to captured variables will escape the closure
483 /// ```
f035d41b
XL
484 fn report_fnmut_error(
485 &self,
923072b8 486 errci: &ErrorConstraintInfo<'tcx>,
f035d41b 487 kind: ReturnConstraint,
5e7ed085 488 ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
dfeec247 489 let ErrorConstraintInfo { outlived_fr, span, .. } = errci;
e1599b0c 490
dfeec247 491 let mut diag = self
e1599b0c 492 .infcx
a1dfa0c6
XL
493 .tcx
494 .sess
e1599b0c 495 .struct_span_err(*span, "captured variable cannot escape `FnMut` closure body");
0bf4aa26 496
f035d41b 497 let mut output_ty = self.regioncx.universal_regions().unnormalized_output_ty;
1b1a35ee 498 if let ty::Opaque(def_id, _) = *output_ty.kind() {
f035d41b
XL
499 output_ty = self.infcx.tcx.type_of(def_id)
500 };
501
502 debug!("report_fnmut_error: output_ty={:?}", output_ty);
503
1b1a35ee 504 let message = match output_ty.kind() {
f035d41b
XL
505 ty::Closure(_, _) => {
506 "returns a closure that contains a reference to a captured variable, which then \
507 escapes the closure body"
508 }
5e7ed085 509 ty::Adt(def, _) if self.infcx.tcx.is_diagnostic_item(sym::gen_future, def.did()) => {
f035d41b
XL
510 "returns an `async` block that contains a reference to a captured variable, which then \
511 escapes the closure body"
512 }
513 _ => "returns a reference to a captured variable which escapes the closure body",
0bf4aa26
XL
514 };
515
e1599b0c 516 diag.span_label(*span, message);
0bf4aa26 517
5099ac24 518 if let ReturnConstraint::ClosureUpvar(upvar_field) = kind {
f035d41b
XL
519 let def_id = match self.regioncx.universal_regions().defining_ty {
520 DefiningTy::Closure(def_id, _) => def_id,
1b1a35ee 521 ty => bug!("unexpected DefiningTy {:?}", ty),
f035d41b
XL
522 };
523
5099ac24
FG
524 let captured_place = &self.upvars[upvar_field.index()].place;
525 let defined_hir = match captured_place.place.base {
526 PlaceBase::Local(hirid) => Some(hirid),
527 PlaceBase::Upvar(upvar) => Some(upvar.var_path.hir_id),
528 _ => None,
529 };
530
923072b8 531 if let Some(def_hir) = defined_hir {
5099ac24 532 let upvars_map = self.infcx.tcx.upvars_mentioned(def_id).unwrap();
923072b8
FG
533 let upvar_def_span = self.infcx.tcx.hir().span(def_hir);
534 let upvar_span = upvars_map.get(&def_hir).unwrap().span;
5099ac24
FG
535 diag.span_label(upvar_def_span, "variable defined here");
536 diag.span_label(upvar_span, "variable captured here");
537 }
f035d41b
XL
538 }
539
3dfed10e
XL
540 if let Some(fr_span) = self.give_region_a_name(*outlived_fr).unwrap().span() {
541 diag.span_label(fr_span, "inferred to be a `FnMut` closure");
0bf4aa26
XL
542 }
543
a1dfa0c6
XL
544 diag.note(
545 "`FnMut` closures only have access to their captured variables while they are \
546 executing...",
547 );
0bf4aa26
XL
548 diag.note("...therefore, they cannot allow references to captured variables to escape");
549
e1599b0c 550 diag
0bf4aa26
XL
551 }
552
94222f64 553 /// Reports an error specifically for when data is escaping a closure.
0bf4aa26
XL
554 ///
555 /// ```text
556 /// error: borrowed data escapes outside of function
557 /// --> $DIR/lifetime-bound-will-change-warning.rs:44:5
558 /// |
559 /// LL | fn test2<'a>(x: &'a Box<Fn()+'a>) {
560 /// | - `x` is a reference that is only valid in the function body
561 /// LL | // but ref_obj will not, so warn.
562 /// LL | ref_obj(x)
563 /// | ^^^^^^^^^^ `x` escapes the function body here
564 /// ```
5e7ed085
FG
565 fn report_escaping_data_error(
566 &self,
923072b8 567 errci: &ErrorConstraintInfo<'tcx>,
5e7ed085 568 ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
dfeec247
XL
569 let ErrorConstraintInfo { span, category, .. } = errci;
570
571 let fr_name_and_span = self.regioncx.get_var_name_and_span_for_region(
572 self.infcx.tcx,
573 &self.body,
574 &self.local_names,
575 &self.upvars,
576 errci.fr,
577 );
578 let outlived_fr_name_and_span = self.regioncx.get_var_name_and_span_for_region(
579 self.infcx.tcx,
580 &self.body,
581 &self.local_names,
582 &self.upvars,
60c5eb7d
XL
583 errci.outlived_fr,
584 );
8faf50e0 585
74b04a01
XL
586 let (_, escapes_from) = self
587 .infcx
588 .tcx
589 .article_and_description(self.regioncx.universal_regions().defining_ty.def_id());
8faf50e0 590
0bf4aa26
XL
591 // Revert to the normal error in these cases.
592 // Assignments aren't "escapes" in function items.
593 if (fr_name_and_span.is_none() && outlived_fr_name_and_span.is_none())
74b04a01
XL
594 || (*category == ConstraintCategory::Assignment
595 && self.regioncx.universal_regions().defining_ty.is_fn_def())
596 || self.regioncx.universal_regions().defining_ty.is_const()
0bf4aa26 597 {
dfeec247
XL
598 return self.report_general_error(&ErrorConstraintInfo {
599 fr_is_local: true,
600 outlived_fr_is_local: false,
601 ..*errci
602 });
8faf50e0
XL
603 }
604
dfeec247
XL
605 let mut diag =
606 borrowck_errors::borrowed_data_escapes_closure(self.infcx.tcx, *span, escapes_from);
8faf50e0 607
0bf4aa26
XL
608 if let Some((Some(outlived_fr_name), outlived_fr_span)) = outlived_fr_name_and_span {
609 diag.span_label(
610 outlived_fr_span,
04454e1e 611 format!("`{outlived_fr_name}` declared here, outside of the {escapes_from} body",),
0bf4aa26 612 );
8faf50e0
XL
613 }
614
0bf4aa26
XL
615 if let Some((Some(fr_name), fr_span)) = fr_name_and_span {
616 diag.span_label(
617 fr_span,
618 format!(
04454e1e 619 "`{fr_name}` is a reference that is only valid in the {escapes_from} body",
0bf4aa26
XL
620 ),
621 );
8faf50e0 622
04454e1e 623 diag.span_label(*span, format!("`{fr_name}` escapes the {escapes_from} body here"));
8faf50e0
XL
624 }
625
c295e0f8
XL
626 // Only show an extra note if we can find an 'error region' for both of the region
627 // variables. This avoids showing a noisy note that just mentions 'synthetic' regions
628 // that don't help the user understand the error.
923072b8
FG
629 match (self.to_error_region(errci.fr), self.to_error_region(errci.outlived_fr)) {
630 (Some(f), Some(o)) => {
631 self.maybe_suggest_constrain_dyn_trait_impl(&mut diag, f, o, category);
c295e0f8 632
923072b8
FG
633 let fr_region_name = self.give_region_a_name(errci.fr).unwrap();
634 fr_region_name.highlight_region_name(&mut diag);
635 let outlived_fr_region_name = self.give_region_a_name(errci.outlived_fr).unwrap();
636 outlived_fr_region_name.highlight_region_name(&mut diag);
637
638 diag.span_label(
639 *span,
640 format!(
641 "{}requires that `{}` must outlive `{}`",
642 category.description(),
643 fr_region_name,
644 outlived_fr_region_name,
645 ),
646 );
647 }
648 _ => {}
c295e0f8 649 }
923072b8 650
e1599b0c 651 diag
8faf50e0
XL
652 }
653
0bf4aa26
XL
654 /// Reports a region inference error for the general case with named/synthesized lifetimes to
655 /// explain what is happening.
656 ///
657 /// ```text
658 /// error: unsatisfied lifetime constraints
659 /// --> $DIR/regions-creating-enums3.rs:17:5
660 /// |
661 /// LL | fn mk_add_bad1<'a,'b>(x: &'a ast<'a>, y: &'b ast<'b>) -> ast<'a> {
662 /// | -- -- lifetime `'b` defined here
663 /// | |
664 /// | lifetime `'a` defined here
665 /// LL | ast::add(x, y)
666 /// | ^^^^^^^^^^^^^^ function was supposed to return data with lifetime `'a` but it
667 /// | is returning data with lifetime `'b`
668 /// ```
5e7ed085
FG
669 fn report_general_error(
670 &self,
923072b8 671 errci: &ErrorConstraintInfo<'tcx>,
5e7ed085 672 ) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
e1599b0c 673 let ErrorConstraintInfo {
dfeec247
XL
674 fr,
675 fr_is_local,
676 outlived_fr,
677 outlived_fr_is_local,
678 span,
679 category,
680 ..
e1599b0c
XL
681 } = errci;
682
dfeec247
XL
683 let mut diag =
684 self.infcx.tcx.sess.struct_span_err(*span, "lifetime may not live long enough");
8faf50e0 685
29967ef6
XL
686 let (_, mir_def_name) =
687 self.infcx.tcx.article_and_description(self.mir_def_id().to_def_id());
8faf50e0 688
dfeec247 689 let fr_name = self.give_region_a_name(*fr).unwrap();
e1599b0c 690 fr_name.highlight_region_name(&mut diag);
dfeec247 691 let outlived_fr_name = self.give_region_a_name(*outlived_fr).unwrap();
e1599b0c
XL
692 outlived_fr_name.highlight_region_name(&mut diag);
693
8faf50e0 694 match (category, outlived_fr_is_local, fr_is_local) {
f035d41b 695 (ConstraintCategory::Return(_), true, _) => {
a1dfa0c6 696 diag.span_label(
e1599b0c 697 *span,
a1dfa0c6 698 format!(
04454e1e
FG
699 "{mir_def_name} was supposed to return data with lifetime `{outlived_fr_name}` but it is returning \
700 data with lifetime `{fr_name}`",
a1dfa0c6
XL
701 ),
702 );
703 }
8faf50e0 704 _ => {
a1dfa0c6 705 diag.span_label(
e1599b0c 706 *span,
a1dfa0c6
XL
707 format!(
708 "{}requires that `{}` must outlive `{}`",
709 category.description(),
710 fr_name,
711 outlived_fr_name,
712 ),
713 );
714 }
8faf50e0
XL
715 }
716
dfeec247 717 self.add_static_impl_trait_suggestion(&mut diag, *fr, fr_name, *outlived_fr);
04454e1e 718 self.suggest_adding_lifetime_params(&mut diag, *fr, *outlived_fr);
0bf4aa26 719
e1599b0c 720 diag
8faf50e0
XL
721 }
722
94222f64 723 /// Adds a suggestion to errors where an `impl Trait` is returned.
0bf4aa26
XL
724 ///
725 /// ```text
dc9dc135 726 /// help: to allow this `impl Trait` to capture borrowed data with lifetime `'1`, add `'_` as
0bf4aa26
XL
727 /// a constraint
728 /// |
729 /// LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> + 'a {
730 /// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
731 /// ```
732 fn add_static_impl_trait_suggestion(
733 &self,
5e7ed085 734 diag: &mut Diagnostic,
0bf4aa26
XL
735 fr: RegionVid,
736 // We need to pass `fr_name` - computing it again will label it twice.
737 fr_name: RegionName,
738 outlived_fr: RegionVid,
739 ) {
04454e1e
FG
740 if let (Some(f), Some(outlived_f)) =
741 (self.to_error_region(fr), self.to_error_region(outlived_fr))
a1dfa0c6 742 {
04454e1e
FG
743 if *outlived_f != ty::ReStatic {
744 return;
745 }
746
747 let fn_returns = self
dfeec247 748 .infcx
a1dfa0c6
XL
749 .tcx
750 .is_suitable_region(f)
04454e1e
FG
751 .map(|r| self.infcx.tcx.return_type_impl_or_dyn_traits(r.def_id))
752 .unwrap_or_default();
0bf4aa26 753
04454e1e
FG
754 if fn_returns.is_empty() {
755 return;
0bf4aa26 756 }
04454e1e
FG
757
758 let param = if let Some(param) = find_param_with_region(self.infcx.tcx, f, outlived_f) {
759 param
760 } else {
761 return;
762 };
763
064997fb 764 let lifetime = if f.has_name() { fr_name.name } else { kw::UnderscoreLifetime };
04454e1e
FG
765
766 let arg = match param.param.pat.simple_ident() {
767 Some(simple_ident) => format!("argument `{}`", simple_ident),
768 None => "the argument".to_string(),
769 };
770 let captures = format!("captures data from {}", arg);
771
772 return nice_region_error::suggest_new_region_bound(
773 self.infcx.tcx,
774 diag,
775 fn_returns,
064997fb 776 lifetime.to_string(),
04454e1e
FG
777 Some(arg),
778 captures,
779 Some((param.param_ty_span, param.param_ty.to_string())),
780 );
0bf4aa26
XL
781 }
782 }
04454e1e 783
923072b8
FG
784 fn maybe_suggest_constrain_dyn_trait_impl(
785 &self,
786 diag: &mut DiagnosticBuilder<'tcx, ErrorGuaranteed>,
787 f: Region<'tcx>,
788 o: Region<'tcx>,
789 category: &ConstraintCategory<'tcx>,
790 ) {
791 if !o.is_static() {
792 return;
793 }
794
795 let tcx = self.infcx.tcx;
796
797 let instance = if let ConstraintCategory::CallArgument(Some(func_ty)) = category {
798 let (fn_did, substs) = match func_ty.kind() {
799 ty::FnDef(fn_did, substs) => (fn_did, substs),
800 _ => return,
801 };
802 debug!(?fn_did, ?substs);
803
804 // Only suggest this on function calls, not closures
805 let ty = tcx.type_of(fn_did);
806 debug!("ty: {:?}, ty.kind: {:?}", ty, ty.kind());
807 if let ty::Closure(_, _) = ty.kind() {
808 return;
809 }
810
811 if let Ok(Some(instance)) = ty::Instance::resolve(
812 tcx,
813 self.param_env,
814 *fn_did,
815 self.infcx.resolve_vars_if_possible(substs),
816 ) {
817 instance
818 } else {
819 return;
820 }
821 } else {
822 return;
823 };
824
825 let param = match find_param_with_region(tcx, f, o) {
826 Some(param) => param,
827 None => return,
828 };
829 debug!(?param);
830
831 let mut visitor = TraitObjectVisitor(FxHashSet::default());
832 visitor.visit_ty(param.param_ty);
833
834 let Some((ident, self_ty)) =
835 self.get_impl_ident_and_self_ty_from_trait(instance.def_id(), &visitor.0) else {return};
836
837 self.suggest_constrain_dyn_trait_in_impl(diag, &visitor.0, ident, self_ty);
838 }
839
840 #[instrument(skip(self, err), level = "debug")]
841 fn suggest_constrain_dyn_trait_in_impl(
842 &self,
843 err: &mut Diagnostic,
844 found_dids: &FxHashSet<DefId>,
845 ident: Ident,
846 self_ty: &hir::Ty<'_>,
847 ) -> bool {
848 debug!("err: {:#?}", err);
849 let mut suggested = false;
850 for found_did in found_dids {
851 let mut traits = vec![];
852 let mut hir_v = HirTraitObjectVisitor(&mut traits, *found_did);
853 hir_v.visit_ty(&self_ty);
854 debug!("trait spans found: {:?}", traits);
855 for span in &traits {
856 let mut multi_span: MultiSpan = vec![*span].into();
064997fb
FG
857 multi_span
858 .push_span_label(*span, "this has an implicit `'static` lifetime requirement");
923072b8
FG
859 multi_span.push_span_label(
860 ident.span,
064997fb 861 "calling this method introduces the `impl`'s 'static` requirement",
923072b8
FG
862 );
863 err.span_note(multi_span, "the used `impl` has a `'static` requirement");
864 err.span_suggestion_verbose(
865 span.shrink_to_hi(),
866 "consider relaxing the implicit `'static` requirement",
867 " + '_",
868 Applicability::MaybeIncorrect,
869 );
870 suggested = true;
871 }
872 }
873 suggested
874 }
875
04454e1e
FG
876 fn suggest_adding_lifetime_params(
877 &self,
878 diag: &mut Diagnostic,
879 sub: RegionVid,
880 sup: RegionVid,
881 ) {
882 let (Some(sub), Some(sup)) = (self.to_error_region(sub), self.to_error_region(sup)) else {
883 return
884 };
885
886 let Some((ty_sub, _)) = self
887 .infcx
888 .tcx
889 .is_suitable_region(sub)
890 .and_then(|anon_reg| find_anon_type(self.infcx.tcx, sub, &anon_reg.boundregion)) else {
891 return
892 };
893
894 let Some((ty_sup, _)) = self
895 .infcx
896 .tcx
897 .is_suitable_region(sup)
898 .and_then(|anon_reg| find_anon_type(self.infcx.tcx, sup, &anon_reg.boundregion)) else {
899 return
900 };
901
902 suggest_adding_lifetime_params(self.infcx.tcx, sub, ty_sup, ty_sub, diag);
903 }
8faf50e0 904}