Skip to content

Commit aa92f78

Browse files
committed
make Eq a true query
1 parent dd8e744 commit aa92f78

File tree

9 files changed

+80
-48
lines changed

9 files changed

+80
-48
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ use rustc_data_structures::stable_hasher::{StableHasher, HashStable};
7070
use std::fmt;
7171
use std::hash::Hash;
7272
use syntax_pos::symbol::InternedString;
73-
use traits::query::{CanonicalProjectionGoal,
74-
CanonicalTyGoal, CanonicalPredicateGoal};
73+
use traits::query::{
74+
CanonicalProjectionGoal, CanonicalTyGoal, CanonicalTypeOpEqGoal, CanonicalPredicateGoal,
75+
};
7576
use ty::{TyCtxt, Instance, InstanceDef, ParamEnv, ParamEnvAnd, PolyTraitRef, Ty};
7677
use ty::subst::Substs;
7778

@@ -647,6 +648,7 @@ define_dep_nodes!( <'tcx>
647648
[] NormalizeTyAfterErasingRegions(ParamEnvAnd<'tcx, Ty<'tcx>>),
648649
[] DropckOutlives(CanonicalTyGoal<'tcx>),
649650
[] EvaluateObligation(CanonicalPredicateGoal<'tcx>),
651+
[] TypeOpEq(CanonicalTypeOpEqGoal<'tcx>),
650652

651653
[] SubstituteNormalizeAndTestPredicates { key: (DefId, &'tcx Substs<'tcx>) },
652654

src/librustc/traits/query/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ pub type CanonicalTyGoal<'tcx> = Canonical<'tcx, ty::ParamEnvAnd<'tcx, Ty<'tcx>>
3232
pub type CanonicalPredicateGoal<'tcx> =
3333
Canonical<'tcx, ty::ParamEnvAnd<'tcx, ty::Predicate<'tcx>>>;
3434

35+
pub type CanonicalTypeOpEqGoal<'tcx> =
36+
Canonical<'tcx, type_op::eq::Eq<'tcx>>;
37+
3538
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
3639
pub struct NoSolution;
3740

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

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

11-
use infer::canonical::{CanonicalizedQueryResult, Canonical};
12-
use traits::query::NoSolution;
13-
use traits::{FulfillmentContext, ObligationCause};
11+
use infer::canonical::{Canonical, CanonicalizedQueryResult};
1412
use ty::{self, ParamEnv, Ty, TyCtxt};
15-
use syntax::codemap::DUMMY_SP;
1613

17-
#[derive(Copy, Clone, Debug)]
14+
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
1815
pub struct Eq<'tcx> {
19-
param_env: ParamEnv<'tcx>,
20-
a: Ty<'tcx>,
21-
b: Ty<'tcx>,
16+
pub param_env: ParamEnv<'tcx>,
17+
pub a: Ty<'tcx>,
18+
pub b: Ty<'tcx>,
2219
}
2320

2421
impl<'tcx> Eq<'tcx> {
@@ -46,20 +43,7 @@ impl<'gcx: 'tcx, 'tcx> super::QueryTypeOp<'gcx, 'tcx> for Eq<'tcx> {
4643
tcx: TyCtxt<'_, 'gcx, 'tcx>,
4744
canonicalized: Canonical<'gcx, Eq<'gcx>>,
4845
) -> CanonicalizedQueryResult<'gcx, ()> {
49-
let tcx = tcx.global_tcx();
50-
tcx.infer_ctxt()
51-
.enter(|ref infcx| {
52-
let (Eq { param_env, a, b }, canonical_inference_vars) =
53-
infcx.instantiate_canonical_with_fresh_inference_vars(DUMMY_SP, &canonicalized);
54-
let fulfill_cx = &mut FulfillmentContext::new();
55-
let obligations = match infcx.at(&ObligationCause::dummy(), param_env).eq(a, b) {
56-
Ok(v) => v.into_obligations(),
57-
Err(_) => return Err(NoSolution),
58-
};
59-
fulfill_cx.register_predicate_obligations(infcx, obligations);
60-
infcx.make_canonicalized_query_result(canonical_inference_vars, (), fulfill_cx)
61-
})
62-
.unwrap()
46+
tcx.type_op_eq(canonicalized).unwrap()
6347
}
6448
}
6549

@@ -79,3 +63,7 @@ BraceStructLiftImpl! {
7963
b,
8064
}
8165
}
66+
67+
impl_stable_hash_for! {
68+
struct Eq<'tcx> { param_env, a, b }
69+
}

src/librustc/ty/query/config.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use dep_graph::SerializedDepNodeIndex;
1212
use dep_graph::DepNode;
1313
use hir::def_id::{CrateNum, DefId, DefIndex};
1414
use mir::interpret::{GlobalId, ConstValue};
15-
use traits::query::{CanonicalPredicateGoal, CanonicalProjectionGoal, CanonicalTyGoal};
15+
use traits::query::{
16+
CanonicalPredicateGoal, CanonicalProjectionGoal, CanonicalTyGoal, CanonicalTypeOpEqGoal,
17+
};
1618
use ty::{self, ParamEnvAnd, Ty, TyCtxt};
1719
use ty::subst::Substs;
1820
use ty::query::queries;
@@ -102,6 +104,12 @@ impl<'tcx> QueryDescription<'tcx> for queries::evaluate_obligation<'tcx> {
102104
}
103105
}
104106

