Skip to content

Commit d895ceb

Browse files
authored
Rollup merge of rust-lang#69189 - matthewjasper:erase-the-world, r=nikomatsakis
Erase regions in writeback Regions in `TypeckTables` (except canonicalized user annotations) are now erased. Further, we no longer do lexical region solving on item bodies with `-Zborrowck=mir`. cc rust-lang#68261 r? @nikomatsakis
2 parents 39fb6ca + f8a08a9 commit d895ceb

32 files changed

+260
-234
lines changed

src/librustc/ty/context.rs

-9
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use crate::mir::{
2222
};
2323
use crate::traits;
2424
use crate::traits::{Clause, Clauses, Goal, GoalKind, Goals};
25-
use crate::ty::free_region_map::FreeRegionMap;
2625
use crate::ty::layout::{LayoutDetails, TargetDataLayout, VariantIdx};
2726
use crate::ty::query;
2827
use crate::ty::steal::Steal;
@@ -415,11 +414,6 @@ pub struct TypeckTables<'tcx> {
415414
/// this field will be set to `true`.
416415
pub tainted_by_errors: bool,
417416

418-
/// Stores the free-region relationships that were deduced from
419-
/// its where-clauses and parameter types. These are then
420-
/// read-again by borrowck.
421-
pub free_region_map: FreeRegionMap<'tcx>,
422-
423417
/// All the opaque types that are restricted to concrete types
424418
/// by this function.
425419
pub concrete_opaque_types: FxHashMap<DefId, ResolvedOpaqueTy<'tcx>>,
@@ -455,7 +449,6 @@ impl<'tcx> TypeckTables<'tcx> {
455449
coercion_casts: Default::default(),
456450
used_trait_imports: Lrc::new(Default::default()),
457451
tainted_by_errors: false,
458-
free_region_map: Default::default(),
459452
concrete_opaque_types: Default::default(),
460453
upvar_list: Default::default(),
461454
generator_interior_types: Default::default(),
@@ -718,7 +711,6 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for TypeckTables<'tcx> {
718711

719712
ref used_trait_imports,
720713
tainted_by_errors,
721-
ref free_region_map,
722714
ref concrete_opaque_types,
723715
ref upvar_list,
724716
ref generator_interior_types,
@@ -756,7 +748,6 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for TypeckTables<'tcx> {
756748
coercion_casts.hash_stable(hcx, hasher);
757749
used_trait_imports.hash_stable(hcx, hasher);
758750
tainted_by_errors.hash_stable(hcx, hasher);
759-
free_region_map.hash_stable(hcx, hasher);
760751
concrete_opaque_types.hash_stable(hcx, hasher);
761752
upvar_list.hash_stable(hcx, hasher);
762753
generator_interior_types.hash_stable(hcx, hasher);

src/librustc_infer/infer/error_reporting/mod.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ use super::lexical_region_resolve::RegionResolutionError;
4949
use super::region_constraints::GenericKind;
5050
use super::{InferCtxt, RegionVariableOrigin, SubregionOrigin, TypeTrace, ValuePairs};
5151

52-
use crate::infer::opaque_types;
53-
use crate::infer::{self, SuppressRegionErrors};
52+
use crate::infer::{self, opaque_types};
5453
use crate::traits::error_reporting::report_object_safety_error;
5554
use crate::traits::{
5655
IfExpressionCause, MatchExpressionArmCause, ObligationCause, ObligationCauseCode,
@@ -293,17 +292,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
293292
&self,
294293
region_scope_tree: &region::ScopeTree,
295294
errors: &Vec<RegionResolutionError<'tcx>>,
296-
suppress: SuppressRegionErrors,
297295
) {
298-
debug!(
299-
"report_region_errors(): {} errors to start, suppress = {:?}",
300-
errors.len(),
301-
suppress
302-
);
303-
304-
if suppress.suppressed() {
305-
return;
306-
}
296+
debug!("report_region_errors(): {} errors to start", errors.len());
307297

308298
// try to pre-process the errors, which will group some of them
309299
// together into a `ProcessedErrors` group:

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_infer/infer/lexical_region_resolve/mod.rs

+33-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::infer::region_constraints::RegionConstraintData;
77
use crate::infer::region_constraints::VarInfos;
88
use crate::infer::region_constraints::VerifyBound;
99
use crate::infer::RegionVariableOrigin;
10+
use crate::infer::RegionckMode;
1011
use crate::infer::SubregionOrigin;
1112
use rustc::middle::free_region::RegionRelations;
1213
use rustc::ty::fold::TypeFoldable;
@@ -33,12 +34,29 @@ pub fn resolve<'tcx>(
3334
region_rels: &RegionRelations<'_, 'tcx>,
3435
var_infos: VarInfos,
3536
data: RegionConstraintData<'tcx>,
37+
mode: RegionckMode,
3638
) -> (LexicalRegionResolutions<'tcx>, Vec<RegionResolutionError<'tcx>>) {
3739
debug!("RegionConstraintData: resolve_regions()");
3840
let mut errors = vec![];
3941
let mut resolver = LexicalResolver { region_rels, var_infos, data };
40-
let values = resolver.infer_variable_values(&mut errors);
41-
(values, errors)
42+
match mode {
43+
RegionckMode::Solve => {
44+
let values = resolver.infer_variable_values(&mut errors);
45+
(values, errors)
46+
}
47+
RegionckMode::Erase { suppress_errors: false } => {
48+
// Do real inference to get errors, then erase the results.
49+
let mut values = resolver.infer_variable_values(&mut errors);
50+
let re_erased = region_rels.tcx.lifetimes.re_erased;
51+
52+
values.values.iter_mut().for_each(|v| *v = VarValue::Value(re_erased));
53+
(values, errors)
54+
}
55+
RegionckMode::Erase { suppress_errors: true } => {
56+
// Skip region inference entirely.
57+
(resolver.erased_data(region_rels.tcx), Vec::new())
58+
}
59+
}
4260
}
4361

4462
/// Contains the result of lexical region resolution. Offers methods
@@ -163,6 +181,19 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
163181
}
164182
}
165183

184+
/// An erased version of the lexical region resolutions. Used when we're
185+
/// erasing regions and suppressing errors: in item bodies with
186+
/// `-Zborrowck=mir`.
187+
fn erased_data(&self, tcx: TyCtxt<'tcx>) -> LexicalRegionResolutions<'tcx> {
188+
LexicalRegionResolutions {
189+
error_region: tcx.lifetimes.re_static,
190+
values: IndexVec::from_elem_n(
191+
VarValue::Value(tcx.lifetimes.re_erased),
192+
self.num_vars(),
193+
),
194+
}
195+
}
196+
166197
fn dump_constraints(&self, free_regions: &RegionRelations<'_, 'tcx>) {
167198
debug!("----() Start constraint listing (context={:?}) ()----", free_regions.context);
168199
for (idx, (constraint, _)) in self.data.constraints.iter().enumerate() {

0 commit comments

Comments
 (0)