Skip to main content

rustc_borrowck/polonius/legacy/
loan_invalidations.rs

1use std::ops::ControlFlow;
2
3use rustc_data_structures::graph::dominators::Dominators;
4use rustc_middle::bug;
5use rustc_middle::mir::visit::Visitor;
6use rustc_middle::mir::*;
7use rustc_middle::ty::TyCtxt;
8use tracing::debug;
9
10use super::{PoloniusFacts, PoloniusLocationTable};
11use crate::borrow_set::BorrowSet;
12use crate::path_utils::*;
13use crate::{
14    AccessDepth, Activation, ArtificialField, BorrowIndex, Deep, LocalMutationIsAllowed, Read,
15    ReadKind, ReadOrWrite, Reservation, Shallow, Write, WriteKind,
16};
17
18/// Emit `loan_invalidated_at` facts.
19pub(super) fn emit_loan_invalidations<'tcx>(
20    tcx: TyCtxt<'tcx>,
21    facts: &mut PoloniusFacts,
22    body: &Body<'tcx>,
23    location_table: &PoloniusLocationTable,
24    borrow_set: &BorrowSet<'tcx>,
25) {
26    let dominators = body.basic_blocks.dominators();
27    let mut visitor =
28        LoanInvalidationsGenerator { facts, borrow_set, tcx, location_table, body, dominators };
29    visitor.visit_body(body);
30}
31
32struct LoanInvalidationsGenerator<'a, 'tcx> {
33    tcx: TyCtxt<'tcx>,
34    facts: &'a mut PoloniusFacts,
35    body: &'a Body<'tcx>,
36    location_table: &'a PoloniusLocationTable,
37    dominators: &'a Dominators<BasicBlock>,
38    borrow_set: &'a BorrowSet<'tcx>,
39}
40
41/// Visits the whole MIR and generates `invalidates()` facts.
42/// Most of the code implementing this was stolen from `borrow_check/mod.rs`.
43impl<'a, 'tcx> Visitor<'tcx> for LoanInvalidationsGenerator<'a, 'tcx> {
44    fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
45        self.check_activations(location);
46
47        match &statement.kind {
48            StatementKind::Assign((lhs, rhs)) => {
49                self.consume_rvalue(location, rhs);
50
51                self.mutate_place(location, *lhs, Shallow(None));
52            }
53            StatementKind::FakeRead((_, _)) => {
54                // Only relevant for initialized/liveness/safety checks.
55            }
56            StatementKind::Intrinsic(NonDivergingIntrinsic::Assume(op)) => {
57                self.consume_operand(location, op);
58            }
59            StatementKind::Intrinsic(NonDivergingIntrinsic::CopyNonOverlapping(
60                CopyNonOverlapping { src, dst, count },
61            )) => {
62                self.consume_operand(location, src);
63                self.consume_operand(location, dst);
64                self.consume_operand(location, count);
65            }
66            // Only relevant for mir typeck
67            StatementKind::AscribeUserType(..)
68            // Only relevant for liveness and unsafeck
69            | StatementKind::PlaceMention(..)
70            // Doesn't have any language semantics
71            | StatementKind::Coverage(..)
72            // Does not actually affect borrowck
73            | StatementKind::StorageLive(..) => {}
74            StatementKind::StorageDead(local) => {
75                self.access_place(
76                    location,
77                    Place::from(*local),
78                    (Shallow(None), Write(WriteKind::StorageDeadOrDrop)),
79                    LocalMutationIsAllowed::Yes,
80                );
81            }
82            StatementKind::ConstEvalCounter
83            | StatementKind::Nop
84            | StatementKind::BackwardIncompatibleDropHint { .. }
85            | StatementKind::SetDiscriminant { .. } => {
86                ::rustc_middle::util::bug::bug_fmt(format_args!("Statement not allowed in this MIR phase"))bug!("Statement not allowed in this MIR phase")
87            }
88        }
89
90        self.super_statement(statement, location);
91    }
92
93    fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
94        self.check_activations(location);
95
96        match &terminator.kind {
97            TerminatorKind::SwitchInt { discr, targets: _ } => {
98                self.consume_operand(location, discr);
99            }
100            TerminatorKind::Drop { place: drop_place, target: _, unwind: _, replace, drop: _ } => {
101                let write_kind =
102                    if *replace { WriteKind::Replace } else { WriteKind::StorageDeadOrDrop };
103                self.access_place(
104                    location,
105                    *drop_place,
106                    (AccessDepth::Drop, Write(write_kind)),
107                    LocalMutationIsAllowed::Yes,
108                );
109            }
110            TerminatorKind::Call {
111                func,
112                args,
113                destination,
114                target: _,
115                unwind: _,
116                call_source: _,
117                fn_span: _,
118            } => {
119                self.consume_operand(location, func);
120                for arg in args {
121                    self.consume_operand(location, &arg.node);
122                }
123                self.mutate_place(location, *destination, Deep);
124            }
125            TerminatorKind::TailCall { func, args, .. } => {
126                self.consume_operand(location, func);
127                for arg in args {
128                    self.consume_operand(location, &arg.node);
129                }
130            }
131            TerminatorKind::Assert { cond, expected: _, msg, target: _, unwind: _ } => {
132                self.consume_operand(location, cond);
133                use rustc_middle::mir::AssertKind;
134                if let AssertKind::BoundsCheck { len, index } = &**msg {
135                    self.consume_operand(location, len);
136                    self.consume_operand(location, index);
137                }
138            }
139            TerminatorKind::Yield { value, resume, resume_arg, drop: _ } => {
140                self.consume_operand(location, value);
141
142                // Invalidate all borrows of local places
143                let borrow_set = self.borrow_set;
144                let resume = self.location_table.start_index(resume.start_location());
145                for (i, data) in borrow_set.iter_enumerated() {
146                    if borrow_of_local_data(data.borrowed_place) {
147                        self.facts.loan_invalidated_at.push((resume, i));
148                    }
149                }
150
151                self.mutate_place(location, *resume_arg, Deep);
152            }
153            TerminatorKind::UnwindResume
154            | TerminatorKind::Return
155            | TerminatorKind::CoroutineDrop => {
156                // Invalidate all borrows of local places
157                let borrow_set = self.borrow_set;
158                let start = self.location_table.start_index(location);
159                for (i, data) in borrow_set.iter_enumerated() {
160                    if borrow_of_local_data(data.borrowed_place) {
161                        self.facts.loan_invalidated_at.push((start, i));
162                    }
163                }
164            }
165            TerminatorKind::InlineAsm {
166                asm_macro: _,
167                template: _,
168                operands,
169                options: _,
170                line_spans: _,
171                targets: _,
172                unwind: _,
173            } => {
174                for op in operands {
175                    match op {
176                        InlineAsmOperand::In { reg: _, value } => {
177                            self.consume_operand(location, value);
178                        }
179                        InlineAsmOperand::Out { reg: _, late: _, place, .. } => {
180                            if let &Some(place) = place {
181                                self.mutate_place(location, place, Shallow(None));
182                            }
183                        }
184                        InlineAsmOperand::InOut { reg: _, late: _, in_value, out_place } => {
185                            self.consume_operand(location, in_value);
186                            if let &Some(out_place) = out_place {
187                                self.mutate_place(location, out_place, Shallow(None));
188                            }
189                        }
190                        InlineAsmOperand::Const { value: _ }
191                        | InlineAsmOperand::SymFn { value: _ }
192                        | InlineAsmOperand::SymStatic { def_id: _ }
193                        | InlineAsmOperand::Label { target_index: _ } => {}
194                    }
195                }
196            }
197            TerminatorKind::Goto { target: _ }
198            | TerminatorKind::UnwindTerminate(_)
199            | TerminatorKind::Unreachable
200            | TerminatorKind::FalseEdge { real_target: _, imaginary_target: _ }
201            | TerminatorKind::FalseUnwind { real_target: _, unwind: _ } => {
202                // no data used, thus irrelevant to borrowck
203            }
204        }
205
206        self.super_terminator(terminator, location);
207    }
208}
209
210impl<'a, 'tcx> LoanInvalidationsGenerator<'a, 'tcx> {
211    /// Simulates mutation of a place.
212    fn mutate_place(&mut self, location: Location, place: Place<'tcx>, kind: AccessDepth) {
213        self.access_place(
214            location,
215            place,
216            (kind, Write(WriteKind::Mutate)),
217            LocalMutationIsAllowed::ExceptUpvars,
218        );
219    }
220
221    /// Simulates consumption of an operand.
222    fn consume_operand(&mut self, location: Location, operand: &Operand<'tcx>) {
223        match *operand {
224            Operand::Copy(place) => {
225                self.access_place(
226                    location,
227                    place,
228                    (Deep, Read(ReadKind::Copy)),
229                    LocalMutationIsAllowed::No,
230                );
231            }
232            Operand::Move(place) => {
233                self.access_place(
234                    location,
235                    place,
236                    (Deep, Write(WriteKind::Move)),
237                    LocalMutationIsAllowed::Yes,
238                );
239            }
240            Operand::Constant(_) | Operand::RuntimeChecks(_) => {}
241        }
242    }
243
244    // Simulates consumption of an rvalue
245    fn consume_rvalue(&mut self, location: Location, rvalue: &Rvalue<'tcx>) {
246        match rvalue {
247            &Rvalue::Ref(_ /*rgn*/, bk, place) => {
248                let access_kind = match bk {
249                    BorrowKind::Fake(FakeBorrowKind::Shallow) => {
250                        (Shallow(Some(ArtificialField::FakeBorrow)), Read(ReadKind::Borrow(bk)))
251                    }
252                    BorrowKind::Shared | BorrowKind::Fake(FakeBorrowKind::Deep) => {
253                        (Deep, Read(ReadKind::Borrow(bk)))
254                    }
255                    BorrowKind::Mut { .. } => {
256                        let wk = WriteKind::MutableBorrow(bk);
257                        if bk.is_two_phase_borrow() {
258                            (Deep, Reservation(wk))
259                        } else {
260                            (Deep, Write(wk))
261                        }
262                    }
263                };
264
265                self.access_place(location, place, access_kind, LocalMutationIsAllowed::No);
266            }
267
268            &Rvalue::Reborrow(_target, mutability, place) => {
269                let access_kind = (
270                    Deep,
271                    if mutability == Mutability::Mut {
272                        Reservation(WriteKind::MutableBorrow(BorrowKind::Mut {
273                            kind: MutBorrowKind::TwoPhaseBorrow,
274                        }))
275                    } else {
276                        Read(ReadKind::Borrow(BorrowKind::Shared))
277                    },
278                );
279
280                self.access_place(location, place, access_kind, LocalMutationIsAllowed::No);
281            }
282
283            &Rvalue::RawPtr(kind, place) => {
284                let access_kind = match kind {
285                    RawPtrKind::Mut => (
286                        Deep,
287                        Write(WriteKind::MutableBorrow(BorrowKind::Mut {
288                            kind: MutBorrowKind::Default,
289                        })),
290                    ),
291                    RawPtrKind::Const => (Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),
292                    RawPtrKind::FakeForPtrMetadata => {
293                        (Shallow(Some(ArtificialField::ArrayLength)), Read(ReadKind::Copy))
294                    }
295                };
296
297                self.access_place(location, place, access_kind, LocalMutationIsAllowed::No);
298            }
299
300            Rvalue::ThreadLocalRef(_) => {}
301
302            Rvalue::Use(operand, _)
303            | Rvalue::Repeat(operand, _)
304            | Rvalue::UnaryOp(_ /*un_op*/, operand)
305            | Rvalue::Cast(_ /*cast_kind*/, operand, _ /*ty*/) => {
306                self.consume_operand(location, operand)
307            }
308
309            &Rvalue::Discriminant(place) => {
310                self.access_place(
311                    location,
312                    place,
313                    (Shallow(None), Read(ReadKind::Copy)),
314                    LocalMutationIsAllowed::No,
315                );
316            }
317
318            Rvalue::BinaryOp(_bin_op, (operand1, operand2)) => {
319                self.consume_operand(location, operand1);
320                self.consume_operand(location, operand2);
321            }
322
323            Rvalue::Aggregate(_, operands) => {
324                for operand in operands {
325                    self.consume_operand(location, operand);
326                }
327            }
328
329            Rvalue::WrapUnsafeBinder(op, _) => {
330                self.consume_operand(location, op);
331            }
332
333            Rvalue::CopyForDeref(_) => ::rustc_middle::util::bug::bug_fmt(format_args!("`CopyForDeref` in borrowck"))bug!("`CopyForDeref` in borrowck"),
334        }
335    }
336
337    /// Simulates an access to a place.
338    fn access_place(
339        &mut self,
340        location: Location,
341        place: Place<'tcx>,
342        kind: (AccessDepth, ReadOrWrite),
343        _is_local_mutation_allowed: LocalMutationIsAllowed,
344    ) {
345        let (sd, rw) = kind;
346        // note: not doing check_access_permissions checks because they don't generate invalidates
347        self.check_access_for_conflict(location, place, sd, rw);
348    }
349
350    fn check_access_for_conflict(
351        &mut self,
352        location: Location,
353        place: Place<'tcx>,
354        sd: AccessDepth,
355        rw: ReadOrWrite,
356    ) {
357        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_borrowck/src/polonius/legacy/loan_invalidations.rs:357",
                        "rustc_borrowck::polonius::legacy::loan_invalidations",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_borrowck/src/polonius/legacy/loan_invalidations.rs"),
                        ::tracing_core::__macro_support::Option::Some(357u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_borrowck::polonius::legacy::loan_invalidations"),
                        ::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!("check_access_for_conflict(location={0:?}, place={1:?}, sd={2:?}, rw={3:?})",
                                                    location, place, sd, rw) as &dyn Value))])
            });
    } else { ; }
};debug!(
358            "check_access_for_conflict(location={:?}, place={:?}, sd={:?}, rw={:?})",
359            location, place, sd, rw,
360        );
361        each_borrow_involving_path(
362            self,
363            self.tcx,
364            self.body,
365            (sd, place),
366            self.borrow_set,
367            |_| true,
368            |this, borrow_index, borrow| {
369                match (rw, borrow.kind) {
370                    // Obviously an activation is compatible with its own
371                    // reservation (or even prior activating uses of same
372                    // borrow); so don't check if they interfere.
373                    //
374                    // NOTE: *reservations* do conflict with themselves;
375                    // thus aren't injecting unsoundness w/ this check.)
376                    (Activation(_, activating), _) if activating == borrow_index => {
377                        // Activating a borrow doesn't generate any invalidations, since we
378                        // have already taken the reservation
379                    }
380
381                    (Read(_), BorrowKind::Fake(_) | BorrowKind::Shared)
382                    | (
383                        Read(ReadKind::Borrow(BorrowKind::Fake(FakeBorrowKind::Shallow))),
384                        BorrowKind::Mut { .. },
385                    ) => {
386                        // Reads don't invalidate shared or shallow borrows
387                    }
388
389                    (Read(_), BorrowKind::Mut { .. }) => {
390                        // Reading from mere reservations of mutable-borrows is OK.
391                        if !is_active(this.dominators, borrow, location) {
392                            // If the borrow isn't active yet, reads don't invalidate it
393                            if !borrow.kind.is_two_phase_borrow() {
    ::core::panicking::panic("assertion failed: borrow.kind.is_two_phase_borrow()")
};assert!(borrow.kind.is_two_phase_borrow());
394                            return ControlFlow::Continue(());
395                        }
396
397                        // Unique and mutable borrows are invalidated by reads from any
398                        // involved path
399                        this.emit_loan_invalidated_at(borrow_index, location);
400                    }
401
402                    (Reservation(_) | Activation(_, _) | Write(_), _) => {
403                        // unique or mutable borrows are invalidated by writes.
404                        // Reservations count as writes since we need to check
405                        // that activating the borrow will be OK
406                        // FIXME(bob_twinkles) is this actually the right thing to do?
407                        this.emit_loan_invalidated_at(borrow_index, location);
408                    }
409                }
410                ControlFlow::Continue(())
411            },
412        );
413    }
414
415    /// Generates a new `loan_invalidated_at(L, B)` fact.
416    fn emit_loan_invalidated_at(&mut self, b: BorrowIndex, l: Location) {
417        let lidx = self.location_table.start_index(l);
418        self.facts.loan_invalidated_at.push((lidx, b));
419    }
420
421    fn check_activations(&mut self, location: Location) {
422        // Two-phase borrow support: For each activation that is newly
423        // generated at this statement, check if it interferes with
424        // another borrow.
425        for &borrow_index in self.borrow_set.activations_at_location(location) {
426            let borrow = &self.borrow_set[borrow_index];
427
428            // only mutable borrows should be 2-phase
429            if !match borrow.kind {
            BorrowKind::Shared | BorrowKind::Fake(_) => false,
            BorrowKind::Mut { .. } => true,
        } {
    ::core::panicking::panic("assertion failed: match borrow.kind {\n    BorrowKind::Shared | BorrowKind::Fake(_) => false,\n    BorrowKind::Mut { .. } => true,\n}")
};assert!(match borrow.kind {
430                BorrowKind::Shared | BorrowKind::Fake(_) => false,
431                BorrowKind::Mut { .. } => true,
432            });
433
434            self.access_place(
435                location,
436                borrow.borrowed_place,
437                (Deep, Activation(WriteKind::MutableBorrow(borrow.kind), borrow_index)),
438                LocalMutationIsAllowed::No,
439            );
440
441            // We do not need to call `check_if_path_or_subpath_is_moved`
442            // again, as we already called it when we made the
443            // initial reservation.
444        }
445    }
446}