Skip to content

Commit ee4a4c9

Browse files
committed
Store relationships on Inherent
1 parent 134b2d0 commit ee4a4c9

File tree

9 files changed

+28
-61
lines changed

9 files changed

+28
-61
lines changed

compiler/rustc_hir_typeck/src/fallback.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,6 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
196196
) -> FxHashMap<Ty<'tcx>, Ty<'tcx>> {
197197
debug!("calculate_diverging_fallback({:?})", unsolved_variables);
198198

199-
let relationships = self.fulfillment_cx.borrow_mut().relationships().clone();
200-
201199
// Construct a coercion graph where an edge `A -> B` indicates
202200
// a type variable is that is coerced
203201
let coercion_graph = self.create_coercion_graph();
@@ -282,7 +280,6 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
282280
);
283281

284282
debug!("obligations: {:#?}", self.fulfillment_cx.borrow_mut().pending_obligations());
285-
debug!("relationships: {:#?}", relationships);
286283

287284
// For each diverging variable, figure out whether it can
288285
// reach a member of N. If so, it falls back to `()`. Else
@@ -298,8 +295,8 @@ impl<'tcx> FnCtxt<'_, 'tcx> {
298295

299296
let mut relationship = ty::FoundRelationships { self_in_trait: false, output: false };
300297

301-
for (vid, rel) in relationships.iter() {
302-
if self.root_var(*vid) == root_vid {
298+
for (vid, rel) in self.inh.relationships.borrow().iter() {
299+
if self.infcx.root_var(*vid) == root_vid {
303300
relationship.self_in_trait |= rel.self_in_trait;
304301
relationship.output |= rel.output;
305302
}

compiler/rustc_hir_typeck/src/inherited.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::callee::DeferredCallResolution;
22

3-
use rustc_data_structures::fx::FxHashSet;
3+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
44
use rustc_hir as hir;
55
use rustc_hir::def_id::LocalDefId;
66
use rustc_hir::HirIdMap;
@@ -63,6 +63,8 @@ pub struct Inherited<'tcx> {
6363
/// we record that type variable here. This is later used to inform
6464
/// fallback. See the `fallback` module for details.
6565
pub(super) diverging_type_vars: RefCell<FxHashSet<Ty<'tcx>>>,
66+
67+
pub(super) relationships: RefCell<FxHashMap<ty::TyVid, ty::FoundRelationships>>,
6668
}
6769

6870
impl<'tcx> Deref for Inherited<'tcx> {
@@ -128,6 +130,7 @@ impl<'tcx> Inherited<'tcx> {
128130
deferred_generator_interiors: RefCell::new(Vec::new()),
129131
diverging_type_vars: RefCell::new(Default::default()),
130132
body_id,
133+
relationships: RefCell::new(Default::default()),
131134
}
132135
}
133136

@@ -136,6 +139,13 @@ impl<'tcx> Inherited<'tcx> {
136139
if obligation.has_escaping_bound_vars() {
137140
span_bug!(obligation.cause.span, "escaping bound vars in predicate {:?}", obligation);
138141
}
142+
143+
super::relationships::update(
144+
&self.infcx,
145+
&mut self.relationships.borrow_mut(),
146+
&obligation,
147+
);
148+
139149
self.fulfillment_cx.borrow_mut().register_predicate_obligation(self, obligation);
140150
}
141151

compiler/rustc_hir_typeck/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ mod method;
4040
mod op;
4141
mod pat;
4242
mod place_op;
43+
mod relationships;
4344
mod rvalue_scopes;
4445
mod upvar;
4546
mod writeback;

compiler/rustc_trait_selection/src/traits/relationships.rs renamed to compiler/rustc_hir_typeck/src/relationships.rs

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
use crate::infer::InferCtxt;
2-
use crate::traits::query::evaluate_obligation::InferCtxtExt;
3-
use crate::traits::PredicateObligation;
4-
use rustc_infer::traits::TraitEngine;
1+
use rustc_data_structures::fx::FxHashMap;
52
use rustc_middle::ty;
3+
use rustc_trait_selection::infer::InferCtxt;
4+
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
5+
use rustc_trait_selection::traits::PredicateObligation;
66

7-
pub(crate) fn update<'tcx, T>(
8-
engine: &mut T,
7+
pub fn update<'tcx>(
98
infcx: &InferCtxt<'tcx>,
9+
relationships: &mut FxHashMap<ty::TyVid, ty::FoundRelationships>,
1010
obligation: &PredicateObligation<'tcx>,
11-
) where
12-
T: TraitEngine<'tcx>,
13-
{
11+
) {
1412
// (*) binder skipped
1513
if let ty::PredicateKind::Clause(ty::Clause::Trait(tpred)) = obligation.predicate.kind().skip_binder()
1614
&& let Some(ty) = infcx.shallow_resolve(tpred.self_ty()).ty_vid().map(|t| infcx.root_var(t))
@@ -31,7 +29,7 @@ pub(crate) fn update<'tcx, T>(
3129
);
3230
// Don't report overflow errors. Otherwise equivalent to may_hold.
3331
if let Ok(result) = infcx.probe(|_| infcx.evaluate_obligation(&o)) && result.may_apply() {
34-
engine.relationships().entry(ty).or_default().self_in_trait = true;
32+
relationships.entry(ty).or_default().self_in_trait = true;
3533
}
3634
}
3735

