Skip to content

Commit 980da66

Browse files
Add InferCtxt::register_hidden_type_in_new_solver
1 parent 97c11ff commit 980da66

File tree

5 files changed

+47
-41
lines changed

5 files changed

+47
-41
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use rustc_middle::mir::tcx::PlaceTy;
2626
use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
2727
use rustc_middle::mir::AssertKind;
2828
use rustc_middle::mir::*;
29-
use rustc_middle::traits::ObligationCause;
3029
use rustc_middle::ty::adjustment::PointerCast;
3130
use rustc_middle::ty::cast::CastTy;
3231
use rustc_middle::ty::subst::{SubstsRef, UserSubsts};

compiler/rustc_infer/src/infer/opaque_types.rs

+23
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,29 @@ impl<'tcx> InferCtxt<'tcx> {
557557
Ok(InferOk { value: (), obligations })
558558
}
559559

560+
/// Registers an opaque's hidden type -- only should be used when the opaque
561+
/// can be defined. For something more fallible -- checks the anchors, tries
562+
/// to unify opaques in both dirs, etc. -- use `InferCtxt::handle_opaque_type`.
563+
pub fn register_hidden_type_in_new_solver(
564+
&self,
565+
opaque_type_key: OpaqueTypeKey<'tcx>,
566+
param_env: ty::ParamEnv<'tcx>,
567+
hidden_ty: Ty<'tcx>,
568+
) -> InferResult<'tcx, ()> {
569+
assert!(self.tcx.trait_solver_next());
570+
let origin = self
571+
.opaque_type_origin(opaque_type_key.def_id)
572+
.expect("should be called for defining usages only");
573+
self.register_hidden_type(
574+
opaque_type_key,
575+
ObligationCause::dummy(),
576+
param_env,
577+
hidden_ty,
578+
origin,
579+
true,
580+
)
581+
}
582+
560583
pub fn add_item_bounds_for_hidden_type(
561584
&self,
562585
OpaqueTypeKey { def_id, substs }: OpaqueTypeKey<'tcx>,

compiler/rustc_trait_selection/src/solve/eval_ctxt.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_hir::def_id::DefId;
1+
use rustc_hir::def_id::{DefId, LocalDefId};
22
use rustc_infer::infer::at::ToTrace;
33
use rustc_infer::infer::canonical::CanonicalVarValues;
44
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
@@ -192,13 +192,7 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
192192

193193
for &(a, b) in &input.predefined_opaques_in_body.opaque_types {
194194
let InferOk { value: (), obligations } = infcx
195-
.handle_opaque_type(
196-
tcx.mk_opaque(a.def_id.to_def_id(), a.substs),
197-
b,
198-
true,
199-
&ObligationCause::dummy(),
200-
input.goal.param_env,
201-
)
195+
.register_hidden_type_in_new_solver(a, input.goal.param_env, b)
202196
.expect("expected opaque type instantiation to succeed");
203197
// We're only registering opaques already defined by the caller,
204198
// so we're not responsible for proving that they satisfy their
@@ -727,19 +721,18 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
727721
}
728722
}
729723

