Skip to content

Commit 429e107

Browse files
committed
Remove DefiningAnchor from InferCtxt entirely
1 parent 209103f commit 429e107

File tree

11 files changed

+21
-72
lines changed

11 files changed

+21
-72
lines changed

compiler/rustc_borrowck/src/consumers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn get_body_with_borrowck_facts(
3333
def: ty::WithOptConstParam<LocalDefId>,
3434
) -> BodyWithBorrowckFacts<'_> {
3535
let (input_body, promoted) = tcx.mir_promoted(def);
36-
let infcx = tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bind(def.did)).build();
36+
let infcx = tcx.infer_ctxt().build();
3737
let input_body: &Body<'_> = &input_body.borrow();
3838
let promoted: &IndexVec<_, _> = &promoted.borrow();
3939
*super::do_mir_borrowck(&infcx, input_body, promoted, true, DefiningAnchor::Bind(def.did))

compiler/rustc_borrowck/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ fn mir_borrowck(tcx: TyCtxt<'_>, def: ty::WithOptConstParam<LocalDefId>) -> &Bor
152152

153153
let hir_owner = tcx.hir().local_def_id_to_hir_id(def.did).owner;
154154

155-
let infcx =
156-
tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bind(hir_owner.def_id)).build();
155+
let infcx = tcx.infer_ctxt().build();
157156
let input_body: &Body<'_> = &input_body.borrow();
158157
let promoted: &IndexVec<_, _> = &promoted.borrow();
159158
let opt_closure_req = do_mir_borrowck(

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use rustc_data_structures::vec_map::VecMap;
33
use rustc_errors::ErrorGuaranteed;
44
use rustc_hir::def_id::LocalDefId;
55
use rustc_hir::OpaqueTyOrigin;
6+
use rustc_infer::infer::InferCtxt;
67
use rustc_infer::infer::TyCtxtInferExt as _;
7-
use rustc_infer::infer::{DefiningAnchor, InferCtxt};
88
use rustc_infer::traits::{Obligation, ObligationCause};
99
use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts};
1010
use rustc_middle::ty::visit::TypeVisitableExt;
@@ -273,11 +273,10 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
273273
// This logic duplicates most of `check_opaque_meets_bounds`.
274274
// FIXME(oli-obk): Also do region checks here and then consider removing `check_opaque_meets_bounds` entirely.
275275
let param_env = self.tcx.param_env(def_id);
276+
let infcx = self.tcx.infer_ctxt().build();
276277
// HACK This bubble is required for this tests to pass:
277278
// nested-return-type2-tait2.rs
278279
// nested-return-type2-tait3.rs
279-
let infcx =
280-
self.tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bubble).build();
281280
let ocx = ObligationCtxt::new_with_opaque_type_bubbling(&infcx);
282281
// Require the hidden type to be well-formed with only the generics of the opaque type.
283282
// Defining use functions may have more bounds than the opaque type, which is ok, as long as the

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,7 @@ fn check_opaque_meets_bounds<'tcx>(
413413
};
414414
let param_env = tcx.param_env(defining_use_anchor);
415415

416-
let infcx = tcx
417-
.infer_ctxt()
418-
.with_opaque_type_inference(DefiningAnchor::Bind(defining_use_anchor))
419-
.build();
416+
let infcx = tcx.infer_ctxt().build();
420417
let ocx = ObligationCtxt::new_with_opaque_type_anchor(&infcx, defining_use_anchor);
421418
let opaque_ty = tcx.mk_opaque(def_id.to_def_id(), substs);
422419

