Skip to content

Commit 68d70fc

Browse files
committed
only use FnCtxt for regionck inside of bodies
1 parent edd45f9 commit 68d70fc

File tree

7 files changed

+54
-54
lines changed

7 files changed

+54
-54
lines changed

compiler/rustc_infer/src/infer/outlives/obligations.rs

+13
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ use crate::traits::{ObligationCause, ObligationCauseCode};
6969
use rustc_middle::ty::subst::GenericArgKind;
7070
use rustc_middle::ty::{self, Region, Ty, TyCtxt, TypeFoldable};
7171

72+
use crate::infer::outlives::env::OutlivesEnvironment;
7273
use rustc_data_structures::fx::FxHashMap;
7374
use rustc_data_structures::undo_log::UndoLogs;
7475
use rustc_hir as hir;
@@ -177,6 +178,18 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
177178
}
178179
}
179180
}
181+
182+
pub fn check_region_obligations_and_report_errors(
183+
&self,
184+
outlives_env: &OutlivesEnvironment<'tcx>,
185+
) {
186+
self.process_registered_region_obligations(
187+
outlives_env.region_bound_pairs_map(),
188+
outlives_env.param_env,
189+
);
190+
191+
self.resolve_regions_and_report_errors(outlives_env)
192+
}
180193
}
181194

182195
/// The `TypeOutlives` struct has the job of "lowering" a `T: 'a`

compiler/rustc_typeck/src/check/check.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_hir::def_id::{DefId, LocalDefId};
1313
use rustc_hir::intravisit::Visitor;
1414
use rustc_hir::lang_items::LangItem;
1515
use rustc_hir::{ItemKind, Node, PathSegment};
16+
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
1617
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
1718
use rustc_infer::infer::{RegionVariableOrigin, TyCtxtInferExt};
1819
use rustc_infer::traits::Obligation;
@@ -736,10 +737,9 @@ fn check_opaque_meets_bounds<'tcx>(
736737
hir::OpaqueTyOrigin::FnReturn(..) | hir::OpaqueTyOrigin::AsyncFn(..) => {}
737738
// Can have different predicates to their defining use
738739
hir::OpaqueTyOrigin::TyAlias => {
739-
// Finally, resolve all regions. This catches wily misuses of
740-
// lifetime parameters.
741-
let fcx = FnCtxt::new(&inh, param_env, hir_id);
742-
fcx.regionck_item(hir_id, span, FxHashSet::default());
740+
let mut outlives_environment = OutlivesEnvironment::new(param_env);
741+
outlives_environment.save_implied_bounds(hir_id);
742+
infcx.check_region_obligations_and_report_errors(&outlives_environment);
743743
}
744744
}
745745

compiler/rustc_typeck/src/check/compare_method.rs

+22-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use crate::check::regionck::OutlivesEnvironmentExt;
12
use crate::errors::LifetimesOrBoundsMismatchOnTrait;
23
use rustc_data_structures::stable_set::FxHashSet;
34
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticId, ErrorGuaranteed};
45
use rustc_hir as hir;
56
use rustc_hir::def::{DefKind, Res};
67
use rustc_hir::intravisit;
78
use rustc_hir::{GenericParamKind, ImplItemKind, TraitItemKind};
9+
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
810
use rustc_infer::infer::{self, InferOk, TyCtxtInferExt};
911
use rustc_infer::traits::util;
1012
use rustc_middle::ty::error::{ExpectedFound, TypeError};
@@ -78,10 +80,11 @@ fn compare_predicate_entailment<'tcx>(
7880
let trait_to_impl_substs = impl_trait_ref.substs;
7981

