Skip to content

Commit 41ce2e9

Browse files
committed
Simplified closure handling to need no new cosntraint categories.
1 parent 3d0e933 commit 41ce2e9

File tree

1 file changed

+45
-67
lines changed
  • src/librustc_mir/borrow_check/nll/region_infer/error_reporting

1 file changed

+45
-67
lines changed

src/librustc_mir/borrow_check/nll/region_infer/error_reporting/mod.rs

Lines changed: 45 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ mod var_name;
3131
enum ConstraintCategory {
3232
Cast,
3333
Assignment,
34-
AssignmentToUpvar,
3534
Return,
36-
CallArgumentToUpvar,
3735
CallArgument,
3836
Other,
3937
Boring,
@@ -42,14 +40,10 @@ enum ConstraintCategory {
4240
impl fmt::Display for ConstraintCategory {
4341
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4442
match self {
45-
ConstraintCategory::Assignment | ConstraintCategory::AssignmentToUpvar => {
46-
write!(f, "assignment")
47-
}
43+
ConstraintCategory::Assignment => write!(f, "assignment"),
4844
ConstraintCategory::Return => write!(f, "return"),
4945
ConstraintCategory::Cast => write!(f, "cast"),
50-
ConstraintCategory::CallArgument | ConstraintCategory::CallArgumentToUpvar => {
51-
write!(f, "argument")
52-
}
46+
ConstraintCategory::CallArgument => write!(f, "argument"),
5347
_ => write!(f, "free region"),
5448
}
5549
}
@@ -224,10 +218,10 @@ impl<'tcx> RegionInferenceContext<'tcx> {
224218
"constraint_is_interesting: locations={:?} constraint={:?}",
225219
constraint.locations, constraint
226220
);
227-
if let Locations::Interesting(_) = constraint.locations {
228-
true
229-
} else {
230-
false
221+
222+
match constraint.locations {
223+
Locations::Interesting(_) | Locations::All => true,
224+
_ => false,
231225
}
232226
}
233227

@@ -320,45 +314,25 @@ impl<'tcx> RegionInferenceContext<'tcx> {
320314
}
321315
}
322316

323-
let category = match (
324-
category,
317+
let (fr_is_local, outlived_fr_is_local): (bool, bool) = (
325318
self.universal_regions.is_local_free_region(fr),
326319
self.universal_regions.is_local_free_region(outlived_fr),
327-
) {
328-
(ConstraintCategory::Assignment, true, false) => ConstraintCategory::AssignmentToUpvar,
329-
(ConstraintCategory::CallArgument, true, false) => {
330-
ConstraintCategory::CallArgumentToUpvar
331-
}
332-
(category, _, _) => category,
320+
);
321+
debug!("report_error: fr_is_local={:?} outlived_fr_is_local={:?} category={:?}",
322+
fr_is_local, outlived_fr_is_local, category);
323+
324+
match (fr_is_local, outlived_fr_is_local) {
325+
(true, false) =>
326+
self.report_escapes_closure_error(mir, infcx, mir_def_id, fr, outlived_fr,
327+
category, span, errors_buffer),
328+
_ =>
329+
self.report_general_error(mir, infcx, mir_def_id, fr, fr_is_local,
330+
outlived_fr, outlived_fr_is_local,
331+
category, span, errors_buffer),
333332
};
334-
335-
debug!("report_error: category={:?}", category);
336-
match category {
337-
ConstraintCategory::AssignmentToUpvar | ConstraintCategory::CallArgumentToUpvar => self
338-
.report_closure_error(
339-
mir,
340-
infcx,
341-
mir_def_id,
342-
fr,
343-
outlived_fr,
344-
category,
345-
span,
346-
errors_buffer,
347-
),
348-
_ => self.report_general_error(
349-
mir,
350-
infcx,
351-
mir_def_id,
352-
fr,
353-
outlived_fr,
354-
category,
355-
span,
356-
errors_buffer,
357-
),
358-
}
359333
}
360334

361-
fn report_closure_error(
335+
fn report_escapes_closure_error(
362336
&self,
363337
mir: &Mir<'tcx>,
364338
infcx: &InferCtxt<'_, '_, 'tcx>,
@@ -374,16 +348,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
374348
self.get_var_name_and_span_for_region(infcx.tcx, mir, outlived_fr);
375349

376350
if fr_name_and_span.is_none() && outlived_fr_name_and_span.is_none() {
377-
return self.report_general_error(
378-
mir,
379-
infcx,
380-
mir_def_id,
381-
fr,
382-
outlived_fr,
383-
category,
384-
span,
385-
errors_buffer,
386-
);
351+
return self.report_general_error(mir, infcx, mir_def_id,
352+
fr, true, outlived_fr, false,
353+
category, span, errors_buffer);
387354
}
388355

389356
let mut diag = infcx
@@ -423,7 +390,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
423390
infcx: &InferCtxt<'_, '_, 'tcx>,
424391
mir_def_id: DefId,
425392
fr: RegionVid,
393+
fr_is_local: bool,
426394
outlived_fr: RegionVid,
395+
outlived_fr_is_local: bool,
427396
category: ConstraintCategory,
428397
span: Span,
429398
errors_buffer: &mut Vec<Diagnostic>,
@@ -434,17 +403,26 @@ impl<'tcx> RegionInferenceContext<'tcx> {
434403
);
435404

436405
let counter = &mut 1;
437-
let fr_name = self.give_region_a_name(infcx.tcx, mir, mir_def_id, fr, counter, &mut diag);
438-
let outlived_fr_name =
439-
self.give_region_a_name(infcx.tcx, mir, mir_def_id, outlived_fr, counter, &mut diag);
440-
441-
diag.span_label(
442-
span,
443-
format!(
444-
"{} requires that `{}` must outlive `{}`",
445-
category, fr_name, outlived_fr_name,
446-
),
447-
);
406+
let fr_name = self.give_region_a_name(
407+
infcx.tcx, mir, mir_def_id, fr, counter, &mut diag);
408+
let outlived_fr_name = self.give_region_a_name(
409+
infcx.tcx, mir, mir_def_id, outlived_fr, counter, &mut diag);
410+
411+
match (category, outlived_fr_is_local, fr_is_local) {
412+
(ConstraintCategory::Return, true, _) => {
413+
diag.span_label(span, format!(
414+
"closure was supposed to return data with lifetime `{}` but it is returning \
415+
data with lifetime `{}`",
416+
fr_name, outlived_fr_name,
417+
));
418+
},
419+
_ => {
420+
diag.span_label(span, format!(
421+
"{} requires that `{}` must outlive `{}`",
422+
category, fr_name, outlived_fr_name,
423+
));
424+
},
425+
}
448426

449427
diag.buffer(errors_buffer);
450428
}

0 commit comments

Comments
 (0)