Skip to content

Commit 05abaed

Browse files
Uplift TypeRelation and Relate
1 parent dcbc704 commit 05abaed

File tree

32 files changed

+1138
-853
lines changed

32 files changed

+1138
-853
lines changed

Cargo.lock

+2
Original file line numberDiff line numberDiff line change
@@ -4007,6 +4007,7 @@ dependencies = [
40074007
"rustc_middle",
40084008
"rustc_span",
40094009
"rustc_target",
4010+
"rustc_type_ir",
40104011
"smallvec",
40114012
"tracing",
40124013
]
@@ -4314,6 +4315,7 @@ dependencies = [
43144315
"rustc_serialize",
43154316
"rustc_type_ir",
43164317
"rustc_type_ir_macros",
4318+
"tracing",
43174319
]
43184320

43194321
[[package]]

compiler/rustc_borrowck/src/constraints/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_data_structures::graph::scc::Sccs;
22
use rustc_index::{IndexSlice, IndexVec};
33
use rustc_middle::mir::ConstraintCategory;
4-
use rustc_middle::ty::{RegionVid, VarianceDiagInfo};
4+
use rustc_middle::ty::{RegionVid, TyCtxt, VarianceDiagInfo};
55
use rustc_span::Span;
66
use std::fmt;
77
use std::ops::Index;

compiler/rustc_borrowck/src/type_check/relate_tys.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use rustc_data_structures::fx::FxHashMap;
22
use rustc_errors::ErrorGuaranteed;
3+
use rustc_infer::infer::relate::{ObligationEmittingRelation, StructurallyRelateAliases};
4+
use rustc_infer::infer::relate::{Relate, RelateResult, TypeRelation};
35
use rustc_infer::infer::NllRegionVariableOrigin;
4-
use rustc_infer::infer::{ObligationEmittingRelation, StructurallyRelateAliases};
56
use rustc_infer::traits::{Obligation, PredicateObligations};
67
use rustc_middle::mir::ConstraintCategory;
78
use rustc_middle::span_bug;
89
use rustc_middle::traits::query::NoSolution;
910
use rustc_middle::traits::ObligationCause;
1011
use rustc_middle::ty::fold::FnMutDelegate;
11-
use rustc_middle::ty::relate::{Relate, RelateResult, TypeRelation};
1212
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
1313
use rustc_span::symbol::sym;
1414
use rustc_span::{Span, Symbol};
@@ -314,7 +314,7 @@ impl<'me, 'bccx, 'tcx> NllTypeRelating<'me, 'bccx, 'tcx> {
314314
}
315315
}
316316

