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