@@ -30,8 +30,6 @@ use ty::BorrowKind::ImmBorrow;
30
30
31
31
use crate :: fn_ctxt:: FnCtxt ;
32
32
33
- type McResult < T > = Result < T , ErrorGuaranteed > ;
34
-
35
33
/// This trait defines the callbacks you can expect to receive when
36
34
/// employing the ExprUseVisitor.
37
35
pub trait Delegate < ' tcx > {
@@ -219,6 +217,8 @@ impl<'tcx> TypeInformationCtxt<'tcx> for (&LateContext<'tcx>, LocalDefId) {
219
217
/// This is the code that actually walks the tree.
220
218
pub struct ExprUseVisitor < ' tcx , Cx : TypeInformationCtxt < ' tcx > , D : Delegate < ' tcx > > {
221
219
cx : Cx ,
220
+ /// We use a `RefCell` here so that delegates can mutate themselves, but we can
221
+ /// still have calls to our own helper functions.
222
222
delegate : RefCell < D > ,
223
223
upvars : Option < & ' tcx FxIndexMap < HirId , hir:: Upvar > > ,
224
224
}
@@ -517,14 +517,14 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
517
517
discr : & Expr < ' _ > ,
518
518
discr_place : PlaceWithHirId < ' tcx > ,
519
519
pats : impl Iterator < Item = & ' t hir:: Pat < ' t > > ,
520
- ) -> McResult < ( ) > {
520
+ ) -> Result < ( ) , ErrorGuaranteed > {
521
521
// Matching should not always be considered a use of the place, hence
522
522
// discr does not necessarily need to be borrowed.
523
523
// We only want to borrow discr if the pattern contain something other
524
524
// than wildcards.
525
525
let mut needs_to_be_read = false ;
526
526
for pat in pats {
527
- self . cat_pattern ( discr_place. clone ( ) , pat, |place, pat| {
527
+ self . cat_pattern ( discr_place. clone ( ) , pat, & mut |place, pat| {
528
528
match & pat. kind {
529
529
PatKind :: Binding ( .., opt_sub_pat) => {
530
530
// If the opt_sub_pat is None, then the binding does not count as
@@ -836,7 +836,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
836
836
debug ! ( "walk_pat(discr_place={:?}, pat={:?}, has_guard={:?})" , discr_place, pat, has_guard) ;
837
837
838
838
let tcx = self . cx . tcx ( ) ;
839
- return_if_err ! ( self . cat_pattern( discr_place. clone( ) , pat, |place, pat| {
839
+ return_if_err ! ( self . cat_pattern( discr_place. clone( ) , pat, & mut |place, pat| {
840
840
if let PatKind :: Binding ( _, canonical_id, ..) = pat. kind {
841
841
debug!( "walk_pat: binding place={:?} pat={:?}" , place, pat) ;
842
842
if let Some ( bm) =
@@ -1022,7 +1022,11 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1022
1022
}
1023
1023
}
1024
1024
1025
- fn resolve_type_vars_or_error ( & self , id : HirId , ty : Option < Ty < ' tcx > > ) -> McResult < Ty < ' tcx > > {
1025
+ fn resolve_type_vars_or_error (
1026
+ & self ,
1027
+ id : HirId ,
1028
+ ty : Option < Ty < ' tcx > > ,
1029
+ ) -> Result < Ty < ' tcx > , ErrorGuaranteed > {
1026
1030
match ty {
1027
1031
Some ( ty) => {
1028
1032
let ty = self . cx . resolve_vars_if_possible ( ty) ;
@@ -1053,15 +1057,15 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1053
1057
}
1054
1058
}
1055
1059
1056
- fn node_ty ( & self , hir_id : HirId ) -> McResult < Ty < ' tcx > > {
1060
+ fn node_ty ( & self , hir_id : HirId ) -> Result < Ty < ' tcx > , ErrorGuaranteed > {
1057
1061
self . resolve_type_vars_or_error ( hir_id, self . cx . typeck_results ( ) . node_type_opt ( hir_id) )
1058
1062
}
1059
1063
1060
- fn expr_ty ( & self , expr : & hir:: Expr < ' _ > ) -> McResult < Ty < ' tcx > > {
1064
+ fn expr_ty ( & self , expr : & hir:: Expr < ' _ > ) -> Result < Ty < ' tcx > , ErrorGuaranteed > {
1061
1065
self . resolve_type_vars_or_error ( expr. hir_id , self . cx . typeck_results ( ) . expr_ty_opt ( expr) )
1062
1066
}
1063
1067
1064
- fn expr_ty_adjusted ( & self , expr : & hir:: Expr < ' _ > ) -> McResult < Ty < ' tcx > > {
1068
+ fn expr_ty_adjusted ( & self , expr : & hir:: Expr < ' _ > ) -> Result < Ty < ' tcx > , ErrorGuaranteed > {
1065
1069
self . resolve_type_vars_or_error (
1066
1070
expr. hir_id ,
1067
1071
self . cx . typeck_results ( ) . expr_ty_adjusted_opt ( expr) ,
@@ -1078,7 +1082,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1078
1082
/// implicit deref patterns attached (e.g., it is really
1079
1083
/// `&Some(x)`). In that case, we return the "outermost" type
1080
1084
/// (e.g., `&Option<T>`).
1081
- fn pat_ty_adjusted ( & self , pat : & hir:: Pat < ' _ > ) -> McResult < Ty < ' tcx > > {
1085
+ fn pat_ty_adjusted ( & self , pat : & hir:: Pat < ' _ > ) -> Result < Ty < ' tcx > , ErrorGuaranteed > {
1082
1086
// Check for implicit `&` types wrapping the pattern; note
1083
1087
// that these are never attached to binding patterns, so
1084
1088
// actually this is somewhat "disjoint" from the code below
@@ -1093,8 +1097,8 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1093
1097
self . pat_ty_unadjusted ( pat)
1094
1098
}
1095
1099
1096
- /// Like `pat_ty`, but ignores implicit `&` patterns.
1097
- fn pat_ty_unadjusted ( & self , pat : & hir:: Pat < ' _ > ) -> McResult < Ty < ' tcx > > {
1100
+ /// Like `TypeckResults:: pat_ty`, but ignores implicit `&` patterns.
1101
+ fn pat_ty_unadjusted ( & self , pat : & hir:: Pat < ' _ > ) -> Result < Ty < ' tcx > , ErrorGuaranteed > {
1098
1102
let base_ty = self . node_ty ( pat. hir_id ) ?;
1099
1103
trace ! ( ?base_ty) ;
1100
1104
@@ -1136,7 +1140,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1136
1140
}
1137
1141
}
1138
1142
1139
- fn cat_expr ( & self , expr : & hir:: Expr < ' _ > ) -> McResult < PlaceWithHirId < ' tcx > > {
1143
+ fn cat_expr ( & self , expr : & hir:: Expr < ' _ > ) -> Result < PlaceWithHirId < ' tcx > , ErrorGuaranteed > {
1140
1144
self . cat_expr_ ( expr, self . cx . typeck_results ( ) . expr_adjustments ( expr) )
1141
1145
}
1142
1146
@@ -1146,7 +1150,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1146
1150
& self ,
1147
1151
expr : & hir:: Expr < ' _ > ,
1148
1152
adjustments : & [ adjustment:: Adjustment < ' tcx > ] ,
1149
- ) -> McResult < PlaceWithHirId < ' tcx > > {
1153
+ ) -> Result < PlaceWithHirId < ' tcx > , ErrorGuaranteed > {
1150
1154
match adjustments. split_last ( ) {
1151
1155
None => self . cat_expr_unadjusted ( expr) ,
1152
1156
Some ( ( adjustment, previous) ) => {
@@ -1160,7 +1164,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1160
1164
expr : & hir:: Expr < ' _ > ,
1161
1165
previous : PlaceWithHirId < ' tcx > ,
1162
1166
adjustment : & adjustment:: Adjustment < ' tcx > ,
1163
- ) -> McResult < PlaceWithHirId < ' tcx > > {
1167
+ ) -> Result < PlaceWithHirId < ' tcx > , ErrorGuaranteed > {
1164
1168
self . cat_expr_adjusted_with ( expr, || Ok ( previous) , adjustment)
1165
1169
}
1166
1170
@@ -1169,9 +1173,9 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1169
1173
expr : & hir:: Expr < ' _ > ,
1170
1174
previous : F ,
1171
1175
adjustment : & adjustment:: Adjustment < ' tcx > ,
1172
- ) -> McResult < PlaceWithHirId < ' tcx > >
1176
+ ) -> Result < PlaceWithHirId < ' tcx > , ErrorGuaranteed >
1173
1177
where
1174
- F : FnOnce ( ) -> McResult < PlaceWithHirId < ' tcx > > ,
1178
+ F : FnOnce ( ) -> Result < PlaceWithHirId < ' tcx > , ErrorGuaranteed > ,
1175
1179
{
1176
1180
let target = self . cx . resolve_vars_if_possible ( adjustment. target ) ;
1177
1181
match adjustment. kind {
@@ -1196,7 +1200,10 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1196
1200
}
1197
1201
}
1198
1202
1199
- fn cat_expr_unadjusted ( & self , expr : & hir:: Expr < ' _ > ) -> McResult < PlaceWithHirId < ' tcx > > {
1203
+ fn cat_expr_unadjusted (
1204
+ & self ,
1205
+ expr : & hir:: Expr < ' _ > ,
1206
+ ) -> Result < PlaceWithHirId < ' tcx > , ErrorGuaranteed > {
1200
1207
let expr_ty = self . expr_ty ( expr) ?;
1201
1208
match expr. kind {
1202
1209
hir:: ExprKind :: Unary ( hir:: UnOp :: Deref , e_base) => {
@@ -1287,7 +1294,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1287
1294
span : Span ,
1288
1295
expr_ty : Ty < ' tcx > ,
1289
1296
res : Res ,
1290
- ) -> McResult < PlaceWithHirId < ' tcx > > {
1297
+ ) -> Result < PlaceWithHirId < ' tcx > , ErrorGuaranteed > {
1291
1298
match res {
1292
1299
Res :: Def (
1293
1300
DefKind :: Ctor ( ..)
@@ -1321,7 +1328,11 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1321
1328
/// Note: the actual upvar access contains invisible derefs of closure
1322
1329
/// environment and upvar reference as appropriate. Only regionck cares
1323
1330
/// about these dereferences, so we let it compute them as needed.
1324
- fn cat_upvar ( & self , hir_id : HirId , var_id : HirId ) -> McResult < PlaceWithHirId < ' tcx > > {
1331
+ fn cat_upvar (
1332
+ & self ,
1333
+ hir_id : HirId ,
1334
+ var_id : HirId ,
1335
+ ) -> Result < PlaceWithHirId < ' tcx > , ErrorGuaranteed > {
1325
1336
let closure_expr_def_id = self . cx . body_owner_def_id ( ) ;
1326
1337
1327
1338
let upvar_id = ty:: UpvarId {
@@ -1370,7 +1381,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1370
1381
& self ,
1371
1382
expr : & hir:: Expr < ' _ > ,
1372
1383
base : & hir:: Expr < ' _ > ,
1373
- ) -> McResult < PlaceWithHirId < ' tcx > > {
1384
+ ) -> Result < PlaceWithHirId < ' tcx > , ErrorGuaranteed > {
1374
1385
// Reconstruct the output assuming it's a reference with the
1375
1386
// same region and mutability as the receiver. This holds for
1376
1387
// `Deref(Mut)::Deref(_mut)` and `Index(Mut)::index(_mut)`.
@@ -1392,7 +1403,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1392
1403
& self ,
1393
1404
node : HirId ,
1394
1405
base_place : PlaceWithHirId < ' tcx > ,
1395
- ) -> McResult < PlaceWithHirId < ' tcx > > {
1406
+ ) -> Result < PlaceWithHirId < ' tcx > , ErrorGuaranteed > {
1396
1407
let base_curr_ty = base_place. place . ty ( ) ;
1397
1408
let deref_ty = match self
1398
1409
. cx
@@ -1417,26 +1428,14 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1417
1428
Ok ( PlaceWithHirId :: new ( node, base_place. place . base_ty , base_place. place . base , projections) )
1418
1429
}
1419
1430
1420
- fn cat_pattern < F > (
1421
- & self ,
1422
- place : PlaceWithHirId < ' tcx > ,
1423
- pat : & hir:: Pat < ' _ > ,
1424
- mut op : F ,
1425
- ) -> McResult < ( ) >
1426
- where
1427
- F : FnMut ( & PlaceWithHirId < ' tcx > , & hir:: Pat < ' _ > ) ,
1428
- {
1429
- self . cat_pattern_ ( place, pat, & mut op)
1430
- }
1431
-
1432
1431
/// Returns the variant index for an ADT used within a Struct or TupleStruct pattern
1433
1432
/// Here `pat_hir_id` is the HirId of the pattern itself.
1434
1433
fn variant_index_for_adt (
1435
1434
& self ,
1436
1435
qpath : & hir:: QPath < ' _ > ,
1437
1436
pat_hir_id : HirId ,
1438
1437
span : Span ,
1439
- ) -> McResult < VariantIdx > {
1438
+ ) -> Result < VariantIdx , ErrorGuaranteed > {
1440
1439
let res = self . cx . typeck_results ( ) . qpath_res ( qpath, pat_hir_id) ;
1441
1440
let ty = self . cx . typeck_results ( ) . node_type ( pat_hir_id) ;
1442
1441
let ty:: Adt ( adt_def, _) = self . cx . try_structurally_resolve_type ( span, ty) . kind ( ) else {
@@ -1471,7 +1470,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1471
1470
pat_hir_id : HirId ,
1472
1471
variant_index : VariantIdx ,
1473
1472
span : Span ,
1474
- ) -> McResult < usize > {
1473
+ ) -> Result < usize , ErrorGuaranteed > {
1475
1474
let ty = self . cx . typeck_results ( ) . node_type ( pat_hir_id) ;
1476
1475
match self . cx . try_structurally_resolve_type ( span, ty) . kind ( ) {
1477
1476
ty:: Adt ( adt_def, _) => Ok ( adt_def. variant ( variant_index) . fields . len ( ) ) ,
@@ -1486,7 +1485,11 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1486
1485
1487
1486
/// Returns the total number of fields in a tuple used within a Tuple pattern.
1488
1487
/// Here `pat_hir_id` is the HirId of the pattern itself.
1489
- fn total_fields_in_tuple ( & self , pat_hir_id : HirId , span : Span ) -> McResult < usize > {
1488
+ fn total_fields_in_tuple (
1489
+ & self ,
1490
+ pat_hir_id : HirId ,
1491
+ span : Span ,
1492
+ ) -> Result < usize , ErrorGuaranteed > {
1490
1493
let ty = self . cx . typeck_results ( ) . node_type ( pat_hir_id) ;
1491
1494
match self . cx . try_structurally_resolve_type ( span, ty) . kind ( ) {
1492
1495
ty:: Tuple ( args) => Ok ( args. len ( ) ) ,
@@ -1504,12 +1507,12 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1504
1507
/// In general, the way that this works is that we walk down the pattern,
1505
1508
/// constructing a `PlaceWithHirId` that represents the path that will be taken
1506
1509
/// to reach the value being matched.
1507
- fn cat_pattern_ < F > (
1510
+ fn cat_pattern < F > (
1508
1511
& self ,
1509
1512
mut place_with_id : PlaceWithHirId < ' tcx > ,
1510
1513
pat : & hir:: Pat < ' _ > ,
1511
1514
op : & mut F ,
1512
- ) -> McResult < ( ) >
1515
+ ) -> Result < ( ) , ErrorGuaranteed >
1513
1516
where
1514
1517
F : FnMut ( & PlaceWithHirId < ' tcx > , & hir:: Pat < ' _ > ) ,
1515
1518
{
@@ -1580,7 +1583,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1580
1583
subpat_ty,
1581
1584
projection_kind,
1582
1585
) ;
1583
- self . cat_pattern_ ( sub_place, subpat, op) ?;
1586
+ self . cat_pattern ( sub_place, subpat, op) ?;
1584
1587
}
1585
1588
}
1586
1589
@@ -1600,7 +1603,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1600
1603
subpat_ty,
1601
1604
projection_kind,
1602
1605
) ;
1603
- self . cat_pattern_ ( sub_place, subpat, op) ?;
1606
+ self . cat_pattern ( sub_place, subpat, op) ?;
1604
1607
}
1605
1608
}
1606
1609
@@ -1625,26 +1628,26 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1625
1628
field_ty,
1626
1629
ProjectionKind :: Field ( field_index, variant_index) ,
1627
1630
) ;
1628
- self . cat_pattern_ ( field_place, fp. pat , op) ?;
1631
+ self . cat_pattern ( field_place, fp. pat , op) ?;
1629
1632
}
1630
1633
}
1631
1634
1632
1635
PatKind :: Or ( pats) => {
1633
1636
for pat in pats {
1634
- self . cat_pattern_ ( place_with_id. clone ( ) , pat, op) ?;
1637
+ self . cat_pattern ( place_with_id. clone ( ) , pat, op) ?;
1635
1638
}
1636
1639
}
1637
1640
1638
1641
PatKind :: Binding ( .., Some ( subpat) ) => {
1639
- self . cat_pattern_ ( place_with_id, subpat, op) ?;
1642
+ self . cat_pattern ( place_with_id, subpat, op) ?;
1640
1643
}
1641
1644
1642
1645
PatKind :: Box ( subpat) | PatKind :: Ref ( subpat, _) => {
1643
1646
// box p1, &p1, &mut p1. we can ignore the mutability of
1644
1647
// PatKind::Ref since that information is already contained
1645
1648
// in the type.
1646
1649
let subplace = self . cat_deref ( pat. hir_id , place_with_id) ?;
1647
- self . cat_pattern_ ( subplace, subpat, op) ?;
1650
+ self . cat_pattern ( subplace, subpat, op) ?;
1648
1651
}
1649
1652
PatKind :: Deref ( subpat) => {
1650
1653
let mutable = self . cx . typeck_results ( ) . pat_has_ref_mut_binding ( subpat) ;
@@ -1654,7 +1657,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1654
1657
let ty = Ty :: new_ref ( self . cx . tcx ( ) , re_erased, ty, mutability) ;
1655
1658
// A deref pattern generates a temporary.
1656
1659
let place = self . cat_rvalue ( pat. hir_id , ty) ;
1657
- self . cat_pattern_ ( place, subpat, op) ?;
1660
+ self . cat_pattern ( place, subpat, op) ?;
1658
1661
}
1659
1662
1660
1663
PatKind :: Slice ( before, ref slice, after) => {
@@ -1673,7 +1676,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1673
1676
ProjectionKind :: Index ,
1674
1677
) ;
1675
1678
for before_pat in before {
1676
- self . cat_pattern_ ( elt_place. clone ( ) , before_pat, op) ?;
1679
+ self . cat_pattern ( elt_place. clone ( ) , before_pat, op) ?;
1677
1680
}
1678
1681
if let Some ( slice_pat) = * slice {
1679
1682
let slice_pat_ty = self . pat_ty_adjusted ( slice_pat) ?;
@@ -1683,10 +1686,10 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
1683
1686
slice_pat_ty,
1684
1687
ProjectionKind :: Subslice ,
1685
1688
) ;
1686
- self . cat_pattern_ ( slice_place, slice_pat, op) ?;
1689
+ self . cat_pattern ( slice_place, slice_pat, op) ?;
1687
1690
}
1688
1691
for after_pat in after {
1689
- self . cat_pattern_ ( elt_place. clone ( ) , after_pat, op) ?;
1692
+ self . cat_pattern ( elt_place. clone ( ) , after_pat, op) ?;
1690
1693
}
1691
1694
}
1692
1695
0 commit comments