Skip to content

Commit 509a144

Browse files
committedApr 3, 2025·
add TypingMode::Borrowck
1 parent 990201c commit 509a144

File tree

131 files changed

+881
-740
lines changed

Some content is hidden

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

131 files changed

+881
-740
lines changed
 

‎compiler/rustc_borrowck/src/lib.rs

+7-31
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use rustc_infer::infer::{
3535
};
3636
use rustc_middle::mir::*;
3737
use rustc_middle::query::Providers;
38-
use rustc_middle::ty::{self, ParamEnv, RegionVid, TyCtxt, TypingMode, fold_regions};
38+
use rustc_middle::ty::{self, ParamEnv, RegionVid, TyCtxt, TypingMode};
3939
use rustc_middle::{bug, span_bug};
4040
use rustc_mir_dataflow::impls::{
4141
EverInitializedPlaces, MaybeInitializedPlaces, MaybeUninitializedPlaces,
@@ -171,12 +171,6 @@ fn do_mir_borrowck<'tcx>(
171171
let free_regions = nll::replace_regions_in_mir(&infcx, &mut body_owned, &mut promoted);
172172
let body = &body_owned; // no further changes
173173

174-
// FIXME(-Znext-solver): A bit dubious that we're only registering
175-
// predefined opaques in the typeck root.
176-
if infcx.next_trait_solver() && !infcx.tcx.is_typeck_child(body.source.def_id()) {
177-
infcx.register_predefined_opaques_for_next_solver(def);
178-
}
179-
180174
let location_table = PoloniusLocationTable::new(body);
181175

182176
let move_data = MoveData::gather_moves(body, tcx, |_| true);
@@ -431,7 +425,12 @@ pub(crate) struct BorrowckInferCtxt<'tcx> {
431425

432426
impl<'tcx> BorrowckInferCtxt<'tcx> {
433427
pub(crate) fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
434-
let infcx = tcx.infer_ctxt().build(TypingMode::analysis_in_body(tcx, def_id));
428+
let typing_mode = if tcx.use_typing_mode_borrowck() {
429+
TypingMode::borrowck(tcx, def_id)
430+
} else {
431+
TypingMode::analysis_in_body(tcx, def_id)
432+
};
433+
let infcx = tcx.infer_ctxt().build(typing_mode);
435434
let param_env = tcx.param_env(def_id);
436435
BorrowckInferCtxt { infcx, reg_var_to_origin: RefCell::new(Default::default()), param_env }
437436
}
@@ -478,29 +477,6 @@ impl<'tcx> BorrowckInferCtxt<'tcx> {
478477

479478
next_region
480479
}
481-
482-
/// With the new solver we prepopulate the opaque type storage during
483-
/// MIR borrowck with the hidden types from HIR typeck. This is necessary
484-
/// to avoid ambiguities as earlier goals can rely on the hidden type
485-
/// of an opaque which is only constrained by a later goal.
486-
fn register_predefined_opaques_for_next_solver(&self, def_id: LocalDefId) {
487-
let tcx = self.tcx;
488-
// OK to use the identity arguments for each opaque type key, since
489-
// we remap opaques from HIR typeck back to their definition params.
490-
for data in tcx.typeck(def_id).concrete_opaque_types.iter().map(|(k, v)| (*k, *v)) {
491-
// HIR typeck did not infer the regions of the opaque, so we instantiate
492-
// them with fresh inference variables.
493-
let (key, hidden_ty) = fold_regions(tcx, data, |_, _| {
494-
self.next_nll_region_var_in_universe(
495-
NllRegionVariableOrigin::Existential { from_forall: false },
496-
ty::UniverseIndex::ROOT,
497-
)
498-
});
499-
500-
let prev = self.register_hidden_type_in_storage(key, hidden_ty);
501-
assert_eq!(prev, None);
502-
}
503-
}
504480
}
505481

506482
impl<'tcx> Deref for BorrowckInferCtxt<'tcx> {

‎compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use rustc_data_structures::fx::FxIndexMap;
22
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin};
33
use rustc_macros::extension;
44
use rustc_middle::ty::{
5-
self, OpaqueHiddenType, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable, TypeVisitableExt, fold_regions,
5+
self, DefiningScopeKind, OpaqueHiddenType, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable,
6+
TypeVisitableExt, fold_regions,
67
};
78
use rustc_span::Span;
89
use rustc_trait_selection::opaque_types::check_opaque_type_parameter_valid;
@@ -266,14 +267,21 @@ impl<'tcx> InferCtxt<'tcx> {
266267
return Ty::new_error(self.tcx, e);
267268
}
268269

269-
if let Err(guar) =
270-
check_opaque_type_parameter_valid(self, opaque_type_key, instantiated_ty.span)
271-
{
270+
if let Err(guar) = check_opaque_type_parameter_valid(
271+
self,
272+
opaque_type_key,
273+
instantiated_ty.span,
274+
DefiningScopeKind::MirBorrowck,
275+
) {
272276
return Ty::new_error(self.tcx, guar);
273277
}
274278

275279
let definition_ty = instantiated_ty
276-
.remap_generic_params_to_declaration_params(opaque_type_key, self.tcx, false)
280+
.remap_generic_params_to_declaration_params(
281+
opaque_type_key,
282+
self.tcx,
283+
DefiningScopeKind::MirBorrowck,
284+
)
277285
.ty;
278286

279287
if let Err(e) = definition_ty.error_reported() {

0 commit comments

Comments
 (0)
Please sign in to comment.