Skip to content

Commit 1c89335

Browse files
committed
Revert "Auto merge of #133858 - dianne:better-blame-constraints-for-static, r=lcnr"
This reverts commit 6afee11, reversing changes made to 9c87288.
1 parent 6afee11 commit 1c89335

File tree

99 files changed

+620
-731
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+620
-731
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4115,6 +4115,7 @@ name = "rustc_middle"
41154115
version = "0.0.0"
41164116
dependencies = [
41174117
"bitflags",
4118+
"derive-where",
41184119
"either",
41194120
"field-offset",
41204121
"gsgdt",

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

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

15181518
self.explain_why_borrow_contains_point(location, borrow, None)
1519-
.add_explanation_to_diagnostic(&self, &mut err, "", Some(borrow_span), 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+
);
15201528
self.suggest_copy_for_type_in_cloned_ref(&mut err, place);
15211529
let typeck_results = self.infcx.tcx.typeck(self.mir_def_id());
15221530
if let Some(expr) = self.find_expr(borrow_span) {
@@ -1583,7 +1591,15 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
15831591
});
15841592

15851593
self.explain_why_borrow_contains_point(location, borrow, None)
1586-
.add_explanation_to_diagnostic(&self, &mut err, "", None, 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+
);
15871603
err
15881604
}
15891605

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

18721888
explanation.add_explanation_to_diagnostic(
1873-
&self,
1889+
self.infcx.tcx,
1890+
self.body,
1891+
&self.local_names,
18741892
&mut err,
18751893
first_borrow_desc,
18761894
None,
@@ -3028,7 +3046,15 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
30283046

30293047
if let BorrowExplanation::MustBeValidFor { .. } = explanation {
30303048
} else {
3031-
explanation.add_explanation_to_diagnostic(&self, &mut err, "", None, None);
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+
);
30323058
}
30333059
} else {
30343060
err.span_label(borrow_span, "borrowed value does not live long enough");
@@ -3041,7 +3067,15 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
30413067
}
30423068
});
30433069

3044-
explanation.add_explanation_to_diagnostic(&self, &mut err, "", Some(borrow_span), None);
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+
);
30453079
}
30463080

30473081
err
@@ -3094,7 +3128,15 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
30943128
_ => {}
30953129
}
30963130

3097-
explanation.add_explanation_to_diagnostic(&self, &mut err, "", None, None);
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+
);
30983140

30993141
self.buffer_error(err);
31003142
}
@@ -3267,7 +3309,15 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
32673309
}
32683310
_ => {}
32693311
}
3270-
explanation.add_explanation_to_diagnostic(&self, &mut err, "", None, None);
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+
);
32713321

32723322
borrow_spans.args_subdiag(&mut err, |args_span| {
32733323
crate::session_diagnostics::CaptureArgLabel::Capture {
@@ -3758,8 +3808,15 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
37583808
}
37593809
});
37603810

3761-
self.explain_why_borrow_contains_point(location, loan, None)
3762-
.add_explanation_to_diagnostic(&self, &mut err, "", None, None);
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+
);
37633820