317-
impl<'bccx, 'tcx> TypeRelation<'tcx> for NllTypeRelating<'_, 'bccx, 'tcx> {
317+
impl<'bccx, 'tcx> TypeRelation<TyCtxt<'tcx>> for NllTypeRelating<'_, 'bccx, 'tcx> {
318318
fn tcx(&self) -> TyCtxt<'tcx> {
319319
self.type_checker.infcx.tcx
320320
}
@@ -324,7 +324,7 @@ impl<'bccx, 'tcx> TypeRelation<'tcx> for NllTypeRelating<'_, 'bccx, 'tcx> {
324324
}
325325

326326
#[instrument(skip(self, info), level = "trace", ret)]
327-
fn relate_with_variance<T: Relate<'tcx>>(
327+
fn relate_with_variance<T: Relate<TyCtxt<'tcx>>>(
328328
&mut self,
329329
variance: ty::Variance,
330330
info: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
@@ -445,7 +445,7 @@ impl<'bccx, 'tcx> TypeRelation<'tcx> for NllTypeRelating<'_, 'bccx, 'tcx> {
445445
b: ty::Binder<'tcx, T>,
446446
) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
447447
where
448-
T: Relate<'tcx>,
448+
T: Relate<TyCtxt<'tcx>>,
449449
{
450450
// We want that
451451
//

compiler/rustc_hir_typeck/src/coercion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use rustc_errors::{codes::*, struct_span_code_err, Applicability, Diag};
4141
use rustc_hir as hir;
4242
use rustc_hir::def_id::{DefId, LocalDefId};
4343
use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
44+
use rustc_infer::infer::relate::RelateResult;
4445
use rustc_infer::infer::{Coercion, DefineOpaqueTypes, InferOk, InferResult};
4546
use rustc_infer::traits::{IfExpressionCause, MatchExpressionArmCause};
4647
use rustc_infer::traits::{Obligation, PredicateObligation};
@@ -51,7 +52,6 @@ use rustc_middle::ty::adjustment::{
5152
Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability, PointerCoercion,
5253
};
5354
use rustc_middle::ty::error::TypeError;
54-
use rustc_middle::ty::relate::RelateResult;
5555
use rustc_middle::ty::visit::TypeVisitableExt;
5656
use rustc_middle::ty::{self, GenericArgsRef, Ty, TyCtxt};
5757
use rustc_session::parse::feature_err;

compiler/rustc_infer/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ rustc_macros = { path = "../rustc_macros" }
1818
rustc_middle = { path = "../rustc_middle" }
1919
rustc_span = { path = "../rustc_span" }
2020
rustc_target = { path = "../rustc_target" }
21+
rustc_type_ir = { path = "../rustc_type_ir" }
2122
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
2223
tracing = "0.1"
2324
# tidy-alphabetical-end

compiler/rustc_infer/src/infer/at.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
2828
use super::*;
2929

30+
use crate::infer::relate::{Relate, StructurallyRelateAliases, TypeRelation};
3031
use rustc_middle::bug;
31-
use rustc_middle::ty::relate::{Relate, TypeRelation};
3232
use rustc_middle::ty::{Const, ImplSubject};
3333

3434
/// Whether we should define opaque types or just treat them opaquely.
@@ -90,7 +90,7 @@ impl<'tcx> InferCtxt<'tcx> {
9090
}
9191
}
9292

93-
pub trait ToTrace<'tcx>: Relate<'tcx> + Copy {
93+
pub trait ToTrace<'tcx>: Relate<TyCtxt<'tcx>> + Copy {
9494
fn to_trace(
9595
cause: &ObligationCause<'tcx>,
9696
a_is_expected: bool,

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ use crate::traits::{
5858
PredicateObligation,
5959
};
6060

61+
use crate::infer::relate::{self, RelateResult, TypeRelation};
6162
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
6263
use rustc_errors::{
6364
codes::*, pluralize, struct_span_code_err, Applicability, Diag, DiagCtxt, DiagStyledString,
@@ -73,7 +74,6 @@ use rustc_middle::bug;
7374
use rustc_middle::dep_graph::DepContext;
7475
use rustc_middle::ty::error::TypeErrorToStringExt;
7576
use rustc_middle::ty::print::{with_forced_trimmed_paths, PrintError, PrintTraitRefExt as _};
76-
use rustc_middle::ty::relate::{self, RelateResult, TypeRelation};
7777
use rustc_middle::ty::Upcast;
7878
use rustc_middle::ty::{
7979
self, error::TypeError, IsSuggestable, List, Region, Ty, TyCtxt, TypeFoldable,
@@ -2687,15 +2687,15 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
26872687
/// with the other type. A TyVar inference type is compatible with any type, and an IntVar or
26882688
/// FloatVar inference type are compatible with themselves or their concrete types (Int and
26892689
/// Float types, respectively). When comparing two ADTs, these rules apply recursively.
2690-
pub fn same_type_modulo_infer<T: relate::Relate<'tcx>>(&self, a: T, b: T) -> bool {
2690+
pub fn same_type_modulo_infer<T: relate::Relate<TyCtxt<'tcx>>>(&self, a: T, b: T) -> bool {
26912691
let (a, b) = self.resolve_vars_if_possible((a, b));
26922692
SameTypeModuloInfer(self).relate(a, b).is_ok()
26932693
}
26942694
}
26952695

26962696
struct SameTypeModuloInfer<'a, 'tcx>(&'a InferCtxt<'tcx>);
26972697

2698-
impl<'tcx> TypeRelation<'tcx> for SameTypeModuloInfer<'_, 'tcx> {
2698+
impl<'tcx> TypeRelation<TyCtxt<'tcx>> for SameTypeModuloInfer<'_, 'tcx> {
26992699
fn tcx(&self) -> TyCtxt<'tcx> {
27002700
self.0.tcx
27012701
}
@@ -2704,7 +2704,7 @@ impl<'tcx> TypeRelation<'tcx> for SameTypeModuloInfer<'_, 'tcx> {
27042704
"SameTypeModuloInfer"
27052705
}
27062706

2707-
fn relate_with_variance<T: relate::Relate<'tcx>>(
2707+
fn relate_with_variance<T: relate::Relate<TyCtxt<'tcx>>>(
27082708
&mut self,
27092709
_variance: ty::Variance,
27102710
_info: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
@@ -2755,7 +2755,7 @@ impl<'tcx> TypeRelation<'tcx> for SameTypeModuloInfer<'_, 'tcx> {
27552755
b: ty::Binder<'tcx, T>,
27562756
) -> relate::RelateResult<'tcx, ty::Binder<'tcx, T>>
27572757
where
2758-
T: relate::Relate<'tcx>,
2758+
T: relate::Relate<TyCtxt<'tcx>>,
27592759
{
27602760
Ok(a.rebind(self.relate(a.skip_binder(), b.skip_binder())?))
27612761
}

compiler/rustc_infer/src/infer/mod.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
pub use at::DefineOpaqueTypes;
22
pub use freshen::TypeFreshener;
33
pub use lexical_region_resolve::RegionResolutionError;
4-
pub use relate::combine::CombineFields;
5-
pub use relate::combine::ObligationEmittingRelation;
6-
pub use relate::StructurallyRelateAliases;
74
pub use rustc_macros::{TypeFoldable, TypeVisitable};
85
pub use rustc_middle::ty::IntVarValue;
96
pub use BoundRegionConversionTime::*;
107
pub use RegionVariableOrigin::*;
118
pub use SubregionOrigin::*;
129
pub use ValuePairs::*;
1310

11+
use crate::infer::relate::{CombineFields, RelateResult};
1412
use crate::traits::{
1513
self, ObligationCause, ObligationInspector, PredicateObligations, TraitEngine, TraitEngineExt,
1614
};
@@ -39,7 +37,6 @@ use rustc_middle::traits::select;
3937
use rustc_middle::ty::error::{ExpectedFound, TypeError};
4038
use rustc_middle::ty::fold::BoundVarReplacerDelegate;
4139
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
42-
use rustc_middle::ty::relate::RelateResult;
4340
use rustc_middle::ty::visit::TypeVisitableExt;
4441
use rustc_middle::ty::{self, GenericParamDefKind, InferConst, InferTy, Ty, TyCtxt};
4542
use rustc_middle::ty::{ConstVid, EffectVid, FloatVid, IntVid, TyVid};
@@ -62,7 +59,7 @@ pub mod opaque_types;
6259
pub mod outlives;
6360
mod projection;
6461
pub mod region_constraints;
65-
mod relate;
62+
pub mod relate;
6663
pub mod resolve;
6764
pub(crate) mod snapshot;
6865
pub mod type_variable;

compiler/rustc_infer/src/infer/outlives/test_type_match.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
use std::collections::hash_map::Entry;
22

33
use rustc_data_structures::fx::FxHashMap;
4+
use rustc_middle::ty::error::TypeError;
45
use rustc_middle::ty::TypeVisitableExt;
5-
use rustc_middle::ty::{
6-
self,
7-
error::TypeError,
8-
relate::{self, Relate, RelateResult, TypeRelation},
9-
Ty, TyCtxt,
10-
};
6+
use rustc_middle::ty::{self, Ty, TyCtxt};
117

128
use crate::infer::region_constraints::VerifyIfEq;
9+
use crate::infer::relate::{self as relate, Relate, RelateResult, TypeRelation};
1310

1411
/// Given a "verify-if-eq" type test like:
1512
///
@@ -135,7 +132,7 @@ impl<'tcx> MatchAgainstHigherRankedOutlives<'tcx> {
135132
}
136133
}
137134

138-
impl<'tcx> TypeRelation<'tcx> for MatchAgainstHigherRankedOutlives<'tcx> {
135+
impl<'tcx> TypeRelation<TyCtxt<'tcx>> for MatchAgainstHigherRankedOutlives<'tcx> {
139136
fn tag(&self) -> &'static str {
140137
"MatchAgainstHigherRankedOutlives"
141138
}
@@ -145,7 +142,7 @@ impl<'tcx> TypeRelation<'tcx> for MatchAgainstHigherRankedOutlives<'tcx> {
145142
}
146143

147144
#[instrument(level = "trace", skip(self))]
148-
fn relate_with_variance<T: Relate<'tcx>>(
145+
fn relate_with_variance<T: Relate<TyCtxt<'tcx>>>(
149146
&mut self,
150147
variance: ty::Variance,
151148
_: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
@@ -208,7 +205,7 @@ impl<'tcx> TypeRelation<'tcx> for MatchAgainstHigherRankedOutlives<'tcx> {
208205
value: ty::Binder<'tcx, T>,
209206
) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
210207
where
211-
T: Relate<'tcx>,
208+
T: Relate<TyCtxt<'tcx>>,
212209
{
213210
self.pattern_depth.shift_in(1);
214211
let result = Ok(pattern.rebind(self.relate(pattern.skip_binder(), value.skip_binder())?));

compiler/rustc_infer/src/infer/region_constraints/leak_check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use super::*;
2+
use crate::infer::relate::RelateResult;
23
use crate::infer::snapshot::CombinedSnapshot;
34
use rustc_data_structures::fx::FxIndexMap;
45
use rustc_data_structures::graph::{scc::Sccs, vec_graph::VecGraph};
56
use rustc_index::Idx;
67
use rustc_middle::span_bug;
78
use rustc_middle::ty::error::TypeError;
8-
use rustc_middle::ty::relate::RelateResult;
99

1010
impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
1111
/// Searches new universes created during `snapshot`, looking for

compiler/rustc_middle/src/ty/_match.rs renamed to compiler/rustc_infer/src/infer/relate/_match.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
use crate::ty::error::TypeError;
2-
use crate::ty::relate::{self, Relate, RelateResult, TypeRelation};
3-
use crate::ty::{self, InferConst, Ty, TyCtxt};
1+
use rustc_middle::ty::error::{ExpectedFound, TypeError};
2+
use rustc_middle::ty::{self, InferConst, Ty, TyCtxt};
43
use tracing::{debug, instrument};
54

5+
use super::{structurally_relate_tys, Relate, RelateResult, TypeRelation};
6+
use crate::infer::relate;
7+
68
/// A type "A" *matches* "B" if the fresh types in B could be
79
/// instantiated with values so as to make it equal to A. Matching is
810
/// intended to be used only on freshened types, and it basically
@@ -29,7 +31,7 @@ impl<'tcx> MatchAgainstFreshVars<'tcx> {
2931
}
3032
}
3133

32-
impl<'tcx> TypeRelation<'tcx> for MatchAgainstFreshVars<'tcx> {
34+
impl<'tcx> TypeRelation<TyCtxt<'tcx>> for MatchAgainstFreshVars<'tcx> {
3335
fn tag(&self) -> &'static str {
3436
"MatchAgainstFreshVars"
3537
}
@@ -38,7 +40,7 @@ impl<'tcx> TypeRelation<'tcx> for MatchAgainstFreshVars<'tcx> {
3840
self.tcx
3941
}
4042

