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::NotNull, ty::PatternKind::NotNull) => Ok(a),
39 (&ty::PatternKind::Or(a), &ty::PatternKind::Or(b)) => {
40 if a.len() != b.len() {
41 return Err(TypeError::Mismatch);
42 }
43 let v = iter::zip(a, b).map(|(a, b)| relation.relate(a, b));
44 let patterns = tcx.mk_patterns_from_iter(v)?;
45 Ok(tcx.mk_pat(ty::PatternKind::Or(patterns)))
46 }
47 (
48 ty::PatternKind::NotNull | ty::PatternKind::Range { .. } | ty::PatternKind::Or(_),
49 _,
50 ) => Err(TypeError::Mismatch),
51 }
52 }
53}
54
55impl<'tcx> Relate<TyCtxt<'tcx>> for &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> {
56 fn relate<R: TypeRelation<TyCtxt<'tcx>>>(
57 relation: &mut R,
58 a: Self,
59 b: Self,
60 ) -> RelateResult<'tcx, Self> {
61 let tcx = relation.cx();
62 if a.len() != b.len() {
66 return Err(TypeError::ExistentialMismatch(ExpectedFound::new(a, b)));
67 }
68 let v =
69 iter::zip(a, b).map(|(ep_a, ep_b)| match (ep_a.skip_binder(), ep_b.skip_binder()) {
70 (ty::ExistentialPredicate::Trait(a), ty::ExistentialPredicate::Trait(b)) => {
71 Ok(ep_a.rebind(ty::ExistentialPredicate::Trait(
72 relation.relate(ep_a.rebind(a), ep_b.rebind(b))?.skip_binder(),
73 )))
74 }
75 (
76 ty::ExistentialPredicate::Projection(a),
77 ty::ExistentialPredicate::Projection(b),
78 ) => Ok(ep_a.rebind(ty::ExistentialPredicate::Projection(
79 relation.relate(ep_a.rebind(a), ep_b.rebind(b))?.skip_binder(),
80 ))),
81 (
82 ty::ExistentialPredicate::AutoTrait(a),
83 ty::ExistentialPredicate::AutoTrait(b),
84 ) if a == b => Ok(ep_a.rebind(ty::ExistentialPredicate::AutoTrait(a))),
85 _ => Err(TypeError::ExistentialMismatch(ExpectedFound::new(a, b))),
86 });
87 tcx.mk_poly_existential_predicates_from_iter(v)
88 }
89}
90
91impl<'tcx> Relate<TyCtxt<'tcx>> for ty::GenericArgsRef<'tcx> {
92 fn relate<R: TypeRelation<TyCtxt<'tcx>>>(
93 relation: &mut R,
94 a: ty::GenericArgsRef<'tcx>,
95 b: ty::GenericArgsRef<'tcx>,
96 ) -> RelateResult<'tcx, ty::GenericArgsRef<'tcx>> {
97 relate_args_invariantly(relation, a, b)
98 }
99}
100
101impl<'tcx> Relate<TyCtxt<'tcx>> for ty::Region<'tcx> {
102 fn relate<R: TypeRelation<TyCtxt<'tcx>>>(
103 relation: &mut R,
104 a: ty::Region<'tcx>,
105 b: ty::Region<'tcx>,
106 ) -> RelateResult<'tcx, ty::Region<'tcx>> {
107 relation.regions(a, b)
108 }
109}
110
111impl<'tcx> Relate<TyCtxt<'tcx>> for ty::Const<'tcx> {
112 fn relate<R: TypeRelation<TyCtxt<'tcx>>>(
113 relation: &mut R,
114 a: ty::Const<'tcx>,
115 b: ty::Const<'tcx>,
116 ) -> RelateResult<'tcx, ty::Const<'tcx>> {
117 relation.consts(a, b)
118 }
119}
120
121impl<'tcx> Relate<TyCtxt<'tcx>> for ty::Expr<'tcx> {
122 fn relate<R: TypeRelation<TyCtxt<'tcx>>>(
123 relation: &mut R,
124 ae: ty::Expr<'tcx>,
125 be: ty::Expr<'tcx>,
126 ) -> RelateResult<'tcx, ty::Expr<'tcx>> {
127 match (ae.kind, be.kind) {
134 (ty::ExprKind::Binop(a_binop), ty::ExprKind::Binop(b_binop)) if a_binop == b_binop => {}
135 (ty::ExprKind::UnOp(a_unop), ty::ExprKind::UnOp(b_unop)) if a_unop == b_unop => {}
136 (ty::ExprKind::FunctionCall, ty::ExprKind::FunctionCall) => {}
137 (ty::ExprKind::Cast(a_kind), ty::ExprKind::Cast(b_kind)) if a_kind == b_kind => {}
138 _ => return Err(TypeError::Mismatch),
139 }
140
141 let args = relation.relate(ae.args(), be.args())?;
142 Ok(ty::Expr::new(ae.kind, args))
143 }
144}
145
146impl<'tcx> Relate<TyCtxt<'tcx>> for ty::GenericArg<'tcx> {
147 fn relate<R: TypeRelation<TyCtxt<'tcx>>>(
148 relation: &mut R,
149 a: ty::GenericArg<'tcx>,
150 b: ty::GenericArg<'tcx>,
151 ) -> RelateResult<'tcx, ty::GenericArg<'tcx>> {
152 match (a.kind(), b.kind()) {
153 (ty::GenericArgKind::Lifetime(a_lt), ty::GenericArgKind::Lifetime(b_lt)) => {
154 Ok(relation.relate(a_lt, b_lt)?.into())
155 }
156 (ty::GenericArgKind::Type(a_ty), ty::GenericArgKind::Type(b_ty)) => {
157 Ok(relation.relate(a_ty, b_ty)?.into())
158 }
159 (ty::GenericArgKind::Const(a_ct), ty::GenericArgKind::Const(b_ct)) => {
160 Ok(relation.relate(a_ct, b_ct)?.into())
161 }
162 _ => bug!("impossible case reached: can't relate: {a:?} with {b:?}"),
163 }
164 }
165}
166
167impl<'tcx> Relate<TyCtxt<'tcx>> for ty::Term<'tcx> {
168 fn relate<R: TypeRelation<TyCtxt<'tcx>>>(
169 relation: &mut R,
170 a: Self,
171 b: Self,
172 ) -> RelateResult<'tcx, Self> {
173 Ok(match (a.kind(), b.kind()) {
174 (ty::TermKind::Ty(a), ty::TermKind::Ty(b)) => relation.relate(a, b)?.into(),
175 (ty::TermKind::Const(a), ty::TermKind::Const(b)) => relation.relate(a, b)?.into(),
176 _ => return Err(TypeError::Mismatch),
177 })
178 }
179}