rustc_mir_build/builder/matches/util.rs
1use rustc_data_structures::fx::FxIndexMap;
2use rustc_middle::mir::*;
3use rustc_middle::ty::Ty;
4use rustc_span::Span;
5use tracing::debug;
6
7use crate::builder::Builder;
8use crate::builder::expr::as_place::PlaceBase;
9use crate::builder::matches::{Binding, Candidate, FlatPat, MatchPairTree, TestCase};
10
11impl<'a, 'tcx> Builder<'a, 'tcx> {
12 /// Creates a false edge to `imaginary_target` and a real edge to
13 /// real_target. If `imaginary_target` is none, or is the same as the real
14 /// target, a Goto is generated instead to simplify the generated MIR.
15 pub(crate) fn false_edges(
16 &mut self,
17 from_block: BasicBlock,
18 real_target: BasicBlock,
19 imaginary_target: BasicBlock,
20 source_info: SourceInfo,
21 ) {
22 if imaginary_target != real_target {
23 self.cfg.terminate(
24 from_block,
25 source_info,
26 TerminatorKind::FalseEdge { real_target, imaginary_target },
27 );
28 } else {
29 self.cfg.goto(from_block, source_info, real_target)
30 }
31 }
32}
33
34/// Determine the set of places that have to be stable across match guards.
35///
36/// Returns a list of places that need a fake borrow along with a local to store it.
37///
38/// Match exhaustiveness checking is not able to handle the case where the place being matched on is
39/// mutated in the guards. We add "fake borrows" to the guards that prevent any mutation of the
40/// place being matched. There are a some subtleties:
41///
42/// 1. Borrowing `*x` doesn't prevent assigning to `x`. If `x` is a shared reference, the borrow
43/// isn't even tracked. As such we have to add fake borrows of any prefixes of a place.
44/// 2. We don't want `match x { (Some(_), _) => (), .. }` to conflict with mutable borrows of `x.1`, so we
45/// only add fake borrows for places which are bound or tested by the match.
46/// 3. We don't want `match x { Some(_) => (), .. }` to conflict with mutable borrows of `(x as
47/// Some).0`, so the borrows are a special shallow borrow that only affects the place and not its
48/// projections.
49/// ```rust
50/// let mut x = (Some(0), true);
51/// match x {
52/// (Some(_), false) => {}
53/// _ if { if let Some(ref mut y) = x.0 { *y += 1 }; true } => {}
54/// _ => {}
55/// }
56/// ```
57/// 4. The fake borrows may be of places in inactive variants, e.g. here we need to fake borrow `x`
58/// and `(x as Some).0`, but when we reach the guard `x` may not be `Some`.
59/// ```rust
60/// let mut x = (Some(Some(0)), true);
61/// match x {
62/// (Some(Some(_)), false) => {}
63/// _ if { if let Some(Some(ref mut y)) = x.0 { *y += 1 }; true } => {}
64/// _ => {}
65/// }
66/// ```
67/// So it would be UB to generate code for the fake borrows. They therefore have to be removed by
68/// a MIR pass run after borrow checking.
69pub(super) fn collect_fake_borrows<'tcx>(
70 cx: &mut Builder<'_, 'tcx>,
71 candidates: &[Candidate<'tcx>],
72 temp_span: Span,
73 scrutinee_base: PlaceBase,
74) -> Vec<(Place<'tcx>, Local, FakeBorrowKind)> {
75 if candidates.iter().all(|candidate| !candidate.has_guard) {
76 // Fake borrows are only used when there is a guard.
77 return Vec::new();
78 }
79 let mut collector =
80 FakeBorrowCollector { cx, scrutinee_base, fake_borrows: FxIndexMap::default() };
81 for candidate in candidates.iter() {
82 collector.visit_candidate(candidate);
83 }
84 let fake_borrows = collector.fake_borrows;
85 debug!("add_fake_borrows fake_borrows = {:?}", fake_borrows);
86 let tcx = cx.tcx;
87 fake_borrows
88 .iter()
89 .map(|(matched_place, borrow_kind)| {
90 let fake_borrow_deref_ty = matched_place.ty(&cx.local_decls, tcx).ty;
91 let fake_borrow_ty =
92 Ty::new_imm_ref(tcx, tcx.lifetimes.re_erased, fake_borrow_deref_ty);
93 let mut fake_borrow_temp = LocalDecl::new(fake_borrow_ty, temp_span);
94 fake_borrow_temp.local_info = ClearCrossCrate::Set(Box::new(LocalInfo::FakeBorrow));
95 let fake_borrow_temp = cx.local_decls.push(fake_borrow_temp);
96 (*matched_place, fake_borrow_temp, *borrow_kind)
97 })
98 .collect()
99}
100
101pub(super) struct FakeBorrowCollector<'a, 'b, 'tcx> {
102 cx: &'a mut Builder<'b, 'tcx>,
103 /// Base of the scrutinee place. Used to distinguish bindings inside the scrutinee place from
104 /// bindings inside deref patterns.
105 scrutinee_base: PlaceBase,
106 /// Store for each place the kind of borrow to take. In case of conflicts, we take the strongest
107 /// borrow (i.e. Deep > Shallow).
108 /// Invariant: for any place in `fake_borrows`, all the prefixes of this place that are
109 /// dereferences are also borrowed with the same of stronger borrow kind.
110 fake_borrows: FxIndexMap<Place<'tcx>, FakeBorrowKind>,
111}
112
113impl<'a, 'b, 'tcx> FakeBorrowCollector<'a, 'b, 'tcx> {
114 // Fake borrow this place and its dereference prefixes.
115 fn fake_borrow(&mut self, place: Place<'tcx>, kind: FakeBorrowKind) {
116 if self.fake_borrows.get(&place).is_some_and(|k| *k >= kind) {
117 return;
118 }
119 self.fake_borrows.insert(place, kind);
120 // Also fake borrow the prefixes of any fake borrow.
121 self.fake_borrow_deref_prefixes(place, kind);
122 }
123
124 // Fake borrow the prefixes of this place that are dereferences.
125 fn fake_borrow_deref_prefixes(&mut self, place: Place<'tcx>, kind: FakeBorrowKind) {
126 for (place_ref, elem) in place.as_ref().iter_projections().rev() {
127 if let ProjectionElem::Deref = elem {
128 // Insert a shallow borrow after a deref. For other projections the borrow of
129 // `place_ref` will conflict with any mutation of `place.base`.
130 let place = place_ref.to_place(self.cx.tcx);
131 if self.fake_borrows.get(&place).is_some_and(|k| *k >= kind) {
132 return;
133 }
134 self.fake_borrows.insert(place, kind);
135 }
136 }
137 }
138
139 fn visit_candidate(&mut self, candidate: &Candidate<'tcx>) {
140 for binding in &candidate.extra_data.bindings {
141 if let super::SubpatternBindings::One(binding) = binding {
142 self.visit_binding(binding);
143 }
144 }
145 for match_pair in &candidate.match_pairs {
146 self.visit_match_pair(match_pair);
147 }
148 }
149
150 fn visit_flat_pat(&mut self, flat_pat: &FlatPat<'tcx>) {
151 for binding in &flat_pat.extra_data.bindings {
152 if let super::SubpatternBindings::One(binding) = binding {
153 self.visit_binding(binding);
154 }
155 }
156 for match_pair in &flat_pat.match_pairs {
157 self.visit_match_pair(match_pair);
158 }
159 }
160
161 fn visit_match_pair(&mut self, match_pair: &MatchPairTree<'tcx>) {
162 if let TestCase::Or { pats, .. } = &match_pair.test_case {
163 for flat_pat in pats.iter() {
164 self.visit_flat_pat(flat_pat)
165 }
166 } else if matches!(match_pair.test_case, TestCase::Deref { .. }) {
167 // The subpairs of a deref pattern are all places relative to the deref temporary, so we
168 // don't fake borrow them. Problem is, if we only shallowly fake-borrowed
169 // `match_pair.place`, this would allow:
170 // ```
171 // let mut b = Box::new(false);
172 // match b {
173 // deref!(true) => {} // not reached because `*b == false`
174 // _ if { *b = true; false } => {} // not reached because the guard is `false`
175 // deref!(false) => {} // not reached because the guard changed it
176 // // UB because we reached the unreachable.
177 // }
178 // ```
179 // Hence we fake borrow using a deep borrow.
180 if let Some(place) = match_pair.place {
181 self.fake_borrow(place, FakeBorrowKind::Deep);
182 }
183 } else {
184 // Insert a Shallow borrow of any place that is switched on.
185 if let Some(place) = match_pair.place {
186 self.fake_borrow(place, FakeBorrowKind::Shallow);
187 }
188
189 for subpair in &match_pair.subpairs {
190 self.visit_match_pair(subpair);
191 }
192 }
193 }
194
195 fn visit_binding(&mut self, Binding { source, .. }: &Binding<'tcx>) {
196 if let PlaceBase::Local(l) = self.scrutinee_base
197 && l != source.local
198 {
199 // The base of this place is a temporary created for deref patterns. We don't emit fake
200 // borrows for these as they are not initialized in all branches.
201 return;
202 }
203
204 // Insert a borrows of prefixes of places that are bound and are
205 // behind a dereference projection.
206 //
207 // These borrows are taken to avoid situations like the following:
208 //
209 // match x[10] {
210 // _ if { x = &[0]; false } => (),
211 // y => (), // Out of bounds array access!
212 // }
213 //
214 // match *x {
215 // // y is bound by reference in the guard and then by copy in the
216 // // arm, so y is 2 in the arm!
217 // y if { y == 1 && (x = &2) == () } => y,
218 // _ => 3,
219 // }
220 //
221 // We don't just fake borrow the whole place because this is allowed:
222 // match u {
223 // _ if { u = true; false } => (),
224 // x => (),
225 // }
226 self.fake_borrow_deref_prefixes(*source, FakeBorrowKind::Shallow);
227 }
228}
229
230#[must_use]
231pub(crate) fn ref_pat_borrow_kind(ref_mutability: Mutability) -> BorrowKind {
232 match ref_mutability {
233 Mutability::Mut => BorrowKind::Mut { kind: MutBorrowKind::Default },
234 Mutability::Not => BorrowKind::Shared,
235 }
236}