Skip to content

Commit 9580eab

Browse files
committed
Auto merge of rust-lang#131343 - compiler-errors:remove-combine-fields, r=<try>
Remove `CombineFields` This conflicts with rust-lang#131263, but if this one lands first then perhaps rust-lang#131263 could then go ahead and remove all the branching on solver in `TypeRelating`. We could perhaps then rename `TypeRelating` to `OldSolverRelating` or something, idk. r? lcnr
2 parents 690332a + 0c5d2f9 commit 9580eab

File tree

7 files changed

+168
-281
lines changed

7 files changed

+168
-281
lines changed

compiler/rustc_hir_typeck/src/coercion.rs

+7-19
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
141141
let at = self.at(&self.cause, self.fcx.param_env);
142142

143143
let res = if self.use_lub {
144-
at.lub(DefineOpaqueTypes::Yes, b, a)
144+
at.lub(b, a)
145145
} else {
146146
at.sup(DefineOpaqueTypes::Yes, b, a)
147147
.map(|InferOk { value: (), obligations }| InferOk { value: b, obligations })
@@ -1190,13 +1190,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11901190
(ty::FnDef(..), ty::FnDef(..)) => {
11911191
// Don't reify if the function types have a LUB, i.e., they
11921192
// are the same function and their parameters have a LUB.
1193-
match self.commit_if_ok(|_| {
1194-
self.at(cause, self.param_env).lub(
1195-
DefineOpaqueTypes::Yes,
1196-
prev_ty,
1197-
new_ty,
1198-
)
1199-
}) {
1193+
match self
1194+
.commit_if_ok(|_| self.at(cause, self.param_env).lub(prev_ty, new_ty))
1195+
{
12001196
// We have a LUB of prev_ty and new_ty, just return it.
12011197
Ok(ok) => return Ok(self.register_infer_ok_obligations(ok)),
12021198
Err(_) => {
@@ -1239,7 +1235,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12391235
let (a_sig, b_sig) = self.normalize(new.span, (a_sig, b_sig));
12401236
let sig = self
12411237
.at(cause, self.param_env)
1242-
.lub(DefineOpaqueTypes::Yes, a_sig, b_sig)
1238+
.lub(a_sig, b_sig)
12431239
.map(|ok| self.register_infer_ok_obligations(ok))?;
12441240

12451241
// Reify both sides and return the reified fn pointer type.
@@ -1330,9 +1326,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13301326
);
13311327

13321328
return Err(self
1333-
.commit_if_ok(|_| {
1334-
self.at(cause, self.param_env).lub(DefineOpaqueTypes::Yes, prev_ty, new_ty)
1335-
})
1329+
.commit_if_ok(|_| self.at(cause, self.param_env).lub(prev_ty, new_ty))
13361330
.unwrap_err());
13371331
}
13381332
}
@@ -1344,13 +1338,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13441338
Err(e)
13451339
} else {
13461340
Err(self
1347-
.commit_if_ok(|_| {
1348-
self.at(cause, self.param_env).lub(
1349-
DefineOpaqueTypes::Yes,
1350-
prev_ty,
1351-
new_ty,
1352-
)
1353-
})
1341+
.commit_if_ok(|_| self.at(cause, self.param_env).lub(prev_ty, new_ty))
13541342
.unwrap_err())
13551343
}
13561344
}

compiler/rustc_infer/src/infer/at.rs