107+
impl<'tcx> QueryDescription<'tcx> for queries::type_op_eq<'tcx> {
108+
fn describe(_tcx: TyCtxt, goal: CanonicalTypeOpEqGoal<'tcx>) -> String {
109+
format!("evaluating `type_op_eq` `{:?}`", goal)
110+
}
111+
}
112+
105113
impl<'tcx> QueryDescription<'tcx> for queries::is_copy_raw<'tcx> {
106114
fn describe(_tcx: TyCtxt, env: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> String {
107115
format!("computing whether `{}` is `Copy`", env.value)

src/librustc/ty/query/keys.rs

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
//! Defines the set of legal keys that can be used in queries.
1212
13+
use infer::canonical::Canonical;
1314
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE, DefIndex};
14-
use traits::query::{CanonicalPredicateGoal, CanonicalProjectionGoal, CanonicalTyGoal};
1515
use ty::{self, Ty, TyCtxt};
1616
use ty::subst::Substs;
1717
use ty::fast_reject::SimplifiedType;
@@ -190,27 +190,12 @@ impl Key for InternedString {
190190
}
191191
}
192192

193-
impl<'tcx> Key for CanonicalProjectionGoal<'tcx> {
194-
fn query_crate(&self) -> CrateNum {
195-
LOCAL_CRATE
196-
}
197-
198-
fn default_span(&self, _tcx: TyCtxt) -> Span {
199-
DUMMY_SP
200-
}
201-
}
202-
203-
impl<'tcx> Key for CanonicalTyGoal<'tcx> {
204-
fn query_crate(&self) -> CrateNum {
205-
LOCAL_CRATE
206-
}
207-
208-
fn default_span(&self, _tcx: TyCtxt) -> Span {
209-
DUMMY_SP
210-
}
211-
}
212-
213-
impl<'tcx> Key for CanonicalPredicateGoal<'tcx> {
193+
/// Canonical query goals correspond to abstract trait operations that
194+
/// are not tied to any crate in particular.
195+
impl<'tcx, T> Key for Canonical<'tcx, T>
196+
where
197+
T: Debug + Hash + Clone + Eq,
198+
{
214199
fn query_crate(&self) -> CrateNum {
215200
LOCAL_CRATE
216201
}

src/librustc/ty/query/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use session::{CompileResult, CrateDisambiguator};
3434
use session::config::OutputFilenames;
3535
use traits::{self, Vtable};
3636
use traits::query::{CanonicalPredicateGoal, CanonicalProjectionGoal,
37-
CanonicalTyGoal, NoSolution};
37+
CanonicalTyGoal, CanonicalTypeOpEqGoal, NoSolution};
3838
use traits::query::dropck_outlives::{DtorckConstraint, DropckOutlivesResult};
3939
use traits::query::normalize::NormalizationResult;
4040
use traits::specialization_graph;
@@ -446,6 +446,14 @@ define_queries! { <'tcx>
446446
CanonicalPredicateGoal<'tcx>
447447
) -> Result<traits::EvaluationResult, traits::OverflowError>,
448448

449+
/// Do not call this query directly: invoke `infcx.eq()` instead.
450+
[] fn type_op_eq: TypeOpEq(
451+
CanonicalTypeOpEqGoal<'tcx>
452+
) -> Result<
453+
Lrc<Canonical<'tcx, canonical::QueryResult<'tcx, ()>>>,
454+
NoSolution,
455+
>,
456+
449457
[] fn substitute_normalize_and_test_predicates:
450458
substitute_normalize_and_test_predicates_node((DefId, &'tcx Substs<'tcx>)) -> bool,
451459

src/librustc/ty/query/plumbing.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
10281028
DepKind::NormalizeTyAfterErasingRegions |
10291029
DepKind::DropckOutlives |
10301030
DepKind::EvaluateObligation |
1031+
DepKind::TypeOpEq |
10311032
DepKind::SubstituteNormalizeAndTestPredicates |
10321033
DepKind::InstanceDefSizeEstimate |
10331034
DepKind::ProgramClausesForEnv |

src/librustc_traits/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ mod evaluate_obligation;
3434
mod normalize_projection_ty;
3535
mod normalize_erasing_regions;
3636
pub mod lowering;
37+
mod type_op_eq;
3738

3839
use rustc::ty::query::Providers;
3940

@@ -47,6 +48,7 @@ pub fn provide(p: &mut Providers) {
4748
program_clauses_for: lowering::program_clauses_for,
4849
program_clauses_for_env: lowering::program_clauses_for_env,
4950
evaluate_obligation: evaluate_obligation::evaluate_obligation,
51+
type_op_eq: type_op_eq::type_op_eq,
5052
..*p
5153
};
5254
}

