Skip to content

Commit 1c8e75b

Browse files
Uplift super_combine
1 parent c21ba19 commit 1c8e75b

File tree

13 files changed

+379
-281
lines changed

13 files changed

+379
-281
lines changed

compiler/rustc_borrowck/src/type_check/relate_tys.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_middle::span_bug;
1111
use rustc_middle::traits::ObligationCause;
1212
use rustc_middle::traits::query::NoSolution;
1313
use rustc_middle::ty::fold::FnMutDelegate;
14+
use rustc_middle::ty::relate::combine::InferCtxtCombineExt;
1415
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
1516
use rustc_span::symbol::sym;
1617
use rustc_span::{Span, Symbol};

compiler/rustc_infer/src/infer/context.rs

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
///! Definition of `InferCtxtLike` from the librarified type layer.
22
use rustc_hir::def_id::{DefId, LocalDefId};
3+
use rustc_middle::infer::unify_key::EffectVarValue;
34
use rustc_middle::traits::ObligationCause;
45
use rustc_middle::traits::solve::{Goal, NoSolution, SolverMode};
56
use rustc_middle::ty::fold::TypeFoldable;
7+
use rustc_middle::ty::relate::combine::PredicateEmittingRelation;
8+
use rustc_middle::ty::relate::{Relate, RelateResult};
69
use rustc_middle::ty::{self, Ty, TyCtxt};
7-
use rustc_span::DUMMY_SP;
8-
use rustc_type_ir::InferCtxtLike;
9-
use rustc_type_ir::relate::Relate;
10+
use rustc_span::{DUMMY_SP, ErrorGuaranteed};
1011

1112
use super::{BoundRegionConversionTime, InferCtxt, SubregionOrigin};
1213

