Skip to content

Commit 7d83a4c

Browse files
committed
Auto merge of #124822 - matthiaskrgr:rollup-h7fc52t, r=matthiaskrgr
Rollup of 3 pull requests Successful merges: - #124759 (Record impl args in the proof tree in new solver) - #124809 (borrowck: prepopulate opaque storage more eagerly) - #124815 (Update books) r? `@ghost` `@rustbot` modify labels: rollup
2 parents ce652db + 61751b2 commit 7d83a4c

29 files changed

+231
-235
lines changed

compiler/rustc_borrowck/src/consumers.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use rustc_hir::def_id::LocalDefId;
44
use rustc_index::{IndexSlice, IndexVec};
5-
use rustc_infer::infer::TyCtxtInferExt;
65
use rustc_middle::mir::{Body, Promoted};
76
use rustc_middle::ty::TyCtxt;
87
use std::rc::Rc;
@@ -105,8 +104,7 @@ pub fn get_body_with_borrowck_facts(
105104
options: ConsumerOptions,
106105
) -> BodyWithBorrowckFacts<'_> {
107106
let (input_body, promoted) = tcx.mir_promoted(def);
108-
let infcx = tcx.infer_ctxt().with_opaque_type_inference(def).build();
109107
let input_body: &Body<'_> = &input_body.borrow();
110108
let promoted: &IndexSlice<_, _> = &promoted.borrow();
111-
*super::do_mir_borrowck(&infcx, input_body, promoted, Some(options)).1.unwrap()
109+
*super::do_mir_borrowck(tcx, input_body, promoted, Some(options)).1.unwrap()
112110
}

compiler/rustc_borrowck/src/lib.rs

+43-19
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ use rustc_hir as hir;
2323
use rustc_hir::def_id::LocalDefId;
2424
use rustc_index::bit_set::{BitSet, ChunkedBitSet};
2525
use rustc_index::{IndexSlice, IndexVec};
26-
use rustc_infer::infer::{
27-
InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin, TyCtxtInferExt,
28-
};
26+
use rustc_infer::infer::TyCtxtInferExt;
27+
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin};
2928
use rustc_middle::mir::tcx::PlaceTy;
3029
use rustc_middle::mir::*;
3130
use rustc_middle::query::Providers;
@@ -123,9 +122,8 @@ fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> {
123122
return tcx.arena.alloc(result);
124123
}
125124

126-
let infcx = tcx.infer_ctxt().with_opaque_type_inference(def).build();
127125
let promoted: &IndexSlice<_, _> = &promoted.borrow();
128-
let opt_closure_req = do_mir_borrowck(&infcx, input_body, promoted, None).0;
126+
let opt_closure_req = do_mir_borrowck(tcx, input_body, promoted, None).0;
129127
debug!("mir_borrowck done");
130128