730-
pub(super) fn can_define_opaque_ty(&mut self, def_id: DefId) -> bool {
731-
let Some(def_id) = def_id.as_local() else { return false; };
724+
pub(super) fn can_define_opaque_ty(&mut self, def_id: LocalDefId) -> bool {
732725
self.infcx.opaque_type_origin(def_id).is_some()
733726
}
734727

735728
pub(super) fn register_opaque_ty(
736729
&mut self,
737-
a: Ty<'tcx>,
730+
a: ty::OpaqueTypeKey<'tcx>,
738731
b: Ty<'tcx>,
739732
param_env: ty::ParamEnv<'tcx>,
740733
) -> Result<(), NoSolution> {
741734
let InferOk { value: (), obligations } =
742-
self.infcx.handle_opaque_type(a, b, true, &ObligationCause::dummy(), param_env)?;
735+
self.infcx.register_hidden_type_in_new_solver(a, param_env, b)?;
743736
self.add_goals(obligations.into_iter().map(|obligation| obligation.into()));
744737
Ok(())
745738
}
@@ -749,17 +742,15 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
749742
pub(super) fn unify_existing_opaque_tys(
750743
&mut self,
751744
param_env: ty::ParamEnv<'tcx>,
752-
key: ty::AliasTy<'tcx>,
745+
key: ty::OpaqueTypeKey<'tcx>,
753746
ty: Ty<'tcx>,
754747
) -> Vec<CanonicalResponse<'tcx>> {
755-
let Some(def_id) = key.def_id.as_local() else { return vec![]; };
756-
757748
// FIXME: Super inefficient to be cloning this...
758749
let opaques = self.infcx.clone_opaque_types_for_query_response();
759750

760751
let mut values = vec![];
761752
for (candidate_key, candidate_ty) in opaques {
762-
if candidate_key.def_id != def_id {
753+
if candidate_key.def_id != key.def_id {
763754
continue;
764755
}
765756
values.extend(self.probe(|ecx| {

compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs

+10-21
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ use rustc_index::IndexVec;
1515
use rustc_infer::infer::canonical::query_response::make_query_region_constraints;
1616
use rustc_infer::infer::canonical::CanonicalVarValues;
1717
use rustc_infer::infer::canonical::{CanonicalExt, QueryRegionConstraints};
18+
use rustc_infer::infer::InferOk;
1819
use rustc_middle::traits::query::NoSolution;
1920
use rustc_middle::traits::solve::{
2021
ExternalConstraints, ExternalConstraintsData, MaybeCause, PredefinedOpaquesData, QueryInput,
2122
};
22-
use rustc_middle::traits::ObligationCause;
2323
use rustc_middle::ty::{self, BoundVar, GenericArgKind, Ty};
2424
use rustc_span::DUMMY_SP;
2525
use std::iter;
@@ -179,13 +179,12 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
179179
let Response { var_values, external_constraints, certainty } =
180180
response.substitute(self.tcx(), &substitution);
181181

182-
let mut nested_goals =
183-
self.unify_query_var_values(param_env, &original_values, var_values)?;
182+
let nested_goals = self.unify_query_var_values(param_env, &original_values, var_values)?;
184183

185184
let ExternalConstraintsData { region_constraints, opaque_types } =
186185
external_constraints.deref();
187186
self.register_region_constraints(region_constraints);
188-
nested_goals.extend(self.register_opaque_types(param_env, opaque_types)?);
187+
self.register_opaque_types(param_env, opaque_types)?;
189188

190189
Ok((certainty, nested_goals))
191190
}
@@ -310,24 +309,14 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
310309
&mut self,
311310
param_env: ty::ParamEnv<'tcx>,
312311
opaque_types: &[(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)],
313-
) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution> {
314-
let mut nested_goals = vec![];
312+
) -> Result<(), NoSolution> {
315313
for &(a, b) in opaque_types {
316-
nested_goals.extend(
317-
self.infcx
318-
.handle_opaque_type(
319-
self.tcx().mk_opaque(a.def_id.to_def_id(), a.substs),
320-
b,
321-
true,
322-
&ObligationCause::dummy(),
323-
param_env,
324-
)?
325-
.into_obligations()
326-
.into_iter()
327-
.map(Goal::from),
328-
);
314+
let InferOk { value: (), obligations } =
315+
self.infcx.register_hidden_type_in_new_solver(a, param_env, b)?;
316+
// It's sound to drop these obligations, since the normalizes-to goal
317+
// is responsible for proving these obligations.
318+
let _ = obligations;
329319
}
330-
331-
Ok(nested_goals)
320+
Ok(())
332321
}
333322
}

compiler/rustc_trait_selection/src/solve/opaques.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
1818
match goal.param_env.reveal() {
1919
Reveal::UserFacing => match self.solver_mode() {
2020
SolverMode::Normal => {
21+
let Some(opaque_ty_def_id) = opaque_ty.def_id.as_local() else {
22+
return Err(NoSolution);
23+
};
24+
let opaque_ty =
25+
ty::OpaqueTypeKey { def_id: opaque_ty_def_id, substs: opaque_ty.substs };
2126
// FIXME: at some point we should call queries without defining
2227
// new opaque types but having the existing opaque type definitions.
2328
// This will require moving this below "Prefer opaques registered already".
24-
if !self.can_define_opaque_ty(opaque_ty.def_id) {
29+
if !self.can_define_opaque_ty(opaque_ty_def_id) {
2530
return Err(NoSolution);
2631
}
2732
// FIXME: This may have issues when the substs contain aliases...
@@ -47,8 +52,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
4752
}
4853
}
4954
// Otherwise, define a new opaque type
50-
let opaque_ty = tcx.mk_opaque(opaque_ty.def_id, opaque_ty.substs);
51-
self.register_opaque_ty(expected, opaque_ty, goal.param_env)?;
55+
self.register_opaque_ty(opaque_ty, expected, goal.param_env)?;
5256
self.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
5357
}
5458
SolverMode::Coherence => {

0 commit comments

Comments
 (0)