Skip to content

Commit 7fea859

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 e217f94 commit 7fea859

File tree

5 files changed

+68
-131
lines changed

5 files changed

+68
-131
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+9-66
Original file line numberDiff line numberDiff line change
@@ -1517,15 +1517,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
15171517
});
15181518

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

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

@@ -1887,9 +1871,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
18871871
}
18881872

18891873
explanation.add_explanation_to_diagnostic(
1890-
self.infcx.tcx,
1891-
self.body,
1892-
&self.local_names,
1874+
&self,
18931875
&mut err,
18941876
first_borrow_desc,
18951877
None,
@@ -3047,15 +3029,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
30473029

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

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

30823048
err
@@ -3129,15 +3095,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
31293095
_ => {}
31303096
}
31313097

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

31423100
self.buffer_error(err);
31433101
}
@@ -3310,15 +3268,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
33103268
}
33113269
_ => {}
33123270
}
3313-
explanation.add_explanation_to_diagnostic(
3314-
self.infcx.tcx,
3315-
self.body,
3316-
&self.local_names,
3317-
&mut err,
3318-
"",
3319-
None,
3320-
None,
3321-
);
3271+
explanation.add_explanation_to_diagnostic(&self, &mut err, "", None, None);
33223272

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

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

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

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+17-20
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,15 +16,16 @@ 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::symbol::{Symbol, kw};
19+
use rustc_span::symbol::kw;
2120
use rustc_span::{DesugaringKind, Span, sym};
2221
use rustc_trait_selection::error_reporting::traits::FindExprBySpan;
2322
use tracing::{debug, instrument};
2423

2524
use super::{RegionName, UseSpans, find_use};
2625
use crate::borrow_set::BorrowData;
26+
use crate::constraints::OutlivesConstraint;
2727
use crate::nll::ConstraintDescription;
28-
use crate::region_infer::{BlameConstraint, Cause, ExtraConstraintInfo};
28+
use crate::region_infer::{BlameConstraint, Cause};
2929
use crate::{MirBorrowckCtxt, WriteKind};
3030

3131
#[derive(Debug)]
@@ -43,7 +43,7 @@ pub(crate) enum BorrowExplanation<'tcx> {
4343
span: Span,
4444
region_name: RegionName,
4545
opt_place_desc: Option<String>,
46-
extra_info: Vec<ExtraConstraintInfo>,
46+
path: Vec<OutlivesConstraint<'tcx>>,
4747
},
4848
Unexplained,
4949
}
@@ -63,14 +63,16 @@ impl<'tcx> BorrowExplanation<'tcx> {
6363
}
6464
pub(crate) fn add_explanation_to_diagnostic(
6565
&self,
66-
tcx: TyCtxt<'tcx>,
67-
body: &Body<'tcx>,
68-
local_names: &IndexSlice<Local, Option<Symbol>>,
66+
cx: &MirBorrowckCtxt<'_, '_, 'tcx>,
6967
err: &mut Diag<'_>,
7068
borrow_desc: &str,
7169
borrow_span: Option<Span>,
7270
multiple_borrow_span: Option<(Span, Span)>,
7371
) {
72+
let tcx = cx.infcx.tcx;
73+
let body = cx.body;
74+
let local_names = &cx.local_names;
75+
7476
if let Some(span) = borrow_span {
7577
let def_id = body.source.def_id();
7678
if let Some(node) = tcx.hir().get_if_local(def_id)
@@ -306,7 +308,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
306308
ref region_name,
307309
ref opt_place_desc,
308310
from_closure: _,
309-
ref extra_info,
311+
ref path,
310312
} => {
311313
region_name.highlight_region_name(err);
312314

@@ -328,13 +330,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
328330
);
329331
};
330332

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

339335
if let ConstraintCategory::Cast {
340336
is_implicit_coercion: true,
@@ -487,8 +483,9 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
487483
&self,
488484
borrow_region: RegionVid,
489485
outlived_region: RegionVid,
490-
) -> (ConstraintCategory<'tcx>, bool, Span, Option<RegionName>, Vec<ExtraConstraintInfo>) {
491-
let (blame_constraint, extra_info) = self.regioncx.best_blame_constraint(
486+
) -> (ConstraintCategory<'tcx>, bool, Span, Option<RegionName>, Vec<OutlivesConstraint<'tcx>>)
487+
{
488+
let (blame_constraint, path) = self.regioncx.best_blame_constraint(
492489
borrow_region,
493490
NllRegionVariableOrigin::FreeRegion,
494491
|r| self.regioncx.provides_universal_region(r, borrow_region, outlived_region),
@@ -497,7 +494,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
497494

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

500-
(category, from_closure, cause.span, outlived_fr_name, extra_info)
497+
(category, from_closure, cause.span, outlived_fr_name, path)
501498
}
502499

503500
/// Returns structured explanation for *why* the borrow contains the
@@ -596,7 +593,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
596593

597594
None => {
598595
if let Some(region) = self.to_error_region_vid(borrow_region_vid) {
599-
let (category, from_closure, span, region_name, extra_info) =
596+
let (category, from_closure, span, region_name, path) =
600597
self.free_region_constraint_info(borrow_region_vid, region);
601598
if let Some(region_name) = region_name {
602599
let opt_place_desc = self.describe_place(borrow.borrowed_place.as_ref());
@@ -606,7 +603,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
606603
span,
607604
region_name,
608605
opt_place_desc,
609-
extra_info,
606+
path,
610607
}
611608
} else {
612609
debug!("Could not generate a region name");

compiler/rustc_borrowck/src/diagnostics/mod.rs

+32-4
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};
@@ -31,6 +33,7 @@ use tracing::debug;
3133

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

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

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use tracing::{debug, instrument, trace};
3030
use super::{OutlivesSuggestionBuilder, RegionName, RegionNameSource};
3131
use crate::nll::ConstraintDescription;
3232
use crate::region_infer::values::RegionElement;
33-
use crate::region_infer::{BlameConstraint, ExtraConstraintInfo, TypeTest};
33+
use crate::region_infer::{BlameConstraint, TypeTest};
3434
use crate::session_diagnostics::{
3535
FnMutError, FnMutReturnTypeErr, GenericDoesNotLiveLongEnough, LifetimeOutliveErr,
3636
LifetimeReturnCategoryErr, RequireStaticErr, VarHereDenote,
@@ -441,10 +441,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
441441
) {
442442
debug!("report_region_error(fr={:?}, outlived_fr={:?})", fr, outlived_fr);
443443

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

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

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

566559
self.buffer_error(diag);
567560
}

0 commit comments

Comments
 (0)