Skip to content

Commit 815679b

Browse files
committed
add TypingMode::Borrowck
1 parent 7695913 commit 815679b

File tree

131 files changed

+880
-811
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

+880
-811
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() {

compiler/rustc_hir_analysis/src/collect.rs

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub(crate) fn provide(providers: &mut Providers) {
6161
*providers = Providers {
6262
type_of: type_of::type_of,
6363
type_of_opaque: type_of::type_of_opaque,
64+
type_of_opaque_hir_typeck: type_of::type_of_opaque_hir_typeck,
6465
type_alias_is_lazy: type_of::type_alias_is_lazy,
6566
item_bounds: item_bounds::item_bounds,
6667
explicit_item_bounds: item_bounds::explicit_item_bounds,

compiler/rustc_hir_analysis/src/collect/type_of.rs

+55-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use rustc_hir::{self as hir, AmbigArg, HirId};
77
use rustc_middle::query::plumbing::CyclePlaceholder;
88
use rustc_middle::ty::print::with_forced_trimmed_paths;
99
use rustc_middle::ty::util::IntTypeExt;
10-
use rustc_middle::ty::{self, IsSuggestable, Ty, TyCtxt, TypeVisitableExt, fold_regions};
10+
use rustc_middle::ty::{
11+
self, DefiningScopeKind, IsSuggestable, Ty, TyCtxt, TypeVisitableExt, fold_regions,
12+
};
1113
use rustc_middle::{bug, span_bug};
1214
use rustc_span::{DUMMY_SP, Ident, Span};
1315

@@ -324,10 +326,18 @@ pub(super) fn type_of_opaque(
324326
if let Some(def_id) = def_id.as_local() {
325327
Ok(ty::EarlyBinder::bind(match tcx.hir_node_by_def_id(def_id).expect_opaque_ty().origin {
326328
hir::OpaqueTyOrigin::TyAlias { in_assoc_ty: false, .. } => {
327-
opaque::find_opaque_ty_constraints_for_tait(tcx, def_id)
329+
opaque::find_opaque_ty_constraints_for_tait(
330+
tcx,
331+
def_id,
332+
DefiningScopeKind::MirBorrowck,
333+
)
328334
}
329335
hir::OpaqueTyOrigin::TyAlias { in_assoc_ty: true, .. } => {
330-
opaque::find_opaque_ty_constraints_for_impl_trait_in_assoc_type(tcx, def_id)
336+
opaque::find_opaque_ty_constraints_for_impl_trait_in_assoc_type(
337+
tcx,
338+
def_id,
339+
DefiningScopeKind::MirBorrowck,
340+
)
331341
}
332342
// Opaque types desugared from `impl Trait`.
333343
hir::OpaqueTyOrigin::FnReturn { parent: owner, in_trait_or_impl }
@@ -340,7 +350,12 @@ pub(super) fn type_of_opaque(
340350
"tried to get type of this RPITIT with no definition"
341351
);
342352
}
343-
opaque::find_opaque_ty_constraints_for_rpit(tcx, def_id, owner)
353+
opaque::find_opaque_ty_constraints_for_rpit(
354+
tcx,
355+
def_id,
356+
owner,
357+
DefiningScopeKind::MirBorrowck,
358+
)
344359
}
345360
}))
346361
} else {
@@ -350,6 +365,42 @@ pub(super) fn type_of_opaque(
350365
}
351366
}
352367

368+
pub(super) fn type_of_opaque_hir_typeck(
369+
tcx: TyCtxt<'_>,
370+
def_id: LocalDefId,
371+
) -> ty::EarlyBinder<'_, Ty<'_>> {
372+
ty::EarlyBinder::bind(match tcx.hir_node_by_def_id(def_id).expect_opaque_ty().origin {
373+
hir::OpaqueTyOrigin::TyAlias { in_assoc_ty: false, .. } => {
374+
opaque::find_opaque_ty_constraints_for_tait(tcx, def_id, DefiningScopeKind::HirTypeck)
375+
}
376+
hir::OpaqueTyOrigin::TyAlias { in_assoc_ty: true, .. } => {
377+
opaque::find_opaque_ty_constraints_for_impl_trait_in_assoc_type(
378+
tcx,
379+
def_id,
380+
DefiningScopeKind::HirTypeck,
381+
)
382+
}
383+
// Opaque types desugared from `impl Trait`.
384+
hir::OpaqueTyOrigin::FnReturn { parent: owner, in_trait_or_impl }
385+
| hir::OpaqueTyOrigin::AsyncFn { parent: owner, in_trait_or_impl } => {
386+
if in_trait_or_impl == Some(hir::RpitContext::Trait)
387+
&& !tcx.defaultness(owner).has_value()
388+
{
389+
span_bug!(
390+
tcx.def_span(def_id),
391+
"tried to get type of this RPITIT with no definition"
392+
);
393+
}
394+
opaque::find_opaque_ty_constraints_for_rpit(
395+
tcx,
396+
def_id,
397+
owner,
398+
DefiningScopeKind::HirTypeck,
399+
)
400+
}
401+
})
402+
}
403+
353404
fn infer_placeholder_type<'tcx>(
354405
cx: &dyn HirTyLowerer<'tcx>,
355406
def_id: LocalDefId,

0 commit comments

Comments
 (0)