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