Skip to content

Commit e293ba0

Browse files
Uplift TypeRelation and Relate
1 parent 7158d55 commit e293ba0

File tree

32 files changed

+1338
-985
lines changed

32 files changed

+1338
-985
lines changed

Diff for: Cargo.lock

+2
Original file line numberDiff line numberDiff line change
@@ -4005,6 +4005,7 @@ dependencies = [
40054005
"rustc_index",
40064006
"rustc_macros",
40074007
"rustc_middle",
4008+
"rustc_next_trait_solver",
40084009
"rustc_span",
40094010
"rustc_target",
40104011
"smallvec",
@@ -4314,6 +4315,7 @@ dependencies = [
43144315
"rustc_serialize",
43154316
"rustc_type_ir",
43164317
"rustc_type_ir_macros",
4318+
"tracing",
43174319
]
43184320

43194321
[[package]]

Diff for: 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;

Diff for: compiler/rustc_borrowck/src/type_check/relate_tys.rs

+6-6
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::NllRegionVariableOrigin;
4-
use rustc_infer::infer::{ObligationEmittingRelation, StructurallyRelateAliases};
3+
use rustc_infer::infer::relate::{ObligationEmittingRelation, StructurallyRelateAliases};
4+
use rustc_infer::infer::relate::{Relate, RelateResult, TypeRelation};
5+
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin};
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<InferCtxt<'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<InferCtxt<'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<InferCtxt<'tcx>>,
449449
{
450450
// We want that
451451
//

Diff for: 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;

Diff for: compiler/rustc_infer/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ rustc_hir = { path = "../rustc_hir" }
1616
rustc_index = { path = "../rustc_index" }
1717
rustc_macros = { path = "../rustc_macros" }
1818
rustc_middle = { path = "../rustc_middle" }
19+
rustc_next_trait_solver = { path = "../rustc_next_trait_solver" }
1920
rustc_span = { path = "../rustc_span" }
2021
rustc_target = { path = "../rustc_target" }
2122
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }

Diff for: 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<InferCtxt<'tcx>> + Copy {
9494
fn to_trace(
9595
cause: &ObligationCause<'tcx>,
9696
a_is_expected: bool,

Diff for: 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<InferCtxt<'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<InferCtxt<'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<InferCtxt<'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<InferCtxt<'tcx>>,
27592759
{
27602760
Ok(a.rebind(self.relate(a.skip_binder(), b.skip_binder())?))
27612761
}

Diff for: 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;

Diff for: compiler/rustc_infer/src/infer/outlives/test_type_match.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
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};
10+
use crate::infer::InferCtxt;
1311

1412
/// Given a "verify-if-eq" type test like:
1513
///
@@ -135,7 +133,7 @@ impl<'tcx> MatchAgainstHigherRankedOutlives<'tcx> {
135133
}
136134
}
137135

138-
impl<'tcx> TypeRelation<'tcx> for MatchAgainstHigherRankedOutlives<'tcx> {
136+
impl<'tcx> TypeRelation<InferCtxt<'tcx>> for MatchAgainstHigherRankedOutlives<'tcx> {
139137
fn tag(&self) -> &'static str {
140138
"MatchAgainstHigherRankedOutlives"
141139
}
@@ -145,7 +143,7 @@ impl<'tcx> TypeRelation<'tcx> for MatchAgainstHigherRankedOutlives<'tcx> {
145143
}
146144

147145
#[instrument(level = "trace", skip(self))]
148-
fn relate_with_variance<T: Relate<'tcx>>(
146+
fn relate_with_variance<T: Relate<InferCtxt<'tcx>>>(
149147
&mut self,
150148
variance: ty::Variance,
151149
_: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
@@ -208,7 +206,7 @@ impl<'tcx> TypeRelation<'tcx> for MatchAgainstHigherRankedOutlives<'tcx> {
208206
value: ty::Binder<'tcx, T>,
209207
) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
210208
where
211-
T: Relate<'tcx>,
209+
T: Relate<InferCtxt<'tcx>>,
212210
{
213211
self.pattern_depth.shift_in(1);
214212
let result = Ok(pattern.rebind(self.relate(pattern.skip_binder(), value.skip_binder())?));

Diff for: 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

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

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
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+
use crate::infer::InferCtxt;
8+
69
/// A type "A" *matches* "B" if the fresh types in B could be
710
/// instantiated with values so as to make it equal to A. Matching is
811
/// intended to be used only on freshened types, and it basically
@@ -29,7 +32,7 @@ impl<'tcx> MatchAgainstFreshVars<'tcx> {
2932
}
3033
}
3134

