Skip to content

Commit 379c5ac

Browse files
committed
Prefer a local DefiningAnchor over the InferCtxt one
1 parent ff7b19b commit 379c5ac

File tree

13 files changed

+61
-20
lines changed

13 files changed

+61
-20
lines changed

compiler/rustc_borrowck/src/consumers.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ pub fn get_body_with_borrowck_facts(
3636
let infcx = tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bind(def.did)).build();
3737
let input_body: &Body<'_> = &input_body.borrow();
3838
let promoted: &IndexVec<_, _> = &promoted.borrow();
39-
*super::do_mir_borrowck(&infcx, input_body, promoted, true).1.unwrap()
39+
*super::do_mir_borrowck(&infcx, input_body, promoted, true, DefiningAnchor::Bind(def.did))
40+
.1
41+
.unwrap()
4042
}

compiler/rustc_borrowck/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,14 @@ fn mir_borrowck(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> &Bor
156156
tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bind(hir_owner.def_id)).build();
157157
let input_body: &Body<'_> = &input_body.borrow();
158158
let promoted: &IndexVec<_, _> = &promoted.borrow();
159-
let opt_closure_req = do_mir_borrowck(&infcx, input_body, promoted, false).0;
159+
let opt_closure_req = do_mir_borrowck(
160+
&infcx,
161+
input_body,
162+
promoted,
163+
false,
164+
DefiningAnchor::Bind(hir_owner.def_id),
165+
)
166+
.0;
160167
debug!("mir_borrowck done");
161168

162169
tcx.arena.alloc(opt_closure_req)
@@ -173,6 +180,7 @@ fn do_mir_borrowck<'tcx>(
173180
input_body: &Body<'tcx>,
174181
input_promoted: &IndexVec<Promoted, Body<'tcx>>,
175182
return_body_with_facts: bool,
183+
defining_use_anchor: DefiningAnchor,
176184
) -> (BorrowCheckResult<'tcx>, Option<Box<BodyWithBorrowckFacts<'tcx>>>) {
177185
let def = input_body.source.with_opt_param().as_local().unwrap();
178186
debug!(?def);
@@ -275,6 +283,7 @@ fn do_mir_borrowck<'tcx>(
275283
&borrow_set,
276284
&upvars,
277285
use_polonius,
286+
defining_use_anchor,
278287
);
279288

280289
// Dump MIR results into a file, if that is enabled. This let us

