@@ -131,6 +131,12 @@ enum AdjustMode {
131
131
Peel ,
132
132
/// Reset binding mode to the initial mode.
133
133
Reset ,
134
+ /// Produced by ref patterns.
135
+ /// Reset the binding mode to the initial mode,
136
+ /// and if the old biding mode was by-reference
137
+ /// with mutability matching the pattern,
138
+ /// mark the pattern as having consumed this reference.
139
+ RefReset ( Mutability ) ,
134
140
/// Pass on the input binding mode and expected type.
135
141
Pass ,
136
142
}
@@ -174,7 +180,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
174
180
_ => None ,
175
181
} ;
176
182
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) ;
183
+ let ( expected, def_bm, ref_pattern_already_consumed) =
184
+ self . calc_default_binding_mode ( pat, expected, def_bm, adjust_mode) ;
178
185
let pat_info = PatInfo {
179
186
binding_mode : def_bm,
180
187
top_info : ti,
@@ -211,7 +218,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
211
218
}
212
219
PatKind :: Box ( inner) => self . check_pat_box ( pat. span , inner, expected, pat_info) ,
213
220
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) ,
221
+ PatKind :: Ref ( inner, mutbl) => self . check_pat_ref (
222
+ pat,
223
+ inner,
224
+ mutbl,
225
+ expected,
226
+ pat_info,
227
+ ref_pattern_already_consumed,
228
+ ) ,
215
229
PatKind :: Slice ( before, slice, after) => {
216
230
self . check_pat_slice ( pat. span , before, slice, after, expected, pat_info)
217
231
}
@@ -264,17 +278,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
264
278
265
279
/// Compute the new expected type and default binding mode from the old ones
266
280
/// as well as the pattern form we are currently checking.
281
+ ///
282
+ /// Last entry is only relevant for ref patterns (`&` and `&mut`);
283
+ /// if `true`, then the ref pattern consumed a match ergonomics inserted reference
284
+ /// and so does no need to match against a reference in the scrutinee type.
267
285
fn calc_default_binding_mode (
268
286
& self ,
269
287
pat : & ' tcx Pat < ' tcx > ,
270
288
expected : Ty < ' tcx > ,
271
289
def_bm : BindingAnnotation ,
272
290
adjust_mode : AdjustMode ,
273
- ) -> ( Ty < ' tcx > , BindingAnnotation ) {
291
+ ) -> ( Ty < ' tcx > , BindingAnnotation , bool ) {
274
292
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) ,
293
+ AdjustMode :: Pass => ( expected, def_bm, false ) ,
294
+ AdjustMode :: Reset => ( expected, INITIAL_BM , false ) ,
295
+ AdjustMode :: RefReset ( mutbl) => ( expected, INITIAL_BM , def_bm. 0 == ByRef :: Yes ( mutbl) ) ,
296
+ AdjustMode :: Peel => {
297
+ let peeled = self . peel_off_references ( pat, expected, def_bm) ;
298
+ ( peeled. 0 , peeled. 1 , false )
299
+ }
278
300
}
279
301
}
280
302
@@ -329,7 +351,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
329
351
// ```
330
352
//
331
353
// See issue #46688.
332
- PatKind :: Ref ( .. ) => AdjustMode :: Reset ,
354
+ PatKind :: Ref ( _ , mutbl ) => AdjustMode :: RefReset ( * mutbl ) ,
333
355
// A `_` pattern works with any expected type, so there's no need to do anything.
334
356
PatKind :: Wild
335
357
// A malformed pattern doesn't have an expected type, so let's just accept any type.
@@ -853,8 +875,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
853
875
&& let Some ( mt) = self . shallow_resolve ( expected) . builtin_deref ( true )
854
876
&& let ty:: Dynamic ( ..) = mt. ty . kind ( )
855
877
{
856
- // This is "x = SomeTrait" being reduced from
857
- // "let &x = &SomeTrait" or "let box x = Box<SomeTrait>", an error.
878
+ // This is "x = dyn SomeTrait" being reduced from
879
+ // "let &x = &dyn SomeTrait" or "let box x = Box<dyn SomeTrait>", an error.
858
880
let type_str = self . ty_to_string ( expected) ;
859
881
let mut err = struct_span_code_err ! (
860
882
self . dcx( ) ,
@@ -2015,6 +2037,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2015
2037
mutbl : Mutability ,
2016
2038
expected : Ty < ' tcx > ,
2017
2039
pat_info : PatInfo < ' tcx , ' _ > ,
2040
+ already_consumed : bool ,
2018
2041
) -> Ty < ' tcx > {
2019
2042
let tcx = self . tcx ;
2020
2043
let expected = self . shallow_resolve ( expected) ;
@@ -2030,26 +2053,38 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
2030
2053
match * expected. kind ( ) {
2031
2054
ty:: Ref ( _, r_ty, r_mutbl) if r_mutbl == mutbl => ( expected, r_ty) ,
2032
2055
_ => {
2033
- let inner_ty = self . next_ty_var ( TypeVariableOrigin {
2034
- kind : TypeVariableOriginKind :: TypeInference ,
2035
- span : inner. span ,
2036
- } ) ;
2037
- let ref_ty = self . new_ref_ty ( pat. span , mutbl, inner_ty) ;
2038
- debug ! ( "check_pat_ref: demanding {:?} = {:?}" , expected, ref_ty) ;
2039
- let err = self . demand_eqtype_pat_diag (
2040
- pat. span ,
2041
- expected,
2042
- ref_ty,
2043
- pat_info. top_info ,
2044
- ) ;
2056
+ if already_consumed {
2057
+ // We already matched against a match-ergonmics inserted reference,
2058
+ // so we don't need to match against a reference from the original type.
2059
+ // Save this infor for use in lowering later
2060
+ self . inh
2061
+ . typeck_results
2062
+ . borrow_mut ( )
2063
+ . ref_pats_that_dont_deref_mut ( )
2064
+ . insert ( pat. hir_id ) ;
2065
+ ( expected, expected)
2066
+ } else {
2067
+ let inner_ty = self . next_ty_var ( TypeVariableOrigin {
2068
+ kind : TypeVariableOriginKind :: TypeInference ,
2069
+ span : inner. span ,
2070
+ } ) ;
2071
+ let ref_ty = self . new_ref_ty ( pat. span , mutbl, inner_ty) ;
2072
+ debug ! ( "check_pat_ref: demanding {:?} = {:?}" , expected, ref_ty) ;
2073
+ let err = self . demand_eqtype_pat_diag (
2074
+ pat. span ,
2075
+ expected,
2076
+ ref_ty,
2077
+ pat_info. top_info ,
2078
+ ) ;
2045
2079
2046
- // Look for a case like `fn foo(&foo: u32)` and suggest
2047
- // `fn foo(foo: &u32)`
2048
- if let Some ( mut err) = err {
2049
- self . borrow_pat_suggestion ( & mut err, pat) ;
2050
- err. emit ( ) ;
2080
+ // Look for a case like `fn foo(&foo: u32)` and suggest
2081
+ // `fn foo(foo: &u32)`
2082
+ if let Some ( mut err) = err {
2083
+ self . borrow_pat_suggestion ( & mut err, pat) ;
2084
+ err. emit ( ) ;
2085
+ }
2086
+ ( ref_ty, inner_ty)
2051
2087
}
2052
- ( ref_ty, inner_ty)
2053
2088
}
2054
2089
}
2055
2090
}
0 commit comments