131129
tcx.arena.alloc(opt_closure_req)
@@ -136,18 +134,15 @@ fn mir_borrowck(tcx: TyCtxt<'_>, def: LocalDefId) -> &BorrowCheckResult<'_> {
136134
/// Use `consumer_options: None` for the default behavior of returning
137135
/// [`BorrowCheckResult`] only. Otherwise, return [`BodyWithBorrowckFacts`] according
138136
/// to the given [`ConsumerOptions`].
139-
#[instrument(skip(infcx, input_body, input_promoted), fields(id=?input_body.source.def_id()), level = "debug")]
137+
#[instrument(skip(tcx, input_body, input_promoted), fields(id=?input_body.source.def_id()), level = "debug")]
140138
fn do_mir_borrowck<'tcx>(
141-
infcx: &InferCtxt<'tcx>,
139+
tcx: TyCtxt<'tcx>,
142140
input_body: &Body<'tcx>,
143141
input_promoted: &IndexSlice<Promoted, Body<'tcx>>,
144142
consumer_options: Option<ConsumerOptions>,
145143
) -> (BorrowCheckResult<'tcx>, Option<Box<BodyWithBorrowckFacts<'tcx>>>) {
146144
let def = input_body.source.def_id().expect_local();
147-
debug!(?def);
148-
149-
let tcx = infcx.tcx;
150-
let infcx = BorrowckInferCtxt::new(infcx);
145+
let infcx = BorrowckInferCtxt::new(tcx, def);
151146
let param_env = tcx.param_env(def);
152147

153148
let mut local_names = IndexVec::from_elem(None, &input_body.local_decls);
@@ -187,6 +182,12 @@ fn do_mir_borrowck<'tcx>(
187182
nll::replace_regions_in_mir(&infcx, param_env, &mut body_owned, &mut promoted);
188183
let body = &body_owned; // no further changes
189184

185+
// FIXME(-Znext-solver): A bit dubious that we're only registering
186+
// predefined opaques in the typeck root.
187+
if infcx.next_trait_solver() && !infcx.tcx.is_typeck_child(body.source.def_id()) {
188+
infcx.register_predefined_opaques_for_next_solver(def);
189+
}
190+
190191
let location_table = LocationTable::new(body);
191192

192193
let move_data = MoveData::gather_moves(body, tcx, param_env, |_| true);
@@ -440,13 +441,14 @@ fn do_mir_borrowck<'tcx>(
440441
(result, body_with_facts)
441442
}
442443

443-
pub struct BorrowckInferCtxt<'cx, 'tcx> {
444-
pub(crate) infcx: &'cx InferCtxt<'tcx>,
444+
pub struct BorrowckInferCtxt<'tcx> {
445+
pub(crate) infcx: InferCtxt<'tcx>,
445446
pub(crate) reg_var_to_origin: RefCell<FxIndexMap<ty::RegionVid, RegionCtxt>>,
446447
}
447448

448-
impl<'cx, 'tcx> BorrowckInferCtxt<'cx, 'tcx> {
449-
pub(crate) fn new(infcx: &'cx InferCtxt<'tcx>) -> Self {
449+
impl<'tcx> BorrowckInferCtxt<'tcx> {
450+
pub(crate) fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
451+
let infcx = tcx.infer_ctxt().with_opaque_type_inference(def_id).build();
450452
BorrowckInferCtxt { infcx, reg_var_to_origin: RefCell::new(Default::default()) }
451453
}
452454

@@ -492,18 +494,40 @@ impl<'cx, 'tcx> BorrowckInferCtxt<'cx, 'tcx> {
492494

493495
next_region
494496
}
497+
498+
/// With the new solver we prepopulate the opaque type storage during
499+
/// MIR borrowck with the hidden types from HIR typeck. This is necessary
500+
/// to avoid ambiguities as earlier goals can rely on the hidden type
501+
/// of an opaque which is only constrained by a later goal.
502+
fn register_predefined_opaques_for_next_solver(&self, def_id: LocalDefId) {
503+
let tcx = self.tcx;
504+
// OK to use the identity arguments for each opaque type key, since
505+
// we remap opaques from HIR typeck back to their definition params.
506+
for data in tcx.typeck(def_id).concrete_opaque_types.iter().map(|(k, v)| (*k, *v)) {
507+
// HIR typeck did not infer the regions of the opaque, so we instantiate
508+
// them with fresh inference variables.
509+
let (key, hidden_ty) = tcx.fold_regions(data, |_, _| {
510+
self.next_nll_region_var_in_universe(
511+
NllRegionVariableOrigin::Existential { from_forall: false },
512+
ty::UniverseIndex::ROOT,
513+
)
514+
});
515+
516+
self.inject_new_hidden_type_unchecked(key, hidden_ty);
517+
}
518+
}
495519
}
496520

497-
impl<'cx, 'tcx> Deref for BorrowckInferCtxt<'cx, 'tcx> {
521+
impl<'tcx> Deref for BorrowckInferCtxt<'tcx> {
498522
type Target = InferCtxt<'tcx>;
499523

500-
fn deref(&self) -> &'cx Self::Target {
501-
self.infcx
524+
fn deref(&self) -> &Self::Target {
525+
&self.infcx
502526
}
503527
}
504528

505529
struct MirBorrowckCtxt<'cx, 'tcx> {
506-
infcx: &'cx BorrowckInferCtxt<'cx, 'tcx>,
530+
infcx: &'cx BorrowckInferCtxt<'tcx>,
507531
param_env: ParamEnv<'tcx>,
508532
body: &'cx Body<'tcx>,
509533
move_data: &'cx MoveData<'tcx>,

compiler/rustc_borrowck/src/nll.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub(crate) struct NllOutput<'tcx> {
5151
/// `compute_regions`.
5252
#[instrument(skip(infcx, param_env, body, promoted), level = "debug")]
5353
pub(crate) fn replace_regions_in_mir<'tcx>(
54-
infcx: &BorrowckInferCtxt<'_, 'tcx>,
54+
infcx: &BorrowckInferCtxt<'tcx>,
5555
param_env: ty::ParamEnv<'tcx>,
5656
body: &mut Body<'tcx>,
5757
promoted: &mut IndexSlice<Promoted, Body<'tcx>>,
@@ -75,7 +75,7 @@ pub(crate) fn replace_regions_in_mir<'tcx>(
7575
///
7676
/// This may result in errors being reported.
7777
pub(crate) fn compute_regions<'cx, 'tcx>(
78-
infcx: &BorrowckInferCtxt<'_, 'tcx>,
78+
infcx: &BorrowckInferCtxt<'tcx>,
7979
universal_regions: UniversalRegions<'tcx>,
8080
body: &Body<'tcx>,
8181
promoted: &IndexSlice<Promoted, Body<'tcx>>,
@@ -202,7 +202,7 @@ pub(crate) fn compute_regions<'cx, 'tcx>(
202202
}
203203

204204
pub(super) fn dump_mir_results<'tcx>(
205-
infcx: &BorrowckInferCtxt<'_, 'tcx>,
205+
infcx: &BorrowckInferCtxt<'tcx>,
206206
body: &Body<'tcx>,
207207
regioncx: &RegionInferenceContext<'tcx>,
208208
closure_region_requirements: &Option<ClosureRegionRequirements<'tcx>>,
@@ -254,7 +254,7 @@ pub(super) fn dump_mir_results<'tcx>(
254254
#[allow(rustc::diagnostic_outside_of_impl)]
255255
#[allow(rustc::untranslatable_diagnostic)]
256256
pub(super) fn dump_annotation<'tcx>(
257-
infcx: &BorrowckInferCtxt<'_, 'tcx>,
257+
infcx: &BorrowckInferCtxt<'tcx>,
258258
body: &Body<'tcx>,
259259
regioncx: &RegionInferenceContext<'tcx>,
260260
closure_region_requirements: &Option<ClosureRegionRequirements<'tcx>>,

compiler/rustc_borrowck/src/region_infer/mod.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,7 @@ pub enum ExtraConstraintInfo {
250250
}
251251

252252
#[instrument(skip(infcx, sccs), level = "debug")]
253-
fn sccs_info<'cx, 'tcx>(
254-
infcx: &'cx BorrowckInferCtxt<'cx, 'tcx>,
255-
sccs: Rc<Sccs<RegionVid, ConstraintSccIndex>>,
256-
) {
253+
fn sccs_info<'tcx>(infcx: &BorrowckInferCtxt<'tcx>, sccs: Rc<Sccs<RegionVid, ConstraintSccIndex>>) {
257254
use crate::renumber::RegionCtxt;
258255

259256
let var_to_origin = infcx.reg_var_to_origin.borrow();
@@ -322,8 +319,8 @@ impl<'tcx> RegionInferenceContext<'tcx> {
322319
///
323320
/// The `outlives_constraints` and `type_tests` are an initial set
324321
/// of constraints produced by the MIR type check.
325-
pub(crate) fn new<'cx>(
326-
_infcx: &BorrowckInferCtxt<'cx, 'tcx>,
322+
pub(crate) fn new(
323+
_infcx: &BorrowckInferCtxt<'tcx>,
327324
var_infos: VarInfos,
328325
universal_regions: Rc<UniversalRegions<'tcx>>,
329326
placeholder_indices: Rc<PlaceholderIndices>,

compiler/rustc_borrowck/src/renumber.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_span::Symbol;
1111
/// inference variables, returning the number of variables created.
1212
#[instrument(skip(infcx, body, promoted), level = "debug")]
1313
pub fn renumber_mir<'tcx>(
14-
infcx: &BorrowckInferCtxt<'_, 'tcx>,
14+
infcx: &BorrowckInferCtxt<'tcx>,
1515
body: &mut Body<'tcx>,
1616
promoted: &mut IndexSlice<Promoted, Body<'tcx>>,
1717
) {
@@ -57,7 +57,7 @@ impl RegionCtxt {
5757
}
5858

5959
struct RegionRenumberer<'a, 'tcx> {
60-
infcx: &'a BorrowckInferCtxt<'a, 'tcx>,
60+
infcx: &'a BorrowckInferCtxt<'tcx>,
6161
}
6262

6363
impl<'a, 'tcx> RegionRenumberer<'a, 'tcx> {

compiler/rustc_borrowck/src/type_check/mod.rs

+4-73
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use rustc_middle::mir::tcx::PlaceTy;
2424
use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
2525
use rustc_middle::mir::*;
2626
use rustc_middle::traits::query::NoSolution;
27-
use rustc_middle::traits::ObligationCause;
2827
use rustc_middle::ty::adjustment::PointerCoercion;
2928
use rustc_middle::ty::cast::CastTy;
3029
use rustc_middle::ty::visit::TypeVisitableExt;
@@ -122,7 +121,7 @@ mod relate_tys;
122121
/// - `move_data` -- move-data constructed when performing the maybe-init dataflow analysis
123122
/// - `elements` -- MIR region map
124123
pub(crate) fn type_check<'mir, 'tcx>(
125-
infcx: &BorrowckInferCtxt<'_, 'tcx>,
124+
infcx: &BorrowckInferCtxt<'tcx>,
126125
param_env: ty::ParamEnv<'tcx>,
127126
body: &Body<'tcx>,
128127
promoted: &IndexSlice<Promoted, Body<'tcx>>,
@@ -865,7 +864,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
865864
/// way, it accrues region constraints -- these can later be used by
866865
/// NLL region checking.
867866
struct TypeChecker<'a, 'tcx> {
868-
infcx: &'a BorrowckInferCtxt<'a, 'tcx>,
867+
infcx: &'a BorrowckInferCtxt<'tcx>,
869868
param_env: ty::ParamEnv<'tcx>,
870869
last_span: Span,
871870
body: &'a Body<'tcx>,
@@ -1020,15 +1019,15 @@ impl Locations {
10201019

10211020
impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
10221021
fn new(
1023-
infcx: &'a BorrowckInferCtxt<'a, 'tcx>,
1022+
infcx: &'a BorrowckInferCtxt<'tcx>,
10241023
body: &'a Body<'tcx>,
10251024
param_env: ty::ParamEnv<'tcx>,
10261025
region_bound_pairs: &'a RegionBoundPairs<'tcx>,
10271026
known_type_outlives_obligations: &'tcx [ty::PolyTypeOutlivesPredicate<'tcx>],
10281027
implicit_region_bound: ty::Region<'tcx>,
10291028
borrowck_context: &'a mut BorrowCheckContext<'a, 'tcx>,
10301029
) -> Self {
1031-
let mut checker = Self {
1030+
Self {
10321031
infcx,
10331032
last_span: body.span,
10341033
body,
@@ -1039,74 +1038,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
10391038
implicit_region_bound,
10401039
borrowck_context,
10411040
reported_errors: Default::default(),
1042-
};
1043-
1044-
// FIXME(-Znext-solver): A bit dubious that we're only registering
1045-
// predefined opaques in the typeck root.
1046-
if infcx.next_trait_solver() && !infcx.tcx.is_typeck_child(body.source.def_id()) {
1047-
checker.register_predefined_opaques_for_next_solver();
1048-
}
1049-
1050-
checker
1051-
}
1052-
1053-
pub(super) fn register_predefined_opaques_for_next_solver(&mut self) {
1054-
// OK to use the identity arguments for each opaque type key, since
1055-
// we remap opaques from HIR typeck back to their definition params.
1056-
let opaques: Vec<_> = self
1057-
.infcx
1058-
.tcx
1059-
.typeck(self.body.source.def_id().expect_local())
1060-
.concrete_opaque_types
1061-
.iter()
1062-
.map(|(k, v)| (*k, *v))
1063-
.collect();
1064-
1065-
let renumbered_opaques = self.infcx.tcx.fold_regions(opaques, |_, _| {
1066-
self.infcx.next_nll_region_var_in_universe(
1067-
NllRegionVariableOrigin::Existential { from_forall: false },
1068-
ty::UniverseIndex::ROOT,
1069-
)
1070-
});
1071-
1072-
let param_env = self.param_env;
1073-
let result = self.fully_perform_op(
1074-
Locations::All(self.body.span),
1075-
ConstraintCategory::OpaqueType,
1076-
CustomTypeOp::new(
1077-
|ocx| {
1078-
let mut obligations = Vec::new();
1079-
for (opaque_type_key, hidden_ty) in renumbered_opaques {
1080-
let cause = ObligationCause::dummy();
1081-
ocx.infcx.insert_hidden_type(
1082-
opaque_type_key,
1083-
&cause,
1084-
param_env,
1085-
hidden_ty.ty,
1086-
&mut obligations,
1087-
)?;
1088-
1089-
ocx.infcx.add_item_bounds_for_hidden_type(
1090-
opaque_type_key.def_id.to_def_id(),
1091-
opaque_type_key.args,
1092-
cause,
1093-
param_env,
1094-
hidden_ty.ty,
1095-
&mut obligations,
1096-
);
1097-
}
1098-
1099-
ocx.register_obligations(obligations);
1100-
Ok(())
1101-
},
1102-
"register pre-defined opaques",
1103-
),
1104-
);
1105-
1106-
if result.is_err() {
1107-
self.infcx
1108-
.dcx()
1109-
.span_bug(self.body.span, "failed re-defining predefined opaques in mir typeck");
11101041
}
11111042
}
11121043

compiler/rustc_borrowck/src/universal_regions.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl<'tcx> UniversalRegions<'tcx> {
240240
/// signature. This will also compute the relationships that are
241241
/// known between those regions.
242242
pub fn new(
243-
infcx: &BorrowckInferCtxt<'_, 'tcx>,
243+
infcx: &BorrowckInferCtxt<'tcx>,
244244
mir_def: LocalDefId,
245245
param_env: ty::ParamEnv<'tcx>,
246246
) -> Self {
@@ -411,7 +411,7 @@ impl<'tcx> UniversalRegions<'tcx> {
411411
}
412412

413413
struct UniversalRegionsBuilder<'cx, 'tcx> {
414-
infcx: &'cx BorrowckInferCtxt<'cx, 'tcx>,
414+
infcx: &'cx BorrowckInferCtxt<'tcx>,
415415
mir_def: LocalDefId,
416416
param_env: ty::ParamEnv<'tcx>,
417417
}
@@ -796,7 +796,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
796796
}
797797

798798
#[extension(trait InferCtxtExt<'tcx>)]
799-
impl<'cx, 'tcx> BorrowckInferCtxt<'cx, 'tcx> {
799+
impl<'tcx> BorrowckInferCtxt<'tcx> {
800800
#[instrument(skip(self), level = "debug")]
801801
fn replace_free_regions_with_nll_infer_vars<T>(
802802
&self,

compiler/rustc_infer/src/infer/opaque_types/mod.rs

+13
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,19 @@ impl<'tcx> InferCtxt<'tcx> {
485485
Ok(InferOk { value: (), obligations })
486486
}
487487

488+
/// Insert a hidden type into the opaque type storage, making sure
489+
/// it hasn't previously been defined. This does not emit any
490+
/// constraints and it's the responsibility of the caller to make
491+
/// sure that the item bounds of the opaque are checked.
492+
pub fn inject_new_hidden_type_unchecked(
493+
&self,
494+
opaque_type_key: OpaqueTypeKey<'tcx>,
495+
hidden_ty: OpaqueHiddenType<'tcx>,
496+
) {
497+
let prev = self.inner.borrow_mut().opaque_types().register(opaque_type_key, hidden_ty);
498+
assert_eq!(prev, None);
499+
}
500+
488501
/// Insert a hidden type into the opaque type storage, equating it
489502
/// with any previous entries if necessary.
490503
///

compiler/rustc_middle/src/traits/solve/inspect.rs

+2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ pub enum ProbeStep<'tcx> {
123123
/// used whenever there are multiple candidates to prove the
124124
/// current goalby .
125125
NestedProbe(Probe<'tcx>),
126+
/// A trait goal was satisfied by an impl candidate.
127+
RecordImplArgs { impl_args: CanonicalState<'tcx, ty::GenericArgsRef<'tcx>> },
126128
/// A call to `EvalCtxt::evaluate_added_goals_make_canonical_response` with
127129
/// `Certainty` was made. This is the certainty passed in, so it's not unified
128130
/// with the certainty of the `try_evaluate_added_goals` that is done within;

0 commit comments

Comments
 (0)