]> git.proxmox.com Git - rustc.git/blob - src/librustc_typeck/outlives/explicit.rs
New upstream version 1.28.0~beta.14+dfsg1
[rustc.git] / src / librustc_typeck / outlives / explicit.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use rustc::hir;
12 use rustc::hir::def_id::{CrateNum, DefId};
13 use rustc::hir::itemlikevisit::ItemLikeVisitor;
14 use rustc::ty::{self, TyCtxt};
15 use util::nodemap::FxHashMap;
16
17 use super::utils::*;
18
19 pub fn explicit_predicates<'tcx>(
20 tcx: TyCtxt<'_, 'tcx, 'tcx>,
21 crate_num: CrateNum,
22 ) -> FxHashMap<DefId, RequiredPredicates<'tcx>> {
23 let mut predicates = FxHashMap::default();
24
25 // iterate over the entire crate
26 tcx.hir.krate().visit_all_item_likes(&mut ExplicitVisitor {
27 tcx: tcx,
28 explicit_predicates: &mut predicates,
29 crate_num: crate_num,
30 });
31
32 predicates
33 }
34
35 pub struct ExplicitVisitor<'cx, 'tcx: 'cx> {
36 tcx: TyCtxt<'cx, 'tcx, 'tcx>,
37 explicit_predicates: &'cx mut FxHashMap<DefId, RequiredPredicates<'tcx>>,
38 crate_num: CrateNum,
39 }
40
41 impl<'cx, 'tcx> ItemLikeVisitor<'tcx> for ExplicitVisitor<'cx, 'tcx> {
42 fn visit_item(&mut self, item: &'tcx hir::Item) {
43 let def_id = DefId {
44 krate: self.crate_num,
45 index: item.hir_id.owner,
46 };
47
48 let mut required_predicates = RequiredPredicates::default();
49 let local_explicit_predicate = self.tcx.explicit_predicates_of(def_id).predicates;
50
51 for pred in local_explicit_predicate.into_iter() {
52 match pred {
53 ty::Predicate::TypeOutlives(predicate) => {
54 let ty::OutlivesPredicate(ref ty, ref reg) = predicate.skip_binder();
55 insert_outlives_predicate(self.tcx, (*ty).into(), reg, &mut required_predicates)
56 }
57
58 ty::Predicate::RegionOutlives(predicate) => {
59 let ty::OutlivesPredicate(ref reg1, ref reg2) = predicate.skip_binder();
60 insert_outlives_predicate(
61 self.tcx,
62 (*reg1).into(),
63 reg2,
64 &mut required_predicates,
65 )
66 }
67
68 ty::Predicate::Trait(..)
69 | ty::Predicate::Projection(..)
70 | ty::Predicate::WellFormed(..)
71 | ty::Predicate::ObjectSafe(..)
72 | ty::Predicate::ClosureKind(..)
73 | ty::Predicate::Subtype(..)
74 | ty::Predicate::ConstEvaluatable(..) => (),
75 }
76 }
77
78 self.explicit_predicates.insert(def_id, required_predicates);
79 }
80
81 fn visit_trait_item(&mut self, _trait_item: &'tcx hir::TraitItem) {}
82
83 fn visit_impl_item(&mut self, _impl_item: &'tcx hir::ImplItem) {}
84 }