8082
// This node-id should be used for the `body_id` field on each
81-
// `ObligationCause` (and the `FnCtxt`). This is what
82-
// `regionck_item` expects.
83+
// `ObligationCause` (and the `FnCtxt`).
84+
//
85+
// FIXME(@lcnr): remove that after removing `cause.body_id` from
86+
// obligations.
8387
let impl_m_hir_id = tcx.hir().local_def_id_to_hir_id(impl_m.def_id.expect_local());
84-
8588
// We sometimes modify the span further down.
8689
let mut cause = ObligationCause::new(
8790
impl_m_span,
@@ -398,8 +401,10 @@ fn compare_predicate_entailment<'tcx>(
398401

399402
// Finally, resolve all regions. This catches wily misuses of
400403
// lifetime parameters.
401-
let fcx = FnCtxt::new(&inh, param_env, impl_m_hir_id);
402-
fcx.regionck_item(impl_m_hir_id, impl_m_span, wf_tys);
404+
let mut outlives_environment = OutlivesEnvironment::new(param_env);
405+
outlives_environment.add_implied_bounds(infcx, wf_tys, impl_m_hir_id);
406+
outlives_environment.save_implied_bounds(impl_m_hir_id);
407+
infcx.check_region_obligations_and_report_errors(&outlives_environment);
403408

404409
Ok(())
405410
})
@@ -1154,8 +1159,9 @@ pub(crate) fn compare_const_impl<'tcx>(
11541159
return;
11551160
}
11561161

1157-
let fcx = FnCtxt::new(&inh, param_env, impl_c_hir_id);
1158-
fcx.regionck_item(impl_c_hir_id, impl_c_span, FxHashSet::default());
1162+
let mut outlives_environment = OutlivesEnvironment::new(param_env);
1163+
outlives_environment.save_implied_bounds(impl_c_hir_id);
1164+
infcx.resolve_regions_and_report_errors(&outlives_environment);
11591165
});
11601166
}
11611167

@@ -1273,8 +1279,9 @@ fn compare_type_predicate_entailment<'tcx>(
12731279

12741280
// Finally, resolve all regions. This catches wily misuses of
12751281
// lifetime parameters.
1276-
let fcx = FnCtxt::new(&inh, param_env, impl_ty_hir_id);
1277-
fcx.regionck_item(impl_ty_hir_id, impl_ty_span, FxHashSet::default());
1282+
let mut outlives_environment = OutlivesEnvironment::new(param_env);
1283+
outlives_environment.save_implied_bounds(impl_ty_hir_id);
1284+
infcx.check_region_obligations_and_report_errors(&outlives_environment);
12781285

12791286
Ok(())
12801287
})
@@ -1498,12 +1505,17 @@ pub fn check_type_bounds<'tcx>(
14981505

14991506
// Finally, resolve all regions. This catches wily misuses of
15001507
// lifetime parameters.
1508+
//
1509+
// FIXME: Remove that `FnCtxt`.
15011510
let fcx = FnCtxt::new(&inh, param_env, impl_ty_hir_id);
15021511
let implied_bounds = match impl_ty.container {
15031512
ty::TraitContainer(_) => FxHashSet::default(),
15041513
ty::ImplContainer(def_id) => fcx.impl_implied_bounds(def_id, impl_ty_span),
15051514
};
1506-
fcx.regionck_item(impl_ty_hir_id, impl_ty_span, implied_bounds);
1515+
let mut outlives_environment = OutlivesEnvironment::new(param_env);
1516+
outlives_environment.add_implied_bounds(infcx, implied_bounds, impl_ty_hir_id);
1517+
outlives_environment.save_implied_bounds(impl_ty_hir_id);
1518+
infcx.check_region_obligations_and_report_errors(&outlives_environment);
15071519

15081520
Ok(())
15091521
})

compiler/rustc_typeck/src/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ fn typeck_with_fallback<'tcx>(
491491
fcx.check_asms();
492492

493493
if fn_sig.is_some() {
494-
fcx.regionck_fn(id, body, span, wf_tys);
494+
fcx.regionck_fn(id, body, wf_tys);
495495
} else {
496496
fcx.regionck_body(body);
497497
}

compiler/rustc_typeck/src/check/regionck.rs

+6-31
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ pub(crate) trait OutlivesEnvironmentExt<'tcx> {
108108
infcx: &InferCtxt<'_, 'tcx>,
109109
fn_sig_tys: FxHashSet<Ty<'tcx>>,
110110
body_id: hir::HirId,
111-
span: Span,
112111
);
113112
}
114113

