1use std::iter;
2
3use tracing::debug;
4
5use super::{
6 ExpectedFound, RelateResult, StructurallyRelateAliases, TypeRelation,
7 structurally_relate_consts, structurally_relate_tys,
8};
9use crate::error::TypeError;
10use crate::inherent::*;
11use crate::relate::VarianceDiagInfo;
12use crate::solve::Goal;
13use crate::visit::TypeVisitableExt as _;
14use crate::{self as ty, InferCtxtLike, Interner, TypingMode, Upcast};
15
16pub trait PredicateEmittingRelation<Infcx, I = <Infcx as InferCtxtLike>::Interner>:
17 TypeRelation<I>
18where
19 Infcx: InferCtxtLike<Interner = I>,
20 I: Interner,
21{
22 fn span(&self) -> I::Span;
23
24 fn param_env(&self) -> I::ParamEnv;
25
26 fn structurally_relate_aliases(&self) -> StructurallyRelateAliases;
30
31 fn register_goals(&mut self, obligations: impl IntoIterator<Item = Goal<I, I::Predicate>>);
33
34 fn register_predicates(
37 &mut self,
38 obligations: impl IntoIterator<Item: Upcast<I, I::Predicate>>,
39 );
40
41 fn register_alias_relate_predicate(&mut self, a: I::Ty, b: I::Ty);
43}
44
45pub fn super_combine_tys<Infcx, I, R>(
46 infcx: &Infcx,
47 relation: &mut R,
48 a: I::Ty,
49 b: I::Ty,
50) -> RelateResult<I, I::Ty>
51where
52 Infcx: InferCtxtLike<Interner = I>,
53 I: Interner,
54 R: PredicateEmittingRelation<Infcx>,
55{
56 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_type_ir/src/relate/combine.rs:56",
"rustc_type_ir::relate::combine", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_type_ir/src/relate/combine.rs"),
::tracing_core::__macro_support::Option::Some(56u32),
::tracing_core::__macro_support::Option::Some("rustc_type_ir::relate::combine"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("super_combine_tys::<{0}>({1:?}, {2:?})",
std::any::type_name::<R>(), a, b) as &dyn Value))])
});
} else { ; }
};debug!("super_combine_tys::<{}>({:?}, {:?})", std::any::type_name::<R>(), a, b);
57 if true {
if !!a.has_escaping_bound_vars() {
::core::panicking::panic("assertion failed: !a.has_escaping_bound_vars()")
};
};debug_assert!(!a.has_escaping_bound_vars());
58 if true {
if !!b.has_escaping_bound_vars() {
::core::panicking::panic("assertion failed: !b.has_escaping_bound_vars()")
};
};debug_assert!(!b.has_escaping_bound_vars());
59
60 match (a.kind(), b.kind()) {
61 (ty::Error(e), _) | (_, ty::Error(e)) => {
62 infcx.set_tainted_by_errors(e);
63 return Ok(Ty::new_error(infcx.cx(), e));
64 }
65
66 (ty::Infer(ty::IntVar(a_id)), ty::Infer(ty::IntVar(b_id))) => {
68 infcx.equate_int_vids_raw(a_id, b_id);
69 Ok(a)
70 }
71 (ty::Infer(ty::IntVar(v_id)), ty::Int(v)) => {
72 infcx.instantiate_int_var_raw(v_id, ty::IntVarValue::IntType(v));
73 Ok(b)
74 }
75 (ty::Int(v), ty::Infer(ty::IntVar(v_id))) => {
76 infcx.instantiate_int_var_raw(v_id, ty::IntVarValue::IntType(v));
77 Ok(a)
78 }
79 (ty::Infer(ty::IntVar(v_id)), ty::Uint(v)) => {
80 infcx.instantiate_int_var_raw(v_id, ty::IntVarValue::UintType(v));
81 Ok(b)
82 }
83 (ty::Uint(v), ty::Infer(ty::IntVar(v_id))) => {
84 infcx.instantiate_int_var_raw(v_id, ty::IntVarValue::UintType(v));
85 Ok(a)
86 }
87
88 (ty::Infer(ty::FloatVar(a_id)), ty::Infer(ty::FloatVar(b_id))) => {
90 infcx.equate_float_vids_raw(a_id, b_id);
91 Ok(a)
92 }
93 (ty::Infer(ty::FloatVar(v_id)), ty::Float(v)) => {
94 infcx.instantiate_float_var_raw(v_id, ty::FloatVarValue::Known(v));
95 Ok(b)
96 }
97 (ty::Float(v), ty::Infer(ty::FloatVar(v_id))) => {
98 infcx.instantiate_float_var_raw(v_id, ty::FloatVarValue::Known(v));
99 Ok(a)
100 }
101
102 (ty::Alias(..), ty::Infer(ty::TyVar(_))) | (ty::Infer(ty::TyVar(_)), ty::Alias(..))
104 if infcx.next_trait_solver() =>
105 {
106 {
::core::panicking::panic_fmt(format_args!("We do not expect to encounter `TyVar` this late in combine -- they should have been handled earlier"));
}panic!(
107 "We do not expect to encounter `TyVar` this late in combine \
108 -- they should have been handled earlier"
109 )
110 }
111 (_, ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)))
112 | (ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)), _)
113 if infcx.next_trait_solver() =>
114 {
115 {
::core::panicking::panic_fmt(format_args!("We do not expect to encounter `Fresh` variables in the new solver"));
}panic!("We do not expect to encounter `Fresh` variables in the new solver")
116 }
117
118 (_, ty::Alias(..)) | (ty::Alias(..), _) if infcx.next_trait_solver() => {
119 match relation.structurally_relate_aliases() {
120 StructurallyRelateAliases::Yes => structurally_relate_tys(relation, a, b),
121 StructurallyRelateAliases::No => {
122 relation.register_alias_relate_predicate(a, b);
123 Ok(a)
124 }
125 }
126 }
127
128 (ty::Infer(_), _) | (_, ty::Infer(_)) => Err(TypeError::Sorts(ExpectedFound::new(a, b))),
130
131 (ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. }), _)
132 | (_, ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. })) => {
133 if !!infcx.next_trait_solver() {
::core::panicking::panic("assertion failed: !infcx.next_trait_solver()")
};assert!(!infcx.next_trait_solver());
134 match infcx.typing_mode() {
135 TypingMode::Coherence => {
140 relation.register_predicates([ty::Binder::dummy(ty::PredicateKind::Ambiguous)]);
141 Ok(a)
142 }
143 TypingMode::Analysis { .. }
144 | TypingMode::Borrowck { .. }
145 | TypingMode::PostBorrowckAnalysis { .. }
146 | TypingMode::PostAnalysis => structurally_relate_tys(relation, a, b),
147 }
148 }
149
150 _ => structurally_relate_tys(relation, a, b),
151 }
152}
153
154pub fn super_combine_consts<Infcx, I, R>(
155 infcx: &Infcx,
156 relation: &mut R,
157 a: I::Const,
158 b: I::Const,
159) -> RelateResult<I, I::Const>
160where
161 Infcx: InferCtxtLike<Interner = I>,
162 I: Interner,
163 R: PredicateEmittingRelation<Infcx>,
164{
165 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_type_ir/src/relate/combine.rs:165",
"rustc_type_ir::relate::combine", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_type_ir/src/relate/combine.rs"),
::tracing_core::__macro_support::Option::Some(165u32),
::tracing_core::__macro_support::Option::Some("rustc_type_ir::relate::combine"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("super_combine_consts::<{0}>({1:?}, {2:?})",
std::any::type_name::<R>(), a, b) as &dyn Value))])
});
} else { ; }
};debug!("super_combine_consts::<{}>({:?}, {:?})", std::any::type_name::<R>(), a, b);
166 if true {
if !!a.has_escaping_bound_vars() {
::core::panicking::panic("assertion failed: !a.has_escaping_bound_vars()")
};
};debug_assert!(!a.has_escaping_bound_vars());
167 if true {
if !!b.has_escaping_bound_vars() {
::core::panicking::panic("assertion failed: !b.has_escaping_bound_vars()")
};
};debug_assert!(!b.has_escaping_bound_vars());
168
169 if a == b {
170 return Ok(a);
171 }
172
173 let a = infcx.shallow_resolve_const(a);
174 let b = infcx.shallow_resolve_const(b);
175
176 match (a.kind(), b.kind()) {
177 (
178 ty::ConstKind::Infer(ty::InferConst::Var(a_vid)),
179 ty::ConstKind::Infer(ty::InferConst::Var(b_vid)),
180 ) => {
181 infcx.equate_const_vids_raw(a_vid, b_vid);
182 Ok(a)
183 }
184
185 (ty::ConstKind::Infer(ty::InferConst::Var(_)), ty::ConstKind::Infer(_))
187 | (ty::ConstKind::Infer(_), ty::ConstKind::Infer(ty::InferConst::Var(_))) => {
188 {
::core::panicking::panic_fmt(format_args!("tried to combine ConstKind::Infer/ConstKind::Infer(InferConst::Var): {0:?} and {1:?}",
a, b));
}panic!(
189 "tried to combine ConstKind::Infer/ConstKind::Infer(InferConst::Var): {a:?} and {b:?}"
190 )
191 }
192
193 (ty::ConstKind::Infer(ty::InferConst::Var(vid)), _) => {
194 infcx.instantiate_const_var_raw(relation, true, vid, b)?;
195 Ok(b)
196 }
197
198 (_, ty::ConstKind::Infer(ty::InferConst::Var(vid))) => {
199 infcx.instantiate_const_var_raw(relation, false, vid, a)?;
200 Ok(a)
201 }
202
203 (ty::ConstKind::Unevaluated(..), _) | (_, ty::ConstKind::Unevaluated(..))
204 if infcx.cx().features().generic_const_exprs() || infcx.next_trait_solver() =>
205 {
206 match relation.structurally_relate_aliases() {
207 StructurallyRelateAliases::No => {
208 relation.register_predicates([if infcx.next_trait_solver() {
209 ty::PredicateKind::AliasRelate(
210 a.into(),
211 b.into(),
212 ty::AliasRelationDirection::Equate,
213 )
214 } else {
215 ty::PredicateKind::ConstEquate(a, b)
216 }]);
217
218 Ok(b)
219 }
220 StructurallyRelateAliases::Yes => structurally_relate_consts(relation, a, b),
221 }
222 }
223 _ => structurally_relate_consts(relation, a, b),
224 }
225}
226
227pub fn combine_ty_args<Infcx, I, R>(
228 infcx: &Infcx,
229 relation: &mut R,
230 a_ty: I::Ty,
231 b_ty: I::Ty,
232 variances: I::VariancesOf,
233 a_args: I::GenericArgs,
234 b_args: I::GenericArgs,
235 mk: impl FnOnce(I::GenericArgs) -> I::Ty,
236) -> RelateResult<I, I::Ty>
237where
238 Infcx: InferCtxtLike<Interner = I>,
239 I: Interner,
240 R: PredicateEmittingRelation<Infcx>,
241{
242 let cx = infcx.cx();
243 let mut has_unconstrained_bivariant_arg = false;
244 let args = iter::zip(a_args.iter(), b_args.iter()).enumerate().map(|(i, (a, b))| {
245 let variance = variances.get(i).unwrap();
246 let variance_info = match variance {
247 ty::Invariant => {
248 VarianceDiagInfo::Invariant { ty: a_ty, param_index: i.try_into().unwrap() }
249 }
250 ty::Covariant | ty::Contravariant => VarianceDiagInfo::default(),
251 ty::Bivariant => {
252 let has_non_region_infer = |arg: I::GenericArg| {
253 arg.has_non_region_infer()
254 && infcx.resolve_vars_if_possible(arg).has_non_region_infer()
255 };
256 if has_non_region_infer(a) || has_non_region_infer(b) {
257 has_unconstrained_bivariant_arg = true;
258 }
259 VarianceDiagInfo::default()
260 }
261 };
262 relation.relate_with_variance(variance, variance_info, a, b)
263 });
264 let args = cx.mk_args_from_iter(args)?;
265
266 if has_unconstrained_bivariant_arg {
290 relation.register_predicates([
291 ty::ClauseKind::WellFormed(a_ty.into()),
292 ty::ClauseKind::WellFormed(b_ty.into()),
293 ]);
294 }
295
296 if a_args == args { Ok(a_ty) } else { Ok(mk(args)) }
297}