Skip to content

Commit 0a0dae0

Browse files
committed
pull out ParamEnvAnd and remove QueryKey
1 parent b2e899f commit 0a0dae0

File tree

10 files changed

+172
-181
lines changed

10 files changed

+172
-181
lines changed

src/librustc/traits/query/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@ pub type CanonicalPredicateGoal<'tcx> =
3434
Canonical<'tcx, ty::ParamEnvAnd<'tcx, ty::Predicate<'tcx>>>;
3535

3636
pub type CanonicalTypeOpEqGoal<'tcx> =
37-
Canonical<'tcx, type_op::eq::Eq<'tcx>>;
37+
Canonical<'tcx, ty::ParamEnvAnd<'tcx, type_op::eq::Eq<'tcx>>>;
3838

3939
pub type CanonicalTypeOpSubtypeGoal<'tcx> =
40-
Canonical<'tcx, type_op::subtype::Subtype<'tcx>>;
40+
Canonical<'tcx, ty::ParamEnvAnd<'tcx, type_op::subtype::Subtype<'tcx>>>;
4141

4242
pub type CanonicalTypeOpProvePredicateGoal<'tcx> =
43-
Canonical<'tcx, type_op::prove_predicate::ProvePredicate<'tcx>>;
43+
Canonical<'tcx, ty::ParamEnvAnd<'tcx, type_op::prove_predicate::ProvePredicate<'tcx>>>;
4444

4545
pub type CanonicalTypeOpNormalizeGoal<'tcx, T> =
46-
Canonical<'tcx, type_op::normalize::Normalize<'tcx, T>>;
46+
Canonical<'tcx, ty::ParamEnvAnd<'tcx, type_op::normalize::Normalize<T>>>;
4747

4848
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
4949
pub struct NoSolution;

src/librustc/traits/query/type_op/eq.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,36 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use infer::canonical::{Canonical, CanonicalizedQueryResult, QueryResult};
11+
use infer::canonical::{Canonical, Canonicalized, CanonicalizedQueryResult, QueryResult};
1212
use traits::query::Fallible;
13-
use ty::{ParamEnv, Ty, TyCtxt};
13+
use ty::{ParamEnvAnd, Ty, TyCtxt};
1414

1515
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
1616
pub struct Eq<'tcx> {
17-
pub param_env: ParamEnv<'tcx>,
1817
pub a: Ty<'tcx>,
1918
pub b: Ty<'tcx>,
2019
}
2120

