Skip to content

Commit 283abbf

Browse files
committed
Change InferCtxtBuilder from enter to build
1 parent 91269fa commit 283abbf

File tree

53 files changed

+1984
-2200
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1984
-2200
lines changed

compiler/rustc_borrowck/src/consumers.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ pub fn get_body_with_borrowck_facts<'tcx>(
3131
def: ty::WithOptConstParam<LocalDefId>,
3232
) -> BodyWithBorrowckFacts<'tcx> {
3333
let (input_body, promoted) = tcx.mir_promoted(def);
34-
tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bind(def.did)).enter(|infcx| {
35-
let input_body: &Body<'_> = &input_body.borrow();
36-
let promoted: &IndexVec<_, _> = &promoted.borrow();
37-
*super::do_mir_borrowck(&infcx, input_body, promoted, true).1.unwrap()
38-
})
34+
let infcx = tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bind(def.did)).build();
35+
let input_body: &Body<'_> = &input_body.borrow();
36+
let promoted: &IndexVec<_, _> = &promoted.borrow();
37+
*super::do_mir_borrowck(&infcx, input_body, promoted, true).1.unwrap()
3938
}

compiler/rustc_borrowck/src/diagnostics/bound_region_errors.rs

+28-60
Original file line numberDiff line numberDiff line change
@@ -238,20 +238,11 @@ impl<'tcx> TypeOpInfo<'tcx> for PredicateQuery<'tcx> {
238238
placeholder_region: ty::Region<'tcx>,
239239
error_region: Option<ty::Region<'tcx>>,
240240
) -> Option<DiagnosticBuilder<'tcx, ErrorGuaranteed>> {
241-
mbcx.infcx.tcx.infer_ctxt().enter_with_canonical(
242-
cause.span,
243-
&self.canonical_query,
244-
|ref infcx, key, _| {
245-
let mut fulfill_cx = <dyn TraitEngine<'_>>::new(infcx.tcx);
246-
type_op_prove_predicate_with_cause(infcx, &mut *fulfill_cx, key, cause);
247-
try_extract_error_from_fulfill_cx(
248-
fulfill_cx,
249-
infcx,
250-
placeholder_region,
251-
error_region,
252-
)
253-
},
254-
)
241+
let (ref infcx, key, _) =
242+
mbcx.infcx.tcx.infer_ctxt().build_with_canonical(cause.span, &self.canonical_query);
243+
let mut fulfill_cx = <dyn TraitEngine<'_>>::new(infcx.tcx);
244+
type_op_prove_predicate_with_cause(infcx, &mut *fulfill_cx, key, cause);
245+
try_extract_error_from_fulfill_cx(fulfill_cx, infcx, placeholder_region, error_region)
255246
}
256247
}
257248

@@ -288,37 +279,24 @@ where
288279
placeholder_region: ty::Region<'tcx>,
289280
error_region: Option<ty::Region<'tcx>>,
290281
) -> Option<DiagnosticBuilder<'tcx, ErrorGuaranteed>> {
291-
mbcx.infcx.tcx.infer_ctxt().enter_with_canonical(
292-
cause.span,
293-
&self.canonical_query,
294-
|ref infcx, key, _| {
295-
let mut fulfill_cx = <dyn TraitEngine<'_>>::new(infcx.tcx);
296-
297-
let mut selcx = SelectionContext::new(infcx);
298-
299-
// FIXME(lqd): Unify and de-duplicate the following with the actual
300-
// `rustc_traits::type_op::type_op_normalize` query to allow the span we need in the
301-
// `ObligationCause`. The normalization results are currently different between
302-
// `AtExt::normalize` used in the query and `normalize` called below: the former fails
303-
// to normalize the `nll/relate_tys/impl-fn-ignore-binder-via-bottom.rs` test. Check
304-
// after #85499 lands to see if its fixes have erased this difference.
305-
let (param_env, value) = key.into_parts();
306-
let Normalized { value: _, obligations } = rustc_trait_selection::traits::normalize(
307-
&mut selcx,
308-
param_env,
309-
cause,
310-
value.value,
311-
);
312-
fulfill_cx.register_predicate_obligations(infcx, obligations);
313-
314-
try_extract_error_from_fulfill_cx(
315-
fulfill_cx,
316-
infcx,
317-
placeholder_region,
318-
error_region,
319-
)
320-
},
321-
)
282+
let (ref infcx, key, _) =
283+
mbcx.infcx.tcx.infer_ctxt().build_with_canonical(cause.span, &self.canonical_query);
284+
let mut fulfill_cx = <dyn TraitEngine<'_>>::new(infcx.tcx);
285+
286+
let mut selcx = SelectionContext::new(infcx);
287+
288+
// FIXME(lqd): Unify and de-duplicate the following with the actual
289+
// `rustc_traits::type_op::type_op_normalize` query to allow the span we need in the
290+
// `ObligationCause`. The normalization results are currently different between
291+
// `AtExt::normalize` used in the query and `normalize` called below: the former fails
292+
// to normalize the `nll/relate_tys/impl-fn-ignore-binder-via-bottom.rs` test. Check
293+
// after #85499 lands to see if its fixes have erased this difference.
294+
let (param_env, value) = key.into_parts();
295+
let Normalized { value: _, obligations } =
296+
rustc_trait_selection::traits::normalize(&mut selcx, param_env, cause, value.value);
297+
fulfill_cx.register_predicate_obligations(infcx, obligations);
298+
299+
try_extract_error_from_fulfill_cx(fulfill_cx, infcx, placeholder_region, error_region)
322300
}
323301
}
324302

