|
1 | | -use std::{iter, mem}; |
| 1 | +use crate::infer::at::{At, ToTrace}; |
2 | 2 | use crate::infer::sub::Sub; |
| 3 | +use crate::infer::{InferOk, InferResult}; |
3 | 4 | use rustc_middle::ty; |
4 | | -use rustc_middle::ty::relate::{Cause, Relate, relate_generic_arg, RelateResult, TypeRelation}; |
5 | | -use rustc_middle::ty::{Subst, SubstsRef, Ty, TyCtxt}; |
| 5 | +use rustc_middle::ty::relate::{relate_generic_arg, RelateResult, TypeRelation}; |
| 6 | +use rustc_middle::ty::{GenericArg, SubstsRef, Ty}; |
6 | 7 | use rustc_span::def_id::DefId; |
| 8 | +use std::iter; |
7 | 9 |
|
8 | | -pub struct BaseStruct<'combine, 'infcx, 'tcx> { |
9 | | - sub: Sub<'combine, 'infcx, 'tcx>, |
10 | | -} |
11 | 10 |
|
12 | | -impl<'combine, 'infcx, 'tcx> BaseStruct<'combine, 'infcx, 'tcx> { |
13 | | - pub fn new( |
14 | | - sub: Sub<'combine, 'infcx, 'tcx>, |
15 | | - ) -> Self { |
16 | | - BaseStruct { sub } |
17 | | - } |
| 11 | +pub fn base_struct<'a, 'tcx>(at: At<'a, 'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> InferResult<'tcx, ()> { |
| 12 | + let trace = ToTrace::to_trace(at.infcx.tcx, at.cause, true, a, b); |
| 13 | + at.infcx.commit_if_ok(|_| { |
| 14 | + let mut fields = at.infcx.combine_fields(trace, at.param_env, at.define_opaque_types); |
| 15 | + let mut sub = Sub::new(&mut fields, true); |
| 16 | + base_struct_tys(&mut sub, a, b) |
| 17 | + .map(move |_| InferOk { value: (), obligations: fields.obligations }) |
| 18 | + }) |
18 | 19 | } |
19 | 20 |
|
20 | | -impl<'tcx> TypeRelation<'tcx> for BaseStruct<'_, '_, 'tcx> { |
21 | | - fn tag(&self) -> &'static str { |
22 | | - "BaseStruct" |
23 | | - } |
24 | | - |
25 | | - #[inline(always)] |
26 | | - fn tcx(&self) -> TyCtxt<'tcx> { |
27 | | - self.sub.tcx() |
28 | | - } |
29 | | - |
30 | | - #[inline(always)] |
31 | | - fn param_env(&self) -> ty::ParamEnv<'tcx> { |
32 | | - self.sub.param_env() |
33 | | - } |
34 | | - |
35 | | - #[inline(always)] |
36 | | - fn a_is_expected(&self) -> bool { |
37 | | - self.sub.a_is_expected() |
38 | | - } |
39 | | - |
40 | | - #[inline(always)] |
41 | | - fn with_cause<F, R>(&mut self, cause: Cause, f: F) -> R |
42 | | - where |
43 | | - F: FnOnce(&mut Self) -> R, |
44 | | - { |
45 | | - let old_cause = mem::replace(&mut self.sub.fields.cause, Some(cause)); |
46 | | - let r = f(self); |
47 | | - self.sub.fields.cause = old_cause; |
48 | | - r |
| 21 | +pub fn base_struct_tys<'tcx>(sub: &mut Sub<'_, '_, 'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, ()> { |
| 22 | + match (a.kind(), b.kind()) { |
| 23 | + (&ty::Adt(a_def, a_substs), &ty::Adt(b_def, b_substs)) if a_def == b_def => { |
| 24 | + base_struct_substs(sub, a_def.did(), a_substs, b_substs)?; |
| 25 | + Ok(()) |
| 26 | + } |
| 27 | + _ => bug!("not adt ty: {:?} and {:?}", a, b), |
49 | 28 | } |
| 29 | +} |
50 | 30 |
|
51 | | - fn relate_item_substs( |
52 | | - &mut self, |
53 | | - item_def_id: DefId, |
54 | | - a_subst: SubstsRef<'tcx>, |
55 | | - b_subst: SubstsRef<'tcx>, |
56 | | - ) -> RelateResult<'tcx, SubstsRef<'tcx>> { |
57 | | - debug!( |
| 31 | +fn base_struct_substs<'tcx>( |
| 32 | + sub: &mut Sub<'_, '_, 'tcx>, |
| 33 | + item_def_id: DefId, |
| 34 | + a_subst: SubstsRef<'tcx>, |
| 35 | + b_subst: SubstsRef<'tcx>, |
| 36 | +) -> RelateResult<'tcx, ()> { |
| 37 | + debug!( |
58 | 38 | "relate_item_substs(item_def_id={:?}, a_subst={:?}, b_subst={:?})", |
59 | 39 | item_def_id, a_subst, b_subst |
60 | 40 | ); |
61 | 41 |
|
62 | | - let tcx = self.tcx(); |
63 | | - let variances = tcx.variances_of(item_def_id); |
| 42 | + let tcx = sub.tcx(); |
| 43 | + let variances = tcx.variances_of(item_def_id); |
64 | 44 |
|
65 | | - let mut cached_ty = None; |
66 | | - let params = iter::zip(a_subst, b_subst).enumerate().map(|(i, (a, b))| { |
67 | | - let cached_ty = |
68 | | - *cached_ty.get_or_insert_with(|| tcx.bound_type_of(item_def_id).subst(tcx, a_subst)); |
69 | | - relate_generic_arg(&mut self.sub, variances, cached_ty, a, b, i).or_else(|_| { |
70 | | - Ok(b) |
71 | | - }) |
72 | | - }); |
| 45 | + let mut cached_ty = None; |
| 46 | + iter::zip(a_subst, b_subst).enumerate().for_each(|(i, (a, b))| { |
| 47 | + let cached_ty = *cached_ty |
| 48 | + .get_or_insert_with(|| tcx.bound_type_of(item_def_id).subst(tcx, a_subst)); |
| 49 | + let _arg: RelateResult<'tcx, GenericArg<'tcx>> = |
| 50 | + relate_generic_arg(sub, variances, cached_ty, a, b, i).or_else(|_| Ok(b)); |
| 51 | + }); |
73 | 52 |
|
74 | | - tcx.mk_substs(params) |
75 | | - } |
76 | | - |
77 | | - #[inline(always)] |
78 | | - fn relate_with_variance<T: Relate<'tcx>>( |
79 | | - &mut self, |
80 | | - variance: ty::Variance, |
81 | | - info: ty::VarianceDiagInfo<'tcx>, |
82 | | - a: T, |
83 | | - b: T, |
84 | | - ) -> RelateResult<'tcx, T> { |
85 | | - self.sub.relate_with_variance(variance, info, a, b) |
86 | | - } |
87 | | - |
88 | | - #[inline(always)] |
89 | | - #[instrument(skip(self), level = "debug")] |
90 | | - fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> { |
91 | | - match (a.kind(), b.kind()) { |
92 | | - (&ty::Adt(a_def, a_substs), &ty::Adt(b_def, b_substs)) if a_def == b_def => { |
93 | | - let substs = self.relate_item_substs(a_def.did(), a_substs, b_substs)?; |
94 | | - Ok(self.tcx().mk_adt(a_def, substs)) |
95 | | - } |
96 | | - _ => bug!("not adt ty: {:?} and {:?}", a, b) |
97 | | - } |
98 | | - } |
99 | | - |
100 | | - #[inline(always)] |
101 | | - fn regions( |
102 | | - &mut self, |
103 | | - a: ty::Region<'tcx>, |
104 | | - b: ty::Region<'tcx>, |
105 | | - ) -> RelateResult<'tcx, ty::Region<'tcx>> { |
106 | | - self.sub.regions(a, b) |
107 | | - } |
108 | | - |
109 | | - #[inline(always)] |
110 | | - fn consts( |
111 | | - &mut self, |
112 | | - a: ty::Const<'tcx>, |
113 | | - b: ty::Const<'tcx>, |
114 | | - ) -> RelateResult<'tcx, ty::Const<'tcx>> { |
115 | | - self.sub.consts(a, b) |
116 | | - } |
117 | | - |
118 | | - #[inline(always)] |
119 | | - fn binders<T>( |
120 | | - &mut self, |
121 | | - a: ty::Binder<'tcx, T>, |
122 | | - b: ty::Binder<'tcx, T>, |
123 | | - ) -> RelateResult<'tcx, ty::Binder<'tcx, T>> |
124 | | - where |
125 | | - T: Relate<'tcx>, |
126 | | - { |
127 | | - self.sub.binders(a, b) |
128 | | - } |
| 53 | + Ok(()) |
129 | 54 | } |
0 commit comments