32-
impl<'tcx> TypeRelation<'tcx> for MatchAgainstFreshVars<'tcx> {
35+
impl<'tcx> TypeRelation<InferCtxt<'tcx>> for MatchAgainstFreshVars<'tcx> {
3336
fn tag(&self) -> &'static str {
3437
"MatchAgainstFreshVars"
3538
}
@@ -38,7 +41,7 @@ impl<'tcx> TypeRelation<'tcx> for MatchAgainstFreshVars<'tcx> {
3841
self.tcx
3942
}
4043

41-
fn relate_with_variance<T: Relate<'tcx>>(
44+
fn relate_with_variance<T: Relate<InferCtxt<'tcx>>>(
4245
&mut self,
4346
_: ty::Variance,
4447
_: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
@@ -72,12 +75,12 @@ impl<'tcx> TypeRelation<'tcx> for MatchAgainstFreshVars<'tcx> {
7275
) => Ok(a),
7376

7477
(&ty::Infer(_), _) | (_, &ty::Infer(_)) => {
75-
Err(TypeError::Sorts(relate::expected_found(a, b)))
78+
Err(TypeError::Sorts(ExpectedFound::new(true, a, b)))
7679
}
7780

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

80-
_ => relate::structurally_relate_tys(self, a, b),
83+
_ => structurally_relate_tys(self, a, b),
8184
}
8285
}
8386

@@ -97,7 +100,7 @@ impl<'tcx> TypeRelation<'tcx> for MatchAgainstFreshVars<'tcx> {
97100
}
98101

99102
(ty::ConstKind::Infer(_), _) | (_, ty::ConstKind::Infer(_)) => {
100-
return Err(TypeError::ConstMismatch(relate::expected_found(a, b)));
103+
return Err(TypeError::ConstMismatch(ExpectedFound::new(true, a, b)));
101104
}
102105

103106
_ => {}
@@ -112,7 +115,7 @@ impl<'tcx> TypeRelation<'tcx> for MatchAgainstFreshVars<'tcx> {
112115
b: ty::Binder<'tcx, T>,
113116
) -> RelateResult<'tcx, ty::Binder<'tcx, T>>
114117
where
115-
T: Relate<'tcx>,
118+
T: Relate<InferCtxt<'tcx>>,
116119
{
117120
Ok(a.rebind(self.relate(a.skip_binder(), b.skip_binder())?))
118121
}

Diff for: compiler/rustc_infer/src/infer/relate/combine.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ 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, InferOk, TypeTrace};
2628
use crate::traits::{Obligation, PredicateObligations};
2729
use rustc_middle::bug;
2830
use rustc_middle::infer::unify_key::EffectVarValue;
2931
use rustc_middle::traits::ObligationCause;
3032
use rustc_middle::ty::error::{ExpectedFound, TypeError};
31-
use rustc_middle::ty::relate::{RelateResult, TypeRelation};
3233
use rustc_middle::ty::{self, InferConst, Ty, TyCtxt, TypeVisitableExt, Upcast};
3334
use rustc_middle::ty::{IntType, UintType};
3435
use rustc_span::Span;
@@ -124,7 +125,7 @@ impl<'tcx> InferCtxt<'tcx> {
124125
(_, ty::Alias(..)) | (ty::Alias(..), _) if self.next_trait_solver() => {
125126
match relation.structurally_relate_aliases() {
126127
StructurallyRelateAliases::Yes => {
127-
ty::relate::structurally_relate_tys(relation, a, b)
128+
relate::structurally_relate_tys(relation, a, b)
128129
}
129130
StructurallyRelateAliases::No => {
130131
relation.register_type_relate_obligation(a, b);
@@ -135,7 +136,7 @@ impl<'tcx> InferCtxt<'tcx> {
135136

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

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

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

@@ -243,11 +244,11 @@ impl<'tcx> InferCtxt<'tcx> {
243244
Ok(b)
244245
}
245246
StructurallyRelateAliases::Yes => {
246-
ty::relate::structurally_relate_consts(relation, a, b)
247+
relate::structurally_relate_consts(relation, a, b)
247248
}
248249
}
249250
}
250-
_ => ty::relate::structurally_relate_consts(relation, a, b),
251+
_ => relate::structurally_relate_consts(relation, a, b),
251252
}
252253
}
253254

@@ -333,7 +334,7 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
333334
}
334335
}
335336

336-
pub trait ObligationEmittingRelation<'tcx>: TypeRelation<'tcx> {
337+
pub trait ObligationEmittingRelation<'tcx>: TypeRelation<InferCtxt<'tcx>> {
337338
fn span(&self) -> Span;
338339

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

0 commit comments

Comments
 (0)