@@ -135,11 +134,10 @@ impl<'tcx> OutlivesEnvironmentExt<'tcx> for OutlivesEnvironment<'tcx> {
135134
infcx: &InferCtxt<'a, 'tcx>,
136135
fn_sig_tys: FxHashSet<Ty<'tcx>>,
137136
body_id: hir::HirId,
138-
span: Span,
139137
) {
140138
for ty in fn_sig_tys {
141139
let ty = infcx.resolve_vars_if_possible(ty);
142-
let implied_bounds = infcx.implied_outlives_bounds(self.param_env, body_id, ty, span);
140+
let implied_bounds = infcx.implied_outlives_bounds(self.param_env, body_id, ty);
143141
self.add_outlives_bounds(Some(infcx), implied_bounds)
144142
}
145143
}
@@ -166,18 +164,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
166164
rcx.fcx.skip_region_resolution();
167165
}
168166

169-
/// Region checking during the WF phase for items. `wf_tys` are the
170-
/// types from which we should derive implied bounds, if any.
171-
#[instrument(level = "debug", skip(self))]
172-
pub fn regionck_item(&self, item_id: hir::HirId, span: Span, wf_tys: FxHashSet<Ty<'tcx>>) {
173-
let body_owner = self.tcx.hir().local_def_id(item_id);
174-
let mut rcx = RegionCtxt::new(self, body_owner, self.param_env);
175-
rcx.outlives_environment.add_implied_bounds(self, wf_tys, item_id, span);
176-
rcx.outlives_environment.save_implied_bounds(rcx.body_id());
177-
rcx.visit_region_obligations(item_id);
178-
rcx.resolve_regions_and_report_errors();
179-
}
180-
181167
/// Region check a function body. Not invoked on closures, but
182168
/// only on the "root" fn item (in which closures may be
183169
/// embedded). Walks the function body and adds various add'l
@@ -190,19 +176,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
190176
&self,
191177
fn_id: hir::HirId,
192178
body: &'tcx hir::Body<'tcx>,
193-
span: Span,
194179
wf_tys: FxHashSet<Ty<'tcx>>,
195180
) {
196181
debug!("regionck_fn(id={})", fn_id);
197182
let body_owner = self.tcx.hir().body_owner_def_id(body.id());
198183
let mut rcx = RegionCtxt::new(self, body_owner, self.param_env);
199184
// We need to add the implied bounds from the function signature
200-
rcx.outlives_environment.add_implied_bounds(self, wf_tys, fn_id, span);
185+
rcx.outlives_environment.add_implied_bounds(self, wf_tys, fn_id);
201186
rcx.outlives_environment.save_implied_bounds(fn_id);
202187

203188
if !self.errors_reported_since_creation() {
204189
// regionck assumes typeck succeeded
205-
rcx.visit_fn_body(fn_id, body, self.tcx.hir().span(fn_id));
190+
rcx.visit_fn_body(fn_id, body);
206191
}
207192

208193
// Checked by NLL
@@ -294,7 +279,6 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
294279
&mut self,
295280
id: hir::HirId, // the id of the fn itself
296281
body: &'tcx hir::Body<'tcx>,
297-
span: Span,
298282
) {
299283
// When we enter a function, we can derive
300284
debug!("visit_fn_body(id={:?})", id);
@@ -313,7 +297,7 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
313297
let fn_sig_tys: FxHashSet<_> =
314298
fn_sig.inputs().iter().cloned().chain(Some(fn_sig.output())).collect();
315299

316-
self.outlives_environment.add_implied_bounds(self.fcx, fn_sig_tys, body_id.hir_id, span);
300+
self.outlives_environment.add_implied_bounds(self.fcx, fn_sig_tys, body_id.hir_id);
317301
self.outlives_environment.save_implied_bounds(body_id.hir_id);
318302
self.link_fn_params(body.params);
319303
self.visit_body(body);
@@ -349,15 +333,6 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
349333
self.select_all_obligations_or_error();
350334
}
351335

