Skip to content

Commit

Permalink
Revert "Auto merge of rust-lang#133858 - dianne:better-blame-constrai…
Browse files Browse the repository at this point in the history
…nts-for-static, r=lcnr"

This reverts commit 6afee11, reversing
changes made to 9c87288.
  • Loading branch information
lqd committed Jan 8, 2025
1 parent a580b5c commit 977e6c8
Show file tree
Hide file tree
Showing 99 changed files with 630 additions and 733 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4115,6 +4115,7 @@ name = "rustc_middle"
version = "0.0.0"
dependencies = [
"bitflags",
"derive-where",
"either",
"field-offset",
"gsgdt",
Expand Down
75 changes: 66 additions & 9 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1516,7 +1516,15 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
});

self.explain_why_borrow_contains_point(location, borrow, None)
.add_explanation_to_diagnostic(&self, &mut err, "", Some(borrow_span), None);
.add_explanation_to_diagnostic(
self.infcx.tcx,
self.body,
&self.local_names,
&mut err,
"",
Some(borrow_span),
None,
);
self.suggest_copy_for_type_in_cloned_ref(&mut err, place);
let typeck_results = self.infcx.tcx.typeck(self.mir_def_id());
if let Some(expr) = self.find_expr(borrow_span) {
Expand Down Expand Up @@ -1583,7 +1591,15 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
});

self.explain_why_borrow_contains_point(location, borrow, None)
.add_explanation_to_diagnostic(&self, &mut err, "", None, None);
.add_explanation_to_diagnostic(
self.infcx.tcx,
self.body,
&self.local_names,
&mut err,
"",
None,
None,
);
err
}

Expand Down Expand Up @@ -1870,7 +1886,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
}

explanation.add_explanation_to_diagnostic(
&self,
self.infcx.tcx,
self.body,
&self.local_names,
&mut err,
first_borrow_desc,
None,
Expand Down Expand Up @@ -3028,7 +3046,15 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {

if let BorrowExplanation::MustBeValidFor { .. } = explanation {
} else {
explanation.add_explanation_to_diagnostic(&self, &mut err, "", None, None);
explanation.add_explanation_to_diagnostic(
self.infcx.tcx,
self.body,
&self.local_names,
&mut err,
"",
None,
None,
);
}
} else {
err.span_label(borrow_span, "borrowed value does not live long enough");
Expand All @@ -3041,7 +3067,15 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
}
});

explanation.add_explanation_to_diagnostic(&self, &mut err, "", Some(borrow_span), None);
explanation.add_explanation_to_diagnostic(
self.infcx.tcx,
self.body,
&self.local_names,
&mut err,
"",
Some(borrow_span),
None,
);
}

err
Expand Down Expand Up @@ -3094,7 +3128,15 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
_ => {}
}

explanation.add_explanation_to_diagnostic(&self, &mut err, "", None, None);
explanation.add_explanation_to_diagnostic(
self.infcx.tcx,
self.body,
&self.local_names,
&mut err,
"",
None,
None,
);

self.buffer_error(err);
}
Expand Down Expand Up @@ -3267,7 +3309,15 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
}
_ => {}
}
explanation.add_explanation_to_diagnostic(&self, &mut err, "", None, None);
explanation.add_explanation_to_diagnostic(
self.infcx.tcx,
self.body,
&self.local_names,
&mut err,
"",
None,
None,
);

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

self.explain_why_borrow_contains_point(location, loan, None)
.add_explanation_to_diagnostic(&self, &mut err, "", None, None);
self.explain_why_borrow_contains_point(location, loan, None).add_explanation_to_diagnostic(
self.infcx.tcx,
self.body,
&self.local_names,
&mut err,
"",
None,
None,
);

self.explain_deref_coercion(loan, &mut err);

