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