Skip to content

Commit bd6ff06

Browse files
committed
lower implicit deref patterns to THIR
Since this uses `pat_adjustments`, I've also tweaked the documentation to mention implicit deref patterns and made sure the pattern migration diagnostic logic accounts for it. I'll adjust `ExprUseVisitor` in a later commit and add some tests there for closure capture inference.
1 parent 2828650 commit bd6ff06

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

compiler/rustc_middle/src/ty/typeck_results.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,25 @@ pub struct TypeckResults<'tcx> {
7777
/// to a form valid in all Editions, either as a lint diagnostic or hard error.
7878
rust_2024_migration_desugared_pats: ItemLocalMap<Rust2024IncompatiblePatInfo>,
7979

80-
/// Stores the types which were implicitly dereferenced in pattern binding modes
81-
/// for later usage in THIR lowering. For example,
80+
/// Stores the types which were implicitly dereferenced in pattern binding modes or deref
81+
/// patterns for later usage in THIR lowering. For example,
8282
///
8383
/// ```
8484
/// match &&Some(5i32) {
8585
/// Some(n) => {},
8686
/// _ => {},
8787
/// }
8888
/// ```
89-
/// leads to a `vec![&&Option<i32>, &Option<i32>]`. Empty vectors are not stored.
89+
/// leads to a `vec![&&Option<i32>, &Option<i32>]` and
90+
///
91+
/// ```
92+
/// #![feature(deref_patterns)]
93+
/// match &Box::new(Some(5i32)) {
94+
/// Some(n) => {},
95+
/// _ => {},
96+
/// }
97+
/// ```
98+
/// leads to a `vec![&Box<Option<i32>>, Box<Option<i32>>]`. Empty vectors are not stored.
9099
///
91100
/// See:
92101
/// <https://github.com/rust-lang/rfcs/blob/master/text/2005-match-ergonomics.md#definitions>

compiler/rustc_mir_build/src/thir/pattern/migration.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use rustc_data_structures::fx::FxIndexMap;
44
use rustc_errors::MultiSpan;
55
use rustc_hir::{BindingMode, ByRef, HirId, Mutability};
66
use rustc_lint as lint;
7-
use rustc_middle::span_bug;
87
use rustc_middle::ty::{self, Rust2024IncompatiblePatInfo, Ty, TyCtxt};
98
use rustc_span::{Ident, Span};
109

@@ -95,11 +94,8 @@ impl<'a> PatMigration<'a> {
9594
pat_span: Span,
9695
adjustments: &[Ty<'tcx>],
9796
) -> Option<(Span, Mutability)> {
98-
let implicit_deref_mutbls = adjustments.iter().map(|ref_ty| {
99-
let &ty::Ref(_, _, mutbl) = ref_ty.kind() else {
100-
span_bug!(pat_span, "pattern implicitly dereferences a non-ref type");
101-
};
102-
mutbl
97+
let implicit_deref_mutbls = adjustments.iter().filter_map(|ref_ty| {
98+
if let &ty::Ref(_, _, mutbl) = ref_ty.kind() { Some(mutbl) } else { None }
10399
});
104100

105101
if !self.info.suggest_eliding_modes {

compiler/rustc_mir_build/src/thir/pattern/mod.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
6666
self.typeck_results.pat_adjustments().get(pat.hir_id).map_or(&[], |v| &**v);
6767

6868
// Track the default binding mode for the Rust 2024 migration suggestion.
69+
// Implicitly dereferencing references changes the default binding mode, but implicit deref
70+
// patterns do not. Only track binding mode changes if a ref type is in the adjustments.
6971
let mut opt_old_mode_span = None;
7072
if let Some(s) = &mut self.rust_2024_migration
71-
&& !adjustments.is_empty()
73+
&& adjustments.iter().any(|ty| ty.is_ref())
7274
{
7375
opt_old_mode_span = s.visit_implicit_derefs(pat.span, adjustments);
7476
}
@@ -103,15 +105,20 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
103105

104106
let adjusted_pat = adjustments.iter().rev().fold(unadjusted_pat, |thir_pat, ref_ty| {
105107
debug!("{:?}: wrapping pattern with type {:?}", thir_pat, ref_ty);
106-
Box::new(Pat {
107-
span: thir_pat.span,
108-
ty: *ref_ty,
109-
kind: PatKind::Deref { subpattern: thir_pat },
110-
})
108+
let span = thir_pat.span;
109+
// TODO: use an enum to distinguish between builtin and overloaded derefs
110+
let kind = if ref_ty.is_ref() {
111+
PatKind::Deref { subpattern: thir_pat }
112+
} else {
113+
let mutable = self.typeck_results.pat_has_ref_mut_binding(pat);
114+
let mutability = if mutable { hir::Mutability::Mut } else { hir::Mutability::Not };
115+
PatKind::DerefPattern { subpattern: thir_pat, mutability }
116+
};
117+
Box::new(Pat { span, ty: *ref_ty, kind })
111118
});
112119

113120
if let Some(s) = &mut self.rust_2024_migration
114-
&& !adjustments.is_empty()
121+
&& adjustments.iter().any(|ty| ty.is_ref())
115122
{
116123
s.leave_ref(opt_old_mode_span);
117124
}

0 commit comments

Comments
 (0)