rustc_middle/ty/
relate.rs

1use std::iter;
2
3pub use rustc_type_ir::relate::*;
4
5use crate::ty::error::{ExpectedFound, TypeError};
6use crate::ty::{self as ty, Ty, TyCtxt};
7
8pub type RelateResult<'tcx, T> = rustc_type_ir::relate::RelateResult<TyCtxt<'tcx>, T>;
9
10impl<'tcx> Relate<TyCtxt<'tcx>> for Ty<'tcx> {
11    #[inline]
12    fn relate<R: TypeRelation<TyCtxt<'tcx>>>(
13        relation: &mut R,
14        a: Ty<'tcx>,
15        b: Ty<'tcx>,
16    ) -> RelateResult<'tcx, Ty<'tcx>> {
17        relation.tys(a, b)
18    }
19}
20
21impl<'tcx> Relate<TyCtxt<'tcx>> for ty::Pattern<'tcx> {
22    #[inline]
23    fn relate<R: TypeRelation<TyCtxt<'tcx>>>(
24        relation: &mut R,
25        a: Self,
26        b: Self,
27    ) -> RelateResult<'tcx, Self> {
28        let tcx = relation.cx();
29        match (&*a, &*b) {
30            (
31                &ty::PatternKind::Range { start: start_a, end: end_a },
32                &ty::PatternKind::Range { start: start_b, end: end_b },
33            ) => {
34                let start = relation.relate(start_a, start_b)?;
35                let end = relation.relate(end_a, end_b)?;
36                Ok(tcx.mk_pat(ty::PatternKind::Range { start, end }))
37            }
38            (&ty::PatternKind::Or(a), &ty::PatternKind::Or(b)) => {
39                if a.len() != b.len() {
40                    return Err(TypeError::Mismatch);
41                }
42                let v = iter::zip(a, b).map(|(a, b)| relation.relate(a, b));
43                let patterns = tcx.mk_patterns_from_iter(v)?;
44                Ok(tcx.mk_pat(ty::PatternKind::Or(patterns)))
45            }
46            (ty::PatternKind::Range { .. } | ty::PatternKind::Or(_), _) => Err(TypeError::Mismatch),
47        }
48    }
49}
50
51impl<'tcx> Relate<TyCtxt<'tcx>> for &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> {
52    fn relate<R: TypeRelation<TyCtxt<'tcx>>>(
53        relation: &mut R,
54        a: Self,
55        b: Self,
56    ) -> RelateResult<'tcx, Self> {
57        let tcx = relation.cx();
58        // Fast path for when the auto traits do not match, or if the principals
59        // are from different traits and therefore the projections definitely don't
60        // match up.
61        if a.len() != b.len() {
62            return Err(TypeError::ExistentialMismatch(ExpectedFound::new(a, b)));
63        }
64        let v =
65            iter::zip(a, b).map(|(ep_a, ep_b)| match (ep_a.skip_binder(), ep_b.skip_binder()) {
66                (ty::ExistentialPredicate::Trait(a), ty::ExistentialPredicate::Trait(b)) => {
67                    Ok(ep_a.rebind(ty::ExistentialPredicate::Trait(
68                        relation.relate(ep_a.rebind(a), ep_b.rebind(b))?.skip_binder(),
69                    )))
70                }
71                (
72                    ty::ExistentialPredicate::Projection(a),
73                    ty::ExistentialPredicate::Projection(b),
74                ) => Ok(ep_a.rebind(ty::ExistentialPredicate::Projection(
75                    relation.relate(ep_a.rebind(a), ep_b.rebind(b))?.skip_binder(),
76                ))),
77                (
78                    ty::ExistentialPredicate::AutoTrait(a),
79                    ty::ExistentialPredicate::AutoTrait(b),
80                ) if a == b => Ok(ep_a.rebind(ty::ExistentialPredicate::AutoTrait(a))),
81                _ => Err(TypeError::ExistentialMismatch(ExpectedFound::new(a, b))),
82            });
83        tcx.mk_poly_existential_predicates_from_iter(v)
84    }
85}
86
87impl<'tcx> Relate<TyCtxt<'tcx>> for ty::GenericArgsRef<'tcx> {
88    fn relate<R: TypeRelation<TyCtxt<'tcx>>>(
89        relation: &mut R,
90        a: ty::GenericArgsRef<'tcx>,
91        b: ty::GenericArgsRef<'tcx>,
92    ) -> RelateResult<'tcx, ty::GenericArgsRef<'tcx>> {
93        relate_args_invariantly(relation, a, b)
94    }
95}
96
97impl<'tcx> Relate<TyCtxt<'tcx>> for ty::Region<'tcx> {
98    fn relate<R: TypeRelation<TyCtxt<'tcx>>>(
99        relation: &mut R,
100        a: ty::Region<'tcx>,
101        b: ty::Region<'tcx>,
102    ) -> RelateResult<'tcx, ty::Region<'tcx>> {
103        relation.regions(a, b)
104    }
105}
106
107impl<'tcx> Relate<TyCtxt<'tcx>> for ty::Const<'tcx> {
108    fn relate<R: TypeRelation<TyCtxt<'tcx>>>(
109        relation: &mut R,
110        a: ty::Const<'tcx>,
111        b: ty::Const<'tcx>,
112    ) -> RelateResult<'tcx, ty::Const<'tcx>> {
113        relation.consts(a, b)
114    }
115}
116
117impl<'tcx> Relate<TyCtxt<'tcx>> for ty::Expr<'tcx> {
118    fn relate<R: TypeRelation<TyCtxt<'tcx>>>(
119        relation: &mut R,
120        ae: ty::Expr<'tcx>,
121        be: ty::Expr<'tcx>,
122    ) -> RelateResult<'tcx, ty::Expr<'tcx>> {
123        // FIXME(generic_const_exprs): is it possible to relate two consts which are not identical
124        // exprs? Should we care about that?
125        // FIXME(generic_const_exprs): relating the `ty()`s is a little weird since it is supposed to
126        // ICE If they mismatch. Unfortunately `ConstKind::Expr` is a little special and can be thought
127        // of as being generic over the argument types, however this is implicit so these types don't get
128        // related when we relate the args of the item this const arg is for.
129        match (ae.kind, be.kind) {
130            (ty::ExprKind::Binop(a_binop), ty::ExprKind::Binop(b_binop)) if a_binop == b_binop => {}
131            (ty::ExprKind::UnOp(a_unop), ty::ExprKind::UnOp(b_unop)) if a_unop == b_unop => {}
132            (ty::ExprKind::FunctionCall, ty::ExprKind::FunctionCall) => {}
133            (ty::ExprKind::Cast(a_kind), ty::ExprKind::Cast(b_kind)) if a_kind == b_kind => {}
134            _ => return Err(TypeError::Mismatch),
135        }
136
137        let args = relation.relate(ae.args(), be.args())?;
138        Ok(ty::Expr::new(ae.kind, args))
139    }
140}
141
142impl<'tcx> Relate<TyCtxt<'tcx>> for ty::GenericArg<'tcx> {
143    fn relate<R: TypeRelation<TyCtxt<'tcx>>>(
144        relation: &mut R,
145        a: ty::GenericArg<'tcx>,
146        b: ty::GenericArg<'tcx>,
147    ) -> RelateResult<'tcx, ty::GenericArg<'tcx>> {
148        match (a.kind(), b.kind()) {
149            (ty::GenericArgKind::Lifetime(a_lt), ty::GenericArgKind::Lifetime(b_lt)) => {
150                Ok(relation.relate(a_lt, b_lt)?.into())
151            }
152            (ty::GenericArgKind::Type(a_ty), ty::GenericArgKind::Type(b_ty)) => {
153                Ok(relation.relate(a_ty, b_ty)?.into())
154            }
155            (ty::GenericArgKind::Const(a_ct), ty::GenericArgKind::Const(b_ct)) => {
156                Ok(relation.relate(a_ct, b_ct)?.into())
157            }
158            _ => bug!("impossible case reached: can't relate: {a:?} with {b:?}"),
159        }
160    }
161}
162
163impl<'tcx> Relate<TyCtxt<'tcx>> for ty::Term<'tcx> {
164    fn relate<R: TypeRelation<TyCtxt<'tcx>>>(
165        relation: &mut R,
166        a: Self,
167        b: Self,
168    ) -> RelateResult<'tcx, Self> {
169        Ok(match (a.kind(), b.kind()) {
170            (ty::TermKind::Ty(a), ty::TermKind::Ty(b)) => relation.relate(a, b)?.into(),
171            (ty::TermKind::Const(a), ty::TermKind::Const(b)) => relation.relate(a, b)?.into(),
172            _ => return Err(TypeError::Mismatch),
173        })
174    }
175}