Skip to content

Commit da71dfb

Browse files
Inline CombineFields
1 parent 690332a commit da71dfb

File tree

6 files changed

+174
-218
lines changed

6 files changed

+174
-218
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
///
@@ -108,14 +109,16 @@ impl<'a, 'tcx> At<'a, 'tcx> {
108109
where
109110
T: ToTrace<'tcx>,
110111
{
111-
let mut fields = CombineFields::new(
112+
let mut op = TypeRelating::new(
112113
self.infcx,
113114
ToTrace::to_trace(self.cause, expected, actual),
114115
self.param_env,
115116
define_opaque_types,
117+
StructurallyRelateAliases::No,
118+
ty::Contravariant,
116119
);
117-
fields.sup().relate(expected, actual)?;
118-
Ok(InferOk { value: (), obligations: fields.into_obligations() })
120+
op.relate(expected, actual)?;
121+
Ok(InferOk { value: (), obligations: op.into_obligations() })
119122
}
120123

121124
/// Makes `expected <: actual`.
@@ -128,14 +131,16 @@ impl<'a, 'tcx> At<'a, 'tcx> {
128131
where
129132
T: ToTrace<'tcx>,
130133
{
131-
let mut fields = CombineFields::new(
134+
let mut op = TypeRelating::new(
132135
self.infcx,
133136
ToTrace::to_trace(self.cause, expected, actual),
134137
self.param_env,
135138
define_opaque_types,
139+
StructurallyRelateAliases::No,
140+
ty::Covariant,
136141
);
137-
fields.sub().relate(expected, actual)?;
138-
Ok(InferOk { value: (), obligations: fields.into_obligations() })
142+
op.relate(expected, actual)?;
143+
Ok(InferOk { value: (), obligations: op.into_obligations() })
139144
}
140145

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

189187
/// Equates `expected` and `found` while structurally relating aliases.
@@ -198,14 +196,16 @@ impl<'a, 'tcx> At<'a, 'tcx> {
198196
T: ToTrace<'tcx>,
199197
{
200198
assert!(self.infcx.next_trait_solver());
201-
let mut fields = CombineFields::new(
199+
let mut op = TypeRelating::new(
202200
self.infcx,
203201
ToTrace::to_trace(self.cause, expected, actual),
204202
self.param_env,
205203
DefineOpaqueTypes::Yes,
204+
StructurallyRelateAliases::Yes,
205+
ty::Invariant,
206206
);
207-
fields.equate(StructurallyRelateAliases::Yes).relate(expected, actual)?;
208-
Ok(InferOk { value: (), obligations: fields.into_obligations() })
207+
op.relate(expected, actual)?;
208+
Ok(InferOk { value: (), obligations: op.into_obligations() })
209209
}
210210

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

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

279278
/// Computes the least-upper-bound, or mutual supertype, of two
@@ -290,14 +289,15 @@ impl<'a, 'tcx> At<'a, 'tcx> {
290289
where
291290
T: ToTrace<'tcx>,
292291
{
293-
let mut fields = CombineFields::new(
292+
let mut op = LatticeOp::new(
294293
self.infcx,
295294
ToTrace::to_trace(self.cause, expected, actual),
296295
self.param_env,
297296
define_opaque_types,
297+
LatticeOpKind::Lub,
298298
);
299-
let value = fields.lub().relate(expected, actual)?;
300-
Ok(InferOk { value, obligations: fields.into_obligations() })
299+
let value = op.relate(expected, actual)?;
300+
Ok(InferOk { value, obligations: op.into_obligations() })
301301
}
302302

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

compiler/rustc_infer/src/infer/mod.rs

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

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

+3-93
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! There are four type combiners: [TypeRelating], `Lub`, and `Glb`,
1+
//! There are four type combiners: `TypeRelating`, `Lub`, and `Glb`,
22
//! and `NllTypeRelating` in rustc_borrowck, which is only used for NLL.
33
//!
44
//! Each implements the trait [TypeRelation] and contains methods for
@@ -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(crate) 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(crate) 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(crate) fn tcx(&self) -> TyCtxt<'tcx> {
287-
self.infcx.tcx
288-
}
289-
290-
pub(crate) 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(crate) fn sub<'a>(&'a mut self) -> TypeRelating<'a, 'infcx, 'tcx> {
298-
TypeRelating::new(self, StructurallyRelateAliases::No, ty::Covariant)
299-
}
300-
301-
pub(crate) 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(crate) 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(crate) 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)