Skip to content

Commit cefd030

Browse files
committed
Don't use TypeckTables in NiceRegionError
Regions in TypeckTables will be erased, so are unusable for error reporting.
1 parent 5a9ccc9 commit cefd030

File tree

4 files changed

+51
-60
lines changed

4 files changed

+51
-60
lines changed

src/librustc_infer/infer/error_reporting/nice_region_error/different_lifetimes.rs

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
44
use crate::infer::error_reporting::nice_region_error::util::AnonymousParamInfo;
55
use crate::infer::error_reporting::nice_region_error::NiceRegionError;
6+
use crate::infer::lexical_region_resolve::RegionResolutionError;
7+
use crate::infer::SubregionOrigin;
68
use rustc::util::common::ErrorReported;
79

810
use rustc_errors::struct_span_err;
@@ -47,6 +49,15 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
4749
pub(super) fn try_report_anon_anon_conflict(&self) -> Option<ErrorReported> {
4850
let (span, sub, sup) = self.regions()?;
4951

52+
if let Some(RegionResolutionError::ConcreteFailure(
53+
SubregionOrigin::ReferenceOutlivesReferent(..),
54+
..,
55+
)) = self.error
56+
{
57+
// This error doesn't make much sense in this case.
58+
return None;
59+
}
60+
5061
// Determine whether the sub and sup consist of both anonymous (elided) regions.
5162
let anon_reg_sup = self.tcx().is_suitable_region(sup)?;
5263

src/librustc_infer/infer/error_reporting/nice_region_error/mod.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,28 @@ mod util;
1717

1818
impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
1919
pub fn try_report_nice_region_error(&self, error: &RegionResolutionError<'tcx>) -> bool {
20-
if let Some(tables) = self.in_progress_tables {
21-
let tables = tables.borrow();
22-
NiceRegionError::new(self, error.clone(), Some(&tables)).try_report().is_some()
23-
} else {
24-
NiceRegionError::new(self, error.clone(), None).try_report().is_some()
25-
}
20+
NiceRegionError::new(self, error.clone()).try_report().is_some()
2621
}
2722
}
2823

2924
pub struct NiceRegionError<'cx, 'tcx> {
3025
infcx: &'cx InferCtxt<'cx, 'tcx>,
3126
error: Option<RegionResolutionError<'tcx>>,
3227
regions: Option<(Span, ty::Region<'tcx>, ty::Region<'tcx>)>,
33-
tables: Option<&'cx ty::TypeckTables<'tcx>>,
3428
}
3529

3630
impl<'cx, 'tcx> NiceRegionError<'cx, 'tcx> {
37-
pub fn new(
38-
infcx: &'cx InferCtxt<'cx, 'tcx>,
39-
error: RegionResolutionError<'tcx>,
40-
tables: Option<&'cx ty::TypeckTables<'tcx>>,
41-
) -> Self {
42-
Self { infcx, error: Some(error), regions: None, tables }
31+
pub fn new(infcx: &'cx InferCtxt<'cx, 'tcx>, error: RegionResolutionError<'tcx>) -> Self {
32+
Self { infcx, error: Some(error), regions: None }
4333
}
4434

4535
pub fn new_from_span(
4636
infcx: &'cx InferCtxt<'cx, 'tcx>,
4737
span: Span,
4838
sub: ty::Region<'tcx>,
4939
sup: ty::Region<'tcx>,
50-
tables: Option<&'cx ty::TypeckTables<'tcx>>,
5140
) -> Self {
52-
Self { infcx, error: None, regions: Some((span, sub, sup)), tables }
41+
Self { infcx, error: None, regions: Some((span, sub, sup)) }
5342
}
5443

5544
fn tcx(&self) -> TyCtxt<'tcx> {

src/librustc_infer/infer/error_reporting/nice_region_error/util.rs

+35-43
Original file line numberDiff line numberDiff line change
@@ -51,52 +51,44 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
5151
};
5252

5353
let hir = &self.tcx().hir();
54-
if let Some(hir_id) = hir.as_local_hir_id(id) {
55-
if let Some(body_id) = hir.maybe_body_owned_by(hir_id) {
56-
let body = hir.body(body_id);
57-
let owner_id = hir.body_owner(body_id);
58-
let fn_decl = hir.fn_decl_by_hir_id(owner_id).unwrap();
59-
if let Some(tables) = self.tables {
60-
body.params
61-
.iter()
62-
.enumerate()
63-
.filter_map(|(index, param)| {
64-
// May return None; sometimes the tables are not yet populated.
65-
let ty_hir_id = fn_decl.inputs[index].hir_id;
66-
let param_ty_span = hir.span(ty_hir_id);
67-
let ty = tables.node_type_opt(param.hir_id)?;
68-
let mut found_anon_region = false;
69-
let new_param_ty = self.tcx().fold_regions(&ty, &mut false, |r, _| {
70-
if *r == *anon_region {
71-
found_anon_region = true;
72-
replace_region
73-
} else {
74-
r
75-
}
76-
});
77-
if found_anon_region {
78-
let is_first = index == 0;
79-
Some(AnonymousParamInfo {
80-
param,
81-
param_ty: new_param_ty,
82-
param_ty_span,
83-
bound_region,
84-
is_first,
85-
})
86-
} else {
87-
None
88-
}
89-
})
90-
.next()
54+
let hir_id = hir.as_local_hir_id(id)?;
55+
let body_id = hir.maybe_body_owned_by(hir_id)?;
56+
let body = hir.body(body_id);
57+
let owner_id = hir.body_owner(body_id);
58+
let fn_decl = hir.fn_decl_by_hir_id(owner_id).unwrap();
59+
let poly_fn_sig = self.tcx().fn_sig(id);
60+
let fn_sig = self.tcx().liberate_late_bound_regions(id, &poly_fn_sig);
61+
body.params
62+
.iter()
63+
.enumerate()
64+
.filter_map(|(index, param)| {
65+
// May return None; sometimes the tables are not yet populated.
66+
let ty = fn_sig.inputs()[index];
67+
let mut found_anon_region = false;
68+
let new_param_ty = self.tcx().fold_regions(&ty, &mut false, |r, _| {
69+
if *r == *anon_region {
70+
found_anon_region = true;
71+
replace_region
72+
} else {
73+
r
74+
}
75+
});
76+
if found_anon_region {
77+
let ty_hir_id = fn_decl.inputs[index].hir_id;
78+
let param_ty_span = hir.span(ty_hir_id);
79+
let is_first = index == 0;
80+
Some(AnonymousParamInfo {
81+
param,
82+
param_ty: new_param_ty,
83+
param_ty_span,
84+
bound_region,
85+
is_first,
86+
})
9187
} else {
9288
None
9389
}
94-
} else {
95-
None
96-
}
97-
} else {
98-
None
99-
}
90+
})
91+
.next()
10092
}
10193

10294
// Here, we check for the case where the anonymous region

src/librustc_mir/borrow_check/diagnostics/region_errors.rs

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

0 commit comments

Comments
 (0)