1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use anyhow::Result;
use log::debug;
use rustc_data_structures::fx::FxHashSet as HashSet;
use rustc_hir::BodyId;
use rustc_middle::ty::TyCtxt;
use rustc_utils::{mir::borrowck_facts::get_body_with_borrowck_facts, BodyExt};
use serde::Serialize;

#[derive(Debug, Clone, Serialize, Default)]
pub struct PlaygroundOutput {
  outlives: HashSet<(String, String)>,
}

pub fn playground(tcx: TyCtxt, body_id: BodyId) -> Result<PlaygroundOutput> {
  let def_id = tcx.hir().body_owner_def_id(body_id);
  let body_with_facts = get_body_with_borrowck_facts(tcx, def_id);
  let body = &body_with_facts.body;
  debug!("{}", body.to_string(tcx).unwrap());

  let subset_base = &body_with_facts.input_facts.as_ref().unwrap().subset_base;
  let outlives = subset_base
    .iter()
    .map(|(sup, sub, _)| (format!("{sup:?}"), format!("{sub:?}")))
    .collect::<HashSet<_>>();

  Ok(PlaygroundOutput { outlives })
}