Skip to content

Commit 41a3960

Browse files
Make ObligationEmittingRelation deal with Goals only
1 parent 3dac780 commit 41a3960

File tree

7 files changed

+119
-60
lines changed

7 files changed

+119
-60
lines changed

compiler/rustc_borrowck/src/type_check/relate_tys.rs

+27-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use rustc_errors::ErrorGuaranteed;
33
use rustc_infer::infer::relate::{ObligationEmittingRelation, StructurallyRelateAliases};
44
use rustc_infer::infer::relate::{Relate, RelateResult, TypeRelation};
55
use rustc_infer::infer::NllRegionVariableOrigin;
6-
use rustc_infer::traits::{Obligation, PredicateObligation};
6+
use rustc_infer::traits::solve::Goal;
7+
use rustc_infer::traits::Obligation;
78
use rustc_middle::mir::ConstraintCategory;
89
use rustc_middle::span_bug;
910
use rustc_middle::traits::query::NoSolution;
@@ -154,8 +155,13 @@ impl<'me, 'bccx, 'tcx> NllTypeRelating<'me, 'bccx, 'tcx> {
154155
),
155156
};
156157
let cause = ObligationCause::dummy_with_span(self.span());
157-
let obligations = infcx.handle_opaque_type(a, b, &cause, self.param_env())?.obligations;
158-
self.register_obligations(obligations);
158+
self.register_obligations(
159+
infcx
160+
.handle_opaque_type(a, b, &cause, self.param_env())?
161+
.obligations
162+
.into_iter()
163+
.map(Goal::from),
164+
);
159165
Ok(())
160166
}
161167

@@ -550,22 +556,32 @@ impl<'bccx, 'tcx> ObligationEmittingRelation<'tcx> for NllTypeRelating<'_, 'bccx
550556
&mut self,
551557
obligations: impl IntoIterator<Item: ty::Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>>,
552558
) {
559+
let tcx = self.tcx();
560+
let param_env = self.param_env();
553561
self.register_obligations(
554-
obligations
555-
.into_iter()
556-
.map(|to_pred| {
557-
Obligation::new(self.tcx(), ObligationCause::dummy(), self.param_env(), to_pred)
558-
})
559-
.collect(),
562+
obligations.into_iter().map(|to_pred| Goal::new(tcx, param_env, to_pred)),
560563
);
561564
}
562565

563-
fn register_obligations(&mut self, obligations: Vec<PredicateObligation<'tcx>>) {
566+
fn register_obligations(
567+
&mut self,
568+
obligations: impl IntoIterator<Item = Goal<'tcx, ty::Predicate<'tcx>>>,
569+
) {
564570
let _: Result<_, ErrorGuaranteed> = self.type_checker.fully_perform_op(
565571
self.locations,
566572
self.category,
567573
InstantiateOpaqueType {
568-
obligations,
574+
obligations: obligations
575+
.into_iter()
576+
.map(|goal| {
577+
Obligation::new(
578+
self.tcx(),
579+
ObligationCause::dummy_with_span(self.span()),
580+
goal.param_env,
581+
goal.predicate,
582+
)
583+
})
584+
.collect(),
569585
// These fields are filled in during execution of the operation
570586
base_universe: None,
571587
region_constraints: None,

compiler/rustc_infer/src/infer/at.rs

+28-24
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ use crate::infer::relate::{Relate, StructurallyRelateAliases, TypeRelation};
3131
use rustc_middle::bug;
3232
use rustc_middle::ty::{Const, ImplSubject};
3333

34+
use crate::traits::Obligation;
35+
3436
/// Whether we should define opaque types or just treat them opaquely.
3537
///
3638
/// Currently only used to prevent predicate matching from matching anything
@@ -119,10 +121,8 @@ impl<'a, 'tcx> At<'a, 'tcx> {
119121
self.param_env,
120122
define_opaque_types,
121123
);
122-
fields
123-
.sup()
124-
.relate(expected, actual)
125-
.map(|_| InferOk { value: (), obligations: fields.obligations })
124+
fields.sup().relate(expected, actual)?;
125+
Ok(InferOk { value: (), obligations: fields.into_obligations() })
126126
}
127127

128128
/// Makes `expected <: actual`.
@@ -141,10 +141,8 @@ impl<'a, 'tcx> At<'a, 'tcx> {
141141
self.param_env,
142142
define_opaque_types,
143143
);
144-
fields
145-
.sub()
146-
.relate(expected, actual)
147-
.map(|_| InferOk { value: (), obligations: fields.obligations })
144+
fields.sub().relate(expected, actual)?;
145+
Ok(InferOk { value: (), obligations: fields.into_obligations() })
148146
}
149147

150148
/// Makes `expected == actual`.
@@ -163,10 +161,22 @@ impl<'a, 'tcx> At<'a, 'tcx> {
163161
self.param_env,
164162
define_opaque_types,
165163
);
166-
fields
167-
.equate(StructurallyRelateAliases::No)
168-
.relate(expected, actual)
169-
.map(|_| InferOk { value: (), obligations: fields.obligations })
164+
fields.equate(StructurallyRelateAliases::No).relate(expected, actual)?;
165+
Ok(InferOk {
166+
value: (),
167+
obligations: fields
168+
.obligations
169+
.into_iter()
170+
.map(|goal| {
171+
Obligation::new(
172+
self.infcx.tcx,
173+
fields.trace.cause.clone(),
174+
goal.param_env,
175+
goal.predicate,
176+
)
177+
})
178+
.collect(),
179+
})
170180
}
171181

172182
/// Equates `expected` and `found` while structurally relating aliases.
@@ -187,10 +197,8 @@ impl<'a, 'tcx> At<'a, 'tcx> {
187197
self.param_env,
188198
DefineOpaqueTypes::Yes,
189199
);
190-
fields
191-
.equate(StructurallyRelateAliases::Yes)
192-
.relate(expected, actual)
193-
.map(|_| InferOk { value: (), obligations: fields.obligations })
200+
fields.equate(StructurallyRelateAliases::Yes).relate(expected, actual)?;
201+
Ok(InferOk { value: (), obligations: fields.into_obligations() })
194202
}
195203