13-
impl<'tcx> InferCtxtLike for InferCtxt<'tcx> {
14+
impl<'tcx> rustc_type_ir::InferCtxtLike for InferCtxt<'tcx> {
1415
type Interner = TyCtxt<'tcx>;
1516

1617
fn cx(&self) -> TyCtxt<'tcx> {
1718
self.tcx
1819
}
1920

21+
fn next_trait_solver(&self) -> bool {
22+
self.next_trait_solver
23+
}
24+
2025
fn solver_mode(&self) -> ty::solve::SolverMode {
2126
match self.intercrate {
2227
true => SolverMode::Coherence,
@@ -131,6 +136,59 @@ impl<'tcx> InferCtxtLike for InferCtxt<'tcx> {
131136
self.enter_forall(value, f)
132137
}
133138

139+
fn equate_int_vids_raw(&self, a: rustc_type_ir::IntVid, b: rustc_type_ir::IntVid) {
140+
self.inner.borrow_mut().int_unification_table().union(a, b);
141+
}
142+
143+
fn equate_float_vids_raw(&self, a: rustc_type_ir::FloatVid, b: rustc_type_ir::FloatVid) {
144+
self.inner.borrow_mut().float_unification_table().union(a, b);
145+
}
146+
147+
fn equate_const_vids_raw(&self, a: rustc_type_ir::ConstVid, b: rustc_type_ir::ConstVid) {
148+
self.inner.borrow_mut().const_unification_table().union(a, b);
149+
}
150+
151+
fn equate_effect_vids_raw(&self, a: rustc_type_ir::EffectVid, b: rustc_type_ir::EffectVid) {
152+
self.inner.borrow_mut().effect_unification_table().union(a, b);
153+
}
154+
155+
fn instantiate_int_var_raw(
156+
&self,
157+
vid: rustc_type_ir::IntVid,
158+
value: rustc_type_ir::IntVarValue,
159+
) {
160+
self.inner.borrow_mut().int_unification_table().union_value(vid, value);
161+
}
162+
163+
fn instantiate_float_var_raw(
164+
&self,
165+
vid: rustc_type_ir::FloatVid,
166+
value: rustc_type_ir::FloatVarValue,
167+
) {
168+
self.inner.borrow_mut().float_unification_table().union_value(vid, value);
169+
}
170+
171+
fn instantiate_effect_var_raw(&self, vid: rustc_type_ir::EffectVid, value: ty::Const<'tcx>) {
172+
self.inner
173+
.borrow_mut()
174+
.effect_unification_table()
175+
.union_value(vid, EffectVarValue::Known(value));
176+
}
177+
178+
fn instantiate_const_var_raw<R: PredicateEmittingRelation<Self>>(
179+
&self,
180+
relation: &mut R,
181+
target_is_expected: bool,
182+
target_vid: rustc_type_ir::ConstVid,
183+
source_ct: ty::Const<'tcx>,
184+
) -> RelateResult<'tcx, ()> {
185+
self.instantiate_const_var(relation, target_is_expected, target_vid, source_ct)
186+
}
187+
188+
fn set_tainted_by_errors(&self, e: ErrorGuaranteed) {
189+
self.set_tainted_by_errors(e)
190+
}
191+
134192
fn relate<T: Relate<TyCtxt<'tcx>>>(
135193
&self,
136194
param_env: ty::ParamEnv<'tcx>,
@@ -154,6 +212,9 @@ impl<'tcx> InferCtxtLike for InferCtxt<'tcx> {
154212
fn shallow_resolve(&self, ty: Ty<'tcx>) -> Ty<'tcx> {
155213
self.shallow_resolve(ty)
156214
}
215+
fn shallow_resolve_const(&self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
216+
self.shallow_resolve_const(ct)
217+
}
157218

158219
fn resolve_vars_if_possible<T>(&self, value: T) -> T
159220
where

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

Lines changed: 4 additions & 224 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,14 @@
1818
//! On success, the LUB/GLB operations return the appropriate bound. The
1919
//! return value of `Equate` or `Sub` shouldn't really be used.
2020
21-
use rustc_middle::bug;
22-
use rustc_middle::infer::unify_key::EffectVarValue;
2321
use rustc_middle::traits::solve::Goal;
24-
use rustc_middle::ty::error::{ExpectedFound, TypeError};
25-
use rustc_middle::ty::{self, InferConst, IntType, Ty, TyCtxt, TypeVisitableExt, UintType, Upcast};
26-
pub use rustc_next_trait_solver::relate::combine::*;
27-
use tracing::debug;
22+
use rustc_middle::ty::relate::StructurallyRelateAliases;
23+
pub use rustc_middle::ty::relate::combine::*;
24+
use rustc_middle::ty::{self, TyCtxt, Upcast};
2825

2926
use super::lattice::{LatticeOp, LatticeOpKind};
3027
use super::type_relating::TypeRelating;
31-
use super::{RelateResult, StructurallyRelateAliases};
32-
use crate::infer::{DefineOpaqueTypes, InferCtxt, TypeTrace, relate};
28+
use crate::infer::{DefineOpaqueTypes, InferCtxt, TypeTrace};
3329
use crate::traits::{Obligation, PredicateObligation};
3430

3531
#[derive(Clone)]
@@ -71,222 +67,6 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
7167
}
7268
}
7369

74-
impl<'tcx> InferCtxt<'tcx> {
75-
pub fn super_combine_tys<R>(
76-
&self,
77-
relation: &mut R,
78-
a: Ty<'tcx>,
79-
b: Ty<'tcx>,
80-
) -> RelateResult<'tcx, Ty<'tcx>>
81-
where
82-
R: PredicateEmittingRelation<InferCtxt<'tcx>>,
83-
{
84-
debug!("super_combine_tys::<{}>({:?}, {:?})", std::any::type_name::<R>(), a, b);
85-
debug_assert!(!a.has_escaping_bound_vars());
86-
debug_assert!(!b.has_escaping_bound_vars());
87-
88-
match (a.kind(), b.kind()) {
89-
(&ty::Error(e), _) | (_, &ty::Error(e)) => {
90-
self.set_tainted_by_errors(e);
91-
return Ok(Ty::new_error(self.tcx, e));
92-
}
93-
94-
// Relate integral variables to other types
95-
(&ty::Infer(ty::IntVar(a_id)), &ty::Infer(ty::IntVar(b_id))) => {
96-
self.inner.borrow_mut().int_unification_table().union(a_id, b_id);
97-
Ok(a)
98-
}
99-
(&ty::Infer(ty::IntVar(v_id)), &ty::Int(v)) => {
100-
self.unify_integral_variable(v_id, IntType(v));
101-
Ok(b)
102-
}
103-
(&ty::Int(v), &ty::Infer(ty::IntVar(v_id))) => {
104-
self.unify_integral_variable(v_id, IntType(v));
105-
Ok(a)
106-
}
107-
(&ty::Infer(ty::IntVar(v_id)), &ty::Uint(v)) => {
108-
self.unify_integral_variable(v_id, UintType(v));
109-
Ok(b)
110-
}
111-
(&ty::Uint(v), &ty::Infer(ty::IntVar(v_id))) => {
112-
self.unify_integral_variable(v_id, UintType(v));
113-
Ok(a)
114-
}
115-
116-
// Relate floating-point variables to other types
117-
(&ty::Infer(ty::FloatVar(a_id)), &ty::Infer(ty::FloatVar(b_id))) => {
118-
self.inner.borrow_mut().float_unification_table().union(a_id, b_id);
119-
Ok(a)
120-
}
121-
(&ty::Infer(ty::FloatVar(v_id)), &ty::Float(v)) => {
122-
self.unify_float_variable(v_id, ty::FloatVarValue::Known(v));
123-
Ok(b)
124-
}
125-
(&ty::Float(v), &ty::Infer(ty::FloatVar(v_id))) => {
126-
self.unify_float_variable(v_id, ty::FloatVarValue::Known(v));
127-
Ok(a)
128-
}
129-
130-
// We don't expect `TyVar` or `Fresh*` vars at this point with lazy norm.
131-
(ty::Alias(..), ty::Infer(ty::TyVar(_))) | (ty::Infer(ty::TyVar(_)), ty::Alias(..))
132-
if self.next_trait_solver() =>
133-
{
134-
bug!(
135-
"We do not expect to encounter `TyVar` this late in combine \
136-
-- they should have been handled earlier"
137-
)
138-
}
139-
(_, ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)))
140-
| (ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)), _)
141-
if self.next_trait_solver() =>
142-
{
143-
bug!("We do not expect to encounter `Fresh` variables in the new solver")
144-
}
145-
146-
(_, ty::Alias(..)) | (ty::Alias(..), _) if self.next_trait_solver() => {
147-
match relation.structurally_relate_aliases() {
148-
StructurallyRelateAliases::Yes => {
149-
relate::structurally_relate_tys(relation, a, b)
150-
}
151-
StructurallyRelateAliases::No => {
152-
relation.register_alias_relate_predicate(a, b);
153-
Ok(a)
154-
}
155-
}
156-
}
157-
158-
// All other cases of inference are errors
159-
(&ty::Infer(_), _) | (_, &ty::Infer(_)) => {
160-
Err(TypeError::Sorts(ExpectedFound::new(true, a, b)))
161-
}
162-
163-
// During coherence, opaque types should be treated as *possibly*
164-
// equal to any other type (except for possibly itself). This is an
165-
// extremely heavy hammer, but can be relaxed in a forwards-compatible
166-
// way later.
167-
(&ty::Alias(ty::Opaque, _), _) | (_, &ty::Alias(ty::Opaque, _)) if self.intercrate => {
168-
relation.register_predicates([ty::Binder::dummy(ty::PredicateKind::Ambiguous)]);
169-
Ok(a)
170-
}
171-
172-
_ => relate::structurally_relate_tys(relation, a, b),
173-
}
174-
}
175-
176-
pub fn super_combine_consts<R>(
177-
&self,
178-
relation: &mut R,
179-
a: ty::Const<'tcx>,
180-
b: ty::Const<'tcx>,
181-
) -> RelateResult<'tcx, ty::Const<'tcx>>
182-
where
183-
R: PredicateEmittingRelation<InferCtxt<'tcx>>,
184-
{
185-
debug!("super_combine_consts::<{}>({:?}, {:?})", std::any::type_name::<R>(), a, b);
186-
debug_assert!(!a.has_escaping_bound_vars());
187-
debug_assert!(!b.has_escaping_bound_vars());
188-
189-
if a == b {
190-
return Ok(a);
191-
}
192-
193-
let a = self.shallow_resolve_const(a);
194-
let b = self.shallow_resolve_const(b);
195-
196-
match (a.kind(), b.kind()) {
197-
(
198-
ty::ConstKind::Infer(InferConst::Var(a_vid)),
199-
ty::ConstKind::Infer(InferConst::Var(b_vid)),
200-
) => {
201-
self.inner.borrow_mut().const_unification_table().union(a_vid, b_vid);
202-
Ok(a)
203-
}
204-
205-
(
206-
ty::ConstKind::Infer(InferConst::EffectVar(a_vid)),
207-
ty::ConstKind::Infer(InferConst::EffectVar(b_vid)),
208-
) => {
209-
self.inner.borrow_mut().effect_unification_table().union(a_vid, b_vid);
210-
Ok(a)
211-
}
212-
213-
// All other cases of inference with other variables are errors.
214-
(
215-
ty::ConstKind::Infer(InferConst::Var(_) | InferConst::EffectVar(_)),
216-
ty::ConstKind::Infer(_),
217-
)
218-
| (
219-
ty::ConstKind::Infer(_),
220-
ty::ConstKind::Infer(InferConst::Var(_) | InferConst::EffectVar(_)),
221-
) => {
222-
bug!(
223-
"tried to combine ConstKind::Infer/ConstKind::Infer(InferConst::Var): {a:?} and {b:?}"
224-
)
225-
}
226-
227-
(ty::ConstKind::Infer(InferConst::Var(vid)), _) => {
228-
self.instantiate_const_var(relation, true, vid, b)?;
229-
Ok(b)
230-
}
231-
232-
(_, ty::ConstKind::Infer(InferConst::Var(vid))) => {
233-
self.instantiate_const_var(relation, false, vid, a)?;
234-
Ok(a)
235-
}
236-
237-
(ty::ConstKind::Infer(InferConst::EffectVar(vid)), _) => {
238-
Ok(self.unify_effect_variable(vid, b))
239-
}
240-
241-
(_, ty::ConstKind::Infer(InferConst::EffectVar(vid))) => {
242-
Ok(self.unify_effect_variable(vid, a))
243-
}
244-
245-
(ty::ConstKind::Unevaluated(..), _) | (_, ty::ConstKind::Unevaluated(..))
246-
if self.tcx.features().generic_const_exprs || self.next_trait_solver() =>
247-
{
248-
match relation.structurally_relate_aliases() {
249-
StructurallyRelateAliases::No => {
250-
relation.register_predicates([if self.next_trait_solver() {
251-
ty::PredicateKind::AliasRelate(
252-
a.into(),
253-
b.into(),
254-
ty::AliasRelationDirection::Equate,
255-
)
256-
} else {
257-
ty::PredicateKind::ConstEquate(a, b)
258-
}]);
259-
260-
Ok(b)
261-
}
262-
StructurallyRelateAliases::Yes => {
263-
relate::structurally_relate_consts(relation, a, b)
264-
}
265-
}
266-
}
267-
_ => relate::structurally_relate_consts(relation, a, b),
268-
}
269-
}
270-
271-
#[inline(always)]
272-
fn unify_integral_variable(&self, vid: ty::IntVid, val: ty::IntVarValue) {
273-
self.inner.borrow_mut().int_unification_table().union_value(vid, val);
274-
}
275-
276-
#[inline(always)]
277-
fn unify_float_variable(&self, vid: ty::FloatVid, val: ty::FloatVarValue) {
278-
self.inner.borrow_mut().float_unification_table().union_value(vid, val);
279-
}
280-
281-
fn unify_effect_variable(&self, vid: ty::EffectVid, val: ty::Const<'tcx>) -> ty::Const<'tcx> {
282-
self.inner
283-
.borrow_mut()
284-
.effect_unification_table()
285-
.union_value(vid, EffectVarValue::Known(val));
286-
val
287-
}
288-
}
289-
29070
impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
29171
pub fn tcx(&self) -> TyCtxt<'tcx> {
29272
self.infcx.tcx

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl<'tcx> InferCtxt<'tcx> {
181181
///
182182
/// See `tests/ui/const-generics/occurs-check/` for more examples where this is relevant.
183183
#[instrument(level = "debug", skip(self, relation))]
184-
pub(super) fn instantiate_const_var<R: PredicateEmittingRelation<InferCtxt<'tcx>>>(
184+
pub(crate) fn instantiate_const_var<R: PredicateEmittingRelation<InferCtxt<'tcx>>>(
185185
&self,
186186
relation: &mut R,
187187
target_is_expected: bool,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
//! [lattices]: https://en.wikipedia.org/wiki/Lattice_(order)
1919
2020
use rustc_middle::traits::solve::Goal;
21+
use rustc_middle::ty::relate::combine::InferCtxtCombineExt;
2122
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
2223
use rustc_middle::ty::{self, Ty, TyCtxt, TyVar, TypeVisitableExt};
2324
use rustc_span::Span;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! As well as the implementation of `Relate` for interned things (`Ty`/`Const`/etc).
44
55
pub use rustc_middle::ty::relate::RelateResult;
6-
pub use rustc_next_trait_solver::relate::*;
6+
pub use rustc_type_ir::relate::*;
77

88
pub use self::combine::{CombineFields, PredicateEmittingRelation};
99

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_middle::traits::solve::Goal;
2+
use rustc_middle::ty::relate::combine::InferCtxtCombineExt;
23
use rustc_middle::ty::relate::{
34
Relate, RelateResult, TypeRelation, relate_args_invariantly, relate_args_with_variances,
45
};

compiler/rustc_next_trait_solver/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@
1212
pub mod canonicalizer;
1313
pub mod coherence;
1414
pub mod delegate;
15-
pub mod relate;
1615
pub mod resolve;
1716
pub mod solve;

0 commit comments

Comments
 (0)