@@ -42,7 +40,7 @@ pub(crate) fn update<'tcx, T>(
4240
// we need to make it into one.
4341
if let Some(vid) = predicate.term.ty().and_then(|ty| ty.ty_vid()) {
4442
debug!("relationship: {:?}.output = true", vid);
45-
engine.relationships().entry(vid).or_default().output = true;
43+
relationships.entry(vid).or_default().output = true;
4644
}
4745
}
4846
}

compiler/rustc_infer/src/traits/engine.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::infer::InferCtxt;
22
use crate::traits::Obligation;
3-
use rustc_data_structures::fx::FxHashMap;
43
use rustc_hir::def_id::DefId;
54
use rustc_middle::ty::{self, ToPredicate, Ty};
65

@@ -42,8 +41,6 @@ pub trait TraitEngine<'tcx>: 'tcx {
4241
fn select_where_possible(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>>;
4342

4443
fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>>;
45-
46-
fn relationships(&mut self) -> &mut FxHashMap<ty::TyVid, ty::FoundRelationships>;
4744
}
4845

4946
pub trait TraitEngineExt<'tcx> {

compiler/rustc_trait_selection/src/solve/fulfill.rs

-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
use std::mem;
22

3-
use rustc_data_structures::fx::FxHashMap;
43
use rustc_infer::{
54
infer::InferCtxt,
65
traits::{
76
query::NoSolution, FulfillmentError, FulfillmentErrorCode, PredicateObligation,
87
SelectionError, TraitEngine,
98
},
109
};
11-
use rustc_middle::ty;
1210

1311
use super::{search_graph, Certainty, EvalCtxt};
1412

@@ -100,8 +98,4 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentCtxt<'tcx> {
10098
fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {
10199
self.obligations.clone()
102100
}
103-
104-
fn relationships(&mut self) -> &mut FxHashMap<ty::TyVid, ty::FoundRelationships> {
105-
unimplemented!("Should be moved out of `TraitEngine`")
106-
}
107101
}

compiler/rustc_trait_selection/src/traits/chalk_fulfill.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,18 @@ use crate::traits::{
77
ChalkEnvironmentAndGoal, FulfillmentError, FulfillmentErrorCode, PredicateObligation,
88
SelectionError, TraitEngine,
99
};
10-
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
11-
use rustc_middle::ty::{self, TypeVisitable};
10+
use rustc_data_structures::fx::FxIndexSet;
11+
use rustc_middle::ty::TypeVisitable;
1212

1313
pub struct FulfillmentContext<'tcx> {
1414
obligations: FxIndexSet<PredicateObligation<'tcx>>,
1515

16-
relationships: FxHashMap<ty::TyVid, ty::FoundRelationships>,
17-
1816
usable_in_snapshot: bool,
1917
}
2018

