]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/outlives/explicit.rs
Rollup merge of #74109 - nbdd0121:issue-74082, r=petrochenkov
[rust.git] / src / librustc_typeck / 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, RequiredPredicates<'tcx>>,
10 }
11
12 impl<'tcx> ExplicitPredicatesMap<'tcx> {
13     pub fn new() -> ExplicitPredicatesMap<'tcx> {
14         ExplicitPredicatesMap { map: FxHashMap::default() }
15     }
16
17     pub fn explicit_predicates_of(
18         &mut self,
19         tcx: TyCtxt<'tcx>,
20         def_id: DefId,
21     ) -> &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() {
33                     ty::PredicateKind::TypeOutlives(predicate) => {
34                         let OutlivesPredicate(ref ty, ref reg) = predicate.skip_binder();
35                         insert_outlives_predicate(
36                             tcx,
37                             (*ty).into(),
38                             reg,
39                             span,
40                             &mut required_predicates,
41                         )
42                     }
43
44                     ty::PredicateKind::RegionOutlives(predicate) => {
45                         let OutlivesPredicate(ref reg1, ref reg2) = predicate.skip_binder();
46                         insert_outlives_predicate(
47                             tcx,
48                             (*reg1).into(),
49                             reg2,
50                             span,
51                             &mut required_predicates,
52                         )
53                     }
54
55                     ty::PredicateKind::Trait(..)
56                     | ty::PredicateKind::Projection(..)
57                     | ty::PredicateKind::WellFormed(..)
58                     | ty::PredicateKind::ObjectSafe(..)
59                     | ty::PredicateKind::ClosureKind(..)
60                     | ty::PredicateKind::Subtype(..)
61                     | ty::PredicateKind::ConstEvaluatable(..)
62                     | ty::PredicateKind::ConstEquate(..) => (),
63                 }
64             }
65
66             required_predicates
67         })
68     }
69 }