@@ -1536,15 +1533,14 @@ pub(super) fn check_generator_obligations(tcx: TyCtxt<'_>, def_id: LocalDefId) {
15361533
// typeck writeback gives us predicates with their regions erased.
15371534
// As borrowck already has checked lifetimes, we do not need to do it again.
15381535
.ignoring_regions()
1539-
// Bind opaque types to `def_id` as they should have been checked by borrowck.
1540-
.with_opaque_type_inference(DefiningAnchor::Bind(def_id))
15411536
.build();
15421537

15431538
let mut fulfillment_cx = <dyn TraitEngine<'_>>::new(infcx.tcx);
15441539
for (predicate, cause) in generator_interior_predicates {
15451540
let obligation = Obligation::new(tcx, cause.clone(), param_env, *predicate);
15461541
fulfillment_cx.register_predicate_obligation(&infcx, obligation);
15471542
}
1543+
// Bind opaque types to `def_id` as they should have been checked by borrowck.
15481544
let errors = fulfillment_cx.select_all_or_error(&infcx, DefiningAnchor::Bind(def_id));
15491545
debug!(?errors);
15501546
if !errors.is_empty() {

compiler/rustc_hir_typeck/src/inherited.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,7 @@ impl<'tcx> Inherited<'tcx> {
9696
let hir_owner = tcx.hir().local_def_id_to_hir_id(def_id).owner;
9797

9898
InheritedBuilder {
99-
infcx: tcx
100-
.infer_ctxt()
101-
.ignoring_regions()
102-
.with_opaque_type_inference(DefiningAnchor::Bind(hir_owner.def_id)),
99+
infcx: tcx.infer_ctxt().ignoring_regions(),
103100
def_id,
104101
typeck_results: RefCell::new(ty::TypeckResults::new(hir_owner)),
105102
}

compiler/rustc_infer/src/infer/at.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ impl<'tcx> InferCtxt<'tcx> {
6161
pub fn fork(&self) -> Self {
6262
Self {
6363
tcx: self.tcx,
64-
old_defining_use_anchor: self.old_defining_use_anchor,
6564
considering_regions: self.considering_regions,
6665
inner: self.inner.clone(),
6766
skip_leak_check: self.skip_leak_check.clone(),

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -232,18 +232,6 @@ pub enum DefiningAnchor {
232232
pub struct InferCtxt<'tcx> {
233233
pub tcx: TyCtxt<'tcx>,
234234

235-
/// The `DefId` of the item in whose context we are performing inference or typeck.
236-
/// It is used to check whether an opaque type use is a defining use.
237-
///
238-
/// If it is `DefiningAnchor::Bubble`, we can't resolve opaque types here and need to bubble up
239-
/// the obligation. This frequently happens for
240-
/// short lived InferCtxt within queries. The opaque type obligations are forwarded
241-
/// to the outside until the end up in an `InferCtxt` for typeck or borrowck.
242-
///
243-
/// Its default value is `DefiningAnchor::Error`, this way it is easier to catch errors that
244-
/// might come up during inference or typeck.
245-
pub old_defining_use_anchor: DefiningAnchor,
246-
247235
/// Whether this inference context should care about region obligations in
248236
/// the root universe. Most notably, this is used during hir typeck as region
249237
/// solving is left to borrowck instead.
@@ -537,7 +525,6 @@ impl<'tcx> fmt::Display for FixupError<'tcx> {
537525
/// Used to configure inference contexts before their creation
538526
pub struct InferCtxtBuilder<'tcx> {
539527
tcx: TyCtxt<'tcx>,
540-
defining_use_anchor: DefiningAnchor,
541528
considering_regions: bool,
542529
/// Whether we are in coherence mode.
543530
intercrate: bool,
@@ -549,27 +536,11 @@ pub trait TyCtxtInferExt<'tcx> {
549536

550537
impl<'tcx> TyCtxtInferExt<'tcx> for TyCtxt<'tcx> {
551538
fn infer_ctxt(self) -> InferCtxtBuilder<'tcx> {
552-
InferCtxtBuilder {
553-
tcx: self,
554-
defining_use_anchor: DefiningAnchor::Error,
555-
considering_regions: true,
556-
intercrate: false,
557-
}
539+
InferCtxtBuilder { tcx: self, considering_regions: true, intercrate: false }
558540
}
559541
}
560542

561543
impl<'tcx> InferCtxtBuilder<'tcx> {
562-
/// Whenever the `InferCtxt` should be able to handle defining uses of opaque types,
563-
/// you need to call this function. Otherwise the opaque type will be treated opaquely.
564-
///
565-
/// It is only meant to be called in two places, for typeck
566-
/// (via `Inherited::build`) and for the inference context used
567-
/// in mir borrowck.
568-
pub fn with_opaque_type_inference(mut self, defining_use_anchor: DefiningAnchor) -> Self {
569-
self.defining_use_anchor = defining_use_anchor;
570-
self
571-
}
572-
573544
pub fn intercrate(mut self) -> Self {
574545
self.intercrate = true;
575546
self
@@ -601,10 +572,9 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
601572
}
602573

603574
pub fn build(&mut self) -> InferCtxt<'tcx> {
604-
let InferCtxtBuilder { tcx, defining_use_anchor, considering_regions, intercrate } = *self;
575+
let InferCtxtBuilder { tcx, considering_regions, intercrate } = *self;
605576
InferCtxt {
606577
tcx,
607-
old_defining_use_anchor: defining_use_anchor,
608578
considering_regions,
609579
inner: RefCell::new(InferCtxtInner::new()),
610580
lexical_region_resolutions: RefCell::new(None),

compiler/rustc_trait_selection/src/traits/select/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
228228
pub fn new_in_canonical_query(infcx: &'cx InferCtxt<'tcx>) -> SelectionContext<'cx, 'tcx> {
229229
SelectionContext {
230230
query_mode: TraitQueryMode::Canonical,
231+
// This bubble is required for this tests to pass:
232+
// impl-trait/issue99642.rs
231233
defining_use_anchor: DefiningAnchor::Bubble,
232234
..SelectionContext::new(infcx)
233235
}

compiler/rustc_traits/src/codegen.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,8 @@ pub fn codegen_select_candidate<'tcx>(
2929

3030
// Do the initial selection for the obligation. This yields the
3131
// shallow result we are looking for -- that is, what specific impl.
32-
let infcx = tcx
33-
.infer_ctxt()
34-
.ignoring_regions()
35-
.with_opaque_type_inference(DefiningAnchor::Bubble)
36-
.build();
37-
//~^ HACK `Bubble` is required for
32+
let infcx = tcx.infer_ctxt().ignoring_regions().build();
33+
// HACK `Bubble` is required for
3834
// this test to pass: type-alias-impl-trait/assoc-projection-ice.rs
3935
let mut selcx = SelectionContext::new(&infcx).with_defining_use_anchor(DefiningAnchor::Bubble);
4036

compiler/rustc_traits/src/evaluate_obligation.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_infer::infer::{DefiningAnchor, TyCtxtInferExt};
1+
use rustc_infer::infer::TyCtxtInferExt;
22
use rustc_middle::ty::query::Providers;
33
use rustc_middle::ty::{ParamEnvAnd, TyCtxt};
44
use rustc_span::source_map::DUMMY_SP;
@@ -16,12 +16,8 @@ fn evaluate_obligation<'tcx>(
1616
canonical_goal: CanonicalPredicateGoal<'tcx>,
1717
) -> Result<EvaluationResult, OverflowError> {
1818
debug!("evaluate_obligation(canonical_goal={:#?})", canonical_goal);
19-
// HACK This bubble is required for this tests to pass:
20-
// impl-trait/issue99642.rs
21-
let (ref infcx, goal, _canonical_inference_vars) = tcx
22-
.infer_ctxt()
23-
.with_opaque_type_inference(DefiningAnchor::Bubble)
24-
.build_with_canonical(DUMMY_SP, &canonical_goal);
19+
let (ref infcx, goal, _canonical_inference_vars) =
20+
tcx.infer_ctxt().build_with_canonical(DUMMY_SP, &canonical_goal);
2521
debug!("evaluate_obligation: goal={:#?}", goal);
2622
let ParamEnvAnd { param_env, value: predicate } = goal;
2723

0 commit comments

Comments
 (0)