Skip to content

Commit 7fd4cd4

Browse files
Apply nits
1 parent 8b060ab commit 7fd4cd4

File tree

1 file changed

+52
-49
lines changed

1 file changed

+52
-49
lines changed

compiler/rustc_hir_typeck/src/expr_use_visitor.rs

+52-49
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ use ty::BorrowKind::ImmBorrow;
3030

3131
use crate::fn_ctxt::FnCtxt;
3232

33-
type McResult<T> = Result<T, ErrorGuaranteed>;
34-
3533
/// This trait defines the callbacks you can expect to receive when
3634
/// employing the ExprUseVisitor.
3735
pub trait Delegate<'tcx> {
@@ -219,6 +217,8 @@ impl<'tcx> TypeInformationCtxt<'tcx> for (&LateContext<'tcx>, LocalDefId) {
219217
/// This is the code that actually walks the tree.
220218
pub struct ExprUseVisitor<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> {
221219
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.
222222
delegate: RefCell<D>,
223223
upvars: Option<&'tcx FxIndexMap<HirId, hir::Upvar>>,
224224
}
@@ -517,14 +517,14 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
517517
discr: &Expr<'_>,
518518
discr_place: PlaceWithHirId<'tcx>,
519519
pats: impl Iterator<Item = &'t hir::Pat<'t>>,
520-
) -> McResult<()> {
520+
) -> Result<(), ErrorGuaranteed> {
521521
// Matching should not always be considered a use of the place, hence
522522
// discr does not necessarily need to be borrowed.
523523
// We only want to borrow discr if the pattern contain something other
524524
// than wildcards.
525525
let mut needs_to_be_read = false;
526526
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| {
528528
match &pat.kind {
529529
PatKind::Binding(.., opt_sub_pat) => {
530530
// 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
836836
debug!("walk_pat(discr_place={:?}, pat={:?}, has_guard={:?})", discr_place, pat, has_guard);
837837

838838
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| {
840840
if let PatKind::Binding(_, canonical_id, ..) = pat.kind {
841841
debug!("walk_pat: binding place={:?} pat={:?}", place, pat);
842842
if let Some(bm) =
@@ -1022,7 +1022,11 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
10221022
}
10231023
}
10241024

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> {
10261030
match ty {
10271031
Some(ty) => {
10281032
let ty = self.cx.resolve_vars_if_possible(ty);
@@ -1053,15 +1057,15 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
10531057
}
10541058
}
10551059

1056-
fn node_ty(&self, hir_id: HirId) -> McResult<Ty<'tcx>> {
1060+
fn node_ty(&self, hir_id: HirId) -> Result<Ty<'tcx>, ErrorGuaranteed> {
10571061
self.resolve_type_vars_or_error(hir_id, self.cx.typeck_results().node_type_opt(hir_id))
10581062
}
10591063

1060-
fn expr_ty(&self, expr: &hir::Expr<'_>) -> McResult<Ty<'tcx>> {
1064+
fn expr_ty(&self, expr: &hir::Expr<'_>) -> Result<Ty<'tcx>, ErrorGuaranteed> {
10611065
self.resolve_type_vars_or_error(expr.hir_id, self.cx.typeck_results().expr_ty_opt(expr))
10621066
}
10631067

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> {
10651069
self.resolve_type_vars_or_error(
10661070
expr.hir_id,
10671071
self.cx.typeck_results().expr_ty_adjusted_opt(expr),
@@ -1078,7 +1082,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
10781082
/// implicit deref patterns attached (e.g., it is really
10791083
/// `&Some(x)`). In that case, we return the "outermost" type
10801084
/// (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> {
10821086
// Check for implicit `&` types wrapping the pattern; note
10831087
// that these are never attached to binding patterns, so
10841088
// actually this is somewhat "disjoint" from the code below
@@ -1093,8 +1097,8 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
10931097
self.pat_ty_unadjusted(pat)
10941098
}
10951099

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> {
10981102
let base_ty = self.node_ty(pat.hir_id)?;
10991103
trace!(?base_ty);
11001104

@@ -1136,7 +1140,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
11361140
}
11371141
}
11381142

1139-
fn cat_expr(&self, expr: &hir::Expr<'_>) -> McResult<PlaceWithHirId<'tcx>> {
1143+
fn cat_expr(&self, expr: &hir::Expr<'_>) -> Result<PlaceWithHirId<'tcx>, ErrorGuaranteed> {
11401144
self.cat_expr_(expr, self.cx.typeck_results().expr_adjustments(expr))
11411145
}
11421146

@@ -1146,7 +1150,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
11461150
&self,
11471151
expr: &hir::Expr<'_>,
11481152
adjustments: &[adjustment::Adjustment<'tcx>],
1149-
) -> McResult<PlaceWithHirId<'tcx>> {
1153+
) -> Result<PlaceWithHirId<'tcx>, ErrorGuaranteed> {
11501154
match adjustments.split_last() {
11511155
None => self.cat_expr_unadjusted(expr),
11521156
Some((adjustment, previous)) => {
@@ -1160,7 +1164,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
11601164
expr: &hir::Expr<'_>,
11611165
previous: PlaceWithHirId<'tcx>,
11621166
adjustment: &adjustment::Adjustment<'tcx>,
1163-
) -> McResult<PlaceWithHirId<'tcx>> {
1167+
) -> Result<PlaceWithHirId<'tcx>, ErrorGuaranteed> {
11641168
self.cat_expr_adjusted_with(expr, || Ok(previous), adjustment)
11651169
}
11661170

@@ -1169,9 +1173,9 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
11691173
expr: &hir::Expr<'_>,
11701174
previous: F,
11711175
adjustment: &adjustment::Adjustment<'tcx>,
1172-
) -> McResult<PlaceWithHirId<'tcx>>
1176+
) -> Result<PlaceWithHirId<'tcx>, ErrorGuaranteed>
11731177
where
1174-
F: FnOnce() -> McResult<PlaceWithHirId<'tcx>>,
1178+
F: FnOnce() -> Result<PlaceWithHirId<'tcx>, ErrorGuaranteed>,
11751179
{
11761180
let target = self.cx.resolve_vars_if_possible(adjustment.target);
11771181
match adjustment.kind {
@@ -1196,7 +1200,10 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
11961200
}
11971201
}
11981202

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> {
12001207
let expr_ty = self.expr_ty(expr)?;
12011208
match expr.kind {
12021209
hir::ExprKind::Unary(hir::UnOp::Deref, e_base) => {
@@ -1287,7 +1294,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
12871294
span: Span,
12881295
expr_ty: Ty<'tcx>,
12891296
res: Res,
1290-
) -> McResult<PlaceWithHirId<'tcx>> {
1297+
) -> Result<PlaceWithHirId<'tcx>, ErrorGuaranteed> {
12911298
match res {
12921299
Res::Def(
12931300
DefKind::Ctor(..)
@@ -1321,7 +1328,11 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
13211328
/// Note: the actual upvar access contains invisible derefs of closure
13221329
/// environment and upvar reference as appropriate. Only regionck cares
13231330
/// 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> {
13251336
let closure_expr_def_id = self.cx.body_owner_def_id();
13261337

13271338
let upvar_id = ty::UpvarId {
@@ -1370,7 +1381,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
13701381
&self,
13711382
expr: &hir::Expr<'_>,
13721383
base: &hir::Expr<'_>,
1373-
) -> McResult<PlaceWithHirId<'tcx>> {
1384+
) -> Result<PlaceWithHirId<'tcx>, ErrorGuaranteed> {
13741385
// Reconstruct the output assuming it's a reference with the
13751386
// same region and mutability as the receiver. This holds for
13761387
// `Deref(Mut)::Deref(_mut)` and `Index(Mut)::index(_mut)`.
@@ -1392,7 +1403,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
13921403
&self,
13931404
node: HirId,
13941405
base_place: PlaceWithHirId<'tcx>,
1395-
) -> McResult<PlaceWithHirId<'tcx>> {
1406+
) -> Result<PlaceWithHirId<'tcx>, ErrorGuaranteed> {
13961407
let base_curr_ty = base_place.place.ty();
13971408
let deref_ty = match self
13981409
.cx
@@ -1417,26 +1428,14 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
14171428
Ok(PlaceWithHirId::new(node, base_place.place.base_ty, base_place.place.base, projections))
14181429
}
14191430

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-
14321431
/// Returns the variant index for an ADT used within a Struct or TupleStruct pattern
14331432
/// Here `pat_hir_id` is the HirId of the pattern itself.
14341433
fn variant_index_for_adt(
14351434
&self,
14361435
qpath: &hir::QPath<'_>,
14371436
pat_hir_id: HirId,
14381437
span: Span,
1439-
) -> McResult<VariantIdx> {
1438+
) -> Result<VariantIdx, ErrorGuaranteed> {
14401439
let res = self.cx.typeck_results().qpath_res(qpath, pat_hir_id);
14411440
let ty = self.cx.typeck_results().node_type(pat_hir_id);
14421441
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
14711470
pat_hir_id: HirId,
14721471
variant_index: VariantIdx,
14731472
span: Span,
1474-
) -> McResult<usize> {
1473+
) -> Result<usize, ErrorGuaranteed> {
14751474
let ty = self.cx.typeck_results().node_type(pat_hir_id);
14761475
match self.cx.try_structurally_resolve_type(span, ty).kind() {
14771476
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
14861485

14871486
/// Returns the total number of fields in a tuple used within a Tuple pattern.
14881487
/// 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> {
14901493
let ty = self.cx.typeck_results().node_type(pat_hir_id);
14911494
match self.cx.try_structurally_resolve_type(span, ty).kind() {
14921495
ty::Tuple(args) => Ok(args.len()),
@@ -1504,12 +1507,12 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
15041507
/// In general, the way that this works is that we walk down the pattern,
15051508
/// constructing a `PlaceWithHirId` that represents the path that will be taken
15061509
/// to reach the value being matched.
1507-
fn cat_pattern_<F>(
1510+
fn cat_pattern<F>(
15081511
&self,
15091512
mut place_with_id: PlaceWithHirId<'tcx>,
15101513
pat: &hir::Pat<'_>,
15111514
op: &mut F,
1512-
) -> McResult<()>
1515+
) -> Result<(), ErrorGuaranteed>
15131516
where
15141517
F: FnMut(&PlaceWithHirId<'tcx>, &hir::Pat<'_>),
15151518
{
@@ -1580,7 +1583,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
15801583
subpat_ty,
15811584
projection_kind,
15821585
);
1583-
self.cat_pattern_(sub_place, subpat, op)?;
1586+
self.cat_pattern(sub_place, subpat, op)?;
15841587
}
15851588
}
15861589

@@ -1600,7 +1603,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
16001603
subpat_ty,
16011604
projection_kind,
16021605
);
1603-
self.cat_pattern_(sub_place, subpat, op)?;
1606+
self.cat_pattern(sub_place, subpat, op)?;
16041607
}
16051608
}
16061609

@@ -1625,26 +1628,26 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
16251628
field_ty,
16261629
ProjectionKind::Field(field_index, variant_index),
16271630
);
1628-
self.cat_pattern_(field_place, fp.pat, op)?;
1631+
self.cat_pattern(field_place, fp.pat, op)?;
16291632
}
16301633
}
16311634

