@@ -130,7 +130,14 @@ enum AdjustMode {
130
130
/// Peel off all immediate reference types.
131
131
Peel ,
132
132
/// Reset binding mode to the initial mode.
133
+ /// Used for destructuring assignment, where we don't want any match ergonomics.
133
134
Reset ,
135
+ /// Produced by ref patterns.
136
+ /// Reset the binding mode to the initial mode,
137
+ /// and if the old biding mode was by-reference
138
+ /// with mutability matching the pattern,
139
+ /// mark the pattern as having consumed this reference.
140
+ ResetAndConsumeRef ( Mutability ) ,
134
141
/// Pass on the input binding mode and expected type.
135
142
Pass ,
136
143
}
@@ -174,7 +181,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
174
181
_ => None ,
175
182
} ;
176
183
let adjust_mode = self . calc_adjust_mode ( pat, path_res. map ( |( res, ..) | res) ) ;
177
- let ( expected, def_bm) = self . calc_default_binding_mode ( pat, expected, def_bm, adjust_mode) ;
184
+ let ( expected, def_bm, ref_pattern_already_consumed) =
185
+ self . calc_default_binding_mode ( pat, expected, def_bm, adjust_mode) ;
178
186
let pat_info = PatInfo {
179
187
binding_mode : def_bm,
180
188
top_info : ti,
@@ -211,7 +219,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
211
219
}
212
220
PatKind :: Box ( inner) => self . check_pat_box ( pat. span , inner, expected, pat_info) ,
213
221
PatKind :: Deref ( inner) => self . check_pat_deref ( pat. span , inner, expected, pat_info) ,
214
- PatKind :: Ref ( inner, mutbl) => self . check_pat_ref ( pat, inner, mutbl, expected, pat_info) ,
222
+ PatKind :: Ref ( inner, mutbl) => self . check_pat_ref (
223
+ pat,
224
+ inner,
225
+ mutbl,
226
+ expected,
227
+ pat_info,
228
+ ref_pattern_already_consumed,
229
+ ) ,
215
230
PatKind :: Slice ( before, slice, after) => {
216
231
self . check_pat_slice ( pat. span , before, slice, after, expected, pat_info)
217
232
}
@@ -264,17 +279,27 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
264
279
265
280
/// Compute the new expected type and default binding mode from the old ones
266
281
/// as well as the pattern form we are currently checking.
282
+ ///
283
+ /// Last entry is only relevant for ref patterns (`&` and `&mut`);
284
+ /// if `true`, then the ref pattern consumed a match ergonomics inserted reference
285
+ /// and so does no need to match against a reference in the scrutinee type.
267
286
fn calc_default_binding_mode (
268
287
& self ,
269
288
pat : & ' tcx Pat < ' tcx > ,
270
289
expected : Ty < ' tcx > ,
271
290
def_bm : BindingAnnotation ,
272
291
adjust_mode : AdjustMode ,
273
- ) -> ( Ty < ' tcx > , BindingAnnotation ) {
292
+ ) -> ( Ty < ' tcx > , BindingAnnotation , bool ) {
274
293
match adjust_mode {
275
- AdjustMode :: Pass => ( expected, def_bm) ,
276
- AdjustMode :: Reset => ( expected, INITIAL_BM ) ,
277
- AdjustMode :: Peel => self . peel_off_references ( pat, expected, def_bm) ,
294
+ AdjustMode :: Pass => ( expected, def_bm, false ) ,
295
+ AdjustMode :: Reset => ( expected, INITIAL_BM , false ) ,
296
+ AdjustMode :: ResetAndConsumeRef ( mutbl) => {
297
+ ( expected, INITIAL_BM , def_bm. 0 == ByRef :: Yes ( mutbl) )
298
+ }
299
+ AdjustMode :: Peel => {
300
+ let peeled = self . peel_off_references ( pat, expected, def_bm) ;
301
+ ( peeled. 0 , peeled. 1 , false )
302
+ }
278
303
}
279
304
}
280
305
@@ -329,7 +354,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
329
354
// ```
330
355
//
331
356
// See issue #46688.
332
- PatKind :: Ref ( .. ) => AdjustMode :: Reset ,
357
+ PatKind :: Ref ( _ , mutbl ) => AdjustMode :: ResetAndConsumeRef ( * mutbl ) ,
333
358
// A `_` pattern works with any expected type, so there's no need to do anything.
334
359
PatKind :: Wild
335
360
// A malformed pattern doesn't have an expected type, so let's just accept any type.
@@ -840,8 +865,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
840
865
&& let Some ( mt) = self . shallow_resolve ( expected) . builtin_deref ( true )
841
866
&& let ty:: Dynamic ( ..) = mt. ty . kind ( )
842
867
{
843
- // This is "x = SomeTrait" being reduced from
844
- // "let &x = &SomeTrait" or "let box x = Box<SomeTrait>", an error.
868
+ // This is "x = dyn SomeTrait" being reduced from
869
+ // "let &x = &dyn SomeTrait" or "let box x = Box<dyn SomeTrait>", an error.
845
870
let type_str = self . ty_to_string ( expected) ;
846
871
let mut err = struct_span_code_err ! (
847
872
self . dcx( ) ,
@@ -2036,6 +2061,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2036
2061
mutbl : Mutability ,
2037
2062
expected : Ty < ' tcx > ,
2038
2063
pat_info : PatInfo < ' tcx , ' _ > ,
2064
+ consumed_inherited_ref : bool ,
2039
2065
) -> Ty < ' tcx > {
2040
2066
let tcx = self . tcx ;
2041
2067
let expected = self . shallow_resolve ( expected) ;
@@ -2051,26 +2077,37 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2051
2077
match * expected. kind ( ) {
2052
2078
ty:: Ref ( _, r_ty, r_mutbl) if r_mutbl == mutbl => ( expected, r_ty) ,
2053
2079
_ => {
2054
- let inner_ty = self . next_ty_var ( TypeVariableOrigin {
2055
- kind : TypeVariableOriginKind :: TypeInference ,
2056
- span : inner. span ,
2057
- } ) ;
2058
- let ref_ty = self . new_ref_ty ( pat. span , mutbl, inner_ty) ;
2059
- debug ! ( "check_pat_ref: demanding {:?} = {:?}" , expected, ref_ty) ;
2060
- let err = self . demand_eqtype_pat_diag (
2061
- pat. span ,
2062
- expected,
2063
- ref_ty,
2064
- pat_info. top_info ,
2065
- ) ;
2080
+ if consumed_inherited_ref && self . tcx . features ( ) . ref_pat_everywhere {
2081
+ // We already matched against a match-ergonmics inserted reference,
2082
+ // so we don't need to match against a reference from the original type.
2083
+ // Save this infor for use in lowering later
2084
+ self . typeck_results
2085
+ . borrow_mut ( )
2086
+ . skipped_ref_pats_mut ( )
2087
+ . insert ( pat. hir_id ) ;
2088
+ ( expected, expected)
2089
+ } else {
2090
+ let inner_ty = self . next_ty_var ( TypeVariableOrigin {
2091
+ kind : TypeVariableOriginKind :: TypeInference ,
2092
+ span : inner. span ,
2093
+ } ) ;
2094
+ let ref_ty = self . new_ref_ty ( pat. span , mutbl, inner_ty) ;
2095
+ debug ! ( "check_pat_ref: demanding {:?} = {:?}" , expected, ref_ty) ;
2096
+ let err = self . demand_eqtype_pat_diag (
2097
+ pat. span ,
2098
+ expected,
2099
+ ref_ty,
2100
+ pat_info. top_info ,
2101
+ ) ;
2066
2102
2067
- // Look for a case like `fn foo(&foo: u32)` and suggest
2068
- // `fn foo(foo: &u32)`
2069
- if let Some ( mut err) = err {
2070
- self . borrow_pat_suggestion ( & mut err, pat) ;
2071
- err. emit ( ) ;
2103
+ // Look for a case like `fn foo(&foo: u32)` and suggest
2104
+ // `fn foo(foo: &u32)`
2105
+ if let Some ( mut err) = err {
2106
+ self . borrow_pat_suggestion ( & mut err, pat) ;
2107
+ err. emit ( ) ;
2108
+ }
2109
+ ( ref_ty, inner_ty)
2072
2110
}
2073
- ( ref_ty, inner_ty)
2074
2111
}
2075
2112
}
2076
2113
}
0 commit comments