41-
fn relate_with_variance<T: Relate<'tcx>>(
43+
fn relate_with_variance<T: Relate<TyCtxt<'tcx>>>(
4244
&mut self,
4345
_: ty::Variance,
4446
_: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
@@ -72,12 +74,12 @@ impl<'tcx> TypeRelation<'tcx> for MatchAgainstFreshVars<'tcx> {
7274
) => Ok(a),
7375

7476
(&ty::Infer(_), _) | (_, &ty::Infer(_)) => {
75-
Err(TypeError::Sorts(relate::expected_found(a, b)))
77+
Err(TypeError::Sorts(ExpectedFound::new(true, a, b)))
7678
}
7779

7880
(&ty::Error(guar), _) | (_, &ty::Error(guar)) => Ok(Ty::new_error(self.tcx(), guar)),
7981

80-
_ => relate::structurally_relate_tys(self, a, b),
82+
_ => structurally_relate_tys(self, a, b),
8183
}
8284
}
8385

@@ -97,7 +99,7 @@ impl<'tcx> TypeRelation<'tcx> for MatchAgainstFreshVars<'tcx> {
9799
}
98100

99101
(ty::ConstKind::Infer(_), _) | (_, ty::ConstKind::Infer(_)) => {
100-
return Err(TypeError::ConstMismatch(relate::expected_found(a, b)));
102+
return Err(TypeError::ConstMismatch(ExpectedFound::new(true, a, b)));
101103
}
102104