16321635
PatKind::Or(pats) => {
16331636
for pat in pats {
1634-
self.cat_pattern_(place_with_id.clone(), pat, op)?;
1637+
self.cat_pattern(place_with_id.clone(), pat, op)?;
16351638
}
16361639
}
16371640

16381641
PatKind::Binding(.., Some(subpat)) => {
1639-
self.cat_pattern_(place_with_id, subpat, op)?;
1642+
self.cat_pattern(place_with_id, subpat, op)?;
16401643
}
16411644

16421645
PatKind::Box(subpat) | PatKind::Ref(subpat, _) => {
16431646
// box p1, &p1, &mut p1. we can ignore the mutability of
16441647
// PatKind::Ref since that information is already contained
16451648
// in the type.
16461649
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)?;
16481651
}
16491652
PatKind::Deref(subpat) => {
16501653
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
16541657
let ty = Ty::new_ref(self.cx.tcx(), re_erased, ty, mutability);
16551658
// A deref pattern generates a temporary.
16561659
let place = self.cat_rvalue(pat.hir_id, ty);
1657-
self.cat_pattern_(place, subpat, op)?;
1660+
self.cat_pattern(place, subpat, op)?;
16581661
}
16591662

16601663
PatKind::Slice(before, ref slice, after) => {
@@ -1673,7 +1676,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
16731676
ProjectionKind::Index,
16741677
);
16751678
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)?;
16771680
}
16781681
if let Some(slice_pat) = *slice {
16791682
let slice_pat_ty = self.pat_ty_adjusted(slice_pat)?;
@@ -1683,10 +1686,10 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
16831686
slice_pat_ty,
16841687
ProjectionKind::Subslice,
16851688
);
1686-
self.cat_pattern_(slice_place, slice_pat, op)?;
1689+
self.cat_pattern(slice_place, slice_pat, op)?;
16871690
}
16881691
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)?;
16901693
}
16911694
}
16921695

0 commit comments

Comments
 (0)