2221
impl<'tcx> Eq<'tcx> {
23-
pub fn new(param_env: ParamEnv<'tcx>, a: Ty<'tcx>, b: Ty<'tcx>) -> Self {
24-
Self { param_env, a, b }
22+
pub fn new(a: Ty<'tcx>, b: Ty<'tcx>) -> Self {
23+
Self { a, b }
2524
}
2625
}
2726

2827
impl<'gcx: 'tcx, 'tcx> super::QueryTypeOp<'gcx, 'tcx> for Eq<'tcx> {
29-
type QueryKey = Self;
3028
type QueryResult = ();
3129

32-
fn prequery(self, _tcx: TyCtxt<'_, 'gcx, 'tcx>) -> Result<Self::QueryResult, Self> {
33-
if self.a == self.b {
34-
Ok(())
30+
fn prequery(_tcx: TyCtxt<'_, 'gcx, 'tcx>, key: &ParamEnvAnd<'tcx, Eq<'tcx>>) -> Option<Self::QueryResult> {
31+
if key.value.a == key.value.b {
32+
Some(())
3533
} else {
36-
Err(self)
34+
None
3735
}
3836
}
3937

40-
fn param_env(key: &Self::QueryKey) -> ParamEnv<'tcx> {
41-
key.param_env
42-
}
43-
4438
fn perform_query(
4539
tcx: TyCtxt<'_, 'gcx, 'tcx>,
46-
canonicalized: Canonical<'gcx, Eq<'gcx>>,
40+
canonicalized: Canonicalized<'gcx, ParamEnvAnd<'tcx, Self>>,
4741
) -> Fallible<CanonicalizedQueryResult<'gcx, ()>> {
4842
tcx.type_op_eq(canonicalized)
4943
}
@@ -57,7 +51,6 @@ impl<'gcx: 'tcx, 'tcx> super::QueryTypeOp<'gcx, 'tcx> for Eq<'tcx> {
5751

5852
BraceStructTypeFoldableImpl! {
5953
impl<'tcx> TypeFoldable<'tcx> for Eq<'tcx> {
60-
param_env,
6154
a,
6255
b,
6356
}
@@ -66,12 +59,11 @@ BraceStructTypeFoldableImpl! {
6659
BraceStructLiftImpl! {
6760
impl<'a, 'tcx> Lift<'tcx> for Eq<'a> {
6861
type Lifted = Eq<'tcx>;
69-
param_env,
7062
a,
7163
b,
7264
}
7365
}
7466

7567
impl_stable_hash_for! {
76-
struct Eq<'tcx> { param_env, a, b }
68+
struct Eq<'tcx> { a, b }
7769
}

src/librustc/traits/query/type_op/mod.rs

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use infer::canonical::{
12-
Canonical, Canonicalized, CanonicalizedQueryResult, QueryRegionConstraint, QueryResult,
13-
};
11+
use infer::canonical::{Canonical, Canonicalized, CanonicalizedQueryResult, QueryRegionConstraint,
12+
QueryResult};
1413
use infer::{InferCtxt, InferOk};
1514
use std::fmt;
1615
use std::rc::Rc;
1716
use traits::query::Fallible;
1817
use traits::ObligationCause;
1918
use ty::fold::TypeFoldable;
20-
use ty::{Lift, ParamEnv, TyCtxt};
19+
use ty::{Lift, ParamEnvAnd, TyCtxt};
2120

2221
pub mod custom;
2322
pub mod eq;
2423
pub mod normalize;
2524
pub mod outlives;
2625
pub mod prove_predicate;
26+
use self::prove_predicate::ProvePredicate;
2727
pub mod subtype;
2828

2929
pub trait TypeOp<'gcx, 'tcx>: Sized + fmt::Debug {
@@ -38,16 +38,18 @@ pub trait TypeOp<'gcx, 'tcx>: Sized + fmt::Debug {
3838
) -> Fallible<(Self::Output, Option<Rc<Vec<QueryRegionConstraint<'tcx>>>>)>;
3939
}
4040

41-
pub trait QueryTypeOp<'gcx: 'tcx, 'tcx>: fmt::Debug + Sized {
42-
type QueryKey: TypeFoldable<'tcx> + Lift<'gcx>;
41+
pub trait QueryTypeOp<'gcx: 'tcx, 'tcx>:
42+
fmt::Debug + Sized + TypeFoldable<'tcx> + Lift<'gcx>
43+
{
4344
type QueryResult: TypeFoldable<'tcx> + Lift<'gcx>;
4445

4546
/// Either converts `self` directly into a `QueryResult` (for
4647
/// simple cases) or into a `QueryKey` (for more complex cases
4748
/// where we actually have work to do).
48-
fn prequery(self, tcx: TyCtxt<'_, 'gcx, 'tcx>) -> Result<Self::QueryResult, Self::QueryKey>;
49-
50-
fn param_env(key: &Self::QueryKey) -> ParamEnv<'tcx>;
49+
fn prequery(
50+
tcx: TyCtxt<'_, 'gcx, 'tcx>,
51+
key: &ParamEnvAnd<'tcx, Self>,
52+
) -> Option<Self::QueryResult>;
5153

5254
/// Performs the actual query with the canonicalized key -- the
5355
/// real work happens here. This method is not given an `infcx`
@@ -57,7 +59,7 @@ pub trait QueryTypeOp<'gcx: 'tcx, 'tcx>: fmt::Debug + Sized {
5759
/// not captured in the return value.
5860
fn perform_query(
5961
tcx: TyCtxt<'_, 'gcx, 'tcx>,
60-
canonicalized: Canonicalized<'gcx, Self::QueryKey>,
62+
canonicalized: Canonicalized<'gcx, ParamEnvAnd<'tcx, Self>>,
6163
) -> Fallible<CanonicalizedQueryResult<'gcx, Self::QueryResult>>;
6264

6365
/// Casts a lifted query result (which is in the gcx lifetime)
@@ -77,52 +79,53 @@ pub trait QueryTypeOp<'gcx: 'tcx, 'tcx>: fmt::Debug + Sized {
7779
) -> &'a Canonical<'tcx, QueryResult<'tcx, Self::QueryResult>>;
7880

7981
fn fully_perform_into(
80-
self,
82+
query_key: ParamEnvAnd<'tcx, Self>,
8183
infcx: &InferCtxt<'_, 'gcx, 'tcx>,
8284
output_query_region_constraints: &mut Vec<QueryRegionConstraint<'tcx>>,
8385
) -> Fallible<Self::QueryResult> {
84-
match QueryTypeOp::prequery(self, infcx.tcx) {
85-
Ok(result) => Ok(result),
86-
Err(query_key) => {
87-
// FIXME(#33684) -- We need to use
88-
// `canonicalize_hr_query_hack` here because of things
89-
// like the subtype query, which go awry around
90-
// `'static` otherwise.
91-
let (canonical_self, canonical_var_values) =
92-
infcx.canonicalize_hr_query_hack(&query_key);
93-
let canonical_result = Self::perform_query(infcx.tcx, canonical_self)?;
94-
let canonical_result = Self::shrink_to_tcx_lifetime(&canonical_result);
95-
96-
let param_env = Self::param_env(&query_key);
97-
98-
let InferOk { value, obligations } = infcx
99-
.instantiate_nll_query_result_and_region_obligations(
100-
&ObligationCause::dummy(),
101-
param_env,
102-
&canonical_var_values,
103-
canonical_result,
104-
output_query_region_constraints,
105-
)?;
106-
107-
// Typically, instantiating NLL query results does not
108-
// create obligations. However, in some cases there
109-
// are unresolved type variables, and unify them *can*
110-
// create obligations. In that case, we have to go
111-
// fulfill them. We do this via a (recursive) query.
112-
for obligation in obligations {
113-
let () = prove_predicate::ProvePredicate::new(
114-
obligation.param_env,
115-
obligation.predicate,
116-
).fully_perform_into(infcx, output_query_region_constraints)?;
117-
}
118-
119-
Ok(value)
120-
}
86+
if let Some(result) = QueryTypeOp::prequery(infcx.tcx, &query_key) {
87+
return Ok(result);
12188
}
89+
90+
// FIXME(#33684) -- We need to use
91+
// `canonicalize_hr_query_hack` here because of things
92+
// like the subtype query, which go awry around
93+
// `'static` otherwise.
94+
let (canonical_self, canonical_var_values) = infcx.canonicalize_hr_query_hack(&query_key);
95+
let canonical_result = Self::perform_query(infcx.tcx, canonical_self)?;
96+
let canonical_result = Self::shrink_to_tcx_lifetime(&canonical_result);
97+
98+
let param_env = query_key.param_env;
99+
100+
let InferOk { value, obligations } = infcx
101+
.instantiate_nll_query_result_and_region_obligations(
102+
&ObligationCause::dummy(),
103+
param_env,
104+
&canonical_var_values,
105+
canonical_result,
106+
output_query_region_constraints,
107+
)?;
108+
109+
// Typically, instantiating NLL query results does not
110+
// create obligations. However, in some cases there
111+
// are unresolved type variables, and unify them *can*
112+
// create obligations. In that case, we have to go
113+
// fulfill them. We do this via a (recursive) query.
114+
for obligation in obligations {
115+
let () = ProvePredicate::fully_perform_into(
116+
obligation
117+
.param_env
118+
.and(ProvePredicate::new(obligation.predicate)),
119+
infcx,
120+
output_query_region_constraints,
121+
)?;
122+
}
123+
124+
Ok(value)
122125
}
123126
}
124127

125-
impl<'gcx: 'tcx, 'tcx, Q> TypeOp<'gcx, 'tcx> for Q
128+
impl<'gcx: 'tcx, 'tcx, Q> TypeOp<'gcx, 'tcx> for ParamEnvAnd<'tcx, Q>
126129
where
127130
Q: QueryTypeOp<'gcx, 'tcx>,
128131
{

src/librustc/traits/query/type_op/normalize.rs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,45 +12,39 @@ use infer::canonical::{Canonical, Canonicalized, CanonicalizedQueryResult, Query
1212
use std::fmt;
1313
use traits::query::Fallible;
1414
use ty::fold::TypeFoldable;
15-
use ty::{self, Lift, ParamEnv, Ty, TyCtxt};
15+
use ty::{self, Lift, ParamEnvAnd, Ty, TyCtxt};
1616

1717
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
18-
pub struct Normalize<'tcx, T> {
19-
pub param_env: ParamEnv<'tcx>,
18+
pub struct Normalize<T> {
2019
pub value: T,
2120
}
2221

23-
impl<'tcx, T> Normalize<'tcx, T>
22+
impl<'tcx, T> Normalize<T>
2423
where
2524
T: fmt::Debug + TypeFoldable<'tcx>,
2625
{
27-
pub fn new(param_env: ParamEnv<'tcx>, value: T) -> Self {
28-
Self { param_env, value }
26+
pub fn new(value: T) -> Self {
27+
Self { value }
2928
}
3029
}
3130

32-
impl<'gcx: 'tcx, 'tcx, T> super::QueryTypeOp<'gcx, 'tcx> for Normalize<'tcx, T>
31+
impl<'gcx: 'tcx, 'tcx, T> super::QueryTypeOp<'gcx, 'tcx> for Normalize<T>
3332
where
3433
T: Normalizable<'gcx, 'tcx>,
3534
{
36-
type QueryKey = Self;
3735
type QueryResult = T;
3836

39-
fn prequery(self, _tcx: TyCtxt<'_, 'gcx, 'tcx>) -> Result<T, Self> {
40-
if !self.value.has_projections() {
41-
Ok(self.value)
37+
fn prequery(_tcx: TyCtxt<'_, 'gcx, 'tcx>, key: &ParamEnvAnd<'tcx, Self>) -> Option<T> {
38+
if !key.value.value.has_projections() {
39+
Some(key.value.value)
4240
} else {
43-
Err(self)
41+
None
4442
}
4543
}
4644

47-
fn param_env(key: &Self::QueryKey) -> ParamEnv<'tcx> {
48-
key.param_env
49-
}
50-
5145
fn perform_query(
5246
tcx: TyCtxt<'_, 'gcx, 'tcx>,
53-
canonicalized: Canonicalized<'gcx, Self>,
47+
canonicalized: Canonicalized<'gcx, ParamEnvAnd<'tcx, Self>>,
5448
) -> Fallible<CanonicalizedQueryResult<'gcx, Self::QueryResult>> {
5549
T::type_op_method(tcx, canonicalized)
5650
}
@@ -62,10 +56,10 @@ where
6256
}
6357
}
6458

65-
pub trait Normalizable<'gcx, 'tcx>: fmt::Debug + TypeFoldable<'tcx> + Lift<'gcx> {
59+
pub trait Normalizable<'gcx, 'tcx>: fmt::Debug + TypeFoldable<'tcx> + Lift<'gcx> + Copy {
6660
fn type_op_method(
6761
tcx: TyCtxt<'_, 'gcx, 'tcx>,
68-
canonicalized: Canonicalized<'gcx, Normalize<'gcx, Self>>,
62+
canonicalized: Canonicalized<'gcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
6963
) -> Fallible<CanonicalizedQueryResult<'gcx, Self>>;
7064

7165
/// Convert from the `'gcx` (lifted) form of `Self` into the `tcx`
@@ -81,7 +75,7 @@ where
8175
{
8276
fn type_op_method(
8377
tcx: TyCtxt<'_, 'gcx, 'tcx>,
84-
canonicalized: Canonicalized<'gcx, Normalize<'gcx, Self>>,
78+
canonicalized: Canonicalized<'gcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
8579
) -> Fallible<CanonicalizedQueryResult<'gcx, Self>> {
8680
tcx.type_op_normalize_ty(canonicalized)
8781
}
@@ -99,7 +93,7 @@ where
9993
{
10094
fn type_op_method(
10195
tcx: TyCtxt<'_, 'gcx, 'tcx>,
102-
canonicalized: Canonicalized<'gcx, Normalize<'gcx, Self>>,
96+
canonicalized: Canonicalized<'gcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
10397
) -> Fallible<CanonicalizedQueryResult<'gcx, Self>> {
10498
tcx.type_op_normalize_predicate(canonicalized)
10599
}
@@ -117,7 +111,7 @@ where
117111
{
118112
fn type_op_method(
119113
tcx: TyCtxt<'_, 'gcx, 'tcx>,
120-
canonicalized: Canonicalized<'gcx, Normalize<'gcx, Self>>,
114+
canonicalized: Canonicalized<'gcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
121115
) -> Fallible<CanonicalizedQueryResult<'gcx, Self>> {
122116
tcx.type_op_normalize_poly_fn_sig(canonicalized)
123117
}
@@ -135,7 +129,7 @@ where
135129
{
136130
fn type_op_method(
137131
tcx: TyCtxt<'_, 'gcx, 'tcx>,
138-
canonicalized: Canonicalized<'gcx, Normalize<'gcx, Self>>,
132+
canonicalized: Canonicalized<'gcx, ParamEnvAnd<'tcx, Normalize<Self>>>,
139133
) -> Fallible<CanonicalizedQueryResult<'gcx, Self>> {
140134
tcx.type_op_normalize_fn_sig(canonicalized)
141135
}
@@ -148,22 +142,20 @@ where
148142
}
149143

150144
BraceStructTypeFoldableImpl! {
151-
impl<'tcx, T> TypeFoldable<'tcx> for Normalize<'tcx, T> {
152-
param_env,
145+
impl<'tcx, T> TypeFoldable<'tcx> for Normalize<T> {
153146
value,
154147
} where T: TypeFoldable<'tcx>,
155148
}
156149

157150
BraceStructLiftImpl! {
158-
impl<'a, 'tcx, T> Lift<'tcx> for Normalize<'a, T> {
159-
type Lifted = Normalize<'tcx, T::Lifted>;
160-
param_env,
151+
impl<'tcx, T> Lift<'tcx> for Normalize<T> {
152+
type Lifted = Normalize<T::Lifted>;
161153
value,
162154
} where T: Lift<'tcx>,
163155
}
164156

165157
impl_stable_hash_for! {
166-
impl<'tcx, T> for struct Normalize<'tcx, T> {
167-
param_env, value
158+
impl<'tcx, T> for struct Normalize<T> {
159+
value
168160
}
169161
}

0 commit comments

Comments
 (0)