Expand Down
38 changes: 20 additions & 18 deletions compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::assert_matches::assert_matches;
use rustc_errors::{Applicability, Diag, EmissionGuarantee};
use rustc_hir as hir;
use rustc_hir::intravisit::Visitor;
use rustc_index::IndexSlice;
use rustc_infer::infer::NllRegionVariableOrigin;
use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault;
use rustc_middle::mir::{
Expand All @@ -17,15 +18,14 @@ use rustc_middle::mir::{
use rustc_middle::ty::adjustment::PointerCoercion;
use rustc_middle::ty::{self, RegionVid, Ty, TyCtxt};
use rustc_middle::util::CallKind;
use rustc_span::{DesugaringKind, Span, kw, sym};
use rustc_span::{DesugaringKind, Span, Symbol, kw, sym};
use rustc_trait_selection::error_reporting::traits::FindExprBySpan;
use tracing::{debug, instrument};

use super::{RegionName, UseSpans, find_use};
use crate::borrow_set::BorrowData;
use crate::constraints::OutlivesConstraint;
use crate::nll::ConstraintDescription;
use crate::region_infer::{BlameConstraint, Cause};
use crate::region_infer::{BlameConstraint, Cause, ExtraConstraintInfo};
use crate::{MirBorrowckCtxt, WriteKind};

#[derive(Debug)]
Expand All @@ -43,7 +43,7 @@ pub(crate) enum BorrowExplanation<'tcx> {
span: Span,
region_name: RegionName,
opt_place_desc: Option<String>,
path: Vec<OutlivesConstraint<'tcx>>,
extra_info: Vec<ExtraConstraintInfo>,
},
Unexplained,
}
Expand All @@ -63,16 +63,14 @@ impl<'tcx> BorrowExplanation<'tcx> {
}
pub(crate) fn add_explanation_to_diagnostic<G: EmissionGuarantee>(
&self,
cx: &MirBorrowckCtxt<'_, '_, 'tcx>,
tcx: TyCtxt<'tcx>,
body: &Body<'tcx>,
local_names: &IndexSlice<Local, Option<Symbol>>,
err: &mut Diag<'_, G>,
borrow_desc: &str,
borrow_span: Option<Span>,
multiple_borrow_span: Option<(Span, Span)>,
) {
let tcx = cx.infcx.tcx;
let body = cx.body;
let local_names = &cx.local_names;

if let Some(span) = borrow_span {
let def_id = body.source.def_id();
if let Some(node) = tcx.hir().get_if_local(def_id)
Expand Down Expand Up @@ -308,7 +306,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
ref region_name,
ref opt_place_desc,
from_closure: _,
ref path,
ref extra_info,
} => {
region_name.highlight_region_name(err);

Expand All @@ -330,8 +328,13 @@ impl<'tcx> BorrowExplanation<'tcx> {
);
};

cx.add_placeholder_from_predicate_note(err, &path);
cx.add_sized_or_copy_bound_info(err, category, &path);
for extra in extra_info {
match extra {
ExtraConstraintInfo::PlaceholderFromPredicate(span) => {
err.span_note(*span, "due to current limitations in the borrow checker, this implies a `'static` lifetime");
}
}
}

if let ConstraintCategory::Cast {
is_implicit_coercion: true,
Expand Down Expand Up @@ -484,9 +487,8 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
&self,
borrow_region: RegionVid,
outlived_region: RegionVid,
) -> (ConstraintCategory<'tcx>, bool, Span, Option<RegionName>, Vec<OutlivesConstraint<'tcx>>)
{
let (blame_constraint, path) = self.regioncx.best_blame_constraint(
) -> (ConstraintCategory<'tcx>, bool, Span, Option<RegionName>, Vec<ExtraConstraintInfo>) {
let (blame_constraint, extra_info) = self.regioncx.best_blame_constraint(
borrow_region,
NllRegionVariableOrigin::FreeRegion,
|r| self.regioncx.provides_universal_region(r, borrow_region, outlived_region),
Expand All @@ -495,7 +497,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {

let outlived_fr_name = self.give_region_a_name(outlived_region);

(category, from_closure, cause.span, outlived_fr_name, path)
(category, from_closure, cause.span, outlived_fr_name, extra_info)
}

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

None => {
if let Some(region) = self.to_error_region_vid(borrow_region_vid) {
let (category, from_closure, span, region_name, path) =
let (category, from_closure, span, region_name, extra_info) =
self.free_region_constraint_info(borrow_region_vid, region);
if let Some(region_name) = region_name {
let opt_place_desc = self.describe_place(borrow.borrowed_place.as_ref());
Expand All @@ -604,7 +606,7 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
span,
region_name,
opt_place_desc,
path,
extra_info,
}
} else {
debug!("Could not generate a region name");
Expand Down
60 changes: 5 additions & 55 deletions compiler/rustc_borrowck/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@ use std::collections::BTreeMap;

use rustc_abi::{FieldIdx, VariantIdx};
use rustc_data_structures::fx::FxIndexMap;
use rustc_errors::{Applicability, Diag, EmissionGuarantee, MultiSpan};
use rustc_errors::{Applicability, Diag, MultiSpan};
use rustc_hir::def::{CtorKind, Namespace};
use rustc_hir::{self as hir, CoroutineKind, LangItem};
use rustc_index::IndexSlice;
use rustc_infer::infer::{
BoundRegionConversionTime, NllRegionVariableOrigin, RegionVariableOrigin,
};
use rustc_infer::infer::BoundRegionConversionTime;
use rustc_infer::traits::SelectionError;
use rustc_middle::bug;
use rustc_middle::mir::tcx::PlaceTy;
use rustc_middle::mir::{
AggregateKind, CallSource, ConstOperand, ConstraintCategory, FakeReadCause, Local, LocalInfo,
LocalKind, Location, Operand, Place, PlaceRef, ProjectionElem, Rvalue, Statement,
StatementKind, Terminator, TerminatorKind,
AggregateKind, CallSource, ConstOperand, FakeReadCause, Local, LocalInfo, LocalKind, Location,
Operand, Place, PlaceRef, ProjectionElem, Rvalue, Statement, StatementKind, Terminator,
TerminatorKind,
};
use rustc_middle::ty::print::Print;
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
Expand All @@ -35,9 +33,7 @@ use tracing::debug;

use super::MirBorrowckCtxt;
use super::borrow_set::BorrowData;
use crate::constraints::OutlivesConstraint;
use crate::fluent_generated as fluent;
use crate::nll::ConstraintDescription;
use crate::session_diagnostics::{
CaptureArgLabel, CaptureReasonLabel, CaptureReasonNote, CaptureReasonSuggest, CaptureVarCause,
CaptureVarKind, CaptureVarPathUseCause, OnClosureNote,
Expand Down Expand Up @@ -623,52 +619,6 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
region.print(&mut printer).unwrap();
printer.into_buffer()
}

/// Add a note to region errors and borrow explanations when higher-ranked regions in predicates
/// implicitly introduce an "outlives `'static`" constraint.
fn add_placeholder_from_predicate_note<G: EmissionGuarantee>(
&self,
err: &mut Diag<'_, G>,
path: &[OutlivesConstraint<'tcx>],
) {
let predicate_span = path.iter().find_map(|constraint| {
let outlived = constraint.sub;
if let Some(origin) = self.regioncx.var_infos.get(outlived)
&& let RegionVariableOrigin::Nll(NllRegionVariableOrigin::Placeholder(_)) =
origin.origin
&& let ConstraintCategory::Predicate(span) = constraint.category
{
Some(span)
} else {
None
}
});

if let Some(span) = predicate_span {
err.span_note(span, "due to current limitations in the borrow checker, this implies a `'static` lifetime");
}
}

/// Add a label to region errors and borrow explanations when outlives constraints arise from
/// proving a type implements `Sized` or `Copy`.
fn add_sized_or_copy_bound_info<G: EmissionGuarantee>(
&self,
err: &mut Diag<'_, G>,
blamed_category: ConstraintCategory<'tcx>,
path: &[OutlivesConstraint<'tcx>],
) {
for sought_category in [ConstraintCategory::SizedBound, ConstraintCategory::CopyBound] {
if sought_category != blamed_category
&& let Some(sought_constraint) = path.iter().find(|c| c.category == sought_category)
{
let label = format!(
"requirement occurs due to {}",
sought_category.description().trim_end()
);
err.span_label(sought_constraint.span, label);
}
}
}
}

/// The span(s) associated to a use of a place.
Expand Down
Loading

0 comments on commit 977e6c8

Please sign in to comment.