|
2 | 2 |
|
3 | 3 | use rustc_data_structures::fx::FxIndexMap;
|
4 | 4 | use rustc_errors::MultiSpan;
|
5 |
| -use rustc_hir::HirId; |
| 5 | +use rustc_hir::{BindingMode, ByRef, HirId, Mutability}; |
6 | 6 | use rustc_lint as lint;
|
7 |
| -use rustc_middle::ty::{self, Rust2024IncompatiblePatInfo, TyCtxt}; |
8 |
| -use rustc_span::Span; |
| 7 | +use rustc_middle::span_bug; |
| 8 | +use rustc_middle::ty::{self, Rust2024IncompatiblePatInfo, Ty, TyCtxt}; |
| 9 | +use rustc_span::{Ident, Span}; |
9 | 10 |
|
10 | 11 | use crate::errors::{Rust2024IncompatiblePat, Rust2024IncompatiblePatSugg};
|
11 | 12 | use crate::fluent_generated as fluent;
|
12 | 13 |
|
13 | 14 | /// For patterns flagged for migration during HIR typeck, this handles constructing and emitting
|
14 | 15 | /// a diagnostic suggestion.
|
15 | 16 | pub(super) struct PatMigration<'a> {
|
16 |
| - pub(super) suggestion: Vec<(Span, String)>, |
17 |
| - pub(super) ref_pattern_count: usize, |
18 |
| - pub(super) binding_mode_count: usize, |
| 17 | + suggestion: Vec<(Span, String)>, |
| 18 | + ref_pattern_count: usize, |
| 19 | + binding_mode_count: usize, |
19 | 20 | /// Internal state: the ref-mutability of the default binding mode at the subpattern being
|
20 | 21 | /// lowered, with the span where it was introduced. `None` for a by-value default mode.
|
21 |
| - pub(super) default_mode_span: Option<(Span, ty::Mutability)>, |
| 22 | + default_mode_span: Option<(Span, ty::Mutability)>, |
22 | 23 | /// Labels for where incompatibility-causing by-ref default binding modes were introduced.
|
23 | 24 | // FIXME(ref_pat_eat_one_layer_2024_structural): To track the default binding mode, we duplicate
|
24 | 25 | // logic from HIR typeck (in order to avoid needing to store all changes to the dbm in
|
25 | 26 | // TypeckResults). Since the default binding mode acts differently under this feature gate, the
|
26 | 27 | // labels will be wrong.
|
27 |
| - pub(super) default_mode_labels: FxIndexMap<Span, Mutability>, |
| 28 | + default_mode_labels: FxIndexMap<Span, Mutability>, |
28 | 29 | /// Information collected from typeck, including spans for subpatterns invalid in Rust 2024.
|
29 |
| - pub(super) info: &'a Rust2024IncompatiblePatInfo, |
| 30 | + info: &'a Rust2024IncompatiblePatInfo, |
30 | 31 | }
|
31 | 32 |
|
32 | 33 | impl<'a> PatMigration<'a> {
|
@@ -84,4 +85,98 @@ impl<'a> PatMigration<'a> {
|
84 | 85 | );
|
85 | 86 | }
|
86 | 87 | }
|
| 88 | + |
| 89 | + /// Tracks when we're lowering a pattern that implicitly dereferences the scrutinee. |
| 90 | + /// This should only be called when the pattern type adjustments list `adjustments` is |
| 91 | + /// non-empty. Returns the prior default binding mode; this should be followed by a call to |
| 92 | + /// [`PatMigration::leave_ref`] to restore it when we leave the pattern. |
| 93 | + pub(super) fn visit_implicit_derefs<'tcx>( |
| 94 | + &mut self, |
| 95 | + pat_span: Span, |
| 96 | + adjustments: &[Ty<'tcx>], |
| 97 | + ) -> 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 |
| 103 | + }); |
| 104 | + |
| 105 | + if !self.info.suggest_eliding_modes { |
| 106 | + // If we can't fix the pattern by eliding modifiers, we'll need to make the pattern |
| 107 | + // fully explicit. i.e. we'll need to suggest reference patterns for this. |
| 108 | + let suggestion_str: String = |
| 109 | + implicit_deref_mutbls.clone().map(|mutbl| mutbl.ref_prefix_str()).collect(); |
| 110 | + self.suggestion.push((pat_span.shrink_to_lo(), suggestion_str)); |
| 111 | + self.ref_pattern_count += adjustments.len(); |
| 112 | + } |
| 113 | + |
| 114 | + // Remember if this changed the default binding mode, in case we want to label it. |
| 115 | + let min_mutbl = implicit_deref_mutbls.min().unwrap(); |
| 116 | + if self.default_mode_span.is_none_or(|(_, old_mutbl)| min_mutbl < old_mutbl) { |
| 117 | + // This changes the default binding mode to `ref` or `ref mut`. Return the old mode so |
| 118 | + // it can be reinstated when we leave the pattern. |
| 119 | + self.default_mode_span.replace((pat_span, min_mutbl)) |
| 120 | + } else { |
| 121 | + // This does not change the default binding mode; it was already `ref` or `ref mut`. |
| 122 | + self.default_mode_span |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /// Tracks the default binding mode when we're lowering a `&` or `&mut` pattern. |
| 127 | + /// Returns the prior default binding mode; this should be followed by a call to |
| 128 | + /// [`PatMigration::leave_ref`] to restore it when we leave the pattern. |
| 129 | + pub(super) fn visit_explicit_deref(&mut self) -> Option<(Span, Mutability)> { |
| 130 | + if let Some((default_mode_span, default_ref_mutbl)) = self.default_mode_span { |
| 131 | + // If this eats a by-ref default binding mode, label the binding mode. |
| 132 | + self.default_mode_labels.insert(default_mode_span, default_ref_mutbl); |
| 133 | + } |
| 134 | + // Set the default binding mode to by-value and return the old default binding mode so it |
| 135 | + // can be reinstated when we leave the pattern. |
| 136 | + self.default_mode_span.take() |
| 137 | + } |
| 138 | + |
| 139 | + /// Restores the default binding mode after lowering a pattern that could change it. |
| 140 | + /// This should follow a call to either [`PatMigration::visit_explicit_deref`] or |
| 141 | + /// [`PatMigration::visit_implicit_derefs`]. |
| 142 | + pub(super) fn leave_ref(&mut self, old_mode_span: Option<(Span, Mutability)>) { |
| 143 | + self.default_mode_span = old_mode_span |
| 144 | + } |
| 145 | + |
| 146 | + /// Determines if a binding is relevant to the diagnostic and adjusts the notes/suggestion if |
| 147 | + /// so. Bindings are relevant if they have a modifier under a by-ref default mode (invalid in |
| 148 | + /// Rust 2024) or if we need to suggest a binding modifier for them. |
| 149 | + pub(super) fn visit_binding( |
| 150 | + &mut self, |
| 151 | + pat_span: Span, |
| 152 | + mode: BindingMode, |
| 153 | + explicit_ba: BindingMode, |
| 154 | + ident: Ident, |
| 155 | + ) { |
| 156 | + if explicit_ba != BindingMode::NONE |
| 157 | + && let Some((default_mode_span, default_ref_mutbl)) = self.default_mode_span |
| 158 | + { |
| 159 | + // If this overrides a by-ref default binding mode, label the binding mode. |
| 160 | + self.default_mode_labels.insert(default_mode_span, default_ref_mutbl); |
| 161 | + // If our suggestion is to elide redundnt modes, this will be one of them. |
| 162 | + if self.info.suggest_eliding_modes { |
| 163 | + self.suggestion.push((pat_span.with_hi(ident.span.lo()), String::new())); |
| 164 | + self.binding_mode_count += 1; |
| 165 | + } |
| 166 | + } |
| 167 | + if !self.info.suggest_eliding_modes |
| 168 | + && explicit_ba.0 == ByRef::No |
| 169 | + && let ByRef::Yes(mutbl) = mode.0 |
| 170 | + { |
| 171 | + // If we can't fix the pattern by eliding modifiers, we'll need to make the pattern |
| 172 | + // fully explicit. i.e. we'll need to suggest reference patterns for this. |
| 173 | + let sugg_str = match mutbl { |
| 174 | + Mutability::Not => "ref ", |
| 175 | + Mutability::Mut => "ref mut ", |
| 176 | + }; |
| 177 | + self.suggestion |
| 178 | + .push((pat_span.with_lo(ident.span.lo()).shrink_to_lo(), sugg_str.to_owned())); |
| 179 | + self.binding_mode_count += 1; |
| 180 | + } |
| 181 | + } |
87 | 182 | }
|
0 commit comments