]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_hir_analysis/src/outlives/explicit.rs
rustc_typeck to rustc_hir_analysis
[rust.git] / compiler / rustc_hir_analysis / src / outlives / explicit.rs
1 use rustc_data_structures::fx::FxHashMap;
2 use rustc_hir::def_id::DefId;
3 use rustc_middle::ty::{self, OutlivesPredicate, TyCtxt};
4
5 use super::utils::*;
6
7 #[derive(Debug)]
8 pub struct ExplicitPredicatesMap<'tcx> {
9     map: FxHashMap<DefId, ty::EarlyBinder<RequiredPredicates<'tcx>>>,
10 }
11
12 impl<'tcx> ExplicitPredicatesMap<'tcx> {
13     pub fn new() -> ExplicitPredicatesMap<'tcx> {
14         ExplicitPredicatesMap { map: FxHashMap::default() }
15     }
16
17     pub(crate) fn explicit_predicates_of(
18         &mut self,
19         tcx: TyCtxt<'tcx>,
20         def_id: DefId,
21     ) -> &ty::EarlyBinder<RequiredPredicates<'tcx>> {
22         self.map.entry(def_id).or_insert_with(|| {
23             let predicates = if def_id.is_local() {
24                 tcx.explicit_predicates_of(def_id)
25             } else {
26                 tcx.predicates_of(def_id)
27             };
28             let mut required_predicates = RequiredPredicates::default();
29
30             // process predicates and convert to `RequiredPredicates` entry, see below
31             for &(predicate, span) in predicates.predicates {
32                 match predicate.kind().skip_binder() {
33                     ty::PredicateKind::TypeOutlives(OutlivesPredicate(ty, reg)) => {
34                         insert_outlives_predicate(
35                             tcx,
36                             ty.into(),
37                             reg,
38                             span,
39                             &mut required_predicates,
40                         )
41                     }
42
43                     ty::PredicateKind::RegionOutlives(OutlivesPredicate(reg1, reg2)) => {
44                         insert_outlives_predicate(
45                             tcx,
46                             reg1.into(),
47                             reg2,
48                             span,
49                             &mut required_predicates,
50                         )
51                     }
52
53                     ty::PredicateKind::Trait(..)
54                     | ty::PredicateKind::Projection(..)
55                     | ty::PredicateKind::WellFormed(..)
56                     | ty::PredicateKind::ObjectSafe(..)
57                     | ty::PredicateKind::ClosureKind(..)
58                     | ty::PredicateKind::Subtype(..)
59                     | ty::PredicateKind::Coerce(..)
60                     | ty::PredicateKind::ConstEvaluatable(..)
61                     | ty::PredicateKind::ConstEquate(..)
62                     | ty::PredicateKind::TypeWellFormedFromEnv(..) => (),
63                 }
64             }
65
66             ty::EarlyBinder(required_predicates)
67         })
68     }
69 }