Unnamed repository; edit this file 'description' to name the repository.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
//! A different sort of visitor for walking fn bodies. Unlike the
//! normal visitor, which just walks the entire body in one shot, the
//! `ExprUseVisitor` determines how expressions are being used.
//!
//! This is only used for upvar inference.

use either::Either;
use hir_def::{
    AdtId, HasModule, VariantId,
    attrs::AttrFlags,
    hir::{
        Array, AsmOperand, BindingId, Expr, ExprId, ExprOrPatId, MatchArm, Pat, PatId,
        RecordLitField, RecordSpread, Statement,
    },
    resolver::ValueNs,
};
use macros::{TypeFoldable, TypeVisitable};
use rustc_type_ir::inherent::{IntoKind, Ty as _};
use smallvec::{SmallVec, smallvec};
use stdx::impl_from;
use syntax::ast::{BinaryOp, UnaryOp};
use tracing::{debug, instrument, trace};

use crate::{
    Adjust, Adjustment, AutoBorrow, Span,
    infer::{
        ByRef, CaptureSourceStack, DerefPatBorrowMode, InferenceContext, PatAdjust, PatAdjustment,
        UpvarCapture, closure::analysis::BorrowKind,
    },
    method_resolution::CandidateId,
    next_solver::{ErrorGuaranteed, StoredTy, Ty, TyKind},
    upvars::UpvarsRef,
    utils::EnumerateAndAdjustIterator,
};

type Result<T = (), E = ErrorGuaranteed> = std::result::Result<T, E>;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ProjectionKind {
    /// A dereference of a pointer, reference or `Box<T>` of the given type.
    Deref,

    /// `B.F` where `B` is the base expression and `F` is
    /// the field. The field is identified by which variant
    /// it appears in along with a field index. The variant
    /// is used for enums.
    Field { field_idx: u32, variant_idx: u32 },

    /// Some index like `B[x]`, where `B` is the base
    /// expression. We don't preserve the index `x` because
    /// we won't need it.
    Index,

    /// A subslice covering a range of values like `B[x..y]`.
    Subslice,

    /// `unwrap_binder!(expr)`
    UnwrapUnsafeBinder,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PlaceBase {
    /// A temporary variable.
    Rvalue,
    /// A named `static` item.
    StaticItem,
    /// A named local variable.
    Local(BindingId),
    /// An upvar referenced by closure env.
    Upvar { closure: ExprId, var_id: BindingId },
}

#[derive(Clone, Debug, PartialEq, Eq, Hash, TypeVisitable, TypeFoldable)]
pub struct Projection {
    /// Type after the projection is applied.
    pub ty: StoredTy,

    /// Defines the kind of access made by the projection.
    #[type_visitable(ignore)]
    pub kind: ProjectionKind,
}

/// A `Place` represents how a value is located in memory. This does not
/// always correspond to a syntactic place expression. For example, when
/// processing a pattern, a `Place` can be used to refer to the sub-value
/// currently being inspected.
#[derive(Clone, Debug, PartialEq, Eq, Hash, TypeVisitable, TypeFoldable)]
pub struct Place {
    /// The type of the `PlaceBase`
    pub base_ty: StoredTy,
    /// The "outermost" place that holds this value.
    #[type_visitable(ignore)]
    pub base: PlaceBase,
    /// How this place is derived from the base place.
    pub projections: Vec<Projection>,
}

impl Place {
    /// Returns an iterator of the types that have to be dereferenced to access
    /// the `Place`.
    ///
    /// The types are in the reverse order that they are applied. So if
    /// `x: &*const u32` and the `Place` is `**x`, then the types returned are
    ///`*const u32` then `&*const u32`.
    pub fn deref_tys<'db>(&self) -> impl Iterator<Item = Ty<'db>> {
        self.projections.iter().enumerate().rev().filter_map(move |(index, proj)| {
            if ProjectionKind::Deref == proj.kind {
                Some(self.ty_before_projection(index))
            } else {
                None
            }
        })
    }

    /// Returns the type of this `Place` after all projections have been applied.
    pub fn ty<'db>(&self) -> Ty<'db> {
        self.projections.last().map_or(self.base_ty.as_ref(), |proj| proj.ty.as_ref())
    }

    /// Returns the type of this `Place` immediately before `projection_index`th projection
    /// is applied.
    pub fn ty_before_projection<'db>(&self, projection_index: usize) -> Ty<'db> {
        assert!(projection_index < self.projections.len());
        if projection_index == 0 {
            self.base_ty.as_ref()
        } else {
            self.projections[projection_index - 1].ty.as_ref()
        }
    }
}

/// A `PlaceWithOrigin` represents how a value is located in memory. This does not
/// always correspond to a syntactic place expression. For example, when
/// processing a pattern, a `Place` can be used to refer to the sub-value
/// currently being inspected.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) struct PlaceWithOrigin {
    /// `ExprId`s or `PatId`s of the expressions or patterns producing this value.
    pub origins: SmallVec<[CaptureSourceStack; 2]>,

    /// Information about the `Place`.
    pub place: Place,
}

impl PlaceWithOrigin {
    fn new_no_projections<'db>(
        origin: impl Into<ExprOrPatId>,
        base_ty: Ty<'db>,
        base: PlaceBase,
    ) -> PlaceWithOrigin {
        Self::new(
            smallvec![CaptureSourceStack::from_single(origin.into())],
            base_ty,
            base,
            Vec::new(),
        )
    }

    fn new<'db>(
        origins: SmallVec<[CaptureSourceStack; 2]>,
        base_ty: Ty<'db>,
        base: PlaceBase,
        projections: Vec<Projection>,
    ) -> PlaceWithOrigin {
        debug_assert!(origins.iter().all(|origin| origin.len() == projections.len() + 1));
        PlaceWithOrigin { origins, place: Place { base_ty: base_ty.store(), base, projections } }
    }

    fn push_projection(&mut self, projection: Projection, origin: ExprOrPatId) {
        self.place.projections.push(projection);
        for origin_stack in &mut self.origins {
            origin_stack.push(origin);
        }
    }

    pub(crate) fn span(&self) -> Span {
        match self.origins.first() {
            Some(origin) => origin.final_source().into(),
            None => Span::Dummy,
        }
    }
}

/// The `FakeReadCause` describes the type of pattern why a FakeRead statement exists.
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub enum FakeReadCause {
    /// A fake read injected into a match guard to ensure that the discriminants
    /// that are being matched on aren't modified while the match guard is being
    /// evaluated.
    ///
    /// At the beginning of each match guard, a fake borrow is
    /// inserted for each discriminant accessed in the entire `match` statement.
    ///
    /// Then, at the end of the match guard, a `FakeRead(ForMatchGuard)` is
    /// inserted to keep the fake borrows alive until that point.
    ///
    /// This should ensure that you cannot change the variant for an enum while
    /// you are in the midst of matching on it.
    ForMatchGuard,

    /// Fake read of the scrutinee of a `match` or destructuring `let`
    /// (i.e. `let` with non-trivial pattern).
    ///
    /// In `match x { ... }`, we generate a `FakeRead(ForMatchedPlace, x)`
    /// and insert it into the `otherwise_block` (which is supposed to be
    /// unreachable for irrefutable pattern-matches like `match` or `let`).
    ///
    /// This is necessary because `let x: !; match x {}` doesn't generate any
    /// actual read of x, so we need to generate a `FakeRead` to check that it
    /// is initialized.
    ///
    /// If the `FakeRead(ForMatchedPlace)` is being performed with a closure
    /// that doesn't capture the required upvars, the `FakeRead` within the
    /// closure is omitted entirely.
    ///
    /// To make sure that this is still sound, if a closure matches against
    /// a Place starting with an Upvar, we hoist the `FakeRead` to the
    /// definition point of the closure.
    ///
    /// If the `FakeRead` comes from being hoisted out of a closure like this,
    /// we record the `ExprId` of the closure. Otherwise, the `Option` will be `None`.
    //
    // We can use LocalDefId here since fake read statements are removed
    // before codegen in the `CleanupNonCodegenStatements` pass.
    ForMatchedPlace(Option<ExprId>),

    /// A fake read injected into a match guard to ensure that the places
    /// bound by the pattern are immutable for the duration of the match guard.
    ///
    /// Within a match guard, references are created for each place that the
    /// pattern creates a binding for — this is known as the `RefWithinGuard`
    /// version of the variables. To make sure that the references stay
    /// alive until the end of the match guard, and properly prevent the
    /// places in question from being modified, a `FakeRead(ForGuardBinding)`
    /// is inserted at the end of the match guard.
    ///
    /// For details on how these references are created, see the extensive
    /// documentation on `bind_matched_candidate_for_guard` in
    /// `rustc_mir_build`.
    ForGuardBinding,

    /// Officially, the semantics of
    ///
    /// `let pattern = <expr>;`
    ///
    /// is that `<expr>` is evaluated into a temporary and then this temporary is
    /// into the pattern.
    ///
    /// However, if we see the simple pattern `let var = <expr>`, we optimize this to
    /// evaluate `<expr>` directly into the variable `var`. This is mostly unobservable,
    /// but in some cases it can affect the borrow checker, as in #53695.
    ///
    /// Therefore, we insert a `FakeRead(ForLet)` immediately after each `let`
    /// with a trivial pattern.
    ///
    /// FIXME: `ExprUseVisitor` has an entirely different opinion on what `FakeRead(ForLet)`
    /// is supposed to mean. If it was accurate to what MIR lowering does,
    /// would it even make sense to hoist these out of closures like
    /// `ForMatchedPlace`?
    ForLet(Option<ExprId>),

    /// Currently, index expressions overloaded through the `Index` trait
    /// get lowered differently than index expressions with builtin semantics
    /// for arrays and slices — the latter will emit code to perform
    /// bound checks, and then return a MIR place that will only perform the
    /// indexing "for real" when it gets incorporated into an instruction.
    ///
    /// This is observable in the fact that the following compiles:
    ///
    /// ```
    /// fn f(x: &mut [&mut [u32]], i: usize) {
    ///     x[i][x[i].len() - 1] += 1;
    /// }
    /// ```
    ///
    /// However, we need to be careful to not let the user invalidate the
    /// bound check with an expression like
    ///
    /// `(*x)[1][{ x = y; 4}]`
    ///
    /// Here, the first bounds check would be invalidated when we evaluate the
    /// second index expression. To make sure that this doesn't happen, we
    /// create a fake borrow of `x` and hold it while we evaluate the second
    /// index.
    ///
    /// This borrow is kept alive by a `FakeRead(ForIndex)` at the end of its
    /// scope.
    ForIndex,
}

