Skip to content

Commit e99efa6

Browse files
Uplift TypeRelation and Relate
1 parent 2e70f9b commit e99efa6

File tree

34 files changed

+1116
-866
lines changed

34 files changed

+1116
-866
lines changed

Cargo.lock

+2
Original file line numberDiff line numberDiff line change
@@ -4100,6 +4100,7 @@ dependencies = [
41004100
"rustc_middle",
41014101
"rustc_span",
41024102
"rustc_target",
4103+
"rustc_type_ir",
41034104
"smallvec",
41044105
"tracing",
41054106
]
@@ -4407,6 +4408,7 @@ dependencies = [
44074408
"rustc_serialize",
44084409
"rustc_type_ir",
44094410
"rustc_type_ir_macros",
4411+
"tracing",
44104412
]
44114413

44124414
[[package]]

compiler/rustc_borrowck/src/constraints/mod.rs

+2-2
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;
@@ -97,7 +97,7 @@ pub struct OutlivesConstraint<'tcx> {
9797
pub category: ConstraintCategory<'tcx>,
9898

9999
/// Variance diagnostic information
100-
pub variance_info: VarianceDiagInfo<'tcx>,
100+
pub variance_info: VarianceDiagInfo<TyCtxt<'tcx>>,
101101

102102
/// If this constraint is promoted from closure requirements.
103103
pub from_closure: bool,

compiler/rustc_borrowck/src/region_infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2304,5 +2304,5 @@ pub struct BlameConstraint<'tcx> {
23042304
pub category: ConstraintCategory<'tcx>,
23052305
pub from_closure: bool,
23062306
pub cause: ObligationCause<'tcx>,
2307-
pub variance_info: ty::VarianceDiagInfo<'tcx>,
2307+
pub variance_info: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
23082308
}

compiler/rustc_borrowck/src/type_check/relate_tys.rs

+8-8
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};
@@ -82,7 +82,7 @@ pub struct NllTypeRelating<'me, 'bccx, 'tcx> {
8282
/// - Bivariant means that it doesn't matter.
8383
ambient_variance: ty::Variance,
8484

85-
ambient_variance_info: ty::VarianceDiagInfo<'tcx>,
85+
ambient_variance_info: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
8686
}
8787

8888
impl<'me, 'bccx, 'tcx> NllTypeRelating<'me, 'bccx, 'tcx> {
@@ -296,7 +296,7 @@ impl<'me, 'bccx, 'tcx> NllTypeRelating<'me, 'bccx, 'tcx> {
296296
&mut self,
297297
sup: ty::Region<'tcx>,
298298
sub: ty::Region<'tcx>,
299-
info: ty::VarianceDiagInfo<'tcx>,
299+
info: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
300300
) {
301301
let sub = self.type_checker.borrowck_context.universal_regions.to_region_vid(sub);
302302
let sup = self.type_checker.borrowck_context.universal_regions.to_region_vid(sup);
@@ -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,10 +324,10 @@ 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,
330-
info: ty::VarianceDiagInfo<'tcx>,
330+
info: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
331331
a: T,
332332
b: T,
333333
) -> RelateResult<'tcx, T> {
@@ -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

+6-6
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,10 +2704,10 @@ 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,
2710-
_info: ty::VarianceDiagInfo<'tcx>,
2710+
_info: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
27112711
a: T,
27122712
b: T,
27132713
) -> relate::RelateResult<'tcx, T> {
@@ -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,
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, 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

+7-10
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,10 +142,10 @@ 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,
151-
_: ty::VarianceDiagInfo<'tcx>,
148+
_: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
152149
a: T,
153150
b: T,
154151
) -> RelateResult<'tcx, T> {
@@ -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

+12-10
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,10 +40,10 @@ 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,
44-
_: ty::VarianceDiagInfo<'tcx>,
46+
_: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
4547
a: T,
4648
b: T,
4749
) -> RelateResult<'tcx, T> {
@@ -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
}

0 commit comments

Comments
 (0)