103105
_ => {}
@@ -112,7 +114,7 @@ impl<'tcx> TypeRelation<'tcx> for MatchAgainstFreshVars<'tcx> {
112114
b: ty::Binder<'tcx, T>,
113115
) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
114116
where
115-
T: Relate<'tcx>,
117+
T: Relate<TyCtxt<'tcx>>,
116118
{
117119
Ok(a.rebind(self.relate(a.skip_binder(), b.skip_binder())?))
118120
}

compiler/rustc_infer/src/infer/relate/combine.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ use super::glb::Glb;
2222
use super::lub::Lub;
2323
use super::type_relating::TypeRelating;
2424
use super::StructurallyRelateAliases;
25+
use super::{RelateResult, TypeRelation};
26+
use crate::infer::relate;
2527
use crate::infer::{DefineOpaqueTypes, InferCtxt, TypeTrace};
2628
use crate::traits::{Obligation, PredicateObligations};
2729
use rustc_middle::bug;
2830
use rustc_middle::infer::unify_key::EffectVarValue;
2931
use rustc_middle::ty::error::{ExpectedFound, TypeError};
30-
use rustc_middle::ty::relate::{RelateResult, TypeRelation};
3132
use rustc_middle::ty::{self, InferConst, Ty, TyCtxt, TypeVisitableExt, Upcast};
3233
use rustc_middle::ty::{IntType, UintType};
3334
use rustc_span::Span;
@@ -123,7 +124,7 @@ impl<'tcx> InferCtxt<'tcx> {
123124
(_, ty::Alias(..)) | (ty::Alias(..), _) if self.next_trait_solver() => {
124125
match relation.structurally_relate_aliases() {
125126
StructurallyRelateAliases::Yes => {
126-
ty::relate::structurally_relate_tys(relation, a, b)
127+
relate::structurally_relate_tys(relation, a, b)
127128
}
128129
StructurallyRelateAliases::No => {
129130
relation.register_type_relate_obligation(a, b);
@@ -134,7 +135,7 @@ impl<'tcx> InferCtxt<'tcx> {
134135

135136
// All other cases of inference are errors
136137
(&ty::Infer(_), _) | (_, &ty::Infer(_)) => {
137-
Err(TypeError::Sorts(ty::relate::expected_found(a, b)))
138+
Err(TypeError::Sorts(ExpectedFound::new(true, a, b)))
138139
}
139140

140141
// During coherence, opaque types should be treated as *possibly*
@@ -146,7 +147,7 @@ impl<'tcx> InferCtxt<'tcx> {
146147
Ok(a)
147148
}
148149

149-
_ => ty::relate::structurally_relate_tys(relation, a, b),
150+
_ => relate::structurally_relate_tys(relation, a, b),
150151
}
151152
}
152153

@@ -236,11 +237,11 @@ impl<'tcx> InferCtxt<'tcx> {
236237
Ok(b)
237238
}
238239
StructurallyRelateAliases::Yes => {
239-
ty::relate::structurally_relate_consts(relation, a, b)
240+
relate::structurally_relate_consts(relation, a, b)
240241
}
241242
}
242243
}
243-
_ => ty::relate::structurally_relate_consts(relation, a, b),
244+
_ => relate::structurally_relate_consts(relation, a, b),
244245
}
245246
}
246247

@@ -326,7 +327,7 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
326327
}
327328
}
328329

329-
pub trait ObligationEmittingRelation<'tcx>: TypeRelation<'tcx> {
330+
pub trait ObligationEmittingRelation<'tcx>: TypeRelation<TyCtxt<'tcx>> {
330331
fn span(&self) -> Span;
331332

332333
fn param_env(&self) -> ty::ParamEnv<'tcx>;

0 commit comments

Comments
 (0)