Skip to content

Commit 14caf73

Browse files
committed
Pull opaque type handling out of the type relating delegate
1 parent 1481fd9 commit 14caf73

File tree

3 files changed

+19
-31
lines changed

3 files changed

+19
-31
lines changed

compiler/rustc_borrowck/src/type_check/relate_tys.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_infer::infer::nll_relate::{NormalizationStrategy, TypeRelating, TypeRelatingDelegate};
22
use rustc_infer::infer::NllRegionVariableOrigin;
3-
use rustc_infer::traits::ObligationCause;
3+
use rustc_infer::traits::PredicateObligations;
44
use rustc_middle::mir::ConstraintCategory;
55
use rustc_middle::ty::error::TypeError;
66
use rustc_middle::ty::relate::TypeRelation;
@@ -155,27 +155,16 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx>
155155
true
156156
}
157157

158-
fn register_opaque_type(
158+
fn register_opaque_type_obligations(
159159
&mut self,
160-
a: Ty<'tcx>,
161-
b: Ty<'tcx>,
162-
a_is_expected: bool,
160+
obligations: PredicateObligations<'tcx>,
163161
) -> Result<(), TypeError<'tcx>> {
164-
let param_env = self.param_env();
165-
let span = self.span();
166-
let def_id = self.type_checker.body.source.def_id().expect_local();
167-
let body_id = self.type_checker.tcx().hir().local_def_id_to_hir_id(def_id);
168-
let cause = ObligationCause::misc(span, body_id);
169162
self.type_checker
170163
.fully_perform_op(
171164
self.locations,
172165
self.category,
173166
InstantiateOpaqueType {
174-
obligations: self
175-
.type_checker
176-
.infcx
177-
.handle_opaque_type(a, b, a_is_expected, &cause, param_env)?
178-
.obligations,
167+
obligations,
179168
// These fields are filled in during execution of the operation
180169
base_universe: None,
181170
region_constraints: None,

compiler/rustc_infer/src/infer/canonical/query_response.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use crate::infer::nll_relate::{NormalizationStrategy, TypeRelating, TypeRelating
1616
use crate::infer::region_constraints::{Constraint, RegionConstraintData};
1717
use crate::infer::{InferCtxt, InferOk, InferResult, NllRegionVariableOrigin};
1818
use crate::traits::query::{Fallible, NoSolution};
19-
use crate::traits::TraitEngine;
2019
use crate::traits::{Obligation, ObligationCause, PredicateObligation};
20+
use crate::traits::{PredicateObligations, TraitEngine};
2121
use rustc_data_structures::captures::Captures;
2222
use rustc_index::vec::Idx;
2323
use rustc_index::vec::IndexVec;
@@ -509,7 +509,7 @@ impl<'tcx> InferCtxt<'tcx> {
509509
for &(a, b) in &query_response.value.opaque_types {
510510
let a = substitute_value(self.tcx, &result_subst, a);
511511
let b = substitute_value(self.tcx, &result_subst, b);
512-
obligations.extend(self.handle_opaque_type(a, b, true, cause, param_env)?.obligations);
512+
obligations.extend(self.at(cause, param_env).eq(a, b)?.obligations);
513513
}
514514

515515
Ok(InferOk { value: result_subst, obligations })
@@ -741,17 +741,11 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for QueryTypeRelatingDelegate<'_, 'tcx> {
741741
true
742742
}
743743

744-
fn register_opaque_type(
744+
fn register_opaque_type_obligations(
745745
&mut self,
746-
a: Ty<'tcx>,
747-
b: Ty<'tcx>,
748-
a_is_expected: bool,
746+
obligations: PredicateObligations<'tcx>,
749747
) -> Result<(), TypeError<'tcx>> {
750-
self.obligations.extend(
751-
self.infcx
752-
.handle_opaque_type(a, b, a_is_expected, &self.cause, self.param_env)?
753-
.obligations,
754-
);
748+
self.obligations.extend(obligations);
755749
Ok(())
756750
}
757751
}

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ use crate::infer::combine::ConstEquateRelation;
2525
use crate::infer::InferCtxt;
2626
use crate::infer::{ConstVarValue, ConstVariableValue};
2727
use crate::infer::{TypeVariableOrigin, TypeVariableOriginKind};
28+
use crate::traits::PredicateObligation;
2829
use rustc_data_structures::fx::FxHashMap;
30+
use rustc_middle::traits::ObligationCause;
2931
use rustc_middle::ty::error::TypeError;
3032
use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation};
3133
use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
@@ -91,11 +93,9 @@ pub trait TypeRelatingDelegate<'tcx> {
9193
);
9294

9395
fn const_equate(&mut self, a: ty::Const<'tcx>, b: ty::Const<'tcx>);
94-
fn register_opaque_type(
96+
fn register_opaque_type_obligations(
9597
&mut self,
96-
a: Ty<'tcx>,
97-
b: Ty<'tcx>,
98-
a_is_expected: bool,
98+
obligations: Vec<PredicateObligation<'tcx>>,
9999
) -> Result<(), TypeError<'tcx>>;
100100

101101
/// Creates a new universe index. Used when instantiating placeholders.
@@ -414,7 +414,12 @@ where
414414
(_, &ty::Opaque(..)) => (generalize(a, true)?, b),
415415
_ => unreachable!(),
416416
};
417-
self.delegate.register_opaque_type(a, b, true)?;
417+
let cause = ObligationCause::dummy_with_span(self.delegate.span());
418+
let obligations = self
419+
.infcx
420+
.handle_opaque_type(a, b, true, &cause, self.delegate.param_env())?
421+
.obligations;
422+
self.delegate.register_opaque_type_obligations(obligations)?;
418423
trace!(a = ?a.kind(), b = ?b.kind(), "opaque type instantiated");
419424
Ok(a)
420425
}

0 commit comments

Comments
 (0)