+34-86
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,45 +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-
})
187-
}
188-
189-
/// Equates `expected` and `found` while structurally relating aliases.
190-
/// This should only be used inside of the next generation trait solver
191-
/// when relating rigid aliases.
192-
pub fn eq_structurally_relating_aliases<T>(
193-
self,
194-
expected: T,
195-
actual: T,
196-
) -> InferResult<'tcx, ()>
197-
where
198-
T: ToTrace<'tcx>,
199-
{
200-
assert!(self.infcx.next_trait_solver());
201-
let mut fields = CombineFields::new(
175+
let mut op = TypeRelating::new(
202176
self.infcx,
203-
ToTrace::to_trace(self.cause, expected, actual),
177+
trace,
204178
self.param_env,
205-
DefineOpaqueTypes::Yes,
179+
define_opaque_types,
180+
StructurallyRelateAliases::No,
181+
ty::Invariant,
206182
);
207-
fields.equate(StructurallyRelateAliases::Yes).relate(expected, actual)?;
208-
Ok(InferOk { value: (), obligations: fields.into_obligations() })
183+
op.relate(expected, actual)?;
184+
Ok(InferOk { value: (), obligations: op.into_obligations() })
209185
}
210186

211187
pub fn relate<T>(
@@ -242,19 +218,16 @@ impl<'a, 'tcx> At<'a, 'tcx> {
242218
where
243219
T: Relate<TyCtxt<'tcx>>,
244220
{
245-
let mut fields = CombineFields::new(
221+
let mut op = TypeRelating::new(
246222
self.infcx,
247223
TypeTrace::dummy(self.cause),
248224
self.param_env,
249225
DefineOpaqueTypes::Yes,
250-
);
251-
fields.sub().relate_with_variance(
226+
StructurallyRelateAliases::No,
252227
variance,
253-
ty::VarianceDiagInfo::default(),
254-
expected,
255-
actual,
256-
)?;
257-
Ok(fields.goals)
228+
);
229+
op.relate(expected, actual)?;
230+
Ok(op.into_obligations().into_iter().map(|o| o.into()).collect())
258231
}
259232

260233
/// Used in the new solver since we don't care about tracking an `ObligationCause`.
@@ -266,60 +239,35 @@ impl<'a, 'tcx> At<'a, 'tcx> {
266239
where
267240
T: Relate<TyCtxt<'tcx>>,
268241
{
269-
let mut fields = CombineFields::new(
242+
let mut op = TypeRelating::new(
270243
self.infcx,
271244
TypeTrace::dummy(self.cause),
272245
self.param_env,
273246
DefineOpaqueTypes::Yes,
247+
StructurallyRelateAliases::Yes,
248+
ty::Invariant,
274249
);
275-
fields.equate(StructurallyRelateAliases::Yes).relate(expected, actual)?;
276-
Ok(fields.goals)
250+
op.relate(expected, actual)?;
251+
Ok(op.into_obligations().into_iter().map(|o| o.into()).collect())
277252
}
278253

279254
/// Computes the least-upper-bound, or mutual supertype, of two
280255
/// values. The order of the arguments doesn't matter, but since
281256
/// this can result in an error (e.g., if asked to compute LUB of
282257
/// u32 and i32), it is meaningful to call one of them the
283258
/// "expected type".
284-
pub fn lub<T>(
285-
self,
286-
define_opaque_types: DefineOpaqueTypes,
287-
expected: T,
288-
actual: T,
289-
) -> InferResult<'tcx, T>
259+
pub fn lub<T>(self, expected: T, actual: T) -> InferResult<'tcx, T>
290260
where
291261
T: ToTrace<'tcx>,
292262
{
293-
let mut fields = CombineFields::new(
263+
let mut op = LatticeOp::new(
294264
self.infcx,
295265
ToTrace::to_trace(self.cause, expected, actual),
296266
self.param_env,
297-
define_opaque_types,
298-
);
299-
let value = fields.lub().relate(expected, actual)?;
300-
Ok(InferOk { value, obligations: fields.into_obligations() })
301-
}
302-
303-
/// Computes the greatest-lower-bound, or mutual subtype, of two
304-
/// values. As with `lub` order doesn't matter, except for error
305-
/// cases.
306-
pub fn glb<T>(
307-
self,
308-
define_opaque_types: DefineOpaqueTypes,
309-
expected: T,
310-
actual: T,
311-
) -> InferResult<'tcx, T>
312-
where
313-
T: ToTrace<'tcx>,
314-
{
315-
let mut fields = CombineFields::new(
316-
self.infcx,
317-
ToTrace::to_trace(self.cause, expected, actual),
318-
self.param_env,
319-
define_opaque_types,
267+
LatticeOpKind::Lub,
320268
);
321-
let value = fields.glb().relate(expected, actual)?;
322-
Ok(InferOk { value, obligations: fields.into_obligations() })
269+
let value = op.relate(expected, actual)?;
270+
Ok(InferOk { value, obligations: op.into_obligations() })
323271
}
324272
}
325273

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)