/// This trait defines the callbacks you can expect to receive when
/// employing the ExprUseVisitor.
pub(crate) trait Delegate<'db> {
    /// The value found at `place` is moved, depending
    /// on `mode`. Where `diag_expr_id` is the id used for diagnostics for `place`.
    ///
    /// If the value is `Copy`, [`copy`][Self::copy] is called instead, which
    /// by default falls back to [`borrow`][Self::borrow].
    ///
    /// The parameter `diag_expr_id` indicates the HIR id that ought to be used for
    /// diagnostics. Around pattern matching such as `let pat = expr`, the diagnostic
    /// id will be the id of the expression `expr` but the place itself will have
    /// the id of the binding in the pattern `pat`.
    fn consume(&mut self, place_with_id: PlaceWithOrigin, ctx: &mut InferenceContext<'_, 'db>);

    /// The value found at `place` is used, depending
    /// on `mode`. Where `diag_expr_id` is the id used for diagnostics for `place`.
    ///
    /// Use of a `Copy` type in a ByUse context is considered a use
    /// by `ImmBorrow` and `borrow` is called instead. This is because
    /// a shared borrow is the "minimum access" that would be needed
    /// to perform a copy.
    ///
    ///
    /// The parameter `diag_expr_id` indicates the HIR id that ought to be used for
    /// diagnostics. Around pattern matching such as `let pat = expr`, the diagnostic
    /// id will be the id of the expression `expr` but the place itself will have
    /// the id of the binding in the pattern `pat`.
    fn use_cloned(&mut self, place_with_id: PlaceWithOrigin, ctx: &mut InferenceContext<'_, 'db>);

    /// The value found at `place` is being borrowed with kind `bk`.
    /// `diag_expr_id` is the id used for diagnostics (see `consume` for more details).
    fn borrow(
        &mut self,
        place_with_id: PlaceWithOrigin,
        bk: BorrowKind,
        ctx: &mut InferenceContext<'_, 'db>,
    );

    /// The value found at `place` is being copied.
    /// `diag_expr_id` is the id used for diagnostics (see `consume` for more details).
    ///
    /// If an implementation is not provided, use of a `Copy` type in a ByValue context is instead
    /// considered a use by `ImmBorrow` and `borrow` is called instead. This is because a shared
    /// borrow is the "minimum access" that would be needed to perform a copy.
    fn copy(&mut self, place_with_id: PlaceWithOrigin, ctx: &mut InferenceContext<'_, 'db>) {
        // In most cases, copying data from `x` is equivalent to doing `*&x`, so by default
        // we treat a copy of `x` as a borrow of `x`.
        self.borrow(place_with_id, BorrowKind::Immutable, ctx)
    }

    /// The path at `assignee_place` is being assigned to.
    /// `diag_expr_id` is the id used for diagnostics (see `consume` for more details).
    fn mutate(&mut self, assignee_place: PlaceWithOrigin, ctx: &mut InferenceContext<'_, 'db>);

    /// The path at `binding_place` is a binding that is being initialized.
    ///
    /// This covers cases such as `let x = 42;`
    fn bind(&mut self, binding_place: PlaceWithOrigin, ctx: &mut InferenceContext<'_, 'db>) {
        // Bindings can normally be treated as a regular assignment, so by default we
        // forward this to the mutate callback.
        self.mutate(binding_place, ctx)
    }

    /// The `place` should be a fake read because of specified `cause`.
    fn fake_read(
        &mut self,
        place_with_id: PlaceWithOrigin,
        cause: FakeReadCause,
        ctx: &mut InferenceContext<'_, 'db>,
    );
}

impl<'db, D: Delegate<'db>> Delegate<'db> for &mut D {
    fn consume(&mut self, place_with_id: PlaceWithOrigin, ctx: &mut InferenceContext<'_, 'db>) {
        (**self).consume(place_with_id, ctx)
    }

    fn use_cloned(&mut self, place_with_id: PlaceWithOrigin, ctx: &mut InferenceContext<'_, 'db>) {
        (**self).use_cloned(place_with_id, ctx)
    }

    fn borrow(
        &mut self,
        place_with_id: PlaceWithOrigin,
        bk: BorrowKind,
        ctx: &mut InferenceContext<'_, 'db>,
    ) {
        (**self).borrow(place_with_id, bk, ctx)
    }

    fn copy(&mut self, place_with_id: PlaceWithOrigin, ctx: &mut InferenceContext<'_, 'db>) {
        (**self).copy(place_with_id, ctx)
    }

    fn mutate(&mut self, assignee_place: PlaceWithOrigin, ctx: &mut InferenceContext<'_, 'db>) {
        (**self).mutate(assignee_place, ctx)
    }

    fn bind(&mut self, binding_place: PlaceWithOrigin, ctx: &mut InferenceContext<'_, 'db>) {
        (**self).bind(binding_place, ctx)
    }

    fn fake_read(
        &mut self,
        place_with_id: PlaceWithOrigin,
        cause: FakeReadCause,
        ctx: &mut InferenceContext<'_, 'db>,
    ) {
        (**self).fake_read(place_with_id, cause, ctx)
    }
}

/// A visitor that reports how each expression is being used.
///
/// See [module-level docs][self] and [`Delegate`] for details.
pub(crate) struct ExprUseVisitor<'a, 'b, 'db, D: Delegate<'db>> {
    cx: &'a mut InferenceContext<'b, 'db>,
    delegate: D,
    closure_expr: ExprId,
    upvars: UpvarsRef<'db>,
}