@@ -349,21 +327,11 @@ impl<'tcx> TypeOpInfo<'tcx> for AscribeUserTypeQuery<'tcx> {
349327
placeholder_region: ty::Region<'tcx>,
350328
error_region: Option<ty::Region<'tcx>>,
351329
) -> Option<DiagnosticBuilder<'tcx, ErrorGuaranteed>> {
352-
mbcx.infcx.tcx.infer_ctxt().enter_with_canonical(
353-
cause.span,
354-
&self.canonical_query,
355-
|ref infcx, key, _| {
356-
let mut fulfill_cx = <dyn TraitEngine<'_>>::new(infcx.tcx);
357-
type_op_ascribe_user_type_with_span(infcx, &mut *fulfill_cx, key, Some(cause.span))
358-
.ok()?;
359-
try_extract_error_from_fulfill_cx(
360-
fulfill_cx,
361-
infcx,
362-
placeholder_region,
363-
error_region,
364-
)
365-
},
366-
)
330+
let (ref infcx, key, _) =
331+
mbcx.infcx.tcx.infer_ctxt().build_with_canonical(cause.span, &self.canonical_query);
332+
let mut fulfill_cx = <dyn TraitEngine<'_>>::new(infcx.tcx);
333+
type_op_ascribe_user_type_with_span(infcx, &mut *fulfill_cx, key, Some(cause.span)).ok()?;
334+
try_extract_error_from_fulfill_cx(fulfill_cx, infcx, placeholder_region, error_region)
367335
}
368336
}
369337

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+36-38
Original file line numberDiff line numberDiff line change
@@ -492,11 +492,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
492492
let Some(default_trait) = tcx.get_diagnostic_item(sym::Default) else {
493493
return false;
494494
};
495-
tcx.infer_ctxt().enter(|infcx| {
496-
infcx
497-
.type_implements_trait(default_trait, ty, ty::List::empty(), param_env)
498-
.may_apply()
499-
})
495+
tcx.infer_ctxt()
496+
.build()
497+
.type_implements_trait(default_trait, ty, ty::List::empty(), param_env)
498+
.may_apply()
500499
};
501500

