Skip to content

Commit 737bcc2

Browse files
committed
apply comment/naming suggestions
1 parent 0bf256e commit 737bcc2

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

Diff for: compiler/rustc_hir_typeck/src/pat.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,19 @@ enum AdjustMode<'tcx> {
176176
/// Restrictions on what types to peel when adjusting the expected type and binding mode.
177177
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
178178
enum PeelKind<'tcx> {
179-
/// Only peel reference types. This is used for explicit deref patterns.
180-
RefOnly,
181-
/// If `deref_patterns` is enabled, this also peels library-defined smart pointer ADTs, except
182-
/// for `until_adt` if the pattern is a constructor. Otherwise this is the same as `RefOnly`.
183-
/// See [`ResolvedPat`] for more information.
184-
Overloaded { until_adt: Option<AdtDef<'tcx>> },
179+
/// Only peel reference types. This is used for explicit `deref!(_)` patterns, which dereference
180+
/// any number of `&`/`&mut` references, plus a single smart pointer.
181+
ExplicitDerefPat,
182+
/// Implicitly peel any number of references, and if `deref_patterns` is enabled, smart pointer
183+
/// ADTs. In order to peel only as much as necessary for the pattern to match, the `until_adt`
184+
/// field contains the ADT def that the pattern is a constructor for, if applicable, so that we
185+
/// don't peel it. See [`ResolvedPat`] for more information.
186+
Implicit { until_adt: Option<AdtDef<'tcx>> },
185187
}
186188

187189
impl<'tcx> AdjustMode<'tcx> {
188190
const fn peel_until_adt(opt_adt_def: Option<AdtDef<'tcx>>) -> AdjustMode<'tcx> {
189-
AdjustMode::Peel { kind: PeelKind::Overloaded { until_adt: opt_adt_def } }
191+
AdjustMode::Peel { kind: PeelKind::Implicit { until_adt: opt_adt_def } }
190192
}
191193
const fn peel_all() -> AdjustMode<'tcx> {
192194
AdjustMode::peel_until_adt(None)
@@ -585,7 +587,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
585587
// FIXME(deref_patterns): If box patterns and deref patterns need to coexist, box
586588
// patterns may want `PeelKind::Overloaded`, stopping on encountering a box.
587589
| PatKind::Box(_)
588-
| PatKind::Deref(_) => AdjustMode::Peel { kind: PeelKind::RefOnly },
590+
| PatKind::Deref(_) => AdjustMode::Peel { kind: PeelKind::ExplicitDerefPat },
589591
// A never pattern behaves somewhat like a literal or unit variant.
590592
PatKind::Never => AdjustMode::peel_all(),
591593
// For patterns with paths, how we peel the scrutinee depends on the path's resolution.
@@ -651,7 +653,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
651653
//
652654
// For each ampersand peeled off, update the binding mode and push the original
653655
// type into the adjustments vector.
654-
// When peeling a library-defined type, the default binding mode is not updated.
656+
// When peeling a smart pointer, the default binding mode is not updated.
655657
//
656658
// See the examples in `tests/ui/rfcs/rfc-2005-default-binding-mode/` and
657659
// `tests/ui/pattern/deref-patterns/`.
@@ -670,7 +672,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
670672
});
671673
inner_ty
672674
} else if deref_patterns
673-
&& let PeelKind::Overloaded { until_adt } = peel_kind
675+
&& let PeelKind::Implicit { until_adt } = peel_kind
674676
// For simplicity, only apply overloaded derefs if `expected` is a known ADT.
675677
// FIXME(deref_patterns): we'll get better diagnostics for users trying to
676678
// implicitly deref generics if we allow them here, but primitives, tuples, and

Diff for: compiler/rustc_mir_build/src/thir/pattern/migration.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,16 @@ impl<'a> PatMigration<'a> {
8686
}
8787

8888
/// Tracks when we're lowering a pattern that implicitly dereferences the scrutinee.
89-
/// This should only be called when the pattern type adjustments list `adjustments` is
90-
/// non-empty. Returns the prior default binding mode; this should be followed by a call to
91-
/// [`PatMigration::leave_ref`] to restore it when we leave the pattern.
89+
/// This should only be called when the pattern type adjustments list `adjustments` contains an
90+
/// implicit deref of a reference type. Returns the prior default binding mode; this should be
91+
/// followed by a call to [`PatMigration::leave_ref`] to restore it when we leave the pattern.
9292
pub(super) fn visit_implicit_derefs<'tcx>(
9393
&mut self,
9494
pat_span: Span,
9595
adjustments: &[Ty<'tcx>],
9696
) -> Option<(Span, Mutability)> {
97+
// Implicitly dereferencing references changes the default binding mode, but implicit derefs
98+
// of smart pointers do not. Thus, we only consider implicit derefs of reference types.
9799
let implicit_deref_mutbls = adjustments.iter().filter_map(|ref_ty| {
98100
if let &ty::Ref(_, _, mutbl) = ref_ty.kind() { Some(mutbl) } else { None }
99101
});

0 commit comments

Comments
 (0)