compiler/rustc_borrowck/src/nll.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use rustc_data_structures::vec_map::VecMap;
66
use rustc_hir::def_id::LocalDefId;
77
use rustc_index::vec::IndexVec;
8+
use rustc_infer::infer::DefiningAnchor;
89
use rustc_middle::mir::{create_dump_file, dump_enabled, dump_mir, PassWhere};
910
use rustc_middle::mir::{
1011
BasicBlock, Body, ClosureOutlivesSubject, ClosureRegionRequirements, LocalKind, Location,
@@ -167,6 +168,7 @@ pub(crate) fn compute_regions<'cx, 'tcx>(
167168
borrow_set: &BorrowSet<'tcx>,
168169
upvars: &[Upvar<'tcx>],
169170
use_polonius: bool,
171+
defining_use_anchor: DefiningAnchor,
170172
) -> NllOutput<'tcx> {
171173
let mut all_facts =
172174
(use_polonius || AllFacts::enabled(infcx.tcx)).then_some(AllFacts::default());
@@ -191,6 +193,7 @@ pub(crate) fn compute_regions<'cx, 'tcx>(
191193
elements,
192194
upvars,
193195
use_polonius,
196+
defining_use_anchor,
194197
);
195198

196199
if let Some(all_facts) = &mut all_facts {

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use rustc_infer::infer::outlives::env::RegionBoundPairs;
2121
use rustc_infer::infer::region_constraints::RegionConstraintData;
2222
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
2323
use rustc_infer::infer::{
24-
InferCtxt, InferOk, LateBoundRegion, LateBoundRegionConversionTime, NllRegionVariableOrigin,
24+
DefiningAnchor, InferCtxt, InferOk, LateBoundRegion, LateBoundRegionConversionTime,
25+
NllRegionVariableOrigin,
2526
};
2627
use rustc_middle::mir::tcx::PlaceTy;
2728
use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
@@ -136,6 +137,7 @@ pub(crate) fn type_check<'mir, 'tcx>(
136137
elements: &Rc<RegionValueElements>,
137138
upvars: &[Upvar<'tcx>],
138139
use_polonius: bool,
140+
defining_use_anchor: DefiningAnchor,
139141
) -> MirTypeckResults<'tcx> {
140142
let implicit_region_bound = infcx.tcx.mk_re_var(universal_regions.fr_fn_body);
141143
let mut constraints = MirTypeckRegionConstraints {
@@ -182,6 +184,7 @@ pub(crate) fn type_check<'mir, 'tcx>(
182184
&region_bound_pairs,
183185
implicit_region_bound,
184186
&mut borrowck_context,
187+
defining_use_anchor,
185188
);
186189

187190
let errors_reported = {
@@ -877,6 +880,7 @@ struct TypeChecker<'a, 'tcx> {
877880
implicit_region_bound: ty::Region<'tcx>,
878881
reported_errors: FxHashSet<(Ty<'tcx>, Span)>,
879882
borrowck_context: &'a mut BorrowCheckContext<'a, 'tcx>,
883+
defining_use_anchor: DefiningAnchor,
880884
}
881885

882886
struct BorrowCheckContext<'a, 'tcx> {
@@ -1025,6 +1029,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
10251029
region_bound_pairs: &'a RegionBoundPairs<'tcx>,
10261030
implicit_region_bound: ty::Region<'tcx>,
10271031
borrowck_context: &'a mut BorrowCheckContext<'a, 'tcx>,
1032+
defining_use_anchor: DefiningAnchor,
10281033
) -> Self {
10291034
let mut checker = Self {
10301035
infcx,
@@ -1036,6 +1041,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
10361041
implicit_region_bound,
10371042
borrowck_context,
10381043
reported_errors: Default::default(),
1044+
defining_use_anchor,
10391045
};
10401046
checker.check_user_type_annotations();
10411047
checker

compiler/rustc_borrowck/src/type_check/relate_tys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx>
8888
}
8989

9090
fn defining_use_anchor(&self) -> rustc_infer::infer::DefiningAnchor {
91-
self.type_checker.infcx.defining_use_anchor
91+
self.type_checker.defining_use_anchor
9292
}
9393

9494
fn param_env(&self) -> ty::ParamEnv<'tcx> {

compiler/rustc_hir_typeck/src/demand.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
113113
expected: Ty<'tcx>,
114114
actual: Ty<'tcx>,
115115
) -> Option<DiagnosticBuilder<'tcx, ErrorGuaranteed>> {
116-
match self.at(cause, self.param_env).define_opaque_types(self.defining_use_anchor).sup(expected, actual) {
116+
match self
117+
.at(cause, self.param_env)
118+
.define_opaque_types(self.defining_use_anchor)
119+
.sup(expected, actual)
120+
{
117121
Ok(InferOk { obligations, value: () }) => {
118122
self.register_predicates(obligations);
119123
None

compiler/rustc_infer/src/infer/at.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<'tcx> InferCtxt<'tcx> {
6161
pub fn fork(&self) -> Self {
6262
Self {
6363
tcx: self.tcx,
64-
defining_use_anchor: self.defining_use_anchor,
64+
old_defining_use_anchor: self.old_defining_use_anchor,
6565
considering_regions: self.considering_regions,
6666
inner: self.inner.clone(),
6767
skip_leak_check: self.skip_leak_check.clone(),

compiler/rustc_infer/src/infer/canonical/query_response.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ impl<'tcx> InferCtxt<'tcx> {
501501
debug!(?a, ?b, "constrain opaque type");
502502
obligations.extend(
503503
self.at(cause, param_env)
504-
.define_opaque_types(self.defining_use_anchor)
504+
.define_opaque_types(self.old_defining_use_anchor)
505505
.eq(a, b)?
506506
.obligations,
507507
);

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ pub struct InferCtxt<'tcx> {
242242
///
243243
/// Its default value is `DefiningAnchor::Error`, this way it is easier to catch errors that
244244
/// might come up during inference or typeck.
245-
pub defining_use_anchor: DefiningAnchor,
245+
pub old_defining_use_anchor: DefiningAnchor,
246246

247247
/// Whether this inference context should care about region obligations in
248248
/// the root universe. Most notably, this is used during hir typeck as region
@@ -604,7 +604,7 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
604604
let InferCtxtBuilder { tcx, defining_use_anchor, considering_regions, intercrate } = *self;
605605
InferCtxt {
606606
tcx,
607-
defining_use_anchor,
607+
old_defining_use_anchor: defining_use_anchor,
608608
considering_regions,
609609
inner: RefCell::new(InferCtxtInner::new()),
610610
lexical_region_resolutions: RefCell::new(None),
@@ -1318,7 +1318,7 @@ impl<'tcx> InferCtxt<'tcx> {
13181318

13191319
#[instrument(level = "debug", skip(self), ret)]
13201320
pub fn take_opaque_types(&self) -> opaque_types::OpaqueTypeMap<'tcx> {
1321-
debug_assert_ne!(self.defining_use_anchor, DefiningAnchor::Error);
1321+
debug_assert_ne!(self.old_defining_use_anchor, DefiningAnchor::Error);
13221322
std::mem::take(&mut self.inner.borrow_mut().opaque_type_storage.opaque_types)
13231323
}
13241324

compiler/rustc_trait_selection/src/traits/engine.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_infer::infer::at::ToTrace;
1111
use rustc_infer::infer::canonical::{
1212
Canonical, CanonicalQueryResponse, CanonicalVarValues, QueryResponse,
1313
};
14-
use rustc_infer::infer::{InferCtxt, InferOk};
14+
use rustc_infer::infer::{DefiningAnchor, InferCtxt, InferOk};
1515
use rustc_infer::traits::query::Fallible;
1616
use rustc_infer::traits::{
1717
FulfillmentError, Obligation, ObligationCause, PredicateObligation, TraitEngineExt as _,
@@ -52,15 +52,24 @@ impl<'tcx> TraitEngineExt<'tcx> for dyn TraitEngine<'tcx> {
5252
pub struct ObligationCtxt<'a, 'tcx> {
5353
pub infcx: &'a InferCtxt<'tcx>,
5454
engine: RefCell<Box<dyn TraitEngine<'tcx>>>,
55+
defining_use_anchor: DefiningAnchor,
5556
}
5657

5758
impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
5859
pub fn new(infcx: &'a InferCtxt<'tcx>) -> Self {
59-
Self { infcx, engine: RefCell::new(<dyn TraitEngine<'_>>::new(infcx.tcx)) }
60+
Self {
61+
infcx,
62+
engine: RefCell::new(<dyn TraitEngine<'_>>::new(infcx.tcx)),
63+
defining_use_anchor: infcx.old_defining_use_anchor,
64+
}
6065
}
6166

6267
pub fn new_in_snapshot(infcx: &'a InferCtxt<'tcx>) -> Self {
63-
Self { infcx, engine: RefCell::new(<dyn TraitEngine<'_>>::new_in_snapshot(infcx.tcx)) }
68+
Self {
69+
infcx,
70+
engine: RefCell::new(<dyn TraitEngine<'_>>::new_in_snapshot(infcx.tcx)),
71+
defining_use_anchor: infcx.old_defining_use_anchor,
72+
}
6473
}
6574

6675
pub fn register_obligation(&self, obligation: PredicateObligation<'tcx>) {
@@ -128,7 +137,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
128137
{
129138
self.infcx
130139
.at(cause, param_env)
131-
.define_opaque_types(self.infcx.defining_use_anchor)
140+
.define_opaque_types(self.defining_use_anchor)
132141
.eq_exp(a_is_expected, a, b)
133142
.map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
134143
}
@@ -142,7 +151,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
142151
) -> Result<(), TypeError<'tcx>> {
143152
self.infcx
144153
.at(cause, param_env)
145-
.define_opaque_types(self.infcx.defining_use_anchor)
154+
.define_opaque_types(self.defining_use_anchor)
146155
.eq(expected, actual)
147156
.map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
148157
}
@@ -157,7 +166,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
157166
) -> Result<(), TypeError<'tcx>> {
158167
self.infcx
159168
.at(cause, param_env)
160-
.define_opaque_types(self.infcx.defining_use_anchor)
169+
.define_opaque_types(self.defining_use_anchor)
161170
.sup(expected, actual)
162171
.map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
163172
}
@@ -172,7 +181,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
172181
) -> Result<(), TypeError<'tcx>> {
173182
self.infcx
174183
.at(cause, param_env)
175-
.define_opaque_types(self.infcx.defining_use_anchor)
184+
.define_opaque_types(self.defining_use_anchor)
176185
.sup(expected, actual)
177186
.map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
178187
}

0 commit comments

Comments
 (0)