Skip to content

Commit a04088b

Browse files
committed
simplify base_struct function
1 parent b63aea5 commit a04088b

File tree

6 files changed

+47
-146
lines changed

6 files changed

+47
-146
lines changed

compiler/rustc_hir_typeck/src/demand.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
use crate::FnCtxt;
2+
use rustc_infer::infer::base_struct::base_struct;
3+
use rustc_infer::infer::InferOk;
4+
use rustc_middle::middle::stability::EvalResult;
5+
use rustc_trait_selection::infer::InferCtxtExt as _;
6+
use rustc_trait_selection::traits::ObligationCause;
27
use rustc_ast::util::parser::PREC_POSTFIX;
38
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
49
use rustc_hir as hir;
@@ -181,7 +186,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
181186
expected: Ty<'tcx>,
182187
actual: Ty<'tcx>,
183188
) -> Option<DiagnosticBuilder<'tcx, ErrorGuaranteed>> {
184-
match self.at(cause, self.param_env).base_struct(expected, actual) {
189+
let at = self.at(cause, self.param_env);
190+
match base_struct(at, expected, actual) {
185191
Ok(InferOk { obligations, value: () }) => {
186192
self.register_predicates(obligations);
187193
None

compiler/rustc_infer/src/infer/at.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -218,17 +218,6 @@ impl<'a, 'tcx> At<'a, 'tcx> {
218218
self.trace(expected, actual).glb(expected, actual)
219219
}
220220

221-
pub fn base_struct<T>(
222-
self,
223-
expected: T,
224-
actual: T,
225-
) -> InferResult<'tcx, ()>
226-
where
227-
T: ToTrace<'tcx>,
228-
{
229-
self.trace(expected, actual).base_struct(expected, actual)
230-
}
231-
232221
/// Sets the "trace" values that will be used for
233222
/// error-reporting, but doesn't actually perform any operation
234223
/// yet (this is useful when you want to set the trace using
@@ -270,21 +259,6 @@ impl<'a, 'tcx> Trace<'a, 'tcx> {
270259
})
271260
}
272261

273-
#[instrument(skip(self), level = "debug")]
274-
pub fn base_struct<T>(self, a: T, b: T) -> InferResult<'tcx, ()>
275-
where
276-
T: Relate<'tcx>,
277-
{
278-
let Trace { at, trace, a_is_expected } = self;
279-
at.infcx.commit_if_ok(|_| {
280-
let mut fields = at.infcx.combine_fields(trace, at.param_env, at.define_opaque_types);
281-
fields
282-
.base_struct(a_is_expected)
283-
.relate(a, b)
284-
.map(move |_| InferOk { value: (), obligations: fields.obligations })
285-
})
286-
}
287-
288262
/// Makes `a == b`; the expectation is set by the call to
289263
/// `trace()`.
290264
#[instrument(skip(self), level = "debug")]
Lines changed: 38 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,54 @@
1-
use std::{iter, mem};
1+
use crate::infer::at::{At, ToTrace};
22
use crate::infer::sub::Sub;
3+
use crate::infer::{InferOk, InferResult};
34
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};
67
use rustc_span::def_id::DefId;
8+
use std::iter;
79

8-
pub struct BaseStruct<'combine, 'infcx, 'tcx> {
9-
sub: Sub<'combine, 'infcx, 'tcx>,
10-
}
1110

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+
})
1819
}
1920

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),
4928
}
29+
}
5030

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!(
5838
"relate_item_substs(item_def_id={:?}, a_subst={:?}, b_subst={:?})",
5939
item_def_id, a_subst, b_subst
6040
);
6141

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);
6444

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+
});
7352

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(())
12954
}

compiler/rustc_infer/src/infer/combine.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ use rustc_middle::ty::subst::SubstsRef;
4040
use rustc_middle::ty::{self, InferConst, ToPredicate, Ty, TyCtxt, TypeVisitable};
4141
use rustc_middle::ty::{IntType, UintType};
4242
use rustc_span::{Span, DUMMY_SP};
43-
use crate::infer::base_struct::BaseStruct;
4443

4544
#[derive(Clone)]
4645
pub struct CombineFields<'infcx, 'tcx> {
@@ -301,10 +300,6 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
301300
Sub::new(self, a_is_expected)
302301
}
303302

304-
pub fn base_struct<'a>(&'a mut self, a_is_expected: bool) -> BaseStruct<'a, 'infcx, 'tcx> {
305-
BaseStruct::new(Sub::new(self, a_is_expected))
306-
}
307-
308303
pub fn lub<'a>(&'a mut self, a_is_expected: bool) -> Lub<'a, 'infcx, 'tcx> {
309304
Lub::new(self, a_is_expected)
310305
}

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub mod resolve;
7171
mod sub;
7272
pub mod type_variable;
7373
mod undo_log;
74-
mod base_struct;
74+
pub mod base_struct;
7575

7676
#[must_use]
7777
#[derive(Debug)]

compiler/rustc_middle/src/ty/relate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ pub fn relate_substs_with_variances<'tcx, R: TypeRelation<'tcx>>(
164164
tcx.mk_substs(params)
165165
}
166166

167+
#[inline]
167168
pub fn relate_generic_arg<'tcx, R: TypeRelation<'tcx>>(
168169
relation: &mut R,
169170
variances: &[ty::Variance],

0 commit comments

Comments
 (0)