Skip to main content

rustc_type_ir/
relate.rs

1use std::iter;
2
3use derive_where::derive_where;
4use rustc_ast_ir::Mutability;
5use tracing::{instrument, trace};
6
7use crate::error::{ExpectedFound, TypeError};
8use crate::fold::TypeFoldable;
9use crate::inherent::*;
10use crate::{self as ty, Interner};
11
12pub mod combine;
13pub mod solver_relating;
14
15pub type RelateResult<I, T> = Result<T, TypeError<I>>;
16
17/// Whether aliases should be related structurally or not. Used
18/// to adjust the behavior of generalization and combine.
19///
20/// This should always be `No` unless in a few special-cases when
21/// instantiating canonical responses and in the new solver. Each
22/// such case should have a comment explaining why it is used.
23#[derive(#[automatically_derived]
impl ::core::fmt::Debug for StructurallyRelateAliases {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                StructurallyRelateAliases::Yes => "Yes",
                StructurallyRelateAliases::No => "No",
            })
    }
}Debug, #[automatically_derived]
impl ::core::marker::Copy for StructurallyRelateAliases { }Copy, #[automatically_derived]
impl ::core::clone::Clone for StructurallyRelateAliases {
    #[inline]
    fn clone(&self) -> StructurallyRelateAliases { *self }
}Clone)]
24pub enum StructurallyRelateAliases {
25    Yes,
26    No,
27}
28
29/// Extra information about why we ended up with a particular variance.
30/// This is only used to add more information to error messages, and
31/// has no effect on soundness. While choosing the 'wrong' `VarianceDiagInfo`
32/// may lead to confusing notes in error messages, it will never cause
33/// a miscompilation or unsoundness.
34///
35/// When in doubt, use `VarianceDiagInfo::default()`
36#[automatically_derived]
impl<I: Interner> ::core::default::Default for VarianceDiagInfo<I> where
    I: Interner {
    fn default() -> Self { VarianceDiagInfo::None }
}#[derive_where(Clone, Copy, PartialEq, Debug, Default; I: Interner)]
37pub enum VarianceDiagInfo<I: Interner> {
38    /// No additional information - this is the default.
39    /// We will not add any additional information to error messages.
40    #[derive_where(default)]
41    None,
42    /// We switched our variance because a generic argument occurs inside
43    /// the invariant generic argument of another type.
44    Invariant {
45        /// The generic type containing the generic parameter
46        /// that changes the variance (e.g. `*mut T`, `MyStruct<T>`)
47        ty: I::Ty,
48        /// The index of the generic parameter being used
49        /// (e.g. `0` for `*mut T`, `1` for `MyStruct<'CovariantParam, 'InvariantParam>`)
50        param_index: u32,
51    },
52}
53
54impl<I: Interner> Eq for VarianceDiagInfo<I> {}
55
56impl<I: Interner> VarianceDiagInfo<I> {
57    /// Mirrors `Variance::xform` - used to 'combine' the existing
58    /// and new `VarianceDiagInfo`s when our variance changes.
59    pub fn xform(self, other: VarianceDiagInfo<I>) -> VarianceDiagInfo<I> {
60        // For now, just use the first `VarianceDiagInfo::Invariant` that we see
61        match self {
62            VarianceDiagInfo::None => other,
63            VarianceDiagInfo::Invariant { .. } => self,
64        }
65    }
66}
67
68pub trait TypeRelation<I: Interner>: Sized {
69    fn cx(&self) -> I;
70
71    /// Generic relation routine suitable for most anything.
72    fn relate<T: Relate<I>>(&mut self, a: T, b: T) -> RelateResult<I, T> {
73        Relate::relate(self, a, b)
74    }
75
76    fn relate_ty_args(
77        &mut self,
78        a_ty: I::Ty,
79        b_ty: I::Ty,
80        ty_def_id: I::DefId,
81        a_arg: I::GenericArgs,
82        b_arg: I::GenericArgs,
83        mk: impl FnOnce(I::GenericArgs) -> I::Ty,
84    ) -> RelateResult<I, I::Ty>;
85
86    /// Switch variance for the purpose of relating `a` and `b`.
87    fn relate_with_variance<T: Relate<I>>(
88        &mut self,
89        variance: ty::Variance,
90        info: VarianceDiagInfo<I>,
91        a: T,
92        b: T,
93    ) -> RelateResult<I, T>;
94
95    // Overridable relations. You shouldn't typically call these
96    // directly, instead call `relate()`, which in turn calls
97    // these. This is both more uniform but also allows us to add
98    // additional hooks for other types in the future if needed
99    // without making older code, which called `relate`, obsolete.
100
101    fn tys(&mut self, a: I::Ty, b: I::Ty) -> RelateResult<I, I::Ty>;
102
103    fn regions(&mut self, a: I::Region, b: I::Region) -> RelateResult<I, I::Region>;
104
105    fn consts(&mut self, a: I::Const, b: I::Const) -> RelateResult<I, I::Const>;
106
107    fn binders<T>(
108        &mut self,
109        a: ty::Binder<I, T>,
110        b: ty::Binder<I, T>,
111    ) -> RelateResult<I, ty::Binder<I, T>>
112    where
113        T: Relate<I>;
114}
115
116pub trait Relate<I: Interner>: TypeFoldable<I> + PartialEq + Copy {
117    fn relate<R: TypeRelation<I>>(relation: &mut R, a: Self, b: Self) -> RelateResult<I, Self>;
118}
119
120///////////////////////////////////////////////////////////////////////////
121// Relate impls
122
123#[inline]
124pub fn relate_args_invariantly<I: Interner, R: TypeRelation<I>>(
125    relation: &mut R,
126    a_arg: I::GenericArgs,
127    b_arg: I::GenericArgs,
128) -> RelateResult<I, I::GenericArgs> {
129    relation.cx().mk_args_from_iter(iter::zip(a_arg.iter(), b_arg.iter()).map(|(a, b)| {
130        relation.relate_with_variance(ty::Invariant, VarianceDiagInfo::default(), a, b)
131    }))
132}
133
134pub fn relate_args_with_variances<I: Interner, R: TypeRelation<I>>(
135    relation: &mut R,
136    variances: I::VariancesOf,
137    a_args: I::GenericArgs,
138    b_args: I::GenericArgs,
139) -> RelateResult<I, I::GenericArgs> {
140    let cx = relation.cx();
141    let args = iter::zip(a_args.iter(), b_args.iter()).enumerate().map(|(i, (a, b))| {
142        let variance = variances.get(i).unwrap();
143        relation.relate_with_variance(variance, VarianceDiagInfo::None, a, b)
144    });
145    // FIXME: We can probably try to reuse `a_args` here if it did not change.
146    cx.mk_args_from_iter(args)
147}
148
149impl<I: Interner> Relate<I> for ty::FnSig<I> {
150    fn relate<R: TypeRelation<I>>(
151        relation: &mut R,
152        a: ty::FnSig<I>,
153        b: ty::FnSig<I>,
154    ) -> RelateResult<I, ty::FnSig<I>> {
155        let cx = relation.cx();
156
157        if a.c_variadic() != b.c_variadic() {
158            return Err(TypeError::VariadicMismatch(ExpectedFound::new(
159                a.c_variadic(),
160                b.c_variadic(),
161            )));
162        }
163
164        if a.safety() != b.safety() {
165            return Err(TypeError::SafetyMismatch(ExpectedFound::new(a.safety(), b.safety())));
166        }
167
168        if a.abi() != b.abi() {
169            return Err(TypeError::AbiMismatch(ExpectedFound::new(a.abi(), b.abi())));
170        };
171
172        let a_inputs = a.inputs();
173        let b_inputs = b.inputs();
174        if a_inputs.len() != b_inputs.len() {
175            return Err(TypeError::ArgCount);
176        }
177
178        let inputs_and_output = iter::zip(a_inputs.iter(), b_inputs.iter())
179            .map(|(a, b)| ((a, b), false))
180            .chain(iter::once(((a.output(), b.output()), true)))
181            .map(|((a, b), is_output)| {
182                if is_output {
183                    relation.relate(a, b)
184                } else {
185                    relation.relate_with_variance(
186                        ty::Contravariant,
187                        VarianceDiagInfo::default(),
188                        a,
189                        b,
190                    )
191                }
192            })
193            .enumerate()
194            .map(|(i, r)| match r {
195                Err(TypeError::Sorts(exp_found) | TypeError::ArgumentSorts(exp_found, _)) => {
196                    Err(TypeError::ArgumentSorts(exp_found, i))
197                }
198                Err(TypeError::Mutability | TypeError::ArgumentMutability(_)) => {
199                    Err(TypeError::ArgumentMutability(i))
200                }
201                r => r,
202            });
203        Ok(ty::FnSig {
204            inputs_and_output: cx.mk_type_list_from_iter(inputs_and_output)?,
205            fn_sig_kind: a.fn_sig_kind,
206        })
207    }
208}
209
210impl<I: Interner> Relate<I> for ty::AliasTy<I> {
211    fn relate<R: TypeRelation<I>>(
212        relation: &mut R,
213        a: ty::AliasTy<I>,
214        b: ty::AliasTy<I>,
215    ) -> RelateResult<I, ty::AliasTy<I>> {
216        if a.kind.def_id() != b.kind.def_id() {
217            Err(TypeError::ProjectionMismatched(ExpectedFound::new(a.kind.into(), b.kind.into())))
218        } else {
219            let cx = relation.cx();
220            let args = if let Some(variances) = cx.opt_alias_variances(a.kind) {
221                relate_args_with_variances(relation, variances, a.args, b.args)?
222            } else {
223                relate_args_invariantly(relation, a.args, b.args)?
224            };
225            Ok(ty::AliasTy::new_from_args(cx, a.kind, args))
226        }
227    }
228}
229
230impl<I: Interner> Relate<I> for ty::UnevaluatedConst<I> {
231    fn relate<R: TypeRelation<I>>(
232        relation: &mut R,
233        a: ty::UnevaluatedConst<I>,
234        b: ty::UnevaluatedConst<I>,
235    ) -> RelateResult<I, ty::UnevaluatedConst<I>> {
236        let cx = relation.cx();
237        if a.kind != b.kind {
238            Err(TypeError::ConstMismatch(ExpectedFound::new(
239                Const::new_unevaluated(cx, a),
240                Const::new_unevaluated(cx, b),
241            )))
242        } else {
243            // FIXME(mgca): remove this
244            if true {
    match (&a.type_of(cx).skip_norm_wip(), &b.type_of(cx).skip_norm_wip()) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_eq!(a.type_of(cx).skip_norm_wip(), b.type_of(cx).skip_norm_wip());
245
246            let args = relate_args_invariantly(relation, a.args, b.args)?;
247
248            Ok(ty::UnevaluatedConst::new(cx, a.kind, args))
249        }
250    }
251}
252
253impl<I: Interner> Relate<I> for ty::AliasTerm<I> {
254    fn relate<R: TypeRelation<I>>(
255        relation: &mut R,
256        a: ty::AliasTerm<I>,
257        b: ty::AliasTerm<I>,
258    ) -> RelateResult<I, ty::AliasTerm<I>> {
259        if a.kind != b.kind {
260            Err(TypeError::ProjectionMismatched(ExpectedFound::new(a.kind, b.kind)))
261        } else {
262            let args = match a.kind {
263                ty::AliasTermKind::OpaqueTy { def_id } => relate_args_with_variances(
264                    relation,
265                    relation.cx().variances_of(def_id.into()),
266                    a.args,
267                    b.args,
268                )?,
269                ty::AliasTermKind::ProjectionTy { .. }
270                | ty::AliasTermKind::FreeConst { .. }
271                | ty::AliasTermKind::FreeTy { .. }
272                | ty::AliasTermKind::InherentTy { .. }
273                | ty::AliasTermKind::InherentConst { .. }
274                | ty::AliasTermKind::AnonConst { .. }
275                | ty::AliasTermKind::ProjectionConst { .. } => {
276                    relate_args_invariantly(relation, a.args, b.args)?
277                }
278            };
279            Ok(a.with_args(relation.cx(), args))
280        }
281    }
282}
283
284impl<I: Interner> Relate<I> for ty::ExistentialProjection<I> {
285    fn relate<R: TypeRelation<I>>(
286        relation: &mut R,
287        a: ty::ExistentialProjection<I>,
288        b: ty::ExistentialProjection<I>,
289    ) -> RelateResult<I, ty::ExistentialProjection<I>> {
290        if a.def_id != b.def_id {
291            Err(TypeError::ProjectionMismatched(ExpectedFound::new(
292                relation.cx().alias_term_kind_from_def_id(a.def_id.into()),
293                relation.cx().alias_term_kind_from_def_id(b.def_id.into()),
294            )))
295        } else {
296            let term = relation.relate_with_variance(
297                ty::Invariant,
298                VarianceDiagInfo::default(),
299                a.term,
300                b.term,
301            )?;
302            let args = relation.relate_with_variance(
303                ty::Invariant,
304                VarianceDiagInfo::default(),
305                a.args,
306                b.args,
307            )?;
308            Ok(ty::ExistentialProjection::new_from_args(relation.cx(), a.def_id, args, term))
309        }
310    }
311}
312
313impl<I: Interner> Relate<I> for ty::TraitRef<I> {
314    fn relate<R: TypeRelation<I>>(
315        relation: &mut R,
316        a: ty::TraitRef<I>,
317        b: ty::TraitRef<I>,
318    ) -> RelateResult<I, ty::TraitRef<I>> {
319        // Different traits cannot be related.
320        if a.def_id != b.def_id {
321            Err(TypeError::Traits({
322                let a = a.def_id;
323                let b = b.def_id;
324                ExpectedFound::new(a, b)
325            }))
326        } else {
327            let args = relate_args_invariantly(relation, a.args, b.args)?;
328            Ok(ty::TraitRef::new_from_args(relation.cx(), a.def_id, args))
329        }
330    }
331}
332
333impl<I: Interner> Relate<I> for ty::ExistentialTraitRef<I> {
334    fn relate<R: TypeRelation<I>>(
335        relation: &mut R,
336        a: ty::ExistentialTraitRef<I>,
337        b: ty::ExistentialTraitRef<I>,
338    ) -> RelateResult<I, ty::ExistentialTraitRef<I>> {
339        // Different traits cannot be related.
340        if a.def_id != b.def_id {
341            Err(TypeError::Traits({
342                let a = a.def_id;
343                let b = b.def_id;
344                ExpectedFound::new(a, b)
345            }))
346        } else {
347            let args = relate_args_invariantly(relation, a.args, b.args)?;
348            Ok(ty::ExistentialTraitRef::new_from_args(relation.cx(), a.def_id, args))
349        }
350    }
351}
352
353/// Relates `a` and `b` structurally, calling the relation for all nested values.
354/// Any semantic equality, e.g. of projections, and inference variables have to be
355/// handled by the caller.
356x;#[instrument(level = "trace", skip(relation), ret)]
357pub fn structurally_relate_tys<I: Interner, R: TypeRelation<I>>(
358    relation: &mut R,
359    a: I::Ty,
360    b: I::Ty,
361) -> RelateResult<I, I::Ty> {
362    let cx = relation.cx();
363    match (a.kind(), b.kind()) {
364        (ty::Infer(_), _) | (_, ty::Infer(_)) => {
365            // The caller should handle these cases!
366            panic!("var types encountered in structurally_relate_tys")
367        }
368
369        (ty::Bound(..), _) | (_, ty::Bound(..)) => {
370            panic!("bound types encountered in structurally_relate_tys")
371        }
372
373        (ty::Error(guar), _) | (_, ty::Error(guar)) => Ok(Ty::new_error(cx, guar)),
374
375        (ty::Never, _)
376        | (ty::Char, _)
377        | (ty::Bool, _)
378        | (ty::Int(_), _)
379        | (ty::Uint(_), _)
380        | (ty::Float(_), _)
381        | (ty::Str, _)
382            if a == b =>
383        {
384            Ok(a)
385        }
386
387        (ty::Param(a_p), ty::Param(b_p)) if a_p.index() == b_p.index() => {
388            // FIXME: Put this back
389            //debug_assert_eq!(a_p.name(), b_p.name(), "param types with same index differ in name");
390            Ok(a)
391        }
392
393        (ty::Placeholder(p1), ty::Placeholder(p2)) if p1 == p2 => Ok(a),
394
395        (ty::Adt(a_def, a_args), ty::Adt(b_def, b_args)) if a_def == b_def => {
396            if a_args.is_empty() {
397                Ok(a)
398            } else {
399                relation.relate_ty_args(a, b, a_def.def_id().into(), a_args, b_args, |args| {
400                    Ty::new_adt(cx, a_def, args)
401                })
402            }
403        }
404
405        (ty::Foreign(a_id), ty::Foreign(b_id)) if a_id == b_id => Ok(Ty::new_foreign(cx, a_id)),
406
407        (ty::Dynamic(a_obj, a_region), ty::Dynamic(b_obj, b_region)) => Ok(Ty::new_dynamic(
408            cx,
409            relation.relate(a_obj, b_obj)?,
410            relation.relate(a_region, b_region)?,
411        )),
412
413        (ty::Coroutine(a_id, a_args), ty::Coroutine(b_id, b_args)) if a_id == b_id => {
414            // All Coroutine types with the same id represent
415            // the (anonymous) type of the same coroutine expression. So
416            // all of their regions should be equated.
417            let args = relate_args_invariantly(relation, a_args, b_args)?;
418            Ok(Ty::new_coroutine(cx, a_id, args))
419        }
420
421        (ty::CoroutineWitness(a_id, a_args), ty::CoroutineWitness(b_id, b_args))
422            if a_id == b_id =>
423        {
424            // All CoroutineWitness types with the same id represent
425            // the (anonymous) type of the same coroutine expression. So
426            // all of their regions should be equated.
427            let args = relate_args_invariantly(relation, a_args, b_args)?;
428            Ok(Ty::new_coroutine_witness(cx, a_id, args))
429        }
430
431        (ty::Closure(a_id, a_args), ty::Closure(b_id, b_args)) if a_id == b_id => {
432            // All Closure types with the same id represent
433            // the (anonymous) type of the same closure expression. So
434            // all of their regions should be equated.
435            let args = relate_args_invariantly(relation, a_args, b_args)?;
436            Ok(Ty::new_closure(cx, a_id, args))
437        }
438
439        (ty::CoroutineClosure(a_id, a_args), ty::CoroutineClosure(b_id, b_args))
440            if a_id == b_id =>
441        {
442            let args = relate_args_invariantly(relation, a_args, b_args)?;
443            Ok(Ty::new_coroutine_closure(cx, a_id, args))
444        }
445
446        (ty::RawPtr(a_ty, a_mutbl), ty::RawPtr(b_ty, b_mutbl)) => {
447            if a_mutbl != b_mutbl {
448                return Err(TypeError::Mutability);
449            }
450
451            let (variance, info) = match a_mutbl {
452                Mutability::Not => (ty::Covariant, VarianceDiagInfo::None),
453                Mutability::Mut => {
454                    (ty::Invariant, VarianceDiagInfo::Invariant { ty: a, param_index: 0 })
455                }
456            };
457
458            let ty = relation.relate_with_variance(variance, info, a_ty, b_ty)?;
459
460            Ok(Ty::new_ptr(cx, ty, a_mutbl))
461        }
462
463        (ty::Ref(a_r, a_ty, a_mutbl), ty::Ref(b_r, b_ty, b_mutbl)) => {
464            if a_mutbl != b_mutbl {
465                return Err(TypeError::Mutability);
466            }
467
468            let (variance, info) = match a_mutbl {
469                Mutability::Not => (ty::Covariant, VarianceDiagInfo::None),
470                Mutability::Mut => {
471                    (ty::Invariant, VarianceDiagInfo::Invariant { ty: a, param_index: 0 })
472                }
473            };
474
475            let r = relation.relate(a_r, b_r)?;
476            let ty = relation.relate_with_variance(variance, info, a_ty, b_ty)?;
477
478            Ok(Ty::new_ref(cx, r, ty, a_mutbl))
479        }
480
481        (ty::Array(a_t, sz_a), ty::Array(b_t, sz_b)) => {
482            let t = relation.relate(a_t, b_t)?;
483            match relation.relate(sz_a, sz_b) {
484                Ok(sz) => Ok(Ty::new_array_with_const_len(cx, t, sz)),
485                Err(TypeError::ConstMismatch(_)) => {
486                    Err(TypeError::ArraySize(ExpectedFound::new(sz_a, sz_b)))
487                }
488                Err(e) => Err(e),
489            }
490        }
491
492        (ty::Slice(a_t), ty::Slice(b_t)) => {
493            let t = relation.relate(a_t, b_t)?;
494            Ok(Ty::new_slice(cx, t))
495        }
496
497        (ty::Tuple(as_), ty::Tuple(bs)) => {
498            if as_.len() == bs.len() {
499                Ok(Ty::new_tup_from_iter(
500                    cx,
501                    iter::zip(as_.iter(), bs.iter()).map(|(a, b)| relation.relate(a, b)),
502                )?)
503            } else if !(as_.is_empty() || bs.is_empty()) {
504                Err(TypeError::TupleSize(ExpectedFound::new(as_.len(), bs.len())))
505            } else {
506                Err(TypeError::Sorts(ExpectedFound::new(a, b)))
507            }
508        }
509
510        (ty::FnDef(a_def_id, a_args), ty::FnDef(b_def_id, b_args)) if a_def_id == b_def_id => {
511            if a_args.is_empty() {
512                Ok(a)
513            } else {
514                relation.relate_ty_args(a, b, a_def_id.into(), a_args, b_args, |args| {
515                    Ty::new_fn_def(cx, a_def_id, args)
516                })
517            }
518        }
519
520        (ty::FnPtr(a_sig_tys, a_hdr), ty::FnPtr(b_sig_tys, b_hdr)) => {
521            let fty = relation.relate(a_sig_tys.with(a_hdr), b_sig_tys.with(b_hdr))?;
522            Ok(Ty::new_fn_ptr(cx, fty))
523        }
524
525        // Alias tend to mostly already be handled downstream due to normalization.
526        (ty::Alias(a), ty::Alias(b)) => {
527            let alias_ty = relation.relate(a, b)?;
528            Ok(Ty::new_alias(cx, alias_ty))
529        }
530
531        (ty::Pat(a_ty, a_pat), ty::Pat(b_ty, b_pat)) => {
532            let ty = relation.relate(a_ty, b_ty)?;
533            let pat = relation.relate(a_pat, b_pat)?;
534            Ok(Ty::new_pat(cx, ty, pat))
535        }
536
537        (ty::UnsafeBinder(a_binder), ty::UnsafeBinder(b_binder)) => {
538            Ok(Ty::new_unsafe_binder(cx, relation.binders(*a_binder, *b_binder)?))
539        }
540
541        _ => Err(TypeError::Sorts(ExpectedFound::new(a, b))),
542    }
543}
544
545/// Relates `a` and `b` structurally, calling the relation for all nested values.
546/// Any semantic equality, e.g. of unevaluated consts, and inference variables have
547/// to be handled by the caller.
548///
549/// FIXME: This is not totally structural, which probably should be fixed.
550/// See the HACKs below.
551pub fn structurally_relate_consts<I: Interner, R: TypeRelation<I>>(
552    relation: &mut R,
553    mut a: I::Const,
554    mut b: I::Const,
555) -> RelateResult<I, I::Const> {
556    {
    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.rs:556",
                        "rustc_type_ir::relate", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_type_ir/src/relate.rs"),
                        ::tracing_core::__macro_support::Option::Some(556u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_type_ir::relate"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::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!("structurally_relate_consts::<{0}>(a = {1:?}, b = {2:?})",
                                                    std::any::type_name::<R>(), a, b) as &dyn Value))])
            });
    } else { ; }
};trace!(
557        "structurally_relate_consts::<{}>(a = {:?}, b = {:?})",
558        std::any::type_name::<R>(),
559        a,
560        b
561    );
562    let cx = relation.cx();
563
564    if cx.features().generic_const_exprs() {
565        a = cx.expand_abstract_consts(a);
566        b = cx.expand_abstract_consts(b);
567    }
568
569    {
    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.rs:569",
                        "rustc_type_ir::relate", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_type_ir/src/relate.rs"),
                        ::tracing_core::__macro_support::Option::Some(569u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_type_ir::relate"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::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!("structurally_relate_consts::<{0}>(normed_a = {1:?}, normed_b = {2:?})",
                                                    std::any::type_name::<R>(), a, b) as &dyn Value))])
            });
    } else { ; }
};trace!(
570        "structurally_relate_consts::<{}>(normed_a = {:?}, normed_b = {:?})",
571        std::any::type_name::<R>(),
572        a,
573        b
574    );
575
576    // Currently, the values that can be unified are primitive types,
577    // and those that derive both `PartialEq` and `Eq`, corresponding
578    // to structural-match types.
579    let is_match = match (a.kind(), b.kind()) {
580        (ty::ConstKind::Infer(_), _) | (_, ty::ConstKind::Infer(_)) => {
581            // The caller should handle these cases!
582            {
    ::core::panicking::panic_fmt(format_args!("var types encountered in structurally_relate_consts: {0:?} {1:?}",
            a, b));
}panic!("var types encountered in structurally_relate_consts: {:?} {:?}", a, b)
583        }
584
585        (ty::ConstKind::Error(_), _) => return Ok(a),
586        (_, ty::ConstKind::Error(_)) => return Ok(b),
587
588        (ty::ConstKind::Param(a_p), ty::ConstKind::Param(b_p)) if a_p.index() == b_p.index() => {
589            // FIXME: Put this back
590            // debug_assert_eq!(a_p.name, b_p.name, "param types with same index differ in name");
591            true
592        }
593        (ty::ConstKind::Placeholder(p1), ty::ConstKind::Placeholder(p2)) => p1 == p2,
594        (ty::ConstKind::Value(a_val), ty::ConstKind::Value(b_val)) => {
595            match (a_val.valtree().kind(), b_val.valtree().kind()) {
596                (ty::ValTreeKind::Leaf(scalar_a), ty::ValTreeKind::Leaf(scalar_b)) => {
597                    scalar_a == scalar_b
598                }
599                (ty::ValTreeKind::Branch(branches_a), ty::ValTreeKind::Branch(branches_b))
600                    if branches_a.len() == branches_b.len() =>
601                {
602                    branches_a
603                        .iter()
604                        .zip(branches_b.iter())
605                        .all(|(a, b)| relation.relate(a, b).is_ok())
606                }
607                _ => false,
608            }
609        }
610
611        // While this is slightly incorrect, it shouldn't matter for `min_const_generics`
612        // and is the better alternative to waiting until `generic_const_exprs` can
613        // be stabilized.
614        (ty::ConstKind::Unevaluated(au), ty::ConstKind::Unevaluated(bu)) => {
615            return Ok(Const::new_unevaluated(cx, relation.relate(au, bu)?));
616        }
617        (ty::ConstKind::Expr(ae), ty::ConstKind::Expr(be)) => {
618            let expr = relation.relate(ae, be)?;
619            return Ok(Const::new_expr(cx, expr));
620        }
621        _ => false,
622    };
623    if is_match { Ok(a) } else { Err(TypeError::ConstMismatch(ExpectedFound::new(a, b))) }
624}
625
626impl<I: Interner, T: Relate<I>> Relate<I> for ty::Binder<I, T> {
627    fn relate<R: TypeRelation<I>>(
628        relation: &mut R,
629        a: ty::Binder<I, T>,
630        b: ty::Binder<I, T>,
631    ) -> RelateResult<I, ty::Binder<I, T>> {
632        relation.binders(a, b)
633    }
634}
635
636impl<I: Interner> Relate<I> for ty::TraitPredicate<I> {
637    fn relate<R: TypeRelation<I>>(
638        relation: &mut R,
639        a: ty::TraitPredicate<I>,
640        b: ty::TraitPredicate<I>,
641    ) -> RelateResult<I, ty::TraitPredicate<I>> {
642        let trait_ref = relation.relate(a.trait_ref, b.trait_ref)?;
643        if a.polarity != b.polarity {
644            return Err(TypeError::PolarityMismatch(ExpectedFound::new(a.polarity, b.polarity)));
645        }
646        Ok(ty::TraitPredicate { trait_ref, polarity: a.polarity })
647    }
648}