src/librustc_traits/type_op_eq.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use rustc::infer::canonical::{Canonical, QueryResult};
12+
use rustc::traits::query::type_op::eq::Eq;
13+
use rustc::traits::query::NoSolution;
14+
use rustc::traits::{FulfillmentContext, ObligationCause};
15+
use rustc::ty::TyCtxt;
16+
use rustc_data_structures::sync::Lrc;
17+
use syntax::codemap::DUMMY_SP;
18+
19+
crate fn type_op_eq<'tcx>(
20+
tcx: TyCtxt<'_, 'tcx, 'tcx>,
21+
canonicalized: Canonical<'tcx, Eq<'tcx>>,
22+
) -> Result<Lrc<Canonical<'tcx, QueryResult<'tcx, ()>>>, NoSolution> {
23+
let tcx = tcx.global_tcx();
24+
tcx.infer_ctxt().enter(|ref infcx| {
25+
let (Eq { param_env, a, b }, canonical_inference_vars) =
26+
infcx.instantiate_canonical_with_fresh_inference_vars(DUMMY_SP, &canonicalized);
27+
let fulfill_cx = &mut FulfillmentContext::new();
28+
let obligations = match infcx.at(&ObligationCause::dummy(), param_env).eq(a, b) {
29+
Ok(v) => v.into_obligations(),
30+
Err(_) => return Err(NoSolution),
31+
};
32+
fulfill_cx.register_predicate_obligations(infcx, obligations);
33+
infcx.make_canonicalized_query_result(canonical_inference_vars, (), fulfill_cx)
34+
})
35+
}

0 commit comments

Comments
 (0)