502501
let assign_value = match ty.kind() {
@@ -606,41 +605,40 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
606605
.and_then(|def_id| tcx.hir().get_generics(def_id))
607606
else { return; };
608607
// Try to find predicates on *generic params* that would allow copying `ty`
609-
let predicates: Result<Vec<_>, _> = tcx.infer_ctxt().enter(|infcx| {
610-
let mut fulfill_cx = <dyn rustc_infer::traits::TraitEngine<'_>>::new(infcx.tcx);
608+
let infcx = tcx.infer_ctxt().build();
609+
let mut fulfill_cx = <dyn rustc_infer::traits::TraitEngine<'_>>::new(infcx.tcx);
611610

612-
let copy_did = infcx.tcx.lang_items().copy_trait().unwrap();
613-
let cause = ObligationCause::new(
614-
span,
615-
self.mir_hir_id(),
616-
rustc_infer::traits::ObligationCauseCode::MiscObligation,
617-
);
618-
fulfill_cx.register_bound(
619-
&infcx,
620-
self.param_env,
621-
// Erase any region vids from the type, which may not be resolved
622-
infcx.tcx.erase_regions(ty),
623-
copy_did,
624-
cause,
625-
);
626-
// Select all, including ambiguous predicates
627-
let errors = fulfill_cx.select_all_or_error(&infcx);
628-
629-
// Only emit suggestion if all required predicates are on generic
630-
errors
631-
.into_iter()
632-
.map(|err| match err.obligation.predicate.kind().skip_binder() {
633-
PredicateKind::Trait(predicate) => match predicate.self_ty().kind() {
634-
ty::Param(param_ty) => Ok((
635-
generics.type_param(param_ty, tcx),
636-
predicate.trait_ref.print_only_trait_path().to_string(),
637-
)),
638-
_ => Err(()),
639-
},
611+
let copy_did = infcx.tcx.lang_items().copy_trait().unwrap();
612+
let cause = ObligationCause::new(
613+
span,
614+
self.mir_hir_id(),
615+
rustc_infer::traits::ObligationCauseCode::MiscObligation,
616+
);
617+
fulfill_cx.register_bound(
618+
&infcx,
619+
self.param_env,
620+
// Erase any region vids from the type, which may not be resolved
621+
infcx.tcx.erase_regions(ty),
622+
copy_did,
623+
cause,
624+
);
625+
// Select all, including ambiguous predicates
626+
let errors = fulfill_cx.select_all_or_error(&infcx);
627+
628+
// Only emit suggestion if all required predicates are on generic
629+
let predicates: Result<Vec<_>, _> = errors
630+
.into_iter()
631+
.map(|err| match err.obligation.predicate.kind().skip_binder() {
632+
PredicateKind::Trait(predicate) => match predicate.self_ty().kind() {
633+
ty::Param(param_ty) => Ok((
634+
generics.type_param(param_ty, tcx),
635+
predicate.trait_ref.print_only_trait_path().to_string(),
636+
)),
640637
_ => Err(()),
641-
})
642-
.collect()
643-
});
638+
},
639+
_ => Err(()),
640+
})
641+
.collect();
644642

645643
if let Ok(predicates) = predicates {
646644
suggest_constraining_type_params(

compiler/rustc_borrowck/src/diagnostics/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10251025
if let Some((CallDesugaringKind::ForLoopIntoIter, _)) = desugaring {
10261026
let ty = moved_place.ty(self.body, self.infcx.tcx).ty;
10271027
let suggest = match self.infcx.tcx.get_diagnostic_item(sym::IntoIterator) {
1028-
Some(def_id) => self.infcx.tcx.infer_ctxt().enter(|infcx| {
1028+
Some(def_id) => {
1029+
let infcx = self.infcx.tcx.infer_ctxt().build();
10291030
type_known_to_meet_bound_modulo_regions(
10301031
&infcx,
10311032
self.param_env,
@@ -1036,7 +1037,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10361037
def_id,
10371038
DUMMY_SP,
10381039
)
1039-
}),
1040+
}
10401041
_ => false,
10411042
};
10421043
if suggest {

compiler/rustc_borrowck/src/lib.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,11 @@ fn mir_borrowck<'tcx>(
131131
debug!("run query mir_borrowck: {}", tcx.def_path_str(def.did.to_def_id()));
132132
let hir_owner = tcx.hir().local_def_id_to_hir_id(def.did).owner;
133133

134-
let opt_closure_req = tcx
135-
.infer_ctxt()
136-
.with_opaque_type_inference(DefiningAnchor::Bind(hir_owner.def_id))
137-
.enter(|infcx| {
138-
let input_body: &Body<'_> = &input_body.borrow();
139-
let promoted: &IndexVec<_, _> = &promoted.borrow();
140-
do_mir_borrowck(&infcx, input_body, promoted, false).0
141-
});
134+
let infcx =
135+
tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bind(hir_owner.def_id)).build();
136+
let input_body: &Body<'_> = &input_body.borrow();
137+
let promoted: &IndexVec<_, _> = &promoted.borrow();
138+
let opt_closure_req = do_mir_borrowck(&infcx, input_body, promoted, false).0;
142139
debug!("mir_borrowck done");
143140

144141
tcx.arena.alloc(opt_closure_req)

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+57-61
Original file line numberDiff line numberDiff line change
@@ -266,73 +266,69 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
266266

267267
// Only check this for TAIT. RPIT already supports `src/test/ui/impl-trait/nested-return-type2.rs`
268268
// on stable and we'd break that.
269-
if let OpaqueTyOrigin::TyAlias = origin {
270-
// This logic duplicates most of `check_opaque_meets_bounds`.
271-
// FIXME(oli-obk): Also do region checks here and then consider removing `check_opaque_meets_bounds` entirely.
272-
let param_env = self.tcx.param_env(def_id);
273-
let body_id = self.tcx.local_def_id_to_hir_id(def_id);
274-
// HACK This bubble is required for this tests to pass:
275-
// type-alias-impl-trait/issue-67844-nested-opaque.rs
276-
self.tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bubble).enter(
277-
move |infcx| {
278-
// Require the hidden type to be well-formed with only the generics of the opaque type.
279-
// Defining use functions may have more bounds than the opaque type, which is ok, as long as the
280-
// hidden type is well formed even without those bounds.
281-
let predicate =
282-
ty::Binder::dummy(ty::PredicateKind::WellFormed(definition_ty.into()))
283-
.to_predicate(infcx.tcx);
284-
let mut fulfillment_cx = <dyn TraitEngine<'tcx>>::new(infcx.tcx);
285-
286-
// Require that the hidden type actually fulfills all the bounds of the opaque type, even without
287-
// the bounds that the function supplies.
288-
match infcx.register_hidden_type(
289-
OpaqueTypeKey { def_id, substs: id_substs },
290-
ObligationCause::misc(instantiated_ty.span, body_id),
291-
param_env,
269+
let OpaqueTyOrigin::TyAlias = origin else {
270+
return definition_ty;
271+
};
272+
// This logic duplicates most of `check_opaque_meets_bounds`.
273+
// FIXME(oli-obk): Also do region checks here and then consider removing `check_opaque_meets_bounds` entirely.
274+
let param_env = self.tcx.param_env(def_id);
275+
let body_id = self.tcx.local_def_id_to_hir_id(def_id);
276+
// HACK This bubble is required for this tests to pass:
277+
// type-alias-impl-trait/issue-67844-nested-opaque.rs
278+
let infcx =
279+
self.tcx.infer_ctxt().with_opaque_type_inference(DefiningAnchor::Bubble).build();
280+
// Require the hidden type to be well-formed with only the generics of the opaque type.
281+
// Defining use functions may have more bounds than the opaque type, which is ok, as long as the
282+
// hidden type is well formed even without those bounds.
283+
let predicate = ty::Binder::dummy(ty::PredicateKind::WellFormed(definition_ty.into()))
284+
.to_predicate(infcx.tcx);
285+
let mut fulfillment_cx = <dyn TraitEngine<'tcx>>::new(infcx.tcx);
286+
287+
// Require that the hidden type actually fulfills all the bounds of the opaque type, even without
288+
// the bounds that the function supplies.
289+
match infcx.register_hidden_type(
290+
OpaqueTypeKey { def_id, substs: id_substs },
291+
ObligationCause::misc(instantiated_ty.span, body_id),
292+
param_env,
293+
definition_ty,
294+
origin,
295+
) {
296+
Ok(infer_ok) => {
297+
for obligation in infer_ok.obligations {
298+
fulfillment_cx.register_predicate_obligation(&infcx, obligation);
299+
}
300+
}
301+
Err(err) => {
302+
infcx
303+
.err_ctxt()
304+
.report_mismatched_types(
305+
&ObligationCause::misc(instantiated_ty.span, body_id),
306+
self.tcx.mk_opaque(def_id.to_def_id(), id_substs),
292307
definition_ty,
293-
origin,
294-
) {
295-
Ok(infer_ok) => {
296-
for obligation in infer_ok.obligations {
297-
fulfillment_cx.register_predicate_obligation(&infcx, obligation);
298-
}
299-
}
300-
Err(err) => {
301-
infcx
302-
.err_ctxt()
303-
.report_mismatched_types(
304-
&ObligationCause::misc(instantiated_ty.span, body_id),
305-
self.tcx.mk_opaque(def_id.to_def_id(), id_substs),
306-
definition_ty,
307-
err,
308-
)
309-
.emit();
310-
}
311-
}
308+
err,
309+
)
310+
.emit();
311+
}
312+
}
312313

313-
fulfillment_cx.register_predicate_obligation(
314-
&infcx,
315-
Obligation::misc(instantiated_ty.span, body_id, param_env, predicate),
316-
);
314+
fulfillment_cx.register_predicate_obligation(
315+
&infcx,
316+
Obligation::misc(instantiated_ty.span, body_id, param_env, predicate),
317+
);
317318

318-
// Check that all obligations are satisfied by the implementation's
319-
// version.
320-
let errors = fulfillment_cx.select_all_or_error(&infcx);
319+
// Check that all obligations are satisfied by the implementation's
320+
// version.
321+
let errors = fulfillment_cx.select_all_or_error(&infcx);
321322

322-
// This is still required for many(half of the tests in ui/type-alias-impl-trait)
323-
// tests to pass
324-
let _ = infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
323+
// This is still required for many(half of the tests in ui/type-alias-impl-trait)
324+
// tests to pass
325+
let _ = infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
325326

326-
if errors.is_empty() {
327-
definition_ty
328-
} else {
329-
infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
330-
self.tcx.ty_error()
331-
}
332-
},
333-
)
334-
} else {
327+
if errors.is_empty() {
335328
definition_ty
329+
} else {
330+
infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
331+
self.tcx.ty_error()
336332
}
337333
}
338334
}

0 commit comments

Comments
 (0)