37643821
self.explain_deref_coercion(loan, &mut err);
37653822

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+20-18
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ 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;
1112
use rustc_infer::infer::NllRegionVariableOrigin;
1213
use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault;
1314
use rustc_middle::mir::{
@@ -17,15 +18,14 @@ use rustc_middle::mir::{
1718
use rustc_middle::ty::adjustment::PointerCoercion;
1819
use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt};
1920
use rustc_middle::util::CallKind;
20-
use rustc_span::{DesugaringKind, Span, kw, sym};
21+
use rustc_span::{DesugaringKind, Span, Symbol, kw, sym};
2122
use rustc_trait_selection::error_reporting::traits::FindExprBySpan;
2223
use tracing::{debug, instrument};
2324

2425
use super::{RegionName, UseSpans, find_use};
2526
use crate::borrow_set::BorrowData;
26-
use crate::constraints::OutlivesConstraint;
2727
use crate::nll::ConstraintDescription;
28-
use crate::region_infer::{BlameConstraint, Cause};
28+
use crate::region_infer::{BlameConstraint, Cause, ExtraConstraintInfo};
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-
path: Vec<OutlivesConstraint<'tcx>>,
46+
extra_info: Vec<ExtraConstraintInfo>,
4747
},
4848
Unexplained,
4949
}
@@ -63,16 +63,14 @@ impl<'tcx> BorrowExplanation<'tcx> {
6363
}
6464
pub(crate) fn add_explanation_to_diagnostic(
6565
&self,
66-
cx: &MirBorrowckCtxt<'_, '_, 'tcx>,
66+
tcx: TyCtxt<'tcx>,
67+
body: &Body<'tcx>,
68+
local_names: &IndexSlice<Local, Option<Symbol>>,
6769
err: &mut Diag<'_>,
6870
borrow_desc: &str,
6971
borrow_span: Option<Span>,
7072
multiple_borrow_span: Option<(Span, Span)>,
7173
) {
72-
let tcx = cx.infcx.tcx;
73-
let body = cx.body;
74-
let local_names = &cx.local_names;
75-
7674
if let Some(span) = borrow_span {
7775
let def_id = body.source.def_id();
7876
if let Some(node) = tcx.hir().get_if_local(def_id)
@@ -308,7 +306,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
308306
ref region_name,
309307
ref opt_place_desc,
310308
from_closure: _,
311-
ref path,
309+
ref extra_info,
312310
} => {
313311
region_name.highlight_region_name(err);
314312

@@ -330,8 +328,13 @@ impl<'tcx> BorrowExplanation<'tcx> {
330328
);
331329
};
332330

333-
cx.add_placeholder_from_predicate_note(err, &path);
334-
cx.add_sized_or_copy_bound_info(err, category, &path);
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+
}
335338