352-
fn resolve_regions_and_report_errors(&self) {
353-
self.infcx.process_registered_region_obligations(
354-
self.outlives_environment.region_bound_pairs_map(),
355-
self.param_env,
356-
);
357-
358-
self.fcx.resolve_regions_and_report_errors(&self.outlives_environment);
359-
}
360-
361336
fn constrain_bindings_in_pat(&mut self, pat: &hir::Pat<'_>) {
362337
debug!("regionck::visit_pat(pat={:?})", pat);
363338
pat.each_binding(|_, hir_id, span, _| {
@@ -382,7 +357,7 @@ impl<'a, 'tcx> Visitor<'tcx> for RegionCtxt<'a, 'tcx> {
382357
fk: intravisit::FnKind<'tcx>,
383358
_: &'tcx hir::FnDecl<'tcx>,
384359
body_id: hir::BodyId,
385-
span: Span,
360+
_span: Span,
386361
hir_id: hir::HirId,
387362
) {
388363
assert!(
@@ -396,7 +371,7 @@ impl<'a, 'tcx> Visitor<'tcx> for RegionCtxt<'a, 'tcx> {
396371
let env_snapshot = self.outlives_environment.push_snapshot_pre_typeck_child();
397372

398373
let body = self.tcx.hir().body(body_id);
399-
self.visit_fn_body(hir_id, body, span);
374+
self.visit_fn_body(hir_id, body);
400375

401376
// Restore state from previous function.
402377
self.outlives_environment.pop_snapshot_post_typeck_child(env_snapshot);

compiler/rustc_typeck/src/check/wfcheck.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ impl<'tcx> CheckWfFcxBuilder<'tcx> {
6262
}
6363
let wf_tys = f(&fcx);
6464
fcx.select_all_obligations_or_error();
65-
fcx.regionck_item(id, span, wf_tys);
65+
66+
let mut outlives_environment = OutlivesEnvironment::new(param_env);
67+
outlives_environment.add_implied_bounds(&fcx.infcx, wf_tys, id);
68+
outlives_environment.save_implied_bounds(id);
69+
fcx.infcx.check_region_obligations_and_report_errors(&outlives_environment);
6670
});
6771
}
6872
}
@@ -655,7 +659,7 @@ fn resolve_regions_with_wf_tys<'tcx>(
655659
// call individually.
656660
tcx.infer_ctxt().enter(|infcx| {
657661
let mut outlives_environment = OutlivesEnvironment::new(param_env);
658-
outlives_environment.add_implied_bounds(&infcx, wf_tys.clone(), id, DUMMY_SP);
662+
outlives_environment.add_implied_bounds(&infcx, wf_tys.clone(), id);
659663
outlives_environment.save_implied_bounds(id);
660664
let region_bound_pairs = outlives_environment.region_bound_pairs_map().get(&id).unwrap();
661665

compiler/rustc_typeck/src/outlives/outlives_bounds.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use rustc_hir as hir;
22
use rustc_middle::ty::{self, Ty};
3-
use rustc_span::source_map::Span;
43
use rustc_trait_selection::infer::InferCtxt;
54
use rustc_trait_selection::traits::query::type_op::{self, TypeOp, TypeOpOutput};
65
use rustc_trait_selection::traits::query::NoSolution;
@@ -14,7 +13,6 @@ pub trait InferCtxtExt<'tcx> {
1413
param_env: ty::ParamEnv<'tcx>,
1514
body_id: hir::HirId,
1615
ty: Ty<'tcx>,
17-
span: Span,
1816
) -> Vec<OutlivesBound<'tcx>>;
1917
}
2018

@@ -38,16 +36,14 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'cx, 'tcx> {
3836
/// Note that this may cause outlives obligations to be injected
3937
/// into the inference context with this body-id.
4038
/// - `ty`, the type that we are supposed to assume is WF.
41-
/// - `span`, a span to use when normalizing, hopefully not important,
42-
/// might be useful if a `bug!` occurs.
43-
#[instrument(level = "debug", skip(self, param_env, body_id, span))]
39+
#[instrument(level = "debug", skip(self, param_env, body_id))]
4440
fn implied_outlives_bounds(
4541
&self,
4642
param_env: ty::ParamEnv<'tcx>,
4743
body_id: hir::HirId,
4844
ty: Ty<'tcx>,
49-
span: Span,
5045
) -> Vec<OutlivesBound<'tcx>> {
46+
let span = self.tcx.hir().span(body_id);
5147
let result = param_env
5248
.and(type_op::implied_outlives_bounds::ImpliedOutlivesBounds { ty })
5349
.fully_perform(self);

0 commit comments

Comments
 (0)