1
Fork 0

Add ExtraConstraintInfo

This commit is contained in:
Jack Huey 2022-09-16 17:33:12 -04:00
parent 9929c0ac76
commit f1767dbb42
3 changed files with 31 additions and 17 deletions

View file

@ -15,7 +15,7 @@ use rustc_middle::ty::{self, RegionVid, TyCtxt};
use rustc_span::symbol::{kw, Symbol}; use rustc_span::symbol::{kw, Symbol};
use rustc_span::{sym, DesugaringKind, Span}; use rustc_span::{sym, DesugaringKind, Span};
use crate::region_infer::BlameConstraint; use crate::region_infer::{BlameConstraint, ExtraConstraintInfo};
use crate::{ use crate::{
borrow_set::BorrowData, nll::ConstraintDescription, region_infer::Cause, MirBorrowckCtxt, borrow_set::BorrowData, nll::ConstraintDescription, region_infer::Cause, MirBorrowckCtxt,
WriteKind, WriteKind,
@ -38,6 +38,7 @@ pub(crate) enum BorrowExplanation<'tcx> {
span: Span, span: Span,
region_name: RegionName, region_name: RegionName,
opt_place_desc: Option<String>, opt_place_desc: Option<String>,
extra_info: Vec<ExtraConstraintInfo>,
}, },
Unexplained, Unexplained,
} }
@ -243,6 +244,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
ref region_name, ref region_name,
ref opt_place_desc, ref opt_place_desc,
from_closure: _, from_closure: _,
ref extra_info,
} => { } => {
region_name.highlight_region_name(err); region_name.highlight_region_name(err);
@ -268,6 +270,11 @@ impl<'tcx> BorrowExplanation<'tcx> {
); );
}; };
for extra in extra_info {
match extra {
_ => {}
}
}
self.add_lifetime_bound_suggestion_to_diagnostic(err, &category, span, region_name); self.add_lifetime_bound_suggestion_to_diagnostic(err, &category, span, region_name);
} }
_ => {} _ => {}
@ -309,8 +316,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
&self, &self,
borrow_region: RegionVid, borrow_region: RegionVid,
outlived_region: RegionVid, outlived_region: RegionVid,
) -> (ConstraintCategory<'tcx>, bool, Span, Option<RegionName>) { ) -> (ConstraintCategory<'tcx>, bool, Span, Option<RegionName>, Vec<ExtraConstraintInfo>) {
let blame_constraint = self.regioncx.best_blame_constraint( let (blame_constraint, extra_info) = self.regioncx.best_blame_constraint(
borrow_region, borrow_region,
NllRegionVariableOrigin::FreeRegion, NllRegionVariableOrigin::FreeRegion,
|r| self.regioncx.provides_universal_region(r, borrow_region, outlived_region), |r| self.regioncx.provides_universal_region(r, borrow_region, outlived_region),
@ -319,7 +326,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
let outlived_fr_name = self.give_region_a_name(outlived_region); let outlived_fr_name = self.give_region_a_name(outlived_region);
(category, from_closure, cause.span, outlived_fr_name) (category, from_closure, cause.span, outlived_fr_name, extra_info)
} }
/// Returns structured explanation for *why* the borrow contains the /// Returns structured explanation for *why* the borrow contains the
@ -391,7 +398,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
None => { None => {
if let Some(region) = self.to_error_region_vid(borrow_region_vid) { if let Some(region) = self.to_error_region_vid(borrow_region_vid) {
let (category, from_closure, span, region_name) = let (category, from_closure, span, region_name, extra_info) =
self.free_region_constraint_info(borrow_region_vid, region); self.free_region_constraint_info(borrow_region_vid, region);
if let Some(region_name) = region_name { if let Some(region_name) = region_name {
let opt_place_desc = self.describe_place(borrow.borrowed_place.as_ref()); let opt_place_desc = self.describe_place(borrow.borrowed_place.as_ref());
@ -401,6 +408,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
span, span,
region_name, region_name,
opt_place_desc, opt_place_desc,
extra_info,
} }
} else { } else {
debug!("Could not generate a region name"); debug!("Could not generate a region name");

View file

@ -354,10 +354,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
) { ) {
debug!("report_region_error(fr={:?}, outlived_fr={:?})", fr, outlived_fr); debug!("report_region_error(fr={:?}, outlived_fr={:?})", fr, outlived_fr);
let BlameConstraint { category, cause, variance_info, .. } = let BlameConstraint { category, cause, variance_info, .. } = self
self.regioncx.best_blame_constraint(fr, fr_origin, |r| { .regioncx
.best_blame_constraint(fr, fr_origin, |r| {
self.regioncx.provides_universal_region(r, fr, outlived_fr) self.regioncx.provides_universal_region(r, fr, outlived_fr)
}); })
.0;
debug!("report_region_error: category={:?} {:?} {:?}", category, cause, variance_info); debug!("report_region_error: category={:?} {:?} {:?}", category, cause, variance_info);

View file

@ -245,6 +245,9 @@ enum Trace<'tcx> {
NotVisited, NotVisited,
} }
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ExtraConstraintInfo {}
impl<'tcx> RegionInferenceContext<'tcx> { impl<'tcx> RegionInferenceContext<'tcx> {
/// Creates a new region inference context with a total of /// Creates a new region inference context with a total of
/// `num_region_variables` valid inference variables; the first N /// `num_region_variables` valid inference variables; the first N
@ -1818,10 +1821,9 @@ impl<'tcx> RegionInferenceContext<'tcx> {
fr1_origin: NllRegionVariableOrigin, fr1_origin: NllRegionVariableOrigin,
fr2: RegionVid, fr2: RegionVid,
) -> (ConstraintCategory<'tcx>, ObligationCause<'tcx>) { ) -> (ConstraintCategory<'tcx>, ObligationCause<'tcx>) {
let BlameConstraint { category, cause, .. } = let BlameConstraint { category, cause, .. } = self
self.best_blame_constraint(fr1, fr1_origin, |r| { .best_blame_constraint(fr1, fr1_origin, |r| self.provides_universal_region(r, fr1, fr2))
self.provides_universal_region(r, fr1, fr2) .0;
});
(category, cause) (category, cause)
} }
@ -2010,7 +2012,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
from_region: RegionVid, from_region: RegionVid,
from_region_origin: NllRegionVariableOrigin, from_region_origin: NllRegionVariableOrigin,
target_test: impl Fn(RegionVid) -> bool, target_test: impl Fn(RegionVid) -> bool,
) -> BlameConstraint<'tcx> { ) -> (BlameConstraint<'tcx>, Vec<ExtraConstraintInfo>) {
// Find all paths // Find all paths
let (path, target_region) = let (path, target_region) =
self.find_constraint_paths_between_regions(from_region, target_test).unwrap(); self.find_constraint_paths_between_regions(from_region, target_test).unwrap();
@ -2026,6 +2028,8 @@ impl<'tcx> RegionInferenceContext<'tcx> {
.collect::<Vec<_>>() .collect::<Vec<_>>()
); );
let extra_info = vec![];
// We try to avoid reporting a `ConstraintCategory::Predicate` as our best constraint. // We try to avoid reporting a `ConstraintCategory::Predicate` as our best constraint.
// Instead, we use it to produce an improved `ObligationCauseCode`. // Instead, we use it to produce an improved `ObligationCauseCode`.
// FIXME - determine what we should do if we encounter multiple `ConstraintCategory::Predicate` // FIXME - determine what we should do if we encounter multiple `ConstraintCategory::Predicate`
@ -2175,7 +2179,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
let best_choice = let best_choice =
if blame_source { range.rev().find(find_region) } else { range.find(find_region) }; if blame_source { range.rev().find(find_region) } else { range.find(find_region) };
debug!(?best_choice, ?blame_source); debug!(?best_choice, ?blame_source, ?extra_info);
if let Some(i) = best_choice { if let Some(i) = best_choice {
if let Some(next) = categorized_path.get(i + 1) { if let Some(next) = categorized_path.get(i + 1) {
@ -2184,7 +2188,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
{ {
// The return expression is being influenced by the return type being // The return expression is being influenced by the return type being
// impl Trait, point at the return type and not the return expr. // impl Trait, point at the return type and not the return expr.
return next.clone(); return (next.clone(), extra_info);
} }
} }
@ -2204,7 +2208,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
} }
} }
return categorized_path[i].clone(); return (categorized_path[i].clone(), extra_info);
} }
// If that search fails, that is.. unusual. Maybe everything // If that search fails, that is.. unusual. Maybe everything
@ -2214,7 +2218,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
categorized_path.sort_by(|p0, p1| p0.category.cmp(&p1.category)); categorized_path.sort_by(|p0, p1| p0.category.cmp(&p1.category));
debug!("sorted_path={:#?}", categorized_path); debug!("sorted_path={:#?}", categorized_path);
categorized_path.remove(0) (categorized_path.remove(0), extra_info)
} }
pub(crate) fn universe_info(&self, universe: ty::UniverseIndex) -> UniverseInfo<'tcx> { pub(crate) fn universe_info(&self, universe: ty::UniverseIndex) -> UniverseInfo<'tcx> {