Skip to content

Commit 0535dd3

Browse files
committed
Split librustc_infer.
1 parent c1e3d55 commit 0535dd3

File tree

32 files changed

+2245
-1464
lines changed

32 files changed

+2245
-1464
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4129,11 +4129,13 @@ name = "rustc_trait_selection"
41294129
version = "0.0.0"
41304130
dependencies = [
41314131
"fmt_macros",
4132+
"graphviz",
41324133
"log",
41334134
"rustc",
41344135
"rustc_ast",
41354136
"rustc_attr",
41364137
"rustc_data_structures",
4138+
"rustc_error_codes",
41374139
"rustc_errors",
41384140
"rustc_hir",
41394141
"rustc_index",

src/librustc_infer/infer/canonical/query_response.rs

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::infer::canonical::{
1414
};
1515
use crate::infer::nll_relate::{NormalizationStrategy, TypeRelating, TypeRelatingDelegate};
1616
use crate::infer::region_constraints::{Constraint, RegionConstraintData};
17-
use crate::infer::{InferCtxt, InferCtxtBuilder, InferOk, InferResult, NLLRegionVariableOrigin};
17+
use crate::infer::{InferCtxt, InferOk, InferResult, NLLRegionVariableOrigin};
1818
use crate::traits::query::{Fallible, NoSolution};
1919
use crate::traits::{DomainGoal, TraitEngine};
2020
use crate::traits::{Obligation, ObligationCause, PredicateObligation};
@@ -26,52 +26,8 @@ use rustc::ty::{self, BoundVar, Ty, TyCtxt};
2626
use rustc_data_structures::captures::Captures;
2727
use rustc_index::vec::Idx;
2828
use rustc_index::vec::IndexVec;
29-
use rustc_span::DUMMY_SP;
3029
use std::fmt::Debug;
3130

32-
impl<'tcx> InferCtxtBuilder<'tcx> {
33-
/// The "main method" for a canonicalized trait query. Given the
34-
/// canonical key `canonical_key`, this method will create a new
35-
/// inference context, instantiate the key, and run your operation
36-
/// `op`. The operation should yield up a result (of type `R`) as
37-
/// well as a set of trait obligations that must be fully
38-
/// satisfied. These obligations will be processed and the
39-
/// canonical result created.
40-
///
41-
/// Returns `NoSolution` in the event of any error.
42-
///
43-
/// (It might be mildly nicer to implement this on `TyCtxt`, and
44-
/// not `InferCtxtBuilder`, but that is a bit tricky right now.
45-
/// In part because we would need a `for<'tcx>` sort of
46-
/// bound for the closure and in part because it is convenient to
47-
/// have `'tcx` be free on this function so that we can talk about
48-
/// `K: TypeFoldable<'tcx>`.)
49-
pub fn enter_canonical_trait_query<K, R>(
50-
&mut self,
51-
canonical_key: &Canonical<'tcx, K>,
52-
operation: impl FnOnce(&InferCtxt<'_, 'tcx>, &mut dyn TraitEngine<'tcx>, K) -> Fallible<R>,
53-
) -> Fallible<CanonicalizedQueryResponse<'tcx, R>>
54-
where
55-
K: TypeFoldable<'tcx>,
56-
R: Debug + TypeFoldable<'tcx>,
57-
Canonical<'tcx, QueryResponse<'tcx, R>>: ArenaAllocatable,
58-
{
59-
self.enter_with_canonical(
60-
DUMMY_SP,
61-
canonical_key,
62-
|ref infcx, key, canonical_inference_vars| {
63-
let mut fulfill_cx = TraitEngine::new(infcx.tcx);
64-
let value = operation(infcx, &mut *fulfill_cx, key)?;
65-
infcx.make_canonicalized_query_response(
66-
canonical_inference_vars,
67-
value,
68-
&mut *fulfill_cx,
69-
)
70-
},
71-
)
72-
}
73-
}
74-
7531
impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
7632
/// This method is meant to be invoked as the final step of a canonical query
7733
/// implementation. It is given:

src/librustc_infer/infer/error_reporting/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2156,7 +2156,7 @@ impl<'tcx> ObligationCauseExt<'tcx> for ObligationCause<'tcx> {
21562156
/// This is a bare signal of what kind of type we're dealing with. `ty::TyKind` tracks
21572157
/// extra information about each type, but we only care about the category.
21582158
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
2159-
crate enum TyCategory {
2159+
pub enum TyCategory {
21602160
Closure,
21612161
Opaque,
21622162
Generator,

src/librustc_infer/infer/mod.rs

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ use rustc::infer::canonical::{Canonical, CanonicalVarValues};
1313
use rustc::infer::unify_key::{ConstVarValue, ConstVariableValue};
1414
use rustc::infer::unify_key::{ConstVariableOrigin, ConstVariableOriginKind, ToType};
1515
use rustc::middle::free_region::RegionRelations;
16-
use rustc::middle::lang_items;
1716
use rustc::middle::region;
1817
use rustc::mir;
1918
use rustc::mir::interpret::ConstEvalResult;
2019
use rustc::session::config::BorrowckMode;
20+
use rustc::traits::select;
2121
use rustc::ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric};
2222
use rustc::ty::fold::{TypeFoldable, TypeFolder};
2323
use rustc::ty::relate::RelateResult;
@@ -58,7 +58,6 @@ pub mod lattice;
5858
mod lexical_region_resolve;
5959
mod lub;
6060
pub mod nll_relate;
61-
pub mod opaque_types;
6261
pub mod outlives;
6362
pub mod region_constraints;
6463
pub mod resolve;
@@ -215,10 +214,10 @@ pub struct InferCtxt<'a, 'tcx> {
215214

216215
/// Caches the results of trait selection. This cache is used
217216
/// for things that have to do with the parameters in scope.
218-
pub selection_cache: traits::SelectionCache<'tcx>,
217+
pub selection_cache: select::SelectionCache<'tcx>,
219218

220219
/// Caches the results of trait evaluation.
221-
pub evaluation_cache: traits::EvaluationCache<'tcx>,
220+
pub evaluation_cache: select::EvaluationCache<'tcx>,
222221

223222
/// the set of predicates on which errors have been reported, to
224223
/// avoid reporting the same error twice.
@@ -1474,27 +1473,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14741473
.verify_generic_bound(origin, kind, a, bound);
14751474
}
14761475

1477-
pub fn type_is_copy_modulo_regions(
1478-
&self,
1479-
param_env: ty::ParamEnv<'tcx>,
1480-
ty: Ty<'tcx>,
1481-
span: Span,
1482-
) -> bool {
1483-
let ty = self.resolve_vars_if_possible(&ty);
1484-
1485-
if !(param_env, ty).has_local_value() {
1486-
return ty.is_copy_modulo_regions(self.tcx, param_env, span);
1487-
}
1488-
1489-
let copy_def_id = self.tcx.require_lang_item(lang_items::CopyTraitLangItem, None);
1490-
1491-
// This can get called from typeck (by euv), and `moves_by_default`
1492-
// rightly refuses to work with inference variables, but
1493-
// moves_by_default has a cache, which we want to use in other
1494-
// cases.
1495-
traits::type_known_to_meet_bound_modulo_regions(self, param_env, ty, copy_def_id, span)
1496-
}
1497-
14981476
/// Obtains the latest type of the given closure; this may be a
14991477
/// closure in the current function, in which case its
15001478
/// `ClosureKind` may not yet be known.
@@ -1518,30 +1496,6 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15181496
closure_sig_ty.fn_sig(self.tcx)
15191497
}
15201498

1521-
/// Normalizes associated types in `value`, potentially returning
1522-
/// new obligations that must further be processed.
1523-
pub fn partially_normalize_associated_types_in<T>(
1524-
&self,
1525-
span: Span,
1526-
body_id: hir::HirId,
1527-
param_env: ty::ParamEnv<'tcx>,
1528-
value: &T,
1529-
) -> InferOk<'tcx, T>
1530-
where
1531-
T: TypeFoldable<'tcx>,
1532-
{
1533-
debug!("partially_normalize_associated_types_in(value={:?})", value);
1534-
let mut selcx = traits::SelectionContext::new(self);
1535-
let cause = ObligationCause::misc(span, body_id);
1536-
let traits::Normalized { value, obligations } =
1537-
traits::normalize(&mut selcx, param_env, cause, value);
1538-
debug!(
1539-
"partially_normalize_associated_types_in: result={:?} predicates={:?}",
1540-
value, obligations
1541-
);
1542-
InferOk { value, obligations }
1543-
}
1544-
15451499
/// Clears the selection, evaluation, and projection caches. This is useful when
15461500
/// repeatedly attempting to select an `Obligation` while changing only
15471501
/// its `ParamEnv`, since `FulfillmentContext` doesn't use probing.

src/librustc_infer/infer/outlives/env.rs

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use crate::infer::{GenericKind, InferCtxt};
22
use crate::traits::query::OutlivesBound;
3+
use rustc::ty;
34
use rustc::ty::free_region_map::FreeRegionMap;
4-
use rustc::ty::{self, Ty};
55
use rustc_data_structures::fx::FxHashMap;
66
use rustc_hir as hir;
7-
use rustc_span::Span;
87

98
use super::explicit_outlives_bounds;
109

@@ -144,39 +143,6 @@ impl<'a, 'tcx> OutlivesEnvironment<'tcx> {
144143
self.region_bound_pairs_accum.truncate(len);
145144
}
146145

147-
/// This method adds "implied bounds" into the outlives environment.
148-
/// Implied bounds are outlives relationships that we can deduce
149-
/// on the basis that certain types must be well-formed -- these are
150-
/// either the types that appear in the function signature or else
151-
/// the input types to an impl. For example, if you have a function
152-
/// like
153-
///
154-
/// ```
155-
/// fn foo<'a, 'b, T>(x: &'a &'b [T]) { }
156-
/// ```
157-
///
158-
/// we can assume in the caller's body that `'b: 'a` and that `T:
159-
/// 'b` (and hence, transitively, that `T: 'a`). This method would
160-
/// add those assumptions into the outlives-environment.
161-
///
162-
/// Tests: `src/test/compile-fail/regions-free-region-ordering-*.rs`
163-
pub fn add_implied_bounds(
164-
&mut self,
165-
infcx: &InferCtxt<'a, 'tcx>,
166-
fn_sig_tys: &[Ty<'tcx>],
167-
body_id: hir::HirId,
168-
span: Span,
169-
) {
170-
debug!("add_implied_bounds()");
171-
172-
for &ty in fn_sig_tys {
173-
let ty = infcx.resolve_vars_if_possible(&ty);
174-
debug!("add_implied_bounds: ty = {}", ty);
175-
let implied_bounds = infcx.implied_outlives_bounds(self.param_env, body_id, ty, span);
176-
self.add_outlives_bounds(Some(infcx), implied_bounds)
177-
}
178-
}
179-
180146
/// Save the current set of region-bound pairs under the given `body_id`.
181147
pub fn save_implied_bounds(&mut self, body_id: hir::HirId) {
182148
let old =
@@ -190,8 +156,11 @@ impl<'a, 'tcx> OutlivesEnvironment<'tcx> {
190156
/// contain inference variables, it must be supplied, in which
191157
/// case we will register "givens" on the inference context. (See
192158
/// `RegionConstraintData`.)
193-
fn add_outlives_bounds<I>(&mut self, infcx: Option<&InferCtxt<'a, 'tcx>>, outlives_bounds: I)
194-
where
159+
pub fn add_outlives_bounds<I>(
160+
&mut self,
161+
infcx: Option<&InferCtxt<'a, 'tcx>>,
162+
outlives_bounds: I,
163+
) where
195164
I: IntoIterator<Item = OutlivesBound<'tcx>>,
196165
{
197166
// Record relationships such as `T:'x` that don't go into the

src/librustc_infer/traits/engine.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use crate::infer::InferCtxt;
2+
use crate::traits::Obligation;
3+
use rustc::ty::{self, ToPredicate, Ty, WithConstness};
4+
use rustc_hir::def_id::DefId;
5+
6+
use super::FulfillmentError;
7+
use super::{ObligationCause, PredicateObligation};
8+
9+
pub trait TraitEngine<'tcx>: 'tcx {
10+
fn normalize_projection_type(
11+
&mut self,
12+
infcx: &InferCtxt<'_, 'tcx>,
13+
param_env: ty::ParamEnv<'tcx>,
14+
projection_ty: ty::ProjectionTy<'tcx>,
15+
cause: ObligationCause<'tcx>,
16+
) -> Ty<'tcx>;
17+
18+
/// Requires that `ty` must implement the trait with `def_id` in
19+
/// the given environment. This trait must not have any type
20+
/// parameters (except for `Self`).
21+
fn register_bound(
22+
&mut self,
23+
infcx: &InferCtxt<'_, 'tcx>,
24+
param_env: ty::ParamEnv<'tcx>,
25+
ty: Ty<'tcx>,
26+
def_id: DefId,
27+
cause: ObligationCause<'tcx>,
28+
) {
29+
let trait_ref = ty::TraitRef { def_id, substs: infcx.tcx.mk_substs_trait(ty, &[]) };
30+
self.register_predicate_obligation(
31+
infcx,
32+
Obligation {
33+
cause,
34+
recursion_depth: 0,
35+
param_env,
36+
predicate: trait_ref.without_const().to_predicate(),
37+
},
38+
);
39+
}
40+
41+
fn register_predicate_obligation(
42+
&mut self,
43+
infcx: &InferCtxt<'_, 'tcx>,
44+
obligation: PredicateObligation<'tcx>,
45+
);
46+
47+
fn select_all_or_error(
48+
&mut self,
49+
infcx: &InferCtxt<'_, 'tcx>,
50+
) -> Result<(), Vec<FulfillmentError<'tcx>>>;
51+
52+
fn select_where_possible(
53+
&mut self,
54+
infcx: &InferCtxt<'_, 'tcx>,
55+
) -> Result<(), Vec<FulfillmentError<'tcx>>>;
56+
57+
fn pending_obligations(&self) -> Vec<PredicateObligation<'tcx>>;
58+
}
59+
60+
pub trait TraitEngineExt<'tcx> {
61+
fn register_predicate_obligations(
62+
&mut self,
63+
infcx: &InferCtxt<'_, 'tcx>,
64+
obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
65+
);
66+
}
67+
68+
impl<T: ?Sized + TraitEngine<'tcx>> TraitEngineExt<'tcx> for T {
69+
fn register_predicate_obligations(
70+
&mut self,
71+
infcx: &InferCtxt<'_, 'tcx>,
72+
obligations: impl IntoIterator<Item = PredicateObligation<'tcx>>,
73+
) {
74+
for obligation in obligations {
75+
self.register_predicate_obligation(infcx, obligation);
76+
}
77+
}
78+
}

0 commit comments

Comments
 (0)