Don't use TypeckTables in NiceRegionError

Regions in TypeckTables will be erased, so are unusable for error
reporting.
This commit is contained in:
Matthew Jasper 2020-02-15 12:07:20 +00:00
parent 5a9ccc9ce7
commit cefd0305b1
4 changed files with 51 additions and 60 deletions

View File

@ -3,6 +3,8 @@
use crate::infer::error_reporting::nice_region_error::util::AnonymousParamInfo; use crate::infer::error_reporting::nice_region_error::util::AnonymousParamInfo;
use crate::infer::error_reporting::nice_region_error::NiceRegionError; use crate::infer::error_reporting::nice_region_error::NiceRegionError;
use crate::infer::lexical_region_resolve::RegionResolutionError;
use crate::infer::SubregionOrigin;
use rustc::util::common::ErrorReported; use rustc::util::common::ErrorReported;
use rustc_errors::struct_span_err; use rustc_errors::struct_span_err;
@ -47,6 +49,15 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
pub(super) fn try_report_anon_anon_conflict(&self) -> Option<ErrorReported> { pub(super) fn try_report_anon_anon_conflict(&self) -> Option<ErrorReported> {
let (span, sub, sup) = self.regions()?; let (span, sub, sup) = self.regions()?;
if let Some(RegionResolutionError::ConcreteFailure(
SubregionOrigin::ReferenceOutlivesReferent(..),
..,
)) = self.error
{
// This error doesn't make much sense in this case.
return None;
}
// Determine whether the sub and sup consist of both anonymous (elided) regions. // Determine whether the sub and sup consist of both anonymous (elided) regions.
let anon_reg_sup = self.tcx().is_suitable_region(sup)?; let anon_reg_sup = self.tcx().is_suitable_region(sup)?;

View File

@ -17,12 +17,7 @@ mod util;
impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> { impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
pub fn try_report_nice_region_error(&self, error: &RegionResolutionError<'tcx>) -> bool { pub fn try_report_nice_region_error(&self, error: &RegionResolutionError<'tcx>) -> bool {
if let Some(tables) = self.in_progress_tables { NiceRegionError::new(self, error.clone()).try_report().is_some()
let tables = tables.borrow();
NiceRegionError::new(self, error.clone(), Some(&tables)).try_report().is_some()
} else {
NiceRegionError::new(self, error.clone(), None).try_report().is_some()
}
} }
} }
@ -30,16 +25,11 @@ pub struct NiceRegionError<'cx, 'tcx> {
infcx: &'cx InferCtxt<'cx, 'tcx>, infcx: &'cx InferCtxt<'cx, 'tcx>,
error: Option<RegionResolutionError<'tcx>>, error: Option<RegionResolutionError<'tcx>>,
regions: Option<(Span, ty::Region<'tcx>, ty::Region<'tcx>)>, regions: Option<(Span, ty::Region<'tcx>, ty::Region<'tcx>)>,
tables: Option<&'cx ty::TypeckTables<'tcx>>,
} }
impl<'cx, 'tcx> NiceRegionError<'cx, 'tcx> { impl<'cx, 'tcx> NiceRegionError<'cx, 'tcx> {
pub fn new( pub fn new(infcx: &'cx InferCtxt<'cx, 'tcx>, error: RegionResolutionError<'tcx>) -> Self {
infcx: &'cx InferCtxt<'cx, 'tcx>, Self { infcx, error: Some(error), regions: None }
error: RegionResolutionError<'tcx>,
tables: Option<&'cx ty::TypeckTables<'tcx>>,
) -> Self {
Self { infcx, error: Some(error), regions: None, tables }
} }
pub fn new_from_span( pub fn new_from_span(
@ -47,9 +37,8 @@ impl<'cx, 'tcx> NiceRegionError<'cx, 'tcx> {
span: Span, span: Span,
sub: ty::Region<'tcx>, sub: ty::Region<'tcx>,
sup: ty::Region<'tcx>, sup: ty::Region<'tcx>,
tables: Option<&'cx ty::TypeckTables<'tcx>>,
) -> Self { ) -> Self {
Self { infcx, error: None, regions: Some((span, sub, sup)), tables } Self { infcx, error: None, regions: Some((span, sub, sup)) }
} }
fn tcx(&self) -> TyCtxt<'tcx> { fn tcx(&self) -> TyCtxt<'tcx> {

View File

@ -51,52 +51,44 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
}; };
let hir = &self.tcx().hir(); let hir = &self.tcx().hir();
if let Some(hir_id) = hir.as_local_hir_id(id) { let hir_id = hir.as_local_hir_id(id)?;
if let Some(body_id) = hir.maybe_body_owned_by(hir_id) { let body_id = hir.maybe_body_owned_by(hir_id)?;
let body = hir.body(body_id); let body = hir.body(body_id);
let owner_id = hir.body_owner(body_id); let owner_id = hir.body_owner(body_id);
let fn_decl = hir.fn_decl_by_hir_id(owner_id).unwrap(); let fn_decl = hir.fn_decl_by_hir_id(owner_id).unwrap();
if let Some(tables) = self.tables { let poly_fn_sig = self.tcx().fn_sig(id);
body.params let fn_sig = self.tcx().liberate_late_bound_regions(id, &poly_fn_sig);
.iter() body.params
.enumerate() .iter()
.filter_map(|(index, param)| { .enumerate()
// May return None; sometimes the tables are not yet populated. .filter_map(|(index, param)| {
let ty_hir_id = fn_decl.inputs[index].hir_id; // May return None; sometimes the tables are not yet populated.
let param_ty_span = hir.span(ty_hir_id); let ty = fn_sig.inputs()[index];
let ty = tables.node_type_opt(param.hir_id)?; let mut found_anon_region = false;
let mut found_anon_region = false; let new_param_ty = self.tcx().fold_regions(&ty, &mut false, |r, _| {
let new_param_ty = self.tcx().fold_regions(&ty, &mut false, |r, _| { if *r == *anon_region {
if *r == *anon_region { found_anon_region = true;
found_anon_region = true; replace_region
replace_region } else {
} else { r
r }
} });
}); if found_anon_region {
if found_anon_region { let ty_hir_id = fn_decl.inputs[index].hir_id;
let is_first = index == 0; let param_ty_span = hir.span(ty_hir_id);
Some(AnonymousParamInfo { let is_first = index == 0;
param, Some(AnonymousParamInfo {
param_ty: new_param_ty, param,
param_ty_span, param_ty: new_param_ty,
bound_region, param_ty_span,
is_first, bound_region,
}) is_first,
} else { })
None
}
})
.next()
} else { } else {
None None
} }
} else { })
None .next()
}
} else {
None
}
} }
// Here, we check for the case where the anonymous region // Here, we check for the case where the anonymous region

View File

@ -284,8 +284,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
debug!("report_region_error: category={:?} {:?}", category, span); debug!("report_region_error: category={:?} {:?}", category, span);
// Check if we can use one of the "nice region errors". // Check if we can use one of the "nice region errors".
if let (Some(f), Some(o)) = (self.to_error_region(fr), self.to_error_region(outlived_fr)) { if let (Some(f), Some(o)) = (self.to_error_region(fr), self.to_error_region(outlived_fr)) {
let tables = self.infcx.tcx.typeck_tables_of(self.mir_def_id); let nice = NiceRegionError::new_from_span(self.infcx, span, o, f);
let nice = NiceRegionError::new_from_span(self.infcx, span, o, f, Some(tables));
if let Some(diag) = nice.try_report_from_nll() { if let Some(diag) = nice.try_report_from_nll() {
diag.buffer(&mut self.errors_buffer); diag.buffer(&mut self.errors_buffer);
return; return;