Skip to main content

rustc_mir_dataflow/
rustc_peek.rs

1use rustc_hir::attrs::{AttributeKind, RustcMirKind};
2use rustc_hir::find_attr;
3use rustc_middle::mir::{self, Body, Local, Location};
4use rustc_middle::ty::{self, Ty, TyCtxt};
5use rustc_span::{Span, sym};
6use tracing::{debug, info};
7
8use crate::errors::{
9    PeekArgumentNotALocal, PeekArgumentUntracked, PeekBitNotSet, PeekMustBeNotTemporary,
10    PeekMustBePlaceOrRefPlace, StopAfterDataFlowEndedCompilation,
11};
12use crate::framework::BitSetExt;
13use crate::impls::{MaybeInitializedPlaces, MaybeLiveLocals, MaybeUninitializedPlaces};
14use crate::move_paths::{HasMoveData, LookupResult, MoveData, MovePathIndex};
15use crate::{Analysis, JoinSemiLattice, ResultsCursor};
16
17pub fn sanity_check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
18    let def_id = body.source.def_id();
19    let attrs = tcx.get_all_attrs(def_id);
20    if let Some(kind) = {
    'done:
        {
        for i in attrs {
            let i: &rustc_hir::Attribute = i;
            match i {
                rustc_hir::Attribute::Parsed(AttributeKind::RustcMir(kind)) =>
                    {
                    break 'done Some(kind);
                }
                _ => {}
            }
        }
        None
    }
}find_attr!(attrs, AttributeKind::RustcMir(kind) => kind) {
21        let move_data = MoveData::gather_moves(body, tcx, |_| true);
22        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_dataflow/src/rustc_peek.rs:22",
                        "rustc_mir_dataflow::rustc_peek", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_dataflow/src/rustc_peek.rs"),
                        ::tracing_core::__macro_support::Option::Some(22u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_dataflow::rustc_peek"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("running rustc_peek::SanityCheck on {0}",
                                                    tcx.def_path_str(def_id)) as &dyn Value))])
            });
    } else { ; }
};debug!("running rustc_peek::SanityCheck on {}", tcx.def_path_str(def_id));
23        if kind.contains(&RustcMirKind::PeekMaybeInit) {
24            let flow_inits = MaybeInitializedPlaces::new(tcx, body, &move_data)
25                .iterate_to_fixpoint(tcx, body, None)
26                .into_results_cursor(body);
27            sanity_check_via_rustc_peek(tcx, flow_inits);
28        }
29
30        if kind.contains(&RustcMirKind::PeekMaybeUninit) {
31            let flow_uninits = MaybeUninitializedPlaces::new(tcx, body, &move_data)
32                .iterate_to_fixpoint(tcx, body, None)
33                .into_results_cursor(body);
34            sanity_check_via_rustc_peek(tcx, flow_uninits);
35        }
36
37        if kind.contains(&RustcMirKind::PeekLiveness) {
38            let flow_liveness =
39                MaybeLiveLocals.iterate_to_fixpoint(tcx, body, None).into_results_cursor(body);
40            sanity_check_via_rustc_peek(tcx, flow_liveness);
41        }
42
43        if kind.contains(&RustcMirKind::StopAfterDataflow) {
44            tcx.dcx().emit_fatal(StopAfterDataFlowEndedCompilation);
45        }
46    } else {
47        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_dataflow/src/rustc_peek.rs:47",
                        "rustc_mir_dataflow::rustc_peek", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_dataflow/src/rustc_peek.rs"),
                        ::tracing_core::__macro_support::Option::Some(47u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_dataflow::rustc_peek"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("skipping rustc_peek::SanityCheck on {0}",
                                                    tcx.def_path_str(def_id)) as &dyn Value))])
            });
    } else { ; }
};debug!("skipping rustc_peek::SanityCheck on {}", tcx.def_path_str(def_id));
48    }
49}
50
51/// This function scans `mir` for all calls to the intrinsic
52/// `rustc_peek` that have the expression form `rustc_peek(&expr)`.
53///
54/// For each such call, determines what the dataflow bit-state is for
55/// the L-value corresponding to `expr`; if the bit-state is a 1, then
56/// that call to `rustc_peek` is ignored by the sanity check. If the
57/// bit-state is a 0, then this pass emits an error message saying
58/// "rustc_peek: bit not set".
59///
60/// The intention is that one can write unit tests for dataflow by
61/// putting code into a UI test and using `rustc_peek` to
62/// make observations about the results of dataflow static analyses.
63///
64/// (If there are any calls to `rustc_peek` that do not match the
65/// expression form above, then that emits an error as well, but those
66/// errors are not intended to be used for unit tests.)
67fn sanity_check_via_rustc_peek<'tcx, A>(tcx: TyCtxt<'tcx>, mut cursor: ResultsCursor<'_, 'tcx, A>)
68where
69    A: RustcPeekAt<'tcx>,
70{
71    let def_id = cursor.body().source.def_id();
72    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_dataflow/src/rustc_peek.rs:72",
                        "rustc_mir_dataflow::rustc_peek", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_dataflow/src/rustc_peek.rs"),
                        ::tracing_core::__macro_support::Option::Some(72u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_dataflow::rustc_peek"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("sanity_check_via_rustc_peek def_id: {0:?}",
                                                    def_id) as &dyn Value))])
            });
    } else { ; }
};debug!("sanity_check_via_rustc_peek def_id: {:?}", def_id);
73
74    let peek_calls = cursor.body().basic_blocks.iter_enumerated().filter_map(|(bb, block_data)| {
75        PeekCall::from_terminator(tcx, block_data.terminator()).map(|call| (bb, block_data, call))
76    });
77
78    for (bb, block_data, call) in peek_calls {
79        // Look for a sequence like the following to indicate that we should be peeking at `_1`:
80        //    _2 = &_1;
81        //    rustc_peek(_2);
82        //
83        //    /* or */
84        //
85        //    _2 = _1;
86        //    rustc_peek(_2);
87        let (statement_index, peek_rval) = block_data
88            .statements
89            .iter()
90            .enumerate()
91            .find_map(|(i, stmt)| value_assigned_to_local(stmt, call.arg).map(|rval| (i, rval)))
92            .expect(
93                "call to rustc_peek should be preceded by \
94                    assignment to temporary holding its argument",
95            );
96
97        match (call.kind, peek_rval) {
98            (PeekCallKind::ByRef, mir::Rvalue::Ref(_, _, place))
99            | (
100                PeekCallKind::ByVal,
101                mir::Rvalue::Use(mir::Operand::Move(place) | mir::Operand::Copy(place)),
102            ) => {
103                let loc = Location { block: bb, statement_index };
104                cursor.seek_before_primary_effect(loc);
105                let state = cursor.get();
106                let analysis = cursor.analysis();
107                analysis.peek_at(tcx, *place, state, call);
108            }
109
110            _ => {
111                tcx.dcx().emit_err(PeekMustBePlaceOrRefPlace { span: call.span });
112            }
113        }
114    }
115}
116
117/// If `stmt` is an assignment where the LHS is the given local (with no projections), returns the
118/// RHS of the assignment.
119fn value_assigned_to_local<'a, 'tcx>(
120    stmt: &'a mir::Statement<'tcx>,
121    local: Local,
122) -> Option<&'a mir::Rvalue<'tcx>> {
123    if let mir::StatementKind::Assign(box (place, rvalue)) = &stmt.kind
124        && let Some(l) = place.as_local()
125        && local == l
126    {
127        return Some(&*rvalue);
128    }
129
130    None
131}
132
133#[derive(#[automatically_derived]
impl ::core::clone::Clone for PeekCallKind {
    #[inline]
    fn clone(&self) -> PeekCallKind { *self }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for PeekCallKind { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for PeekCallKind {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f,
            match self {
                PeekCallKind::ByVal => "ByVal",
                PeekCallKind::ByRef => "ByRef",
            })
    }
}Debug)]
134enum PeekCallKind {
135    ByVal,
136    ByRef,
137}
138
139impl PeekCallKind {
140    fn from_arg_ty(arg: Ty<'_>) -> Self {
141        match arg.kind() {
142            ty::Ref(_, _, _) => PeekCallKind::ByRef,
143            _ => PeekCallKind::ByVal,
144        }
145    }
146}
147
148#[derive(#[automatically_derived]
impl ::core::clone::Clone for PeekCall {
    #[inline]
    fn clone(&self) -> PeekCall {
        let _: ::core::clone::AssertParamIsClone<Local>;
        let _: ::core::clone::AssertParamIsClone<PeekCallKind>;
        let _: ::core::clone::AssertParamIsClone<Span>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for PeekCall { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for PeekCall {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f, "PeekCall",
            "arg", &self.arg, "kind", &self.kind, "span", &&self.span)
    }
}Debug)]
149struct PeekCall {
150    arg: Local,
151    kind: PeekCallKind,
152    span: Span,
153}
154
155impl PeekCall {
156    fn from_terminator<'tcx>(
157        tcx: TyCtxt<'tcx>,
158        terminator: &mir::Terminator<'tcx>,
159    ) -> Option<Self> {
160        use mir::Operand;
161
162        let span = terminator.source_info.span;
163        if let mir::TerminatorKind::Call { func: Operand::Constant(func), args, .. } =
164            &terminator.kind
165            && let ty::FnDef(def_id, fn_args) = *func.const_.ty().kind()
166        {
167            if tcx.intrinsic(def_id)?.name != sym::rustc_peek {
168                return None;
169            }
170
171            match (&fn_args.len(), &1) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(fn_args.len(), 1);
172            let kind = PeekCallKind::from_arg_ty(fn_args.type_at(0));
173            let arg = match &args[0].node {
174                Operand::Copy(place) | Operand::Move(place) => {
175                    if let Some(local) = place.as_local() {
176                        local
177                    } else {
178                        tcx.dcx().emit_err(PeekMustBeNotTemporary { span });
179                        return None;
180                    }
181                }
182                _ => {
183                    tcx.dcx().emit_err(PeekMustBeNotTemporary { span });
184                    return None;
185                }
186            };
187
188            return Some(PeekCall { arg, kind, span });
189        }
190
191        None
192    }
193}
194
195trait RustcPeekAt<'tcx>: Analysis<'tcx> {
196    fn peek_at(
197        &self,
198        tcx: TyCtxt<'tcx>,
199        place: mir::Place<'tcx>,
200        state: &Self::Domain,
201        call: PeekCall,
202    );
203}
204
205impl<'tcx, A, D> RustcPeekAt<'tcx> for A
206where
207    A: Analysis<'tcx, Domain = D> + HasMoveData<'tcx>,
208    D: JoinSemiLattice + Clone + BitSetExt<MovePathIndex>,
209{
210    fn peek_at(
211        &self,
212        tcx: TyCtxt<'tcx>,
213        place: mir::Place<'tcx>,
214        state: &Self::Domain,
215        call: PeekCall,
216    ) {
217        match self.move_data().rev_lookup.find(place.as_ref()) {
218            LookupResult::Exact(peek_mpi) => {
219                let bit_state = state.contains(peek_mpi);
220                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_dataflow/src/rustc_peek.rs:220",
                        "rustc_mir_dataflow::rustc_peek", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_dataflow/src/rustc_peek.rs"),
                        ::tracing_core::__macro_support::Option::Some(220u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_dataflow::rustc_peek"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("rustc_peek({0:?} = &{1:?}) bit_state: {2}",
                                                    call.arg, place, bit_state) as &dyn Value))])
            });
    } else { ; }
};debug!("rustc_peek({:?} = &{:?}) bit_state: {}", call.arg, place, bit_state);
221                if !bit_state {
222                    tcx.dcx().emit_err(PeekBitNotSet { span: call.span });
223                }
224            }
225
226            LookupResult::Parent(..) => {
227                tcx.dcx().emit_err(PeekArgumentUntracked { span: call.span });
228            }
229        }
230    }
231}
232
233impl<'tcx> RustcPeekAt<'tcx> for MaybeLiveLocals {
234    fn peek_at(
235        &self,
236        tcx: TyCtxt<'tcx>,
237        place: mir::Place<'tcx>,
238        state: &Self::Domain,
239        call: PeekCall,
240    ) {
241        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_dataflow/src/rustc_peek.rs:241",
                        "rustc_mir_dataflow::rustc_peek", ::tracing::Level::INFO,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_dataflow/src/rustc_peek.rs"),
                        ::tracing_core::__macro_support::Option::Some(241u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_dataflow::rustc_peek"),
                        ::tracing_core::field::FieldSet::new(&["message", "place"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::INFO <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("peek_at")
                                            as &dyn Value)),
                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&place) as
                                            &dyn Value))])
            });
    } else { ; }
};info!(?place, "peek_at");
242        let Some(local) = place.as_local() else {
243            tcx.dcx().emit_err(PeekArgumentNotALocal { span: call.span });
244            return;
245        };
246
247        if !state.contains(local) {
248            tcx.dcx().emit_err(PeekBitNotSet { span: call.span });
249        }
250    }
251}