196204
pub fn relate<T>(
@@ -237,10 +245,8 @@ impl<'a, 'tcx> At<'a, 'tcx> {
237245
self.param_env,
238246
define_opaque_types,
239247
);
240-
fields
241-
.lub()
242-
.relate(expected, actual)
243-
.map(|value| InferOk { value, obligations: fields.obligations })
248+
let value = fields.lub().relate(expected, actual)?;
249+
Ok(InferOk { value, obligations: fields.into_obligations() })
244250
}
245251

246252
/// Computes the greatest-lower-bound, or mutual subtype, of two
@@ -261,10 +267,8 @@ impl<'a, 'tcx> At<'a, 'tcx> {
261267
self.param_env,
262268
define_opaque_types,
263269
);
264-
fields
265-
.glb()
266-
.relate(expected, actual)
267-
.map(|value| InferOk { value, obligations: fields.obligations })
270+
let value = fields.glb().relate(expected, actual)?;
271+
Ok(InferOk { value, obligations: fields.into_obligations() })
268272
}
269273
}
270274

compiler/rustc_infer/src/infer/relate/combine.rs

+29-6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use crate::infer::{DefineOpaqueTypes, InferCtxt, TypeTrace};
2828
use crate::traits::{Obligation, PredicateObligation};
2929
use rustc_middle::bug;
3030
use rustc_middle::infer::unify_key::EffectVarValue;
31+
use rustc_middle::traits::solve::Goal;
3132
use rustc_middle::ty::error::{ExpectedFound, TypeError};
3233
use rustc_middle::ty::{self, InferConst, Ty, TyCtxt, TypeVisitableExt, Upcast};
3334
use rustc_middle::ty::{IntType, UintType};
@@ -38,7 +39,7 @@ pub struct CombineFields<'infcx, 'tcx> {
3839
pub infcx: &'infcx InferCtxt<'tcx>,
3940
pub trace: TypeTrace<'tcx>,
4041
pub param_env: ty::ParamEnv<'tcx>,
41-
pub obligations: Vec<PredicateObligation<'tcx>>,
42+
pub obligations: Vec<Goal<'tcx, ty::Predicate<'tcx>>>,
4243
pub define_opaque_types: DefineOpaqueTypes,
4344
}
4445

@@ -51,6 +52,20 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
5152
) -> Self {
5253
Self { infcx, trace, param_env, define_opaque_types, obligations: vec![] }
5354
}
55+
56+
pub(crate) fn into_obligations(self) -> Vec<PredicateObligation<'tcx>> {
57+
self.obligations
58+
.into_iter()
59+
.map(|goal| {
60+
Obligation::new(
61+
self.infcx.tcx,
62+
self.trace.cause.clone(),
63+
goal.param_env,
64+
goal.predicate,
65+
)
66+
})
67+
.collect()
68+
}
5469
}
5570