impl<'a, 'b, 'db, D: Delegate<'db>> ExprUseVisitor<'a, 'b, 'db, D> {
    /// Creates the ExprUseVisitor, configuring it with the various options provided:
    ///
    /// - `delegate` -- who receives the callbacks
    /// - `param_env` --- parameter environment for trait lookups (esp. pertaining to `Copy`)
    /// - `typeck_results` --- typeck results for the code being analyzed
    pub(crate) fn new(
        cx: &'a mut InferenceContext<'b, 'db>,
        closure_expr: ExprId,
        upvars: UpvarsRef<'db>,
        delegate: D,
    ) -> Self {
        ExprUseVisitor { delegate, closure_expr, upvars, cx }
    }

    pub(crate) fn consume_closure_body(&mut self, params: &[PatId], body: ExprId) -> Result {
        for &param in params {
            let param_ty = self.pat_ty_adjusted(param)?;
            debug!("consume_body: param_ty = {:?}", param_ty);

            let param_place = self.cat_rvalue(param.into(), param_ty);

            self.fake_read_scrutinee(param_place.clone(), false);
            self.walk_pat(param_place, param, false)?;
        }

        self.consume_expr(body)?;

        Ok(())
    }

    #[instrument(skip(self), level = "debug")]
    fn consume_or_copy(&mut self, place_with_id: PlaceWithOrigin) {
        if self.cx.table.type_is_copy_modulo_regions(place_with_id.place.ty()) {
            self.delegate.copy(place_with_id, self.cx);
        } else {
            self.delegate.consume(place_with_id, self.cx);
        }
    }

    #[instrument(skip(self), level = "debug")]
    pub(crate) fn consume_clone_or_copy(&mut self, place_with_id: PlaceWithOrigin) {
        // `x.use` will do one of the following
        // * if it implements `Copy`, it will be a copy
        // * if it implements `UseCloned`, it will be a call to `clone`
        // * otherwise, it is a move
        //
        // we do a conservative approximation of this, treating it as a move unless we know that it implements copy or `UseCloned`
        if self.cx.table.type_is_copy_modulo_regions(place_with_id.place.ty()) {
            self.delegate.copy(place_with_id, self.cx);
        } else if self.cx.table.type_is_use_cloned_modulo_regions(place_with_id.place.ty()) {
            self.delegate.use_cloned(place_with_id, self.cx);
        } else {
            self.delegate.consume(place_with_id, self.cx);
        }
    }

    fn consume_exprs(&mut self, exprs: &[ExprId]) -> Result {
        for &expr in exprs {
            self.consume_expr(expr)?;
        }
        Ok(())
    }

    #[instrument(skip(self), level = "debug")]
    pub(crate) fn consume_expr(&mut self, expr: ExprId) -> Result {
        let place_with_id = self.cat_expr(expr)?;
        self.consume_or_copy(place_with_id);
        self.walk_expr(expr)?;
        Ok(())
    }

    fn mutate_expr(&mut self, expr: ExprId) -> Result {
        let place_with_id = self.cat_expr(expr)?;
        self.delegate.mutate(place_with_id, self.cx);
        self.walk_expr(expr)?;
        Ok(())
    }

    #[instrument(skip(self), level = "debug")]
    fn borrow_expr(&mut self, expr: ExprId, bk: BorrowKind) -> Result {
        let place_with_id = self.cat_expr(expr)?;
        self.delegate.borrow(place_with_id, bk, self.cx);
        self.walk_expr(expr)?;
        Ok(())
    }

    #[instrument(skip(self), level = "debug")]
    pub(crate) fn walk_expr(&mut self, expr: ExprId) -> Result {
        self.walk_adjustment(expr)?;

        match self.cx.store[expr] {
            Expr::Path(_) => {}

            Expr::UnaryOp { op: UnaryOp::Deref, expr: base } => {
                // *base
                self.walk_expr(base)?;
            }

            Expr::Field { expr: base, .. } => {
                // base.f
                self.walk_expr(base)?;
            }

            Expr::Index { base: lhs, index: rhs } => {
                // lhs[rhs]
                self.walk_expr(lhs)?;
                self.consume_expr(rhs)?;
            }

            Expr::Call { callee, ref args } => {
                // callee(args)
                self.consume_expr(callee)?;
                self.consume_exprs(args)?;
            }

            Expr::MethodCall { receiver, ref args, .. } => {
                // callee.m(args)
                self.consume_expr(receiver)?;
                self.consume_exprs(args)?;
            }

            Expr::RecordLit { ref fields, spread, .. } => {
                self.walk_struct_expr(fields, spread)?;
            }

            Expr::Tuple { ref exprs } => {
                self.consume_exprs(exprs)?;
            }

            Expr::If {
                condition: cond_expr,
                then_branch: then_expr,
                else_branch: opt_else_expr,
            } => {
                self.consume_expr(cond_expr)?;
                self.consume_expr(then_expr)?;
                if let Some(else_expr) = opt_else_expr {
                    self.consume_expr(else_expr)?;
                }
            }

            Expr::Let { pat, expr: init } => {
                self.walk_local(init, pat, None, |this| {
                    this.borrow_expr(init, BorrowKind::Immutable)
                })?;
            }

            Expr::Match { expr: discr, ref arms } => {
                let discr_place = self.cat_expr(discr)?;
                self.fake_read_scrutinee(discr_place.clone(), true);
                self.walk_expr(discr)?;

                for arm in arms {
                    self.walk_arm(discr_place.clone(), arm)?;
                }
            }

            Expr::Array(Array::ElementList { elements: ref exprs }) => {
                self.consume_exprs(exprs)?;
            }

            Expr::Ref { expr: base, mutability: m, .. } => {
                // &base
                // make sure that the thing we are pointing out stays valid
                // for the lifetime `scope_r` of the resulting ptr:
                let bk = BorrowKind::from_hir_mutbl(m);
                self.borrow_expr(base, bk)?;
            }

            Expr::InlineAsm(ref asm) => {
                for (_, op) in &asm.operands {
                    match *op {
                        AsmOperand::In { expr, .. } => {
                            self.consume_expr(expr)?;
                        }
                        AsmOperand::Out { expr: Some(expr), .. }
                        | AsmOperand::InOut { expr, .. } => {
                            self.mutate_expr(expr)?;
                        }
                        AsmOperand::SplitInOut { in_expr, out_expr, .. } => {
                            self.consume_expr(in_expr)?;
                            if let Some(out_expr) = out_expr {
                                self.mutate_expr(out_expr)?;
                            }
                        }
                        AsmOperand::Out { expr: None, .. }
                        | AsmOperand::Const { .. }
                        | AsmOperand::Sym { .. } => {}
                        AsmOperand::Label(block) => {
                            self.walk_expr(block)?;
                        }
                    }
                }
            }

            Expr::Continue { .. }
            | Expr::Literal(..)
            | Expr::Const(..)
            | Expr::OffsetOf(..)
            | Expr::Missing
            | Expr::Underscore => {}

            Expr::Loop { body: blk, .. } => {
                self.walk_expr(blk)?;
            }

            Expr::UnaryOp { expr: lhs, .. } => {
                self.consume_expr(lhs)?;
            }

            Expr::BinaryOp {
                lhs,
                rhs,
                op: Some(BinaryOp::ArithOp(..) | BinaryOp::CmpOp(..) | BinaryOp::LogicOp(..)),
            } => {
                self.consume_expr(lhs)?;
                self.consume_expr(rhs)?;
            }

            Expr::Block { ref statements, tail, .. }
            | Expr::Unsafe { ref statements, tail, .. } => {
                for stmt in statements {
                    self.walk_stmt(stmt)?;
                }

                if let Some(tail_expr) = tail {
                    self.consume_expr(tail_expr)?;
                }
            }

            Expr::Break { expr: opt_expr, .. } | Expr::Return { expr: opt_expr } => {
                if let Some(expr) = opt_expr {
                    self.consume_expr(expr)?;
                }
            }

            Expr::Become { expr } | Expr::Await { expr } | Expr::Box { expr } => {
                self.consume_expr(expr)?;
            }

            Expr::Assignment { target, value } => {
                self.walk_expr(value)?;
                let expr_place = self.cat_expr(value)?;
                let update_guard =
                    self.cx.resolver.update_to_inner_scope(self.cx.db, self.cx.store_owner, expr);
                self.walk_pat(expr_place, target, false)?;
                self.cx.resolver.reset_to_guard(update_guard);
            }

            Expr::Cast { expr: base, .. } => {
                self.consume_expr(base)?;
            }

            Expr::BinaryOp { lhs, rhs, op: None | Some(BinaryOp::Assignment { .. }) } => {
                self.consume_expr(lhs)?;
                self.consume_expr(rhs)?;
            }

            Expr::Array(Array::Repeat { initializer: base, .. }) => {
                self.consume_expr(base)?;
            }

            Expr::Closure { .. } => {
                self.walk_captures(expr);
            }

            Expr::Yield { expr: value } | Expr::Yeet { expr: value } => {
                if let Some(value) = value {
                    self.consume_expr(value)?;
                }
            }

            Expr::Range { lhs, rhs, .. } => {
                if let Some(lhs) = lhs {
                    self.consume_expr(lhs)?;
                }
                if let Some(rhs) = rhs {
                    self.consume_expr(rhs)?;
                }
            }
        }
        Ok(())
    }

    fn walk_stmt(&mut self, stmt: &Statement) -> Result {
        match *stmt {
            Statement::Let { pat, initializer: Some(expr), else_branch: els, .. } => {
                self.walk_local(expr, pat, els, |_| Ok(()))?;
            }

            Statement::Let { .. } => {}

            Statement::Item(_) => {
                // We don't visit nested items in this visitor,
                // only the fn body we were given.
            }

            Statement::Expr { expr, .. } => {
                self.consume_expr(expr)?;
            }
        }
        Ok(())
    }

    #[instrument(skip(self), level = "debug")]
    fn fake_read_scrutinee(&mut self, discr_place: PlaceWithOrigin, refutable: bool) {
        let closure_def_id = match discr_place.place.base {
            PlaceBase::Upvar { closure, var_id: _ } => Some(closure),
            _ => None,
        };

        let cause = if refutable {
            FakeReadCause::ForMatchedPlace(closure_def_id)
        } else {
            FakeReadCause::ForLet(closure_def_id)
        };

        self.delegate.fake_read(discr_place, cause, self.cx);
    }

    fn walk_local<F>(&mut self, expr: ExprId, pat: PatId, els: Option<ExprId>, mut f: F) -> Result
    where
        F: FnMut(&mut Self) -> Result,
    {
        self.walk_expr(expr)?;
        let expr_place = self.cat_expr(expr)?;
        f(self)?;
        self.fake_read_scrutinee(expr_place.clone(), els.is_some());
        self.walk_pat(expr_place, pat, false)?;
        if let Some(els) = els {
            self.walk_expr(els)?;
        }
        Ok(())
    }

    fn walk_struct_expr(&mut self, fields: &[RecordLitField], spread: RecordSpread) -> Result {
        // Consume the expressions supplying values for each field.
        for field in fields {
            self.consume_expr(field.expr)?;
        }

        let RecordSpread::Expr(with_expr) = spread else { return Ok(()) };

        let with_place = self.cat_expr(with_expr)?;

        // Select just those fields of the `with`
        // expression that will actually be used
        match self.cx.structurally_resolve_type(with_expr.into(), with_place.place.ty()).kind() {
            TyKind::Adt(adt, args) if adt.is_struct() => {
                let AdtId::StructId(adt) = adt.def_id() else { unreachable!() };
                let adt_fields = VariantId::from(adt).fields(self.cx.db).fields();
                let adt_field_types = self.cx.db.field_types(adt.into());
                // Consume those fields of the with expression that are needed.
                for (f_index, with_field) in adt_fields.iter() {
                    let is_mentioned = fields.iter().any(|f| f.name == with_field.name);
                    if !is_mentioned {
                        let field_place = self.cat_projection(
                            with_expr.into(),
                            with_place.clone(),
                            adt_field_types[f_index]
                                .get()
                                .instantiate(self.cx.interner(), args)
                                .skip_norm_wip(),
                            ProjectionKind::Field {
                                field_idx: f_index.into_raw().into_u32(),
                                variant_idx: 0,
                            },
                        );
                        self.consume_or_copy(field_place);
                    }
                }
            }
            _ => {}
        }

        // walk the with expression so that complex expressions
        // are properly handled.
        self.walk_expr(with_expr)?;

        Ok(())
    }

    fn expr_adjustments(&self, expr: ExprId) -> SmallVec<[Adjustment; 5]> {
        // Due to borrowck problems, we cannot borrow the adjustments, unfortunately.
        self.cx.result.expr_adjustment(expr).unwrap_or_default().into()
    }

    fn pat_adjustments(&self, pat: PatId) -> SmallVec<[PatAdjustment; 5]> {
        // Due to borrowck problems, we cannot borrow the adjustments, unfortunately.
        self.cx.result.pat_adjustment(pat).unwrap_or_default().into()
    }

    /// Invoke the appropriate delegate calls for anything that gets
    /// consumed or borrowed as part of the automatic adjustment
    /// process.
    fn walk_adjustment(&mut self, expr: ExprId) -> Result {
        let adjustments = self.expr_adjustments(expr);
        let mut place_with_id = self.cat_expr_unadjusted(expr)?;
        for adjustment in &adjustments {
            debug!("walk_adjustment expr={:?} adj={:?}", expr, adjustment);
            match adjustment.kind {
                Adjust::NeverToAny | Adjust::Pointer(_) => {
                    // Creating a closure/fn-pointer or unsizing consumes
                    // the input and stores it into the resulting rvalue.
                    self.consume_or_copy(place_with_id.clone());
                }

                Adjust::Deref(None) => {}

                // Autoderefs for overloaded Deref calls in fact reference
                // their receiver. That is, if we have `(*x)` where `x`
                // is of type `Rc<T>`, then this in fact is equivalent to
                // `x.deref()`. Since `deref()` is declared with `&self`,
                // this is an autoref of `x`.
                Adjust::Deref(Some(ref deref)) => {
                    let bk = BorrowKind::from_mutbl(deref.0);
                    self.delegate.borrow(place_with_id.clone(), bk, self.cx);
                }

                Adjust::Borrow(ref autoref) => {
                    self.walk_autoref(expr, place_with_id.clone(), autoref);
                }
            }
            place_with_id = self.cat_expr_adjusted(expr, place_with_id, adjustment)?;
        }
        Ok(())
    }

    /// Walks the autoref `autoref` applied to the autoderef'd
    /// `expr`. `base_place` is `expr` represented as a place,
    /// after all relevant autoderefs have occurred.
    fn walk_autoref(&mut self, expr: ExprId, base_place: PlaceWithOrigin, autoref: &AutoBorrow) {
        debug!("walk_autoref(expr={:?} base_place={:?} autoref={:?})", expr, base_place, autoref);

        match *autoref {
            AutoBorrow::Ref(m) => {
                self.delegate.borrow(base_place, BorrowKind::from_mutbl(m.into()), self.cx);
            }

            AutoBorrow::RawPtr(m) => {
                debug!("walk_autoref: expr={:?} base_place={:?}", expr, base_place);

                self.delegate.borrow(base_place, BorrowKind::from_mutbl(m), self.cx);
            }
        }
    }

    fn walk_arm(&mut self, discr_place: PlaceWithOrigin, arm: &MatchArm) -> Result {
        self.walk_pat(discr_place, arm.pat, arm.guard.is_some())?;

        if let Some(e) = arm.guard {
            self.consume_expr(e)?;
        }

        self.consume_expr(arm.expr)
    }

    /// The core driver for walking a pattern
    ///
    /// This should mirror how pattern-matching gets lowered to MIR, as
    /// otherwise lowering will ICE when trying to resolve the upvars.
    ///
    /// However, it is okay to approximate it here by doing *more* accesses than
    /// the actual MIR builder will, which is useful when some checks are too
    /// cumbersome to perform here. For example, if after typeck it becomes
    /// clear that only one variant of an enum is inhabited, and therefore a
    /// read of the discriminant is not necessary, `walk_pat` will have
    /// over-approximated the necessary upvar capture granularity.
    ///
    /// Do note that discrepancies like these do still create obscure corners
    /// in the semantics of the language, and should be avoided if possible.
    #[instrument(skip(self), level = "debug")]
    fn walk_pat(&mut self, discr_place: PlaceWithOrigin, pat: PatId, has_guard: bool) -> Result {
        self.cat_pattern(discr_place.clone(), pat, &mut |this, place, pat| {
            let walk_deref_pat = |this: &mut Self, subpattern: PatId, place: PlaceWithOrigin| {
                // A deref pattern is a bit special: the binding mode of its inner bindings
                // determines whether to borrow *at the level of the deref pattern* rather than
                // borrowing the bound place (since that inner place is inside the temporary that
                // stores the result of calling `deref()`/`deref_mut()` so can't be captured).
                // Deref patterns on boxes don't borrow, so we ignore them here.
                // HACK: this could be a fake pattern corresponding to a deref inserted by match
                // ergonomics, in which case `pat.hir_id` will be the id of the subpattern.
                if let DerefPatBorrowMode::Borrow(mutability) =
                    this.cx.deref_pat_borrow_mode(place.place.ty(), subpattern)
                {
                    let bk = BorrowKind::from_mutbl(mutability);
                    this.delegate.borrow(place, bk, this.cx);
                }
            };

            let pat = match pat {
                CatPatternPat::PatId(pat) => pat,
                CatPatternPat::DerefPat { inner } => {
                    debug!("walk_pat: Deref {{ inner: {:?} }}", inner);
                    walk_deref_pat(this, inner, place);
                    return Ok(());
                }
            };

            debug!("walk_pat: pat.kind={:?}", this.cx.store[pat]);
            let read_discriminant = {
                let place = place.clone();
                |this: &mut Self| {
                    this.delegate.borrow(place, BorrowKind::Immutable, this.cx);
                }
            };

            match this.cx.store[pat] {
                Pat::Bind { id, .. } => {
                    debug!("walk_pat: binding place={:?} pat={:?}", place, pat);
                    let bm = this.cx.result.binding_modes[pat];
                    debug!("walk_pat: pat.hir_id={:?} bm={:?}", pat, bm);

                    // pat_ty: the type of the binding being produced.
                    let pat_ty = this.node_ty(pat.into())?;
                    debug!("walk_pat: pat_ty={:?}", pat_ty);

                    if let Ok(binding_place) = this.cat_local(pat.into(), pat_ty, id) {
                        this.delegate.bind(binding_place, this.cx);
                    }

                    // Subtle: MIR desugaring introduces immutable borrows for each pattern
                    // binding when lowering pattern guards to ensure that the guard does not
                    // modify the scrutinee.
                    if has_guard {
                        read_discriminant(this);
                    }

                    // It is also a borrow or copy/move of the value being matched.
                    // In a cases of pattern like `let pat = upvar`, don't use the span
                    // of the pattern, as this just looks confusing, instead use the span
                    // of the discriminant.
                    match this.cx.result.binding_mode(pat).ok_or(ErrorGuaranteed)?.0 {
                        ByRef::Yes(m) => {
                            let bk = BorrowKind::from_mutbl(m);
                            this.delegate.borrow(place, bk, this.cx);
                        }
                        ByRef::No => {
                            debug!("walk_pat binding consuming pat");
                            this.consume_or_copy(place);
                        }
                    }
                }
                Pat::Deref { inner: subpattern } => walk_deref_pat(this, subpattern, place),
                Pat::Path(ref path) => {
                    // A `Path` pattern is just a name like `Foo`. This is either a
                    // named constant or else it refers to an ADT variant

                    let is_assoc_const = this
                        .cx
                        .result
                        .assoc_resolutions_for_pat(pat)
                        .is_some_and(|it| matches!(it.0, CandidateId::ConstId(_)));
                    let resolution = this.cx.resolver.resolve_path_in_value_ns_fully(
                        this.cx.db,
                        path,
                        this.cx.store.pat_path_hygiene(pat),
                    );
                    let is_normal_const = matches!(resolution, Some(ValueNs::ConstId(_)));
                    if is_assoc_const || is_normal_const {
                        // Named constants have to be equated with the value
                        // being matched, so that's a read of the value being matched.
                        //
                        // FIXME: Does the MIR code skip this read when matching on a ZST?
                        // If so, we can also skip it here.
                        read_discriminant(this);
                    } else if this.is_multivariant_adt(pat.into(), place.place.ty()) {
                        // Otherwise, this is a struct/enum variant, and so it's
                        // only a read if we need to read the discriminant.
                        read_discriminant(this);
                    }
                }
                Pat::Lit(_) | Pat::ConstBlock(_) | Pat::Range { .. } => {
                    // When matching against a literal or range, we need to
                    // borrow the place to compare it against the pattern.
                    //
                    // Note that we do this read even if the range matches all
                    // possible values, such as 0..=u8::MAX. This is because
                    // we don't want to depend on consteval here.
                    //
                    // FIXME: What if the type being matched only has one
                    // possible value?
                    read_discriminant(this);
                }
                Pat::Record { .. } | Pat::TupleStruct { .. } => {
                    if this.is_multivariant_adt(pat.into(), place.place.ty()) {
                        read_discriminant(this);
                    }
                }
                Pat::Slice { prefix: ref lhs, slice: wild, suffix: ref rhs } => {
                    // We don't need to test the length if the pattern is `[..]`
                    if matches!((&**lhs, wild, &**rhs), (&[], Some(_), &[]))
                        // Arrays have a statically known size, so
                        // there is no need to read their length
                        || place.place.ty().strip_references().is_array()
                    {
                        // No read necessary
                    } else {
                        read_discriminant(this);
                    }
                }
                Pat::Expr(expr) => {
                    // Destructuring assignment.
                    this.mutate_expr(expr)?;
                }
                Pat::Or(_)
                | Pat::Box { .. }
                | Pat::Ref { .. }
                | Pat::Tuple { .. }
                | Pat::Wild
                | Pat::Missing
                | Pat::Rest => {
                    // If the PatKind is Or, Box, Ref, Guard, or Tuple, the relevant accesses
                    // are made later as these patterns contains subpatterns.
                    // If the PatKind is Missing, Wild or Err, any relevant accesses are made when processing
                    // the other patterns that are part of the match
                }
            }

            Ok(())
        })
    }

    /// Handle the case where the current body contains a closure.
    ///
    /// When the current body being handled is a closure, then we must make sure that
    /// - The parent closure only captures Places from the nested closure that are not local to it.
    ///
    /// In the following example the closures `c` only captures `p.x` even though `incr`
    /// is a capture of the nested closure
    ///
    /// ```
    /// struct P { x: i32 }
    /// let mut p = P { x: 4 };
    /// let c = || {
    ///    let incr = 10;
    ///    let nested = || p.x += incr;
    /// };
    /// ```
    ///
    /// - When reporting the Place back to the Delegate, ensure that the UpvarId uses the enclosing
    /// closure as the DefId.
    #[instrument(skip(self), level = "debug")]
    fn walk_captures(&mut self, closure_expr: ExprId) {
        fn upvar_is_local_variable(upvars: UpvarsRef<'_>, var_id: BindingId) -> bool {
            upvars.contains(var_id)
        }

        // If we have a nested closure, we want to include the fake reads present in the nested
        // closure.
        // `remove()` then re-insert and not `get()` due to borrowck errors.
        if let Some(closure_data) = self.cx.result.closures_data.remove(&closure_expr) {
            for (fake_read, cause, origins) in closure_data.fake_reads.iter() {
                match fake_read.base {
                    PlaceBase::Upvar { var_id, closure: _ } => {
                        if upvar_is_local_variable(self.upvars, var_id) {
                            // The nested closure might be fake reading the current (enclosing) closure's local variables.
                            // The only places we want to fake read before creating the parent closure are the ones that
                            // are not local to it/ defined by it.
                            //
                            // ```rust,ignore(cannot-test-this-because-pseudo-code)
                            // let v1 = (0, 1);
                            // let c = || { // fake reads: v1
                            //    let v2 = (0, 1);
                            //    let e = || { // fake reads: v1, v2
                            //       let (_, t1) = v1;
                            //       let (_, t2) = v2;
                            //    }
                            // }
                            // ```
                            // This check is performed when visiting the body of the outermost closure (`c`) and ensures
                            // that we don't add a fake read of v2 in c.
                            continue;
                        }
                    }
                    _ => {
                        panic!(
                            "Do not know how to get ExprId out of Rvalue and StaticItem {:?}",
                            fake_read.base
                        );
                    }
                };
                self.delegate.fake_read(
                    PlaceWithOrigin { place: fake_read.clone(), origins: origins.clone() },
                    *cause,
                    self.cx,
                );
            }

            for (var_id, min_list) in closure_data.min_captures.iter() {
                if !self.upvars.contains(*var_id) {
                    // The nested closure might be capturing the current (enclosing) closure's local variables.
                    // We check if the root variable is ever mentioned within the enclosing closure, if not
                    // then for the current body (if it's a closure) these aren't captures, we will ignore them.
                    continue;
                }
                for captured_place in min_list {
                    let place = &captured_place.place;
                    let capture_info = &captured_place.info;

                    // Mark the place to be captured by the enclosing closure
                    let place_base =
                        PlaceBase::Upvar { var_id: *var_id, closure: self.closure_expr };
                    let place_with_id = PlaceWithOrigin::new(
                        capture_info.sources.clone(),
                        place.base_ty.as_ref(),
                        place_base,
                        place.projections.clone(),
                    );

                    match capture_info.capture_kind {
                        UpvarCapture::ByValue => {
                            self.consume_or_copy(place_with_id);
                        }
                        UpvarCapture::ByUse => {
                            self.consume_clone_or_copy(place_with_id);
                        }
                        UpvarCapture::ByRef(upvar_borrow) => {
                            self.delegate.borrow(place_with_id, upvar_borrow, self.cx);
                        }
                    }
                }
            }

            self.cx.result.closures_data.insert(closure_expr, closure_data);
        }
    }

    fn error_reported_in_ty(&self, ty: Ty<'db>) -> Result {
        if ty.is_ty_error() { Err(ErrorGuaranteed) } else { Ok(()) }
    }
}

