rustc_hir_analysis/outlives/
explicit.rs

1use rustc_data_structures::fx::FxIndexMap;
2use rustc_hir::def_id::DefId;
3use rustc_middle::ty::{self, OutlivesPredicate, TyCtxt};
4
5use super::utils::*;
6
7#[derive(Debug)]
8pub(crate) struct ExplicitPredicatesMap<'tcx> {
9    map: FxIndexMap<DefId, ty::EarlyBinder<'tcx, RequiredPredicates<'tcx>>>,
10}
11
12impl<'tcx> ExplicitPredicatesMap<'tcx> {
13    pub(crate) fn new() -> ExplicitPredicatesMap<'tcx> {
14        ExplicitPredicatesMap { map: FxIndexMap::default() }
15    }
16
17    pub(crate) fn explicit_predicates_of(
18        &mut self,
19        tcx: TyCtxt<'tcx>,
20        def_id: DefId,
21    ) -> &ty::EarlyBinder<'tcx, 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::ClauseKind::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::ClauseKind::RegionOutlives(OutlivesPredicate(reg1, reg2)) => {
44                        insert_outlives_predicate(
45                            tcx,
46                            reg1.into(),
47                            reg2,
48                            span,
49                            &mut required_predicates,
50                        )
51                    }
52                    ty::ClauseKind::Trait(_)
53                    | ty::ClauseKind::Projection(_)
54                    | ty::ClauseKind::ConstArgHasType(_, _)
55                    | ty::ClauseKind::WellFormed(_)
56                    | ty::ClauseKind::ConstEvaluatable(_)
57                    | ty::ClauseKind::HostEffect(..) => {}
58                }
59            }
60
61            ty::EarlyBinder::bind(required_predicates)
62        })
63    }
64}