336339
if let ConstraintCategory::Cast {
337340
is_implicit_coercion: true,
@@ -484,9 +487,8 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
484487
&self,
485488
borrow_region: RegionVid,
486489
outlived_region: RegionVid,
487-
) -> (ConstraintCategory<'tcx>, bool, Span, Option<RegionName>, Vec<OutlivesConstraint<'tcx>>)
488-
{
489-
let (blame_constraint, path) = self.regioncx.best_blame_constraint(
490+
) -> (ConstraintCategory<'tcx>, bool, Span, Option<RegionName>, Vec<ExtraConstraintInfo>) {
491+
let (blame_constraint, extra_info) = self.regioncx.best_blame_constraint(
490492
borrow_region,
491493
NllRegionVariableOrigin::FreeRegion,
492494
|r| self.regioncx.provides_universal_region(r, borrow_region, outlived_region),
@@ -495,7 +497,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
495497

496498
let outlived_fr_name = self.give_region_a_name(outlived_region);
497499

498-
(category, from_closure, cause.span, outlived_fr_name, path)
500+
(category, from_closure, cause.span, outlived_fr_name, extra_info)
499501
}
500502

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

595597
None => {
596598
if let Some(region) = self.to_error_region_vid(borrow_region_vid) {
597-
let (category, from_closure, span, region_name, path) =
599+
let (category, from_closure, span, region_name, extra_info) =
598600
self.free_region_constraint_info(borrow_region_vid, region);
599601
if let Some(region_name) = region_name {
600602
let opt_place_desc = self.describe_place(borrow.borrowed_place.as_ref());
@@ -604,7 +606,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
604606
span,
605607
region_name,
606608
opt_place_desc,
607-
path,
609+
extra_info,
608610
}
609611
} else {
610612
debug!("Could not generate a region name");

compiler/rustc_borrowck/src/diagnostics/mod.rs

+4-54
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@ use rustc_errors::{Applicability, Diag, MultiSpan};
88
use rustc_hir::def::{CtorKind, Namespace};
99
use rustc_hir::{self as hir, CoroutineKind, LangItem};
1010
use rustc_index::IndexSlice;
11-
use rustc_infer::infer::{
12-
BoundRegionConversionTime, NllRegionVariableOrigin, RegionVariableOrigin,
13-
};
11+
use rustc_infer::infer::BoundRegionConversionTime;
1412
use rustc_infer::traits::SelectionError;
1513
use rustc_middle::bug;
1614
use rustc_middle::mir::tcx::PlaceTy;
1715
use rustc_middle::mir::{
18-
AggregateKind, CallSource, ConstOperand, ConstraintCategory, FakeReadCause, Local, LocalInfo,
19-
LocalKind, Location, Operand, Place, PlaceRef, ProjectionElem, Rvalue, Statement,
20-
StatementKind, Terminator, TerminatorKind,
16+
AggregateKind, CallSource, ConstOperand, FakeReadCause, Local, LocalInfo, LocalKind, Location,
17+
Operand, Place, PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind, Terminator,
18+
TerminatorKind,
2119
};
2220
use rustc_middle::ty::print::Print;
2321
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
@@ -35,9 +33,7 @@ use tracing::debug;
3533

3634
use super::MirBorrowckCtxt;
3735
use super::borrow_set::BorrowData;
38-
use crate::constraints::OutlivesConstraint;
3936
use crate::fluent_generated as fluent;
40-
use crate::nll::ConstraintDescription;
4137
use crate::session_diagnostics::{
4238
CaptureArgLabel, CaptureReasonLabel, CaptureReasonNote, CaptureReasonSuggest, CaptureVarCause,
4339
CaptureVarKind, CaptureVarPathUseCause, OnClosureNote,
@@ -623,52 +619,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
623619
region.print(&mut printer).unwrap();
624620
printer.into_buffer()
625621
}
626-
627-
/// Add a note to region errors and borrow explanations when higher-ranked regions in predicates
628-
/// implicitly introduce an "outlives `'static`" constraint.
629-
fn add_placeholder_from_predicate_note(
630-
&self,
631-
err: &mut Diag<'_>,
632-
path: &[OutlivesConstraint<'tcx>],
633-
) {
634-
let predicate_span = path.iter().find_map(|constraint| {
635-
let outlived = constraint.sub;
636-
if let Some(origin) = self.regioncx.var_infos.get(outlived)
637-
&& let RegionVariableOrigin::Nll(NllRegionVariableOrigin::Placeholder(_)) =
638-
origin.origin
639-
&& let ConstraintCategory::Predicate(span) = constraint.category
640-
{
641-
Some(span)
642-
} else {
643-
None
644-
}
645-
});
646-
647-
if let Some(span) = predicate_span {
648-
err.span_note(span, "due to current limitations in the borrow checker, this implies a `'static` lifetime");
649-
}
650-
}
651-
652-
/// Add a label to region errors and borrow explanations when outlives constraints arise from
653-
/// proving a type implements `Sized` or `Copy`.
654-
fn add_sized_or_copy_bound_info(
655-
&self,
656-
err: &mut Diag<'_>,
657-
blamed_category: ConstraintCategory<'tcx>,
658-
path: &[OutlivesConstraint<'tcx>],
659-
) {
660-
for sought_category in [ConstraintCategory::SizedBound, ConstraintCategory::CopyBound] {
661-
if sought_category != blamed_category
662-
&& let Some(sought_constraint) = path.iter().find(|c| c.category == sought_category)
663-
{
664-
let label = format!(
665-
"requirement occurs due to {}",
666-
sought_category.description().trim_end()
667-
);
668-
err.span_label(sought_constraint.span, label);
669-
}
670-
}
671-
}
672622
}
673623

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

0 commit comments

Comments
 (0)