5671
impl<'tcx> InferCtxt<'tcx> {
@@ -290,17 +305,22 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
290305
Glb::new(self)
291306
}
292307

293-
pub fn register_obligations(&mut self, obligations: Vec<PredicateObligation<'tcx>>) {
308+
pub fn register_obligations(
309+
&mut self,
310+
obligations: impl IntoIterator<Item = Goal<'tcx, ty::Predicate<'tcx>>>,
311+
) {
294312
self.obligations.extend(obligations);
295313
}
296314

297315
pub fn register_predicates(
298316
&mut self,
299317
obligations: impl IntoIterator<Item: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>>,
300318
) {
301-
self.obligations.extend(obligations.into_iter().map(|to_pred| {
302-
Obligation::new(self.infcx.tcx, self.trace.cause.clone(), self.param_env, to_pred)
303-
}))
319+
self.obligations.extend(
320+
obligations
321+
.into_iter()
322+
.map(|to_pred| Goal::new(self.infcx.tcx, self.param_env, to_pred)),
323+
)
304324
}
305325
}
306326

@@ -315,7 +335,10 @@ pub trait ObligationEmittingRelation<'tcx>: TypeRelation<TyCtxt<'tcx>> {
315335
fn structurally_relate_aliases(&self) -> StructurallyRelateAliases;
316336

317337
/// Register obligations that must hold in order for this relation to hold
318-
fn register_obligations(&mut self, obligations: Vec<PredicateObligation<'tcx>>);
338+
fn register_obligations(
339+
&mut self,
340+
obligations: impl IntoIterator<Item = Goal<'tcx, ty::Predicate<'tcx>>>,
341+
);
319342

320343
/// Register predicates that must hold in order for this relation to hold. Uses
321344
/// a default obligation cause, [`ObligationEmittingRelation::register_obligations`] should

compiler/rustc_infer/src/infer/relate/glb.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
//! Greatest lower bound. See [`lattice`].
22
3-
use super::{Relate, RelateResult, TypeRelation};
3+
use rustc_middle::traits::solve::Goal;
4+
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
45
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
56
use rustc_span::Span;
67

78
use super::combine::{CombineFields, ObligationEmittingRelation};
89
use super::lattice::{self, LatticeDir};
910
use super::StructurallyRelateAliases;
1011
use crate::infer::{DefineOpaqueTypes, InferCtxt, SubregionOrigin};
11-
use crate::traits::{ObligationCause, PredicateObligation};
12+
use crate::traits::ObligationCause;
1213

1314
/// "Greatest lower bound" (common subtype)
1415
pub struct Glb<'combine, 'infcx, 'tcx> {
@@ -147,7 +148,10 @@ impl<'tcx> ObligationEmittingRelation<'tcx> for Glb<'_, '_, 'tcx> {
147148
self.fields.register_predicates(obligations);
148149
}
149150

150-
fn register_obligations(&mut self, obligations: Vec<PredicateObligation<'tcx>>) {
151+
fn register_obligations(
152+
&mut self,
153+
obligations: impl IntoIterator<Item = Goal<'tcx, ty::Predicate<'tcx>>>,
154+
) {
151155
self.fields.register_obligations(obligations);
152156
}
153157

compiler/rustc_infer/src/infer/relate/lattice.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use super::combine::ObligationEmittingRelation;
2121
use crate::infer::{DefineOpaqueTypes, InferCtxt};
2222
use crate::traits::ObligationCause;
2323

24-
use super::RelateResult;
24+
use rustc_middle::traits::solve::Goal;
25+
use rustc_middle::ty::relate::RelateResult;
2526
use rustc_middle::ty::TyVar;
2627
use rustc_middle::ty::{self, Ty};
2728

@@ -109,7 +110,11 @@ where
109110
&& !this.infcx().next_trait_solver() =>
110111
{
111112
this.register_obligations(
112-
infcx.handle_opaque_type(a, b, this.cause(), this.param_env())?.obligations,
113+
infcx
114+
.handle_opaque_type(a, b, this.cause(), this.param_env())?
115+
.obligations
116+
.into_iter()
117+
.map(Goal::from),
113118
);
114119
Ok(a)
115120
}

compiler/rustc_infer/src/infer/relate/lub.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use super::combine::{CombineFields, ObligationEmittingRelation};
44
use super::lattice::{self, LatticeDir};
55
use super::StructurallyRelateAliases;
66
use crate::infer::{DefineOpaqueTypes, InferCtxt, SubregionOrigin};
7-
use crate::traits::{ObligationCause, PredicateObligation};
7+
use crate::traits::ObligationCause;
88

9-
use super::{Relate, RelateResult, TypeRelation};
9+
use rustc_middle::traits::solve::Goal;
10+
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
1011
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
1112
use rustc_span::Span;
1213

@@ -147,7 +148,10 @@ impl<'tcx> ObligationEmittingRelation<'tcx> for Lub<'_, '_, 'tcx> {
147148
self.fields.register_predicates(obligations);
148149
}
149150

150-
fn register_obligations(&mut self, obligations: Vec<PredicateObligation<'tcx>>) {
151+
fn register_obligations(
152+
&mut self,
153+
obligations: impl IntoIterator<Item = Goal<'tcx, ty::Predicate<'tcx>>>,
154+
) {
151155
self.fields.register_obligations(obligations)
152156
}
153157

compiler/rustc_infer/src/infer/relate/type_relating.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use super::combine::CombineFields;
2+
use crate::infer::relate::{ObligationEmittingRelation, StructurallyRelateAliases};
23
use crate::infer::BoundRegionConversionTime::HigherRankedType;
34
use crate::infer::{DefineOpaqueTypes, SubregionOrigin};
4-
use crate::traits::{Obligation, PredicateObligation};
5-
6-
use super::{
7-
relate_args_invariantly, relate_args_with_variances, ObligationEmittingRelation, Relate,
8-
RelateResult, StructurallyRelateAliases, TypeRelation,
5+
use rustc_middle::traits::solve::Goal;
6+
use rustc_middle::ty::relate::{
7+
relate_args_invariantly, relate_args_with_variances, Relate, RelateResult, TypeRelation,
98
};
109
use rustc_middle::ty::TyVar;
1110
use rustc_middle::ty::{self, Ty, TyCtxt};
@@ -88,9 +87,8 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for TypeRelating<'_, '_, 'tcx> {
8887
ty::Covariant => {
8988
// can't make progress on `A <: B` if both A and B are
9089
// type variables, so record an obligation.
91-
self.fields.obligations.push(Obligation::new(
90+
self.fields.obligations.push(Goal::new(
9291
self.tcx(),
93-
self.fields.trace.cause.clone(),
9492
self.fields.param_env,
9593
ty::Binder::dummy(ty::PredicateKind::Subtype(ty::SubtypePredicate {
9694
a_is_expected: true,
@@ -102,9 +100,8 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for TypeRelating<'_, '_, 'tcx> {
102100
ty::Contravariant => {
103101
// can't make progress on `B <: A` if both A and B are
104102
// type variables, so record an obligation.
105-
self.fields.obligations.push(Obligation::new(
103+
self.fields.obligations.push(Goal::new(
106104
self.tcx(),
107-
self.fields.trace.cause.clone(),
108105
self.fields.param_env,
109106
ty::Binder::dummy(ty::PredicateKind::Subtype(ty::SubtypePredicate {
110107
a_is_expected: false,
@@ -153,10 +150,13 @@ impl<'tcx> TypeRelation<TyCtxt<'tcx>> for TypeRelating<'_, '_, 'tcx> {
153150
&& def_id.is_local()
154151
&& !infcx.next_trait_solver() =>
155152
{
153+
// FIXME: Don't shuttle between Goal and Obligation
156154
self.fields.obligations.extend(
157155
infcx
158156
.handle_opaque_type(a, b, &self.fields.trace.cause, self.param_env())?
159-
.obligations,
157+
.obligations
158+
.into_iter()
159+
.map(Goal::from),
160160
);
161161
}
162162

@@ -318,7 +318,10 @@ impl<'tcx> ObligationEmittingRelation<'tcx> for TypeRelating<'_, '_, 'tcx> {
318318
self.fields.register_predicates(obligations);
319319
}
320320

321-
fn register_obligations(&mut self, obligations: Vec<PredicateObligation<'tcx>>) {
321+
fn register_obligations(
322+
&mut self,
323+
obligations: impl IntoIterator<Item = Goal<'tcx, ty::Predicate<'tcx>>>,
324+
) {
322325
self.fields.register_obligations(obligations);
323326
}
324327

0 commit comments

Comments
 (0)