flowistry_pdg_construction/
approximation.rs

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use log::trace;

use rustc_abi::VariantIdx;

use rustc_hir::def_id::DefId;
use rustc_index::IndexVec;
use rustc_middle::{
    mir::{visit::Visitor, AggregateKind, Location, Place, Rvalue},
    ty::TyKind,
};

use crate::{local_analysis::LocalAnalysis, utils::ArgSlice};

pub(crate) type ApproximationHandler<'tcx, 'a> =
    fn(&LocalAnalysis<'tcx, 'a>, &mut dyn Visitor<'tcx>, ArgSlice<'a, 'tcx>, Place<'tcx>, Location);

impl<'tcx, 'a> LocalAnalysis<'tcx, 'a> {
    /// Special case behavior for calls to functions used in desugaring async functions.
    ///
    /// Ensures that functions like `Pin::new_unchecked` are not modularly-approximated.
    pub(crate) fn can_approximate_async_functions(
        &self,
        def_id: DefId,
    ) -> Option<ApproximationHandler<'tcx, 'a>> {
        let lang_items = self.tcx().lang_items();
        if Some(def_id) == lang_items.new_unchecked_fn() {
            Some(Self::approximate_new_unchecked)
        } else if Some(def_id) == lang_items.into_future_fn()
            // FIXME: better way to get retrieve this stdlib DefId?
            || self.tcx().def_path_str(def_id) == "<F as std::future::IntoFuture>::into_future"
        {
            Some(Self::approximate_into_future)
        } else {
            None
        }
    }

    fn approximate_into_future(
        &self,
        vis: &mut dyn Visitor<'tcx>,
        args: ArgSlice<'_, 'tcx>,
        destination: Place<'tcx>,
        location: Location,
    ) {
        trace!("Handling into_future as assign for {destination:?}");
        let [op] = args else {
            unreachable!();
        };
        vis.visit_assign(&destination, &Rvalue::Use(op.node.clone()), location);
    }

    fn approximate_new_unchecked(
        &self,
        vis: &mut dyn Visitor<'tcx>,
        args: ArgSlice<'_, 'tcx>,
        destination: Place<'tcx>,
        location: Location,
    ) {
        let lang_items = self.tcx().lang_items();
        let [op] = args else {
            unreachable!();
        };
        let mut operands = IndexVec::new();
        operands.push(op.node.clone());
        let TyKind::Adt(adt_id, generics) = destination.ty(&self.mono_body, self.tcx()).ty.kind()
        else {
            unreachable!()
        };
        assert_eq!(adt_id.did(), lang_items.pin_type().unwrap());
        let aggregate_kind =
            AggregateKind::Adt(adt_id.did(), VariantIdx::from_u32(0), generics, None, None);
        let rvalue = Rvalue::Aggregate(Box::new(aggregate_kind), operands);
        trace!("Handling new_unchecked as assign for {destination:?}");
        vis.visit_assign(&destination, &rvalue, location);
    }
}