3 use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
4 use rustc_middle::ty::query::Providers;
5 use rustc_middle::ty::subst::GenericArgKind;
6 use rustc_middle::ty::{self, CratePredicatesMap, ToPredicate, TyCtxt};
7 use rustc_span::symbol::sym;
12 /// Code to write unit test for outlives.
16 pub fn provide(providers: &mut Providers) {
17 *providers = Providers { inferred_outlives_of, inferred_outlives_crate, ..*providers };
20 fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: DefId) -> &[(ty::Predicate<'_>, Span)] {
21 let id = tcx.hir().as_local_hir_id(item_def_id.expect_local());
23 match tcx.hir().get(id) {
24 Node::Item(item) => match item.kind {
25 hir::ItemKind::Struct(..) | hir::ItemKind::Enum(..) | hir::ItemKind::Union(..) => {
26 let crate_map = tcx.inferred_outlives_crate(LOCAL_CRATE);
28 let predicates = crate_map.predicates.get(&item_def_id).copied().unwrap_or(&[]);
30 if tcx.has_attr(item_def_id, sym::rustc_outlives) {
31 let mut pred: Vec<String> = predicates
33 .map(|(out_pred, _)| match out_pred.kind() {
34 ty::PredicateKind::RegionOutlives(p) => p.to_string(),
35 ty::PredicateKind::TypeOutlives(p) => p.to_string(),
36 err => bug!("unexpected predicate {:?}", err),
41 let span = tcx.def_span(item_def_id);
42 let mut err = tcx.sess.struct_span_err(span, "rustc_outlives");
49 debug!("inferred_outlives_of({:?}) = {:?}", item_def_id, predicates);
61 fn inferred_outlives_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> CratePredicatesMap<'_> {
62 assert_eq!(crate_num, LOCAL_CRATE);
64 // Compute a map from each struct/enum/union S to the **explicit**
65 // outlives predicates (`T: 'a`, `'a: 'b`) that the user wrote.
66 // Typically there won't be many of these, except in older code where
67 // they were mandatory. Nonetheless, we have to ensure that every such
68 // predicate is satisfied, so they form a kind of base set of requirements
71 // Compute the inferred predicates
72 let mut exp_map = explicit::ExplicitPredicatesMap::new();
74 let global_inferred_outlives = implicit_infer::infer_predicates(tcx, &mut exp_map);
76 // Convert the inferred predicates into the "collected" form the
77 // global data structure expects.
79 // FIXME -- consider correcting impedance mismatch in some way,
80 // probably by updating the global data structure.
81 let predicates = global_inferred_outlives
83 .map(|(&def_id, set)| {
84 let predicates = &*tcx.arena.alloc_from_iter(set.iter().filter_map(
85 |(ty::OutlivesPredicate(kind1, region2), &span)| {
86 match kind1.unpack() {
87 GenericArgKind::Type(ty1) => Some((
88 ty::PredicateKind::TypeOutlives(ty::OutlivesPredicate(ty1, region2))
90 .potentially_quantified(tcx, ty::PredicateKind::ForAll),
93 GenericArgKind::Lifetime(region1) => Some((
94 ty::PredicateKind::RegionOutlives(ty::OutlivesPredicate(
98 .potentially_quantified(tcx, ty::PredicateKind::ForAll),
101 GenericArgKind::Const(_) => {
102 // Generic consts don't impose any constraints.
112 ty::CratePredicatesMap { predicates }