2119
impl FulfillmentContext<'_> {
2220
pub(super) fn new() -> Self {
23-
FulfillmentContext {
24-
obligations: FxIndexSet::default(),
25-
relationships: FxHashMap::default(),
26-
usable_in_snapshot: false,
27-
}
21+
FulfillmentContext { obligations: FxIndexSet::default(), usable_in_snapshot: false }
2822
}
2923

3024
pub(crate) fn new_in_snapshot() -> Self {
@@ -43,8 +37,6 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
4337
}
4438
let obligation = infcx.resolve_vars_if_possible(obligation);
4539

46-
super::relationships::update(self, infcx, &obligation);
47-
4840
self.obligations.insert(obligation);
4941
}
5042

@@ -154,8 +146,4 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
154146
fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {
155147
self.obligations.iter().cloned().collect()
156148
}
157-
158-
fn relationships(&mut self) -> &mut FxHashMap<ty::TyVid, ty::FoundRelationships> {
159-
&mut self.relationships
160-
}
161149
}

compiler/rustc_trait_selection/src/traits/fulfill.rs

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::infer::{InferCtxt, TyOrConstInferVar};
2-
use rustc_data_structures::fx::FxHashMap;
32
use rustc_data_structures::obligation_forest::ProcessResult;
43
use rustc_data_structures::obligation_forest::{Error, ForestObligation, Outcome};
54
use rustc_data_structures::obligation_forest::{ObligationForest, ObligationProcessor};
@@ -54,8 +53,6 @@ pub struct FulfillmentContext<'tcx> {
5453
// fulfillment context.
5554
predicates: ObligationForest<PendingPredicateObligation<'tcx>>,
5655

57-
relationships: FxHashMap<ty::TyVid, ty::FoundRelationships>,
58-
5956
// Is it OK to register obligations into this infcx inside
6057
// an infcx snapshot?
6158
//
@@ -85,19 +82,11 @@ static_assert_size!(PendingPredicateObligation<'_>, 72);
8582
impl<'a, 'tcx> FulfillmentContext<'tcx> {
8683
/// Creates a new fulfillment context.
8784
pub(super) fn new() -> FulfillmentContext<'tcx> {
88-
FulfillmentContext {
89-
predicates: ObligationForest::new(),
90-
relationships: FxHashMap::default(),
91-
usable_in_snapshot: false,
92-
}
85+
FulfillmentContext { predicates: ObligationForest::new(), usable_in_snapshot: false }
9386
}
9487

9588
pub(super) fn new_in_snapshot() -> FulfillmentContext<'tcx> {
96-
FulfillmentContext {
97-
predicates: ObligationForest::new(),
98-
relationships: FxHashMap::default(),
99-
usable_in_snapshot: true,
100-
}
89+
FulfillmentContext { predicates: ObligationForest::new(), usable_in_snapshot: true }
10190
}
10291

10392
/// Attempts to select obligations using `selcx`.
@@ -139,8 +128,6 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
139128

140129
assert!(!infcx.is_in_snapshot() || self.usable_in_snapshot);
141130

142-
super::relationships::update(self, infcx, &obligation);
143-
144131
self.predicates
145132
.register_obligation(PendingPredicateObligation { obligation, stalled_on: vec![] });
146133
}
@@ -164,10 +151,6 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
164151
fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>> {
165152
self.predicates.map_pending_obligations(|o| o.obligation.clone())
166153
}
167-
168-
fn relationships(&mut self) -> &mut FxHashMap<ty::TyVid, ty::FoundRelationships> {
169-
&mut self.relationships
170-
}
171154
}
172155

173156
struct FulfillProcessor<'a, 'tcx> {

compiler/rustc_trait_selection/src/traits/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ mod object_safety;
1414
pub mod outlives_bounds;
1515
mod project;
1616
pub mod query;
17-
pub(crate) mod relationships;
1817
mod select;
1918
mod specialize;
2019
mod structural_match;

0 commit comments

Comments
 (0)