Skip to content

Commit c616402

Browse files
Inline CombineFields
1 parent 1b3b8e7 commit c616402

File tree

6 files changed

+179
-219
lines changed

6 files changed

+179
-219
lines changed

compiler/rustc_infer/src/infer/at.rs

+45-44
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
//! sometimes useful when the types of `c` and `d` are not traceable
2626
//! things. (That system should probably be refactored.)
2727
28+
use relate::lattice::{LatticeOp, LatticeOpKind};
2829
use rustc_middle::bug;
2930
use rustc_middle::ty::{Const, ImplSubject};
3031

3132
use super::*;
33+
use crate::infer::relate::type_relating::TypeRelating;
3234
use crate::infer::relate::{Relate, StructurallyRelateAliases, TypeRelation};
33-
use crate::traits::Obligation;
3435

3536
/// Whether we should define opaque types or just treat them opaquely.
3637
///
@@ -109,14 +110,16 @@ impl<'a, 'tcx> At<'a, 'tcx> {
109110
where
110111
T: ToTrace<'tcx>,
111112
{
112-
let mut fields = CombineFields::new(
113+
let mut op = TypeRelating::new(
113114
self.infcx,
114115
ToTrace::to_trace(self.cause, expected, actual),
115116
self.param_env,
116117
define_opaque_types,
118+
StructurallyRelateAliases::No,
119+
ty::Contravariant,
117120
);
118-
fields.sup().relate(expected, actual)?;
119-
Ok(InferOk { value: (), obligations: fields.into_obligations() })
121+
op.relate(expected, actual)?;
122+
Ok(InferOk { value: (), obligations: op.into_obligations() })
120123
}
121124

122125
/// Makes `expected <: actual`.
@@ -129,14 +132,16 @@ impl<'a, 'tcx> At<'a, 'tcx> {
129132
where
130133
T: ToTrace<'tcx>,
131134
{
132-
let mut fields = CombineFields::new(
135+
let mut op = TypeRelating::new(
133136
self.infcx,
134137
ToTrace::to_trace(self.cause, expected, actual),
135138
self.param_env,
136139
define_opaque_types,
140+
StructurallyRelateAliases::No,
141+
ty::Covariant,
137142
);
138-
fields.sub().relate(expected, actual)?;
139-
Ok(InferOk { value: (), obligations: fields.into_obligations() })
143+
op.relate(expected, actual)?;
144+
Ok(InferOk { value: (), obligations: op.into_obligations() })
140145
}
141146

142147
/// Makes `expected == actual`.
@@ -168,23 +173,16 @@ impl<'a, 'tcx> At<'a, 'tcx> {
168173
where
169174
T: Relate<TyCtxt<'tcx>>,
170175
{
171-
let mut fields = CombineFields::new(self.infcx, trace, self.param_env, define_opaque_types);
172-
fields.equate(StructurallyRelateAliases::No).relate(expected, actual)?;
173-
Ok(InferOk {
174-
value: (),
175-
obligations: fields
176-
.goals
177-
.into_iter()
178-
.map(|goal| {
179-
Obligation::new(
180-
self.infcx.tcx,
181-
fields.trace.cause.clone(),
182-
goal.param_env,
183-
goal.predicate,
184-
)
185-
})
186-
.collect(),
187-
})
176+
let mut op = TypeRelating::new(
177+
self.infcx,
178+
trace,
179+
self.param_env,
180+
define_opaque_types,
181+
StructurallyRelateAliases::No,
182+
ty::Invariant,
183+
);
184+
op.relate(expected, actual)?;
185+
Ok(InferOk { value: (), obligations: op.into_obligations() })
188186
}
189187

190188
/// Equates `expected` and `found` while structurally relating aliases.
@@ -199,14 +197,16 @@ impl<'a, 'tcx> At<'a, 'tcx> {
199197
T: ToTrace<'tcx>,
200198
{
201199
assert!(self.infcx.next_trait_solver());
202-
let mut fields = CombineFields::new(
200+
let mut op = TypeRelating::new(
203201
self.infcx,
204202
ToTrace::to_trace(self.cause, expected, actual),
205203
self.param_env,
206204
DefineOpaqueTypes::Yes,
205+
StructurallyRelateAliases::Yes,
206+
ty::Invariant,
207207
);
208-
fields.equate(StructurallyRelateAliases::Yes).relate(expected, actual)?;
209-
Ok(InferOk { value: (), obligations: fields.into_obligations() })
208+
op.relate(expected, actual)?;
209+
Ok(InferOk { value: (), obligations: op.into_obligations() })
210210
}
211211

212212
pub fn relate<T>(
@@ -243,19 +243,16 @@ impl<'a, 'tcx> At<'a, 'tcx> {
243243
where
244244
T: Relate<TyCtxt<'tcx>>,
245245
{
246-
let mut fields = CombineFields::new(
246+
let mut op = TypeRelating::new(
247247
self.infcx,
248248
TypeTrace::dummy(self.cause),
249249
self.param_env,
250250
DefineOpaqueTypes::Yes,
251-
);
252-
fields.sub().relate_with_variance(
251+
StructurallyRelateAliases::No,
253252
variance,
254-
ty::VarianceDiagInfo::default(),
255-
expected,
256-
actual,
257-
)?;
258-
Ok(fields.goals)
253+
);
254+
op.relate(expected, actual)?;
255+
Ok(op.into_obligations().into_iter().map(|o| o.into()).collect())
259256
}
260257

261258
/// Used in the new solver since we don't care about tracking an `ObligationCause`.
@@ -267,14 +264,16 @@ impl<'a, 'tcx> At<'a, 'tcx> {
267264
where
268265
T: Relate<TyCtxt<'tcx>>,
269266
{
270-
let mut fields = CombineFields::new(
267+
let mut op = TypeRelating::new(
271268
self.infcx,
272269
TypeTrace::dummy(self.cause),
273270
self.param_env,
274271
DefineOpaqueTypes::Yes,
272+
StructurallyRelateAliases::Yes,
273+
ty::Invariant,
275274
);
276-
fields.equate(StructurallyRelateAliases::Yes).relate(expected, actual)?;
277-
Ok(fields.goals)
275+
op.relate(expected, actual)?;
276+
Ok(op.into_obligations().into_iter().map(|o| o.into()).collect())
278277
}
279278

280279
/// Computes the least-upper-bound, or mutual supertype, of two
@@ -291,14 +290,15 @@ impl<'a, 'tcx> At<'a, 'tcx> {
291290
where
292291
T: ToTrace<'tcx>,
293292
{
294-
let mut fields = CombineFields::new(
293+
let mut op = LatticeOp::new(
295294
self.infcx,
296295
ToTrace::to_trace(self.cause, expected, actual),
297296
self.param_env,
298297
define_opaque_types,
298+
LatticeOpKind::Lub,
299299
);
300-
let value = fields.lub().relate(expected, actual)?;
301-
Ok(InferOk { value, obligations: fields.into_obligations() })
300+
let value = op.relate(expected, actual)?;
301+
Ok(InferOk { value, obligations: op.into_obligations() })
302302
}
303303

304304
/// Computes the greatest-lower-bound, or mutual subtype, of two
@@ -313,14 +313,15 @@ impl<'a, 'tcx> At<'a, 'tcx> {
313313
where
314314
T: ToTrace<'tcx>,
315315
{
316-
let mut fields = CombineFields::new(
316+
let mut op = LatticeOp::new(
317317
self.infcx,
318318
ToTrace::to_trace(self.cause, expected, actual),
319319
self.param_env,
320320
define_opaque_types,
321+
LatticeOpKind::Glb,
321322
);
322-
let value = fields.glb().relate(expected, actual)?;
323-
Ok(InferOk { value, obligations: fields.into_obligations() })
323+
let value = op.relate(expected, actual)?;
324+
Ok(InferOk { value, obligations: op.into_obligations() })
324325
}
325326
}
326327

compiler/rustc_infer/src/infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use region_constraints::{
1414
GenericKind, RegionConstraintCollector, RegionConstraintStorage, VarInfos, VerifyBound,
1515
};
1616
pub use relate::StructurallyRelateAliases;
17-
pub use relate::combine::{CombineFields, PredicateEmittingRelation};
17+
pub use relate::combine::PredicateEmittingRelation;
1818
use rustc_data_structures::captures::Captures;
1919
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
2020
use rustc_data_structures::sync::Lrc;

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

+2-92
Original file line numberDiff line numberDiff line change
@@ -20,56 +20,13 @@
2020
2121
use rustc_middle::bug;
2222
use rustc_middle::infer::unify_key::EffectVarValue;
23-
use rustc_middle::traits::solve::Goal;
2423
use rustc_middle::ty::error::{ExpectedFound, TypeError};
25-
use rustc_middle::ty::{self, InferConst, IntType, Ty, TyCtxt, TypeVisitableExt, UintType, Upcast};
24+
use rustc_middle::ty::{self, InferConst, IntType, Ty, TypeVisitableExt, UintType};
2625
pub use rustc_next_trait_solver::relate::combine::*;
2726
use tracing::debug;
2827

29-
use super::lattice::{LatticeOp, LatticeOpKind};
30-
use super::type_relating::TypeRelating;
3128
use super::{RelateResult, StructurallyRelateAliases};
32-
use crate::infer::{DefineOpaqueTypes, InferCtxt, TypeTrace, relate};
33-
use crate::traits::{Obligation, PredicateObligation};
34-
35-
#[derive(Clone)]
36-
pub struct CombineFields<'infcx, 'tcx> {
37-
pub infcx: &'infcx InferCtxt<'tcx>,
38-
// Immutable fields
39-
pub trace: TypeTrace<'tcx>,
40-
pub param_env: ty::ParamEnv<'tcx>,
41-
pub define_opaque_types: DefineOpaqueTypes,
42-
// Mutable fields
43-
//
44-
// Adding any additional field likely requires
45-
// changes to the cache of `TypeRelating`.
46-
pub goals: Vec<Goal<'tcx, ty::Predicate<'tcx>>>,
47-
}
48-
49-
impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
50-
pub fn new(
51-
infcx: &'infcx InferCtxt<'tcx>,
52-
trace: TypeTrace<'tcx>,
53-
param_env: ty::ParamEnv<'tcx>,
54-
define_opaque_types: DefineOpaqueTypes,
55-
) -> Self {
56-
Self { infcx, trace, param_env, define_opaque_types, goals: vec![] }
57-
}
58-
59-
pub(crate) fn into_obligations(self) -> Vec<PredicateObligation<'tcx>> {
60-
self.goals
61-
.into_iter()
62-
.map(|goal| {
63-
Obligation::new(
64-
self.infcx.tcx,
65-
self.trace.cause.clone(),
66-
goal.param_env,
67-
goal.predicate,
68-
)
69-
})
70-
.collect()
71-
}
72-
}
29+
use crate::infer::{InferCtxt, relate};
7330

7431
impl<'tcx> InferCtxt<'tcx> {
7532
pub fn super_combine_tys<R>(
@@ -281,50 +238,3 @@ impl<'tcx> InferCtxt<'tcx> {
281238
val
282239
}
283240
}
284-
285-
impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
286-
pub fn tcx(&self) -> TyCtxt<'tcx> {
287-
self.infcx.tcx
288-
}
289-
290-
pub fn equate<'a>(
291-
&'a mut self,
292-
structurally_relate_aliases: StructurallyRelateAliases,
293-
) -> TypeRelating<'a, 'infcx, 'tcx> {
294-
TypeRelating::new(self, structurally_relate_aliases, ty::Invariant)
295-
}
296-
297-
pub fn sub<'a>(&'a mut self) -> TypeRelating<'a, 'infcx, 'tcx> {
298-
TypeRelating::new(self, StructurallyRelateAliases::No, ty::Covariant)
299-
}
300-
301-
pub fn sup<'a>(&'a mut self) -> TypeRelating<'a, 'infcx, 'tcx> {
302-
TypeRelating::new(self, StructurallyRelateAliases::No, ty::Contravariant)
303-
}
304-
305-
pub(crate) fn lub<'a>(&'a mut self) -> LatticeOp<'a, 'infcx, 'tcx> {
306-
LatticeOp::new(self, LatticeOpKind::Lub)
307-
}
308-
309-
pub(crate) fn glb<'a>(&'a mut self) -> LatticeOp<'a, 'infcx, 'tcx> {
310-
LatticeOp::new(self, LatticeOpKind::Glb)
311-
}
312-
313-
pub fn register_obligations(
314-
&mut self,
315-
obligations: impl IntoIterator<Item = Goal<'tcx, ty::Predicate<'tcx>>>,
316-
) {
317-
self.goals.extend(obligations);
318-
}
319-
320-
pub fn register_predicates(
321-
&mut self,
322-
obligations: impl IntoIterator<Item: Upcast<TyCtxt<'tcx>, ty::Predicate<'tcx>>>,
323-
) {
324-
self.goals.extend(
325-
obligations
326-
.into_iter()
327-
.map(|to_pred| Goal::new(self.infcx.tcx, self.param_env, to_pred)),
328-
)
329-
}
330-
}

0 commit comments

Comments
 (0)