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
18pub(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
41impl<'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 }
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 StatementKind::AscribeUserType(..)
68 | StatementKind::PlaceMention(..)
70 | StatementKind::Coverage(..)
72 | StatementKind::StorageLive(..)
74 | StatementKind::BackwardIncompatibleDropHint { .. } => {}
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::SetDiscriminant { .. } => {
87 ::rustc_middle::util::bug::bug_fmt(format_args!("Statement not allowed in this MIR phase"))bug!("Statement not allowed in this MIR phase")
88 }
89 }
90
91 self.super_statement(statement, location);
92 }
93
94 fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
95 self.check_activations(location);
96
97 match &terminator.kind {
98 TerminatorKind::SwitchInt { discr, targets: _ } => {
99 self.consume_operand(location, discr);
100 }
101 TerminatorKind::Drop { place: drop_place, target: _, unwind: _, replace, drop: _ } => {
102 let write_kind =
103 if *replace { WriteKind::Replace } else { WriteKind::StorageDeadOrDrop };
104 self.access_place(
105 location,
106 *drop_place,
107 (AccessDepth::Drop, Write(write_kind)),
108 LocalMutationIsAllowed::Yes,
109 );
110 }
111 TerminatorKind::Call {
112 func,
113 args,
114 destination,
115 target: _,
116 unwind: _,
117 call_source: _,
118 fn_span: _,
119 } => {
120 self.consume_operand(location, func);
121 for arg in args {
122 self.consume_operand(location, &arg.node);
123 }
124 self.mutate_place(location, *destination, Deep);
125 }
126 TerminatorKind::TailCall { func, args, .. } => {
127 self.consume_operand(location, func);
128 for arg in args {
129 self.consume_operand(location, &arg.node);
130 }
131 }
132 TerminatorKind::Assert { cond, expected: _, msg, target: _, unwind: _ } => {
133 self.consume_operand(location, cond);
134 use rustc_middle::mir::AssertKind;
135 if let AssertKind::BoundsCheck { len, index } = &**msg {
136 self.consume_operand(location, len);
137 self.consume_operand(location, index);
138 }
139 }
140 TerminatorKind::Yield { value, resume, resume_arg, drop: _ } => {
141 self.consume_operand(location, value);
142
143 let borrow_set = self.borrow_set;
145 let resume = self.location_table.start_index(resume.start_location());
146 for (i, data) in borrow_set.iter_enumerated() {
147 if borrow_of_local_data(data.borrowed_place) {
148 self.facts.loan_invalidated_at.push((resume, i));
149 }
150 }
151
152 self.mutate_place(location, *resume_arg, Deep);
153 }
154 TerminatorKind::UnwindResume
155 | TerminatorKind::Return
156 | TerminatorKind::CoroutineDrop => {
157 let borrow_set = self.borrow_set;
159 let start = self.location_table.start_index(location);
160 for (i, data) in borrow_set.iter_enumerated() {
161 if borrow_of_local_data(data.borrowed_place) {
162 self.facts.loan_invalidated_at.push((start, i));
163 }
164 }
165 }
166 TerminatorKind::InlineAsm {
167 asm_macro: _,
168 template: _,
169 operands,
170 options: _,
171 line_spans: _,
172 targets: _,
173 unwind: _,
174 } => {
175 for op in operands {
176 match op {
177 InlineAsmOperand::In { reg: _, value } => {
178 self.consume_operand(location, value);
179 }
180 InlineAsmOperand::Out { reg: _, late: _, place, .. } => {
181 if let &Some(place) = place {
182 self.mutate_place(location, place, Shallow(None));
183 }
184 }
185 InlineAsmOperand::InOut { reg: _, late: _, in_value, out_place } => {
186 self.consume_operand(location, in_value);
187 if let &Some(out_place) = out_place {
188 self.mutate_place(location, out_place, Shallow(None));
189 }
190 }
191 InlineAsmOperand::Const { value: _ }
192 | InlineAsmOperand::SymFn { value: _ }
193 | InlineAsmOperand::SymStatic { def_id: _ }
194 | InlineAsmOperand::Label { target_index: _ } => {}
195 }
196 }
197 }
198 TerminatorKind::Goto { target: _ }
199 | TerminatorKind::UnwindTerminate(_)
200 | TerminatorKind::Unreachable
201 | TerminatorKind::FalseEdge { real_target: _, imaginary_target: _ }
202 | TerminatorKind::FalseUnwind { real_target: _, unwind: _ } => {
203 }
205 }
206
207 self.super_terminator(terminator, location);
208 }
209}
210
211impl<'a, 'tcx> LoanInvalidationsGenerator<'a, 'tcx> {
212 fn mutate_place(&mut self, location: Location, place: Place<'tcx>, kind: AccessDepth) {
214 self.access_place(
215 location,
216 place,
217 (kind, Write(WriteKind::Mutate)),
218 LocalMutationIsAllowed::ExceptUpvars,
219 );
220 }
221
222 fn consume_operand(&mut self, location: Location, operand: &Operand<'tcx>) {
224 match *operand {
225 Operand::Copy(place) => {
226 self.access_place(
227 location,
228 place,
229 (Deep, Read(ReadKind::Copy)),
230 LocalMutationIsAllowed::No,
231 );
232 }
233 Operand::Move(place) => {
234 self.access_place(
235 location,
236 place,
237 (Deep, Write(WriteKind::Move)),
238 LocalMutationIsAllowed::Yes,
239 );
240 }
241 Operand::Constant(_) | Operand::RuntimeChecks(_) => {}
242 }
243 }
244
245 fn consume_rvalue(&mut self, location: Location, rvalue: &Rvalue<'tcx>) {
247 match rvalue {
248 &Rvalue::Ref(_ , bk, place) => {
249 let access_kind = match bk {
250 BorrowKind::Fake(FakeBorrowKind::Shallow) => {
251 (Shallow(Some(ArtificialField::FakeBorrow)), Read(ReadKind::Borrow(bk)))
252 }
253 BorrowKind::Shared | BorrowKind::Fake(FakeBorrowKind::Deep) => {
254 (Deep, Read(ReadKind::Borrow(bk)))
255 }
256 BorrowKind::Mut { .. } => {
257 let wk = WriteKind::MutableBorrow(bk);
258 if bk.is_two_phase_borrow() {
259 (Deep, Reservation(wk))
260 } else {
261 (Deep, Write(wk))
262 }
263 }
264 };
265
266 self.access_place(location, place, access_kind, LocalMutationIsAllowed::No);
267 }
268
269 &Rvalue::Reborrow(_target, mutability, place) => {
270 let access_kind = (
271 Deep,
272 if mutability == Mutability::Mut {
273 Reservation(WriteKind::MutableBorrow(BorrowKind::Mut {
274 kind: MutBorrowKind::TwoPhaseBorrow,
275 }))
276 } else {
277 Read(ReadKind::Borrow(BorrowKind::Shared))
278 },
279 );
280
281 self.access_place(location, place, access_kind, LocalMutationIsAllowed::No);
282 }
283
284 &Rvalue::RawPtr(kind, place) => {
285 let access_kind = match kind {
286 RawPtrKind::Mut => (
287 Deep,
288 Write(WriteKind::MutableBorrow(BorrowKind::Mut {
289 kind: MutBorrowKind::Default,
290 })),
291 ),
292 RawPtrKind::Const => (Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),
293 RawPtrKind::FakeForPtrMetadata => {
294 (Shallow(Some(ArtificialField::ArrayLength)), Read(ReadKind::Copy))
295 }
296 };
297
298 self.access_place(location, place, access_kind, LocalMutationIsAllowed::No);
299 }
300
301 Rvalue::ThreadLocalRef(_) => {}
302
303 Rvalue::Use(operand, _)
304 | Rvalue::Repeat(operand, _)
305 | Rvalue::UnaryOp(_ , operand)
306 | Rvalue::Cast(_ , operand, _ ) => {
307 self.consume_operand(location, operand)
308 }
309
310 &Rvalue::Discriminant(place) => {
311 self.access_place(
312 location,
313 place,
314 (Shallow(None), Read(ReadKind::Copy)),
315 LocalMutationIsAllowed::No,
316 );
317 }
318
319 Rvalue::BinaryOp(_bin_op, (operand1, operand2)) => {
320 self.consume_operand(location, operand1);
321 self.consume_operand(location, operand2);
322 }
323
324 Rvalue::Aggregate(_, operands) => {
325 for operand in operands {
326 self.consume_operand(location, operand);
327 }
328 }
329
330 Rvalue::WrapUnsafeBinder(op, _) => {
331 self.consume_operand(location, op);
332 }
333
334 Rvalue::CopyForDeref(_) => ::rustc_middle::util::bug::bug_fmt(format_args!("`CopyForDeref` in borrowck"))bug!("`CopyForDeref` in borrowck"),
335 }
336 }
337
338 fn access_place(
340 &mut self,
341 location: Location,
342 place: Place<'tcx>,
343 kind: (AccessDepth, ReadOrWrite),
344 _is_local_mutation_allowed: LocalMutationIsAllowed,
345 ) {
346 let (sd, rw) = kind;
347 self.check_access_for_conflict(location, place, sd, rw);
349 }
350
351 fn check_access_for_conflict(
352 &mut self,
353 location: Location,
354 place: Place<'tcx>,
355 sd: AccessDepth,
356 rw: ReadOrWrite,
357 ) {
358 {
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:358",
"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(358u32),
::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!(
359 "check_access_for_conflict(location={:?}, place={:?}, sd={:?}, rw={:?})",
360 location, place, sd, rw,
361 );
362 each_borrow_involving_path(
363 self,
364 self.tcx,
365 self.body,
366 (sd, place),
367 self.borrow_set,
368 |_| true,
369 |this, borrow_index, borrow| {
370 match (rw, borrow.kind) {
371 (Activation(_, activating), _) if activating == borrow_index => {
378 }
381
382 (Read(_), BorrowKind::Fake(_) | BorrowKind::Shared)
383 | (
384 Read(ReadKind::Borrow(BorrowKind::Fake(FakeBorrowKind::Shallow))),
385 BorrowKind::Mut { .. },
386 ) => {
387 }
389
390 (Read(_), BorrowKind::Mut { .. }) => {
391 if !is_active(this.dominators, borrow, location) {
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());
395 return ControlFlow::Continue(());
396 }
397
398 this.emit_loan_invalidated_at(borrow_index, location);
401 }
402
403 (Reservation(_) | Activation(_, _) | Write(_), _) => {
404 this.emit_loan_invalidated_at(borrow_index, location);
409 }
410 }
411 ControlFlow::Continue(())
412 },
413 );
414 }
415
416 fn emit_loan_invalidated_at(&mut self, b: BorrowIndex, l: Location) {
418 let lidx = self.location_table.start_index(l);
419 self.facts.loan_invalidated_at.push((lidx, b));
420 }
421
422 fn check_activations(&mut self, location: Location) {
423 for &borrow_index in self.borrow_set.activations_at_location(location) {
427 let borrow = &self.borrow_set[borrow_index];
428
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 {
431 BorrowKind::Shared | BorrowKind::Fake(_) => false,
432 BorrowKind::Mut { .. } => true,
433 });
434
435 self.access_place(
436 location,
437 borrow.borrowed_place,
438 (Deep, Activation(WriteKind::MutableBorrow(borrow.kind), borrow_index)),
439 LocalMutationIsAllowed::No,
440 );
441
442 }
446 }
447}