]> git.proxmox.com Git - rustc.git/blobdiff - src/librustc_typeck/outlives/mod.rs
New upstream version 1.47.0+dfsg1
[rustc.git] / src / librustc_typeck / outlives / mod.rs
index 6b861656d7e2dce0188d4803f75c0935fa6e9e6c..94926f480e23e3227ef8b2d2e3d47fc9133bf528 100644 (file)
@@ -1,11 +1,11 @@
 use hir::Node;
-use rustc::hir;
-use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
-use rustc::ty::query::Providers;
-use rustc::ty::subst::GenericArgKind;
-use rustc::ty::{self, CratePredicatesMap, TyCtxt};
-use syntax::symbol::sym;
-use syntax_pos::Span;
+use rustc_hir as hir;
+use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
+use rustc_middle::ty::query::Providers;
+use rustc_middle::ty::subst::GenericArgKind;
+use rustc_middle::ty::{self, CratePredicatesMap, TyCtxt};
+use rustc_span::symbol::sym;
+use rustc_span::Span;
 
 mod explicit;
 mod implicit_infer;
@@ -13,42 +13,33 @@ mod implicit_infer;
 pub mod test;
 mod utils;
 
-pub fn provide(providers: &mut Providers<'_>) {
-    *providers = Providers {
-        inferred_outlives_of,
-        inferred_outlives_crate,
-        ..*providers
-    };
+pub fn provide(providers: &mut Providers) {
+    *providers = Providers { inferred_outlives_of, inferred_outlives_crate, ..*providers };
 }
 
-fn inferred_outlives_of(
-    tcx: TyCtxt<'_>,
-    item_def_id: DefId,
-) -> &[(ty::Predicate<'_>, Span)] {
-    let id = tcx
-        .hir()
-        .as_local_hir_id(item_def_id)
-        .expect("expected local def-id");
+fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[(ty::Predicate<'_>, Span)] {
+    let id = tcx.hir().local_def_id_to_hir_id(item_def_id.expect_local());
 
     match tcx.hir().get(id) {
         Node::Item(item) => match item.kind {
             hir::ItemKind::Struct(..) | hir::ItemKind::Enum(..) | hir::ItemKind::Union(..) => {
                 let crate_map = tcx.inferred_outlives_crate(LOCAL_CRATE);
 
-                let predicates = crate_map
-                    .predicates
-                    .get(&item_def_id)
-                    .map(|p| *p)
-                    .unwrap_or(&[]);
+                let predicates = crate_map.predicates.get(&item_def_id).copied().unwrap_or(&[]);
 
                 if tcx.has_attr(item_def_id, sym::rustc_outlives) {
                     let mut pred: Vec<String> = predicates
                         .iter()
-                        .map(|(out_pred, _)| match out_pred {
-                            ty::Predicate::RegionOutlives(p) => p.to_string(),
-                            ty::Predicate::TypeOutlives(p) => p.to_string(),
+                        .map(|(out_pred, _)| match out_pred.kind() {
+                            ty::PredicateKind::Atom(ty::PredicateAtom::RegionOutlives(p)) => {
+                                p.to_string()
+                            }
+                            ty::PredicateKind::Atom(ty::PredicateAtom::TypeOutlives(p)) => {
+                                p.to_string()
+                            }
                             err => bug!("unexpected predicate {:?}", err),
-                        }).collect();
+                        })
+                        .collect();
                     pred.sort();
 
                     let span = tcx.def_span(item_def_id);
@@ -71,10 +62,7 @@ fn inferred_outlives_of(
     }
 }
 
-fn inferred_outlives_crate(
-    tcx: TyCtxt<'_>,
-    crate_num: CrateNum,
-) -> &CratePredicatesMap<'_> {
+fn inferred_outlives_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> CratePredicatesMap<'_> {
     assert_eq!(crate_num, LOCAL_CRATE);
 
     // Compute a map from each struct/enum/union S to the **explicit**
@@ -97,30 +85,31 @@ fn inferred_outlives_crate(
     let predicates = global_inferred_outlives
         .iter()
         .map(|(&def_id, set)| {
-            let predicates = &*tcx.arena.alloc_from_iter(set
-                .iter()
-                .filter_map(
-                    |(ty::OutlivesPredicate(kind1, region2), &span)| match kind1.unpack() {
-                        GenericArgKind::Type(ty1) => {
-                            Some((ty::Predicate::TypeOutlives(ty::Binder::bind(
-                                ty::OutlivesPredicate(ty1, region2)
-                            )), span))
-                        }
-                        GenericArgKind::Lifetime(region1) => {
-                            Some((ty::Predicate::RegionOutlives(
-                                ty::Binder::bind(ty::OutlivesPredicate(region1, region2))
-                            ), span))
-                        }
+            let predicates = &*tcx.arena.alloc_from_iter(set.iter().filter_map(
+                |(ty::OutlivesPredicate(kind1, region2), &span)| {
+                    match kind1.unpack() {
+                        GenericArgKind::Type(ty1) => Some((
+                            ty::PredicateAtom::TypeOutlives(ty::OutlivesPredicate(ty1, region2))
+                                .potentially_quantified(tcx, ty::PredicateKind::ForAll),
+                            span,
+                        )),
+                        GenericArgKind::Lifetime(region1) => Some((
+                            ty::PredicateAtom::RegionOutlives(ty::OutlivesPredicate(
+                                region1, region2,
+                            ))
+                            .potentially_quantified(tcx, ty::PredicateKind::ForAll),
+                            span,
+                        )),
                         GenericArgKind::Const(_) => {
                             // Generic consts don't impose any constraints.
                             None
                         }
-                    },
-                ));
+                    }
+                },
+            ));
             (def_id, predicates)
-        }).collect();
+        })
+        .collect();
 
-    tcx.arena.alloc(ty::CratePredicatesMap {
-        predicates,
-    })
+    ty::CratePredicatesMap { predicates }
 }