Skip to content

Commit d7ab2ab

Browse files
committed
cleanup: remove ExtraConstraintInfo
`ExtraConstraintInfo` was used only for a single subdiagnostic, so this moves the logic for that to its own function and eliminates the indirection. In order to do so cleanly, this also changes the arguments to `BorrowExplanation::add_explanation_to_diagnostic`, which happens to simplify its call sites.
1 parent 4ba4ac6 commit d7ab2ab

File tree

5 files changed

+68
-131
lines changed

5 files changed

+68
-131
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 9 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,15 +1516,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
15161516
});
15171517

15181518
self.explain_why_borrow_contains_point(location, borrow, None)
1519-
.add_explanation_to_diagnostic(
1520-
self.infcx.tcx,
1521-
self.body,
1522-
&self.local_names,
1523-
&mut err,
1524-
"",
1525-
Some(borrow_span),
1526-
None,
1527-
);
1519+
.add_explanation_to_diagnostic(&self, &mut err, "", Some(borrow_span), None);
15281520
self.suggest_copy_for_type_in_cloned_ref(&mut err, place);
15291521
let typeck_results = self.infcx.tcx.typeck(self.mir_def_id());
15301522
if let Some(expr) = self.find_expr(borrow_span) {
@@ -1591,15 +1583,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
15911583
});
15921584

15931585
self.explain_why_borrow_contains_point(location, borrow, None)
1594-
.add_explanation_to_diagnostic(
1595-
self.infcx.tcx,
1596-
self.body,
1597-
&self.local_names,
1598-
&mut err,
1599-
"",
1600-
None,
1601-
None,
1602-
);
1586+
.add_explanation_to_diagnostic(&self, &mut err, "", None, None);
16031587
err
16041588
}
16051589

@@ -1886,9 +1870,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
18861870
}
18871871

18881872
explanation.add_explanation_to_diagnostic(
1889-
self.infcx.tcx,
1890-
self.body,
1891-
&self.local_names,
1873+
&self,
18921874
&mut err,
18931875
first_borrow_desc,
18941876
None,
@@ -3046,15 +3028,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
30463028

30473029
if let BorrowExplanation::MustBeValidFor { .. } = explanation {
30483030
} else {
3049-
explanation.add_explanation_to_diagnostic(
3050-
self.infcx.tcx,
3051-
self.body,
3052-
&self.local_names,
3053-
&mut err,
3054-
"",
3055-
None,
3056-
None,
3057-
);
3031+
explanation.add_explanation_to_diagnostic(&self, &mut err, "", None, None);
30583032
}
30593033
} else {
30603034
err.span_label(borrow_span, "borrowed value does not live long enough");
@@ -3067,15 +3041,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
30673041
}
30683042
});
30693043

3070-
explanation.add_explanation_to_diagnostic(
3071-
self.infcx.tcx,
3072-
self.body,
3073-
&self.local_names,
3074-
&mut err,
3075-
"",
3076-
Some(borrow_span),
3077-
None,
3078-
);
3044+
explanation.add_explanation_to_diagnostic(&self, &mut err, "", Some(borrow_span), None);
30793045
}
30803046

30813047
err
@@ -3128,15 +3094,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
31283094
_ => {}
31293095
}
31303096

3131-
explanation.add_explanation_to_diagnostic(
3132-
self.infcx.tcx,
3133-
self.body,
3134-
&self.local_names,
3135-
&mut err,
3136-
"",
3137-
None,
3138-
None,
3139-
);
3097+
explanation.add_explanation_to_diagnostic(&self, &mut err, "", None, None);
31403098

31413099
self.buffer_error(err);
31423100
}
@@ -3309,15 +3267,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
33093267
}
33103268
_ => {}
33113269
}
3312-
explanation.add_explanation_to_diagnostic(
3313-
self.infcx.tcx,
3314-
self.body,
3315-
&self.local_names,
3316-
&mut err,
3317-
"",
3318-
None,
3319-
None,
3320-
);
3270+
explanation.add_explanation_to_diagnostic(&self, &mut err, "", None, None);
33213271

33223272
borrow_spans.args_subdiag(&mut err, |args_span| {
33233273
crate::session_diagnostics::CaptureArgLabel::Capture {
@@ -3808,15 +3758,8 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
38083758
}
38093759
});
38103760

3811-
self.explain_why_borrow_contains_point(location, loan, None).add_explanation_to_diagnostic(
3812-
self.infcx.tcx,
3813-
self.body,
3814-
&self.local_names,
3815-
&mut err,
3816-
"",
3817-
None,
3818-
None,
3819-
);
3761+
self.explain_why_borrow_contains_point(location, loan, None)
3762+
.add_explanation_to_diagnostic(&self, &mut err, "", None, None);
38203763

