Skip to content

Commit 49d69f4

Browse files
Uplift super_combine
1 parent 6141ac4 commit 49d69f4

File tree

15 files changed

+381
-284
lines changed

15 files changed

+381
-284
lines changed

compiler/rustc_borrowck/src/type_check/relate_tys.rs

+1
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

+65-4
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

+4-224
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,15 @@
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+
pub use rustc_middle::ty::relate::combine::*;
23+
use rustc_middle::ty::{self, TyCtxt, Upcast};
2824

25+
use super::StructurallyRelateAliases;
2926
use super::glb::Glb;
3027
use super::lub::Lub;
3128
use super::type_relating::TypeRelating;
32-
use super::{RelateResult, StructurallyRelateAliases};
33-
use crate::infer::{DefineOpaqueTypes, InferCtxt, TypeTrace, relate};
29+
use crate::infer::{DefineOpaqueTypes, InferCtxt, TypeTrace};
3430
use crate::traits::{Obligation, PredicateObligation};
3531

3632
#[derive(Clone)]
@@ -67,222 +63,6 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
6763
}
6864
}
6965

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

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

+1-1
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/glb.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_span::Span;
77
use tracing::{debug, instrument};
88

99
use super::StructurallyRelateAliases;
10-
use super::combine::{CombineFields, PredicateEmittingRelation};
10+
use super::combine::{CombineFields, InferCtxtCombineExt, PredicateEmittingRelation};
1111
use super::lattice::{self, LatticeDir};
1212
use crate::infer::{DefineOpaqueTypes, InferCtxt, SubregionOrigin};
1313
use crate::traits::ObligationCause;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_middle::ty::relate::RelateResult;
2121
use rustc_middle::ty::{self, Ty, TyVar};
2222
use tracing::instrument;
2323

24-
use super::combine::PredicateEmittingRelation;
24+
use super::combine::{InferCtxtCombineExt, PredicateEmittingRelation};
2525
use crate::infer::{DefineOpaqueTypes, InferCtxt};
2626
use crate::traits::ObligationCause;
2727

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_span::Span;
77
use tracing::{debug, instrument};
88

99
use super::StructurallyRelateAliases;
10-
use super::combine::{CombineFields, PredicateEmittingRelation};
10+
use super::combine::{CombineFields, InferCtxtCombineExt, PredicateEmittingRelation};
1111
use super::lattice::{self, LatticeDir};
1212
use crate::infer::{DefineOpaqueTypes, InferCtxt, SubregionOrigin};
1313
use crate::traits::ObligationCause;

0 commit comments

Comments
 (0)