#[derive(Debug, Clone, Copy)]
enum CatPatternPat {
    PatId(PatId),
    DerefPat { inner: PatId },
}
impl_from!(PatId for CatPatternPat);

/// The job of the methods whose name starts with `cat_` is to analyze
/// expressions and construct the corresponding [`Place`]s. The `cat`
/// stands for "categorize", this is a leftover from long ago when
/// places were called "categorizations".
///
/// Note that a [`Place`] differs somewhat from the expression itself. For
/// example, auto-derefs are explicit. Also, an index `a[b]` is decomposed into
/// two operations: a dereference to reach the array data and then an index to
/// jump forward to the relevant item.
impl<'db, D: Delegate<'db>> ExprUseVisitor<'_, '_, 'db, D> {
    fn expect_and_resolve_type(&mut self, ty: Option<Ty<'db>>) -> Result<Ty<'db>> {
        match ty {
            Some(ty) => {
                let ty = self.cx.infcx().resolve_vars_if_possible(ty);
                self.error_reported_in_ty(ty)?;
                Ok(ty)
            }
            None => Err(ErrorGuaranteed),
        }
    }

    fn node_ty(&mut self, id: ExprOrPatId) -> Result<Ty<'db>> {
        self.expect_and_resolve_type(self.cx.result.type_of_expr_or_pat(id))
    }

    fn expr_ty(&mut self, expr: ExprId) -> Result<Ty<'db>> {
        self.node_ty(expr.into())
    }

    fn expr_ty_adjusted(&mut self, expr: ExprId) -> Result<Ty<'db>> {
        self.expect_and_resolve_type(self.cx.result.type_of_expr_with_adjust(expr))
    }

    /// Returns the type of value that this pattern matches against.
    /// Some non-obvious cases:
    ///
    /// - a `ref x` binding matches against a value of type `T` and gives
    ///   `x` the type `&T`; we return `T`.
    /// - a pattern with implicit derefs (thanks to default binding
    ///   modes #42640) may look like `Some(x)` but in fact have
    ///   implicit deref patterns attached (e.g., it is really
    ///   `&Some(x)`). In that case, we return the "outermost" type
    ///   (e.g., `&Option<T>`).
    fn pat_ty_adjusted(&mut self, pat: PatId) -> Result<Ty<'db>> {
        // Check for implicit `&` types wrapping the pattern; note
        // that these are never attached to binding patterns, so
        // actually this is somewhat "disjoint" from the code below
        // that aims to account for `ref x`.
        if let Some(vec) = self.cx.result.pat_adjustment(pat) {
            if let Some(first_adjust) = vec.first() {
                debug!("pat_ty(pat={:?}) found adjustment `{:?}`", pat, first_adjust);
                return Ok(first_adjust.source.as_ref());
            }
        } else if let Pat::Ref { pat: subpat, .. } = self.cx.store[pat]
            && self.cx.result.is_skipped_ref_pat(pat)
        {
            return self.pat_ty_adjusted(subpat);
        }

        self.pat_ty_unadjusted(pat)
    }

    /// Like [`Self::pat_ty_adjusted`], but ignores implicit `&` patterns.
    fn pat_ty_unadjusted(&mut self, pat: PatId) -> Result<Ty<'db>> {
        let base_ty = self.node_ty(pat.into())?;
        trace!(?base_ty);

        // This code detects whether we are looking at a `ref x`,
        // and if so, figures out what the type *being borrowed* is.
        match self.cx.store[pat] {
            Pat::Bind { .. } => {
                let bm = self.cx.result.binding_mode(pat).ok_or(ErrorGuaranteed)?;

                if let ByRef::Yes(_) = bm.0 {
                    // a bind-by-ref means that the base_ty will be the type of the ident itself,
                    // but what we want here is the type of the underlying value being borrowed.
                    // So peel off one-level, turning the &T into T.
                    match self
                        .cx
                        .structurally_resolve_type(pat.into(), base_ty)
                        .builtin_deref(false)
                    {
                        Some(ty) => Ok(ty),
                        None => {
                            debug!("By-ref binding of non-derefable type: {base_ty:?}");
                            Err(ErrorGuaranteed)
                        }
                    }
                } else {
                    Ok(base_ty)
                }
            }
            _ => Ok(base_ty),
        }
    }

    fn cat_expr(&mut self, expr: ExprId) -> Result<PlaceWithOrigin> {
        self.cat_expr_(expr, &self.expr_adjustments(expr))
    }

    /// This recursion helper avoids going through *too many*
    /// adjustments, since *only* non-overloaded deref recurses.
    fn cat_expr_(&mut self, expr: ExprId, adjustments: &[Adjustment]) -> Result<PlaceWithOrigin> {
        match adjustments.split_last() {
            None => self.cat_expr_unadjusted(expr),
            Some((adjustment, previous)) => {
                self.cat_expr_adjusted_with(expr, |this| this.cat_expr_(expr, previous), adjustment)
            }
        }
    }

    fn cat_expr_adjusted(
        &mut self,
        expr: ExprId,
        previous: PlaceWithOrigin,
        adjustment: &Adjustment,
    ) -> Result<PlaceWithOrigin> {
        self.cat_expr_adjusted_with(expr, |_this| Ok(previous), adjustment)
    }

    fn cat_expr_adjusted_with<F>(
        &mut self,
        expr: ExprId,
        previous: F,
        adjustment: &Adjustment,
    ) -> Result<PlaceWithOrigin>
    where
        F: FnOnce(&mut Self) -> Result<PlaceWithOrigin>,
    {
        let target = self.cx.infcx().resolve_vars_if_possible(adjustment.target.as_ref());
        match adjustment.kind {
            Adjust::Deref(overloaded) => {
                // Equivalent to *expr or something similar.
                let base = if let Some(deref) = overloaded {
                    let ref_ty = Ty::new_ref(
                        self.cx.interner(),
                        self.cx.types.regions.erased,
                        target,
                        deref.0,
                    );
                    self.cat_rvalue(expr.into(), ref_ty)
                } else {
                    previous(self)?
                };
                self.cat_deref(expr.into(), base)
            }

            Adjust::NeverToAny | Adjust::Pointer(_) | Adjust::Borrow(_) => {
                // Result is an rvalue.
                Ok(self.cat_rvalue(expr.into(), target))
            }
        }
    }

    fn cat_expr_unadjusted(&mut self, expr: ExprId) -> Result<PlaceWithOrigin> {
        let expr_ty = self.expr_ty(expr)?;
        match self.cx.store[expr] {
            Expr::UnaryOp { expr: e_base, op: UnaryOp::Deref } => {
                if self.cx.result.method_resolutions.contains_key(&expr) {
                    self.cat_overloaded_place(expr, e_base)
                } else {
                    let base = self.cat_expr(e_base)?;
                    self.cat_deref(expr.into(), base)
                }
            }

            Expr::Field { expr: base, .. } => {
                let base = self.cat_expr(base)?;
                debug!(?base);

                let field_idx = self
                    .cx
                    .result
                    .field_resolutions
                    .get(&expr)
                    .map(|field| match *field {
                        Either::Left(field) => field.local_id.into_raw().into_u32(),
                        Either::Right(tuple_field) => tuple_field.index,
                    })
                    .ok_or(ErrorGuaranteed)?;

                Ok(self.cat_projection(
                    expr.into(),
                    base,
                    expr_ty,
                    ProjectionKind::Field { field_idx, variant_idx: 0 },
                ))
            }

            Expr::Index { base, index: _ } => {
                // rustc checks if this is an overloaded index, but the check is buggy and treats any indexing
                // as overloaded, see https://rust-lang.zulipchat.com/#narrow/channel/144729-t-types/topic/.E2.9C.94.20Is.20builtin.20indexing.20any.20special.20in.20typeck.3F/near/565881390.
                // So that's what we do here.
                self.cat_overloaded_place(expr, base)
            }

            Expr::Path(ref path) => {
                let resolver_guard =
                    self.cx.resolver.update_to_inner_scope(self.cx.db, self.cx.store_owner, expr);
                let resolution = self.cx.resolver.resolve_path_in_value_ns_fully(
                    self.cx.db,
                    path,
                    self.cx.store.expr_path_hygiene(expr),
                );
                self.cx.resolver.reset_to_guard(resolver_guard);
                match (resolution, self.cx.result.assoc_resolutions_for_expr(expr)) {
                    (_, Some((CandidateId::FunctionId(_) | CandidateId::ConstId(_), _)))
                    | (
                        Some(
                            ValueNs::ConstId(_)
                            | ValueNs::GenericParam(_)
                            | ValueNs::FunctionId(_)
                            | ValueNs::ImplSelf(_)
                            | ValueNs::EnumVariantId(_)
                            | ValueNs::StructId(_),
                        ),
                        None,
                    ) => Ok(self.cat_rvalue(expr.into(), expr_ty)),
                    (Some(ValueNs::StaticId(_)), None) => Ok(PlaceWithOrigin::new_no_projections(
                        expr,
                        expr_ty,
                        PlaceBase::StaticItem,
                    )),
                    (Some(ValueNs::LocalBinding(var_id)), None) => {
                        self.cat_local(expr.into(), expr_ty, var_id)
                    }
                    (None, None) => Err(ErrorGuaranteed),
                }
            }

            _ => Ok(self.cat_rvalue(expr.into(), expr_ty)),
        }
    }

    fn cat_local(
        &mut self,
        id: ExprOrPatId,
        expr_ty: Ty<'db>,
        var_id: BindingId,
    ) -> Result<PlaceWithOrigin> {
        if self.upvars.contains(var_id) {
            self.cat_upvar(id, var_id)
        } else {
            Ok(PlaceWithOrigin::new_no_projections(id, expr_ty, PlaceBase::Local(var_id)))
        }
    }

    /// Categorize an upvar.
    ///
    /// Note: the actual upvar access contains invisible derefs of closure
    /// environment and upvar reference as appropriate. Only regionck cares
    /// about these dereferences, so we let it compute them as needed.
    fn cat_upvar(&mut self, hir_id: ExprOrPatId, var_id: BindingId) -> Result<PlaceWithOrigin> {
        let var_ty = self.expect_and_resolve_type(
            self.cx.result.type_of_binding.get(var_id).map(|it| it.as_ref()),
        )?;

        Ok(PlaceWithOrigin::new_no_projections(
            hir_id,
            var_ty,
            PlaceBase::Upvar { closure: self.closure_expr, var_id },
        ))
    }

    fn cat_rvalue(&self, hir_id: ExprOrPatId, expr_ty: Ty<'db>) -> PlaceWithOrigin {
        PlaceWithOrigin::new_no_projections(hir_id, expr_ty, PlaceBase::Rvalue)
    }

    fn cat_projection(
        &self,
        node: ExprOrPatId,
        mut base_place: PlaceWithOrigin,
        ty: Ty<'db>,
        kind: ProjectionKind,
    ) -> PlaceWithOrigin {
        base_place.push_projection(Projection { kind, ty: ty.store() }, node);
        base_place
    }

    fn cat_overloaded_place(&mut self, expr: ExprId, base: ExprId) -> Result<PlaceWithOrigin> {
        // Reconstruct the output assuming it's a reference with the
        // same region and mutability as the receiver. This holds for
        // `Deref(Mut)::Deref(_mut)` and `Index(Mut)::index(_mut)`.
        let place_ty = self.expr_ty(expr)?;
        let base_ty = self.expr_ty_adjusted(base)?;

        let TyKind::Ref(region, _, mutbl) =
            self.cx.structurally_resolve_type(base.into(), base_ty).kind()
        else {
            return Err(ErrorGuaranteed);
        };
        let ref_ty = Ty::new_ref(self.cx.interner(), region, place_ty, mutbl);

        let base = self.cat_rvalue(expr.into(), ref_ty);
        self.cat_deref(expr.into(), base)
    }

    fn cat_deref(
        &mut self,
        node: ExprOrPatId,
        mut base_place: PlaceWithOrigin,
    ) -> Result<PlaceWithOrigin> {
        let base_curr_ty = base_place.place.ty();
        let Some(deref_ty) =
            self.cx.structurally_resolve_type(node, base_curr_ty).builtin_deref(true)
        else {
            debug!("explicit deref of non-derefable type: {:?}", base_curr_ty);
            return Err(ErrorGuaranteed);
        };
        base_place.push_projection(
            Projection { kind: ProjectionKind::Deref, ty: deref_ty.store() },
            node,
        );
        Ok(base_place)
    }

    /// Returns the variant index for an ADT used within a Struct or TupleStruct pattern
    /// Here `pat_hir_id` is the ExprId of the pattern itself.
    fn variant_index_for_adt(&self, pat_id: PatId) -> Result<(u32, VariantId)> {
        let variant = self.cx.result.variant_resolution_for_pat(pat_id).ok_or(ErrorGuaranteed)?;
        let variant_idx = match variant {
            VariantId::EnumVariantId(variant) => variant.loc(self.cx.db).index,
            VariantId::StructId(_) | VariantId::UnionId(_) => 0,
        };
        Ok((variant_idx, variant))
    }

    /// Returns the total number of fields in a tuple used within a Tuple pattern.
    /// Here `pat_hir_id` is the ExprId of the pattern itself.
    fn total_fields_in_tuple(&mut self, pat_id: PatId) -> usize {
        let ty = self.cx.result.pat_ty(pat_id);
        match self.cx.structurally_resolve_type(pat_id.into(), ty).kind() {
            TyKind::Tuple(args) => args.len(),
            _ => panic!("tuple pattern not applied to a tuple"),
        }
    }

    /// Here, `place` is the `PlaceWithId` being matched and pat is the pattern it
    /// is being matched against.
    ///
    /// In general, the way that this works is that we walk down the pattern,
    /// constructing a `PlaceWithId` that represents the path that will be taken
    /// to reach the value being matched.
    fn cat_pattern<F>(
        &mut self,
        mut place_with_id: PlaceWithOrigin,
        pat: PatId,
        op: &mut F,
    ) -> Result
    where
        F: FnMut(&mut Self, PlaceWithOrigin, CatPatternPat) -> Result,
    {
        // If (pattern) adjustments are active for this pattern, adjust the `PlaceWithId` correspondingly.
        // `PlaceWithId`s are constructed differently from patterns. For example, in
        //
        // ```
        // match foo {
        //     &&Some(x, ) => { ... },
        //     _ => { ... },
        // }
        // ```
        //
        // the pattern `&&Some(x,)` is represented as `Ref { Ref { TupleStruct }}`. To build the
        // corresponding `PlaceWithId` we start with the `PlaceWithId` for `foo`, and then, by traversing the
        // pattern, try to answer the question: given the address of `foo`, how is `x` reached?
        //
        // `&&Some(x,)` `place_foo`
        //  `&Some(x,)` `deref { place_foo}`
        //   `Some(x,)` `deref { deref { place_foo }}`
        //       `(x,)` `field0 { deref { deref { place_foo }}}` <- resulting place
        //
        // The above example has no adjustments. If the code were instead the (after adjustments,
        // equivalent) version
        //
        // ```
        // match foo {
        //     Some(x, ) => { ... },
        //     _ => { ... },
        // }
        // ```
        //
        // Then we see that to get the same result, we must start with
        // `deref { deref { place_foo }}` instead of `place_foo` since the pattern is now `Some(x,)`
        // and not `&&Some(x,)`, even though its assigned type is that of `&&Some(x,)`.
        let adjustments = self.pat_adjustments(pat);
        let mut adjusts = adjustments.iter().peekable();
        while let Some(adjust) = adjusts.next() {
            debug!("applying adjustment to place_with_id={:?}", place_with_id);
            place_with_id = match adjust.kind {
                PatAdjust::BuiltinDeref => self.cat_deref(pat.into(), place_with_id)?,
                PatAdjust::OverloadedDeref => {
                    // This adjustment corresponds to an overloaded deref; unless it's on a box, it
                    // borrows the scrutinee to call `Deref::deref` or `DerefMut::deref_mut`. Invoke
                    // the callback before setting `place_with_id` to the temporary storing the
                    // result of the deref.
                    op(self, place_with_id.clone(), CatPatternPat::DerefPat { inner: pat })?;
                    let target_ty = match adjusts.peek() {
                        Some(next_adjust) => next_adjust.source.as_ref(),
                        // At the end of the deref chain, we get `pat`'s scrutinee.
                        None => self.pat_ty_unadjusted(pat)?,
                    };
                    self.pat_deref_place(pat.into(), place_with_id, pat, target_ty)?
                }
            };
        }
        let place_with_id = place_with_id; // lose mutability
        debug!("applied adjustment derefs to get place_with_id={:?}", place_with_id);

        // Invoke the callback, but only now, after the `place_with_id` has adjusted.
        //
        // To see that this makes sense, consider `match &Some(3) { Some(x) => { ... }}`. In that
        // case, the initial `place_with_id` will be that for `&Some(3)` and the pattern is `Some(x)`. We
        // don't want to call `op` with these incompatible values. As written, what happens instead
        // is that `op` is called with the adjusted place (that for `*&Some(3)`) and the pattern
        // `Some(x)` (which matches). Recursing once more, `*&Some(3)` and the pattern `Some(x)`
        // result in the place `Downcast<Some>(*&Some(3)).0` associated to `x` and invoke `op` with
        // that (where the `ref` on `x` is implied).
        op(self, place_with_id.clone(), pat.into())?;

        match self.cx.store[pat] {
            Pat::Tuple { args: ref subpats, ellipsis: dots_pos } => {
                // (p1, ..., pN)
                let total_fields = self.total_fields_in_tuple(pat);

                for (i, &subpat) in subpats.iter().enumerate_and_adjust(total_fields, dots_pos) {
                    let subpat_ty = self.pat_ty_adjusted(subpat)?;
                    let projection_kind =
                        ProjectionKind::Field { field_idx: i as u32, variant_idx: 0 };
                    let sub_place = self.cat_projection(
                        pat.into(),
                        place_with_id.clone(),
                        subpat_ty,
                        projection_kind,
                    );
                    self.cat_pattern(sub_place, subpat, op)?;
                }
            }

            Pat::TupleStruct { args: ref subpats, ellipsis: dots_pos, .. } => {
                // S(p1, ..., pN)
                let (variant_index, variant) = self.variant_index_for_adt(pat)?;
                let total_fields = variant.fields(self.cx.db).len();

                for (i, &subpat) in subpats.iter().enumerate_and_adjust(total_fields, dots_pos) {
                    let subpat_ty = self.pat_ty_adjusted(subpat)?;
                    let projection_kind =
                        ProjectionKind::Field { variant_idx: variant_index, field_idx: i as u32 };
                    let sub_place = self.cat_projection(
                        pat.into(),
                        place_with_id.clone(),
                        subpat_ty,
                        projection_kind,
                    );
                    self.cat_pattern(sub_place, subpat, op)?;
                }
            }

            Pat::Record { args: ref field_pats, .. } => {
                // S { f1: p1, ..., fN: pN }

                let (variant_index, variant) = self.variant_index_for_adt(pat)?;
                let fields = variant.fields(self.cx.db);

                for fp in field_pats {
                    let field_ty = self.pat_ty_adjusted(fp.pat)?;
                    let field_index = fields.field(&fp.name).ok_or(ErrorGuaranteed)?;

                    let field_place = self.cat_projection(
                        pat.into(),
                        place_with_id.clone(),
                        field_ty,
                        ProjectionKind::Field {
                            variant_idx: variant_index,
                            field_idx: field_index.into_raw().into_u32(),
                        },
                    );
                    self.cat_pattern(field_place, fp.pat, op)?;
                }
            }

            Pat::Or(ref pats) => {
                for &pat in pats {
                    self.cat_pattern(place_with_id.clone(), pat, op)?;
                }
            }

            Pat::Bind { subpat: Some(subpat), .. } => {
                self.cat_pattern(place_with_id, subpat, op)?;
            }

            Pat::Box { inner: subpat } | Pat::Ref { pat: subpat, .. } => {
                // box p1, &p1, &mut p1. we can ignore the mutability of
                // PatKind::Ref since that information is already contained
                // in the type.
                let subplace = self.cat_deref(pat.into(), place_with_id)?;
                self.cat_pattern(subplace, subpat, op)?;
            }
            Pat::Deref { inner: subpat } => {
                let ty = self.pat_ty_adjusted(subpat)?;
                let place = self.pat_deref_place(pat.into(), place_with_id, subpat, ty)?;
                self.cat_pattern(place, subpat, op)?;
            }

            Pat::Slice { prefix: ref before, slice, suffix: ref after } => {
                let Some(element_ty) = self
                    .cx
                    .structurally_resolve_type(pat.into(), place_with_id.place.ty())
                    .builtin_index()
                else {
                    debug!("explicit index of non-indexable type {:?}", place_with_id);
                    return Err(ErrorGuaranteed);
                };
                let elt_place = self.cat_projection(
                    pat.into(),
                    place_with_id.clone(),
                    element_ty,
                    ProjectionKind::Index,
                );
                for &before_pat in before {
                    self.cat_pattern(elt_place.clone(), before_pat, op)?;
                }
                if let Some(slice_pat) = slice {
                    let slice_pat_ty = self.pat_ty_adjusted(slice_pat)?;
                    let slice_place = self.cat_projection(
                        pat.into(),
                        place_with_id,
                        slice_pat_ty,
                        ProjectionKind::Subslice,
                    );
                    self.cat_pattern(slice_place, slice_pat, op)?;
                }
                for &after_pat in after {
                    self.cat_pattern(elt_place.clone(), after_pat, op)?;
                }
            }

            Pat::Bind { subpat: None, .. }
            | Pat::Expr(..)
            | Pat::Path(_)
            | Pat::Lit(..)
            | Pat::ConstBlock(..)
            | Pat::Range { .. }
            | Pat::Missing
            | Pat::Rest
            | Pat::Wild => {
                // always ok
            }
        }

        Ok(())
    }

    /// Represents the place matched on by a deref pattern's interior.
    fn pat_deref_place(
        &mut self,
        node: ExprOrPatId,
        base_place: PlaceWithOrigin,
        inner: PatId,
        target_ty: Ty<'db>,
    ) -> Result<PlaceWithOrigin> {
        match self.cx.deref_pat_borrow_mode(base_place.place.ty(), inner) {
            // Deref patterns on boxes are lowered using a built-in deref.
            DerefPatBorrowMode::Box => self.cat_deref(node, base_place),
            // For other types, we create a temporary to match on.
            DerefPatBorrowMode::Borrow(mutability) => {
                let re_erased = self.cx.types.regions.erased;
                let ty = Ty::new_ref(self.cx.interner(), re_erased, target_ty, mutability);
                // A deref pattern stores the result of `Deref::deref` or `DerefMut::deref_mut` ...
                let base = self.cat_rvalue(node, ty);
                // ... and the inner pattern matches on the place behind that reference.
                self.cat_deref(node, base)
            }
        }
    }

    /// Checks whether a type has multiple variants, and therefore, whether a
    /// read of the discriminant might be necessary. Note that the actual MIR
    /// builder code does a more specific check, filtering out variants that
    /// happen to be uninhabited.
    ///
    /// Here, it is not practical to perform such a check, because inhabitedness
    /// queries require typeck results, and typeck requires closure capture analysis.
    ///
    /// Moreover, the language is moving towards uninhabited variants still semantically
    /// causing a discriminant read, so we *shouldn't* perform any such check.
    ///
    /// FIXME(never_patterns): update this comment once the aforementioned MIR builder
    /// code is changed to be insensitive to inhhabitedness.
    #[instrument(skip(self), level = "debug")]
    fn is_multivariant_adt(&mut self, node: ExprOrPatId, ty: Ty<'db>) -> bool {
        if let TyKind::Adt(def, _) = self.cx.structurally_resolve_type(node, ty).kind() {
            // Note that if a non-exhaustive SingleVariant is defined in another crate, we need
            // to assume that more cases will be added to the variant in the future. This mean
            // that we should handle non-exhaustive SingleVariant the same way we would handle
            // a MultiVariant.
            match def.def_id() {
                AdtId::StructId(_) | AdtId::UnionId(_) => false,
                AdtId::EnumId(did) => {
                    let has_foreign_non_exhaustive = || {
                        AttrFlags::query(self.cx.db, did.into()).contains(AttrFlags::NON_EXHAUSTIVE)
                            && did.krate(self.cx.db) != self.cx.krate()
                    };
                    did.enum_variants(self.cx.db).variants.len() > 1 || has_foreign_non_exhaustive()
                }
            }
        } else {
            false
        }
    }
}