38213764
self.explain_deref_coercion(loan, &mut err);
38223765

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::assert_matches::assert_matches;
88
use rustc_errors::{Applicability, Diag};
99
use rustc_hir as hir;
1010
use rustc_hir::intravisit::Visitor;
11-
use rustc_index::IndexSlice;
1211
use rustc_infer::infer::NllRegionVariableOrigin;
1312
use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault;
1413
use rustc_middle::mir::{
@@ -17,14 +16,15 @@ use rustc_middle::mir::{
1716
};
1817
use rustc_middle::ty::adjustment::PointerCoercion;
1918
use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt};
20-
use rustc_span::{DesugaringKind, Span, Symbol, kw, sym};
19+
use rustc_span::{DesugaringKind, Span, kw, sym};
2120
use rustc_trait_selection::error_reporting::traits::FindExprBySpan;
2221
use tracing::{debug, instrument};
2322

2423
use super::{RegionName, UseSpans, find_use};
2524
use crate::borrow_set::BorrowData;
25+
use crate::constraints::OutlivesConstraint;
2626
use crate::nll::ConstraintDescription;
27-
use crate::region_infer::{BlameConstraint, Cause, ExtraConstraintInfo};
27+
use crate::region_infer::{BlameConstraint, Cause};
2828
use crate::{MirBorrowckCtxt, WriteKind};
2929

3030
#[derive(Debug)]
@@ -42,7 +42,7 @@ pub(crate) enum BorrowExplanation<'tcx> {
4242
span: Span,
4343
region_name: RegionName,
4444
opt_place_desc: Option<String>,
45-
extra_info: Vec<ExtraConstraintInfo>,
45+
path: Vec<OutlivesConstraint<'tcx>>,
4646
},
4747
Unexplained,
4848
}
@@ -62,14 +62,16 @@ impl<'tcx> BorrowExplanation<'tcx> {
6262
}
6363
pub(crate) fn add_explanation_to_diagnostic(
6464
&self,
65-
tcx: TyCtxt<'tcx>,
66-
body: &Body<'tcx>,
67-
local_names: &IndexSlice<Local, Option<Symbol>>,
65+
cx: &MirBorrowckCtxt<'_, '_, 'tcx>,
6866
err: &mut Diag<'_>,
6967
borrow_desc: &str,
7068
borrow_span: Option<Span>,
7169
multiple_borrow_span: Option<(Span, Span)>,
7270
) {
71+
let tcx = cx.infcx.tcx;
72+
let body = cx.body;
73+
let local_names = &cx.local_names;
74+
7375
if let Some(span) = borrow_span {
7476
let def_id = body.source.def_id();
7577
if let Some(node) = tcx.hir().get_if_local(def_id)
@@ -305,7 +307,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
305307
ref region_name,
306308
ref opt_place_desc,
307309
from_closure: _,
308-
ref extra_info,
310+
ref path,
309311
} => {
310312
region_name.highlight_region_name(err);
311313

@@ -327,13 +329,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
327329
);
328330
};
329331

330-
for extra in extra_info {
331-
match extra {
332-
ExtraConstraintInfo::PlaceholderFromPredicate(span) => {
333-
err.span_note(*span, "due to current limitations in the borrow checker, this implies a `'static` lifetime");
334-
}
335-
}
336-
}
332+
cx.add_placeholder_from_predicate_note(err, &path);
337333

338334
if let ConstraintCategory::Cast {
339335
is_implicit_coercion: true,
@@ -486,8 +482,9 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
486482
&self,
487483
borrow_region: RegionVid,
488484
outlived_region: RegionVid,
489-
) -> (ConstraintCategory<'tcx>, bool, Span, Option<RegionName>, Vec<ExtraConstraintInfo>) {
490-
let (blame_constraint, extra_info) = self.regioncx.best_blame_constraint(
485+
) -> (ConstraintCategory<'tcx>, bool, Span, Option<RegionName>, Vec<OutlivesConstraint<'tcx>>)
486+
{
487+
let (blame_constraint, path) = self.regioncx.best_blame_constraint(
491488
borrow_region,
492489
NllRegionVariableOrigin::FreeRegion,
493490
|r| self.regioncx.provides_universal_region(r, borrow_region, outlived_region),
@@ -496,7 +493,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
496493

497494
let outlived_fr_name = self.give_region_a_name(outlived_region);
498495

499-
(category, from_closure, cause.span, outlived_fr_name, extra_info)
496+
(category, from_closure, cause.span, outlived_fr_name, path)
500497
}
501498

502499
/// Returns structured explanation for *why* the borrow contains the
@@ -595,7 +592,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
595592

596593
None => {
597594
if let Some(region) = self.to_error_region_vid(borrow_region_vid) {
598-
let (category, from_closure, span, region_name, extra_info) =
595+
let (category, from_closure, span, region_name, path) =
599596
self.free_region_constraint_info(borrow_region_vid, region);
600597
if let Some(region_name) = region_name {
601598
let opt_place_desc = self.describe_place(borrow.borrowed_place.as_ref());
@@ -605,7 +602,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
605602
span,
606603
region_name,
607604
opt_place_desc,
608-
extra_info,
605+
path,
609606
}
610607
} else {
611608
debug!("Could not generate a region name");

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ use rustc_errors::{Applicability, Diag, MultiSpan};
55
use rustc_hir::def::{CtorKind, Namespace};
66
use rustc_hir::{self as hir, CoroutineKind, LangItem};
77
use rustc_index::IndexSlice;
8-
use rustc_infer::infer::BoundRegionConversionTime;
8+
use rustc_infer::infer::{
9+
BoundRegionConversionTime, NllRegionVariableOrigin, RegionVariableOrigin,
10+
};
911
use rustc_infer::traits::SelectionError;
1012
use rustc_middle::bug;
1113
use rustc_middle::mir::tcx::PlaceTy;
1214
use rustc_middle::mir::{
13-
AggregateKind, CallSource, ConstOperand, FakeReadCause, Local, LocalInfo, LocalKind, Location,
14-
Operand, Place, PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind, Terminator,
15-
TerminatorKind,
15+
AggregateKind, CallSource, ConstOperand, ConstraintCategory, FakeReadCause, Local, LocalInfo,
16+
LocalKind, Location, Operand, Place, PlaceRef, ProjectionElem, Rvalue, Statement,
17+
StatementKind, Terminator, TerminatorKind,
1618
};
1719
use rustc_middle::ty::print::Print;
1820
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
@@ -30,6 +32,7 @@ use tracing::debug;
3032

3133
use super::MirBorrowckCtxt;
3234
use super::borrow_set::BorrowData;
35+
use crate::constraints::OutlivesConstraint;
3336
use crate::fluent_generated as fluent;
3437
use crate::session_diagnostics::{
3538
CaptureArgLabel, CaptureReasonLabel, CaptureReasonNote, CaptureReasonSuggest, CaptureVarCause,
@@ -496,6 +499,31 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
496499
region.print(&mut printer).unwrap();
497500
printer.into_buffer()
498501
}
502+
503+
/// Add a note to region errors and borrow explanations when higher-ranked regions in predicates
504+
/// implicitly introduce an "outlives `'static`" constraint.
505+
fn add_placeholder_from_predicate_note(
506+
&self,
507+
err: &mut Diag<'_>,
508+
path: &[OutlivesConstraint<'tcx>],
509+
) {
510+
let predicate_span = path.iter().find_map(|constraint| {
511+
let outlived = constraint.sub;
512+
if let Some(origin) = self.regioncx.var_infos.get(outlived)
513+
&& let RegionVariableOrigin::Nll(NllRegionVariableOrigin::Placeholder(_)) =
514+
origin.origin
515+
&& let ConstraintCategory::Predicate(span) = constraint.category
516+
{
517+
Some(span)
518+
} else {
519+
None
520+
}
521+
});
522+
523+
if let Some(span) = predicate_span {
524+
err.span_note(span, "due to current limitations in the borrow checker, this implies a `'static` lifetime");
525+
}
526+
}
499527
}
500528

501529
/// The span(s) associated to a use of a place.

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use tracing::{debug, instrument, trace};
2929
use super::{OutlivesSuggestionBuilder, RegionName, RegionNameSource};
3030
use crate::nll::ConstraintDescription;
3131
use crate::region_infer::values::RegionElement;
32-
use crate::region_infer::{BlameConstraint, ExtraConstraintInfo, TypeTest};
32+
use crate::region_infer::{BlameConstraint, TypeTest};
3333
use crate::session_diagnostics::{
3434
FnMutError, FnMutReturnTypeErr, GenericDoesNotLiveLongEnough, LifetimeOutliveErr,
3535
LifetimeReturnCategoryErr, RequireStaticErr, VarHereDenote,
@@ -440,10 +440,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
440440
) {
441441
debug!("report_region_error(fr={:?}, outlived_fr={:?})", fr, outlived_fr);
442442

443-
let (blame_constraint, extra_info) =
444-
self.regioncx.best_blame_constraint(fr, fr_origin, |r| {
445-
self.regioncx.provides_universal_region(r, fr, outlived_fr)
446-
});
443+
let (blame_constraint, path) = self.regioncx.best_blame_constraint(fr, fr_origin, |r| {
444+
self.regioncx.provides_universal_region(r, fr, outlived_fr)
445+
});
447446
let BlameConstraint { category, cause, variance_info, .. } = blame_constraint;
448447

449448
debug!("report_region_error: category={:?} {:?} {:?}", category, cause, variance_info);
@@ -554,13 +553,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
554553
}
555554
}
556555

557-
for extra in extra_info {
558-
match extra {
559-
ExtraConstraintInfo::PlaceholderFromPredicate(span) => {
560-
diag.span_note(span, "due to current limitations in the borrow checker, this implies a `'static` lifetime");
561-
}
562-
}
563-
}
556+
self.add_placeholder_from_predicate_note(&mut diag, &path);
564557

565558
self.buffer_error(diag);
566559
}

0 commit comments

Comments
 (0)