@@ -45,7 +45,8 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
45
45
match projection {
46
46
ProjectionElem :: Deref => {
47
47
if base_place. base == PlaceBase :: Local ( Local :: new ( 1 ) )
48
- && !self . mir . upvar_decls . is_empty ( ) {
48
+ && !self . mir . upvar_decls . is_empty ( )
49
+ {
49
50
item_msg = format ! ( "`{}`" , access_place_desc. unwrap( ) ) ;
50
51
debug_assert ! ( self . mir. local_decls[ Local :: new( 1 ) ] . ty. is_region_ptr( ) ) ;
51
52
debug_assert ! ( is_closure_or_generator(
@@ -59,8 +60,9 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
59
60
}
60
61
} else if {
61
62
if let PlaceBase :: Local ( local) = base_place. base {
62
- if let Some ( ClearCrossCrate :: Set ( BindingForm :: RefForGuard ) )
63
- = self . mir . local_decls [ local] . is_user_variable {
63
+ if let Some ( ClearCrossCrate :: Set ( BindingForm :: RefForGuard ) ) =
64
+ self . mir . local_decls [ local] . is_user_variable
65
+ {
64
66
true
65
67
} else {
66
68
false
@@ -72,18 +74,21 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
72
74
item_msg = format ! ( "`{}`" , access_place_desc. unwrap( ) ) ;
73
75
reason = ", as it is immutable for the pattern guard" . to_string ( ) ;
74
76
} else {
75
- let pointer_type =
76
- if base_place. ty ( self . mir , self . tcx ) . to_ty ( self . tcx ) . is_region_ptr ( ) {
77
- "`&` reference"
78
- } else {
79
- "`*const` pointer"
80
- } ;
77
+ let pointer_type = if base_place
78
+ . ty ( self . mir , self . tcx )
79
+ . to_ty ( self . tcx )
80
+ . is_region_ptr ( )
81
+ {
82
+ "`&` reference"
83
+ } else {
84
+ "`*const` pointer"
85
+ } ;
81
86
if let Some ( desc) = access_place_desc {
82
87
item_msg = format ! ( "`{}`" , desc) ;
83
88
reason = match error_access {
84
- AccessKind :: Move |
85
- AccessKind :: Mutate =>
86
- format ! ( " which is behind a {}" , pointer_type ) ,
89
+ AccessKind :: Move | AccessKind :: Mutate => {
90
+ format ! ( " which is behind a {}" , pointer_type )
91
+ }
87
92
AccessKind :: MutableBorrow => {
88
93
format ! ( ", as it is behind a {}" , pointer_type)
89
94
}
@@ -93,7 +98,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
93
98
reason = "" . to_string ( ) ;
94
99
}
95
100
}
96
- } ,
101
+ }
97
102
ProjectionElem :: Field ( upvar_index, _) => {
98
103
debug_assert ! ( is_closure_or_generator(
99
104
base_place. ty( self . mir, self . tcx) . to_ty( self . tcx)
@@ -110,9 +115,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
110
115
ProjectionElem :: Subslice { .. }
111
116
| ProjectionElem :: Downcast ( ..)
112
117
| ProjectionElem :: ConstantIndex { .. }
113
- | ProjectionElem :: Index ( _) => {
114
- bug ! ( "Unexpected immutable place." )
115
- }
118
+ | ProjectionElem :: Index ( _) => bug ! ( "Unexpected immutable place." ) ,
116
119
}
117
120
} else {
118
121
match the_place_err. base {
@@ -129,10 +132,8 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
129
132
}
130
133
PlaceBase :: Static ( box Static { def_id, ty : _ } ) => {
131
134
if let PlaceBase :: Static ( _) = access_place. base {
132
- item_msg = format ! (
133
- "immutable static item `{}`" ,
134
- access_place_desc. unwrap( )
135
- ) ;
135
+ item_msg =
136
+ format ! ( "immutable static item `{}`" , access_place_desc. unwrap( ) ) ;
136
137
reason = "" . to_string ( ) ;
137
138
} else {
138
139
item_msg = format ! ( "`{}`" , access_place_desc. unwrap( ) ) ;
@@ -151,14 +152,16 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
151
152
152
153
let span = match error_access {
153
154
AccessKind :: Move => {
154
- err = self . tcx
155
+ err = self
156
+ . tcx
155
157
. cannot_move_out_of ( span, & ( item_msg + & reason) , Origin :: Mir ) ;
156
158
act = "move" ;
157
159
acted_on = "moved" ;
158
160
span
159
161
}
160
162
AccessKind :: Mutate => {
161
- err = self . tcx
163
+ err = self
164
+ . tcx
162
165
. cannot_assign ( span, & ( item_msg + & reason) , Origin :: Mir ) ;
163
166
act = "assign" ;
164
167
acted_on = "written" ;
@@ -182,7 +185,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
182
185
"mutable borrow occurs due to use of `{}` in closure" ,
183
186
// always Some() if the message is printed.
184
187
self . describe_place( access_place) . unwrap_or( String :: new( ) ) ,
185
- )
188
+ ) ,
186
189
) ;
187
190
borrow_span
188
191
}
@@ -197,53 +200,52 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
197
200
err. span_label ( span, format ! ( "cannot {ACT}" , ACT = act) ) ;
198
201
err. note (
199
202
"variables bound in patterns are \
200
- immutable until the end of the pattern guard",
203
+ immutable until the end of the pattern guard",
201
204
) ;
202
- } ,
205
+ }
203
206
Some ( _) => {
204
207
// We want to point out when a `&` can be readily replaced
205
208
// with an `&mut`.
206
209
//
207
210
// FIXME: can this case be generalized to work for an
208
211
// arbitrary base for the projection?
209
212
let local_decl = & self . mir . local_decls [ local] ;
210
- let suggestion =
211
- match local_decl . is_user_variable . as_ref ( ) . unwrap ( ) {
213
+ let suggestion = match local_decl . is_user_variable . as_ref ( ) . unwrap ( )
214
+ {
212
215
ClearCrossCrate :: Set ( mir:: BindingForm :: ImplicitSelf ) => {
213
216
Some ( suggest_ampmut_self ( self . tcx , local_decl) )
214
217
}
215
218
216
- ClearCrossCrate :: Set (
217
- mir:: BindingForm :: Var (
218
- mir :: VarBindingForm {
219
- binding_mode : ty :: BindingMode :: BindByValue ( _ ) ,
220
- opt_ty_info ,
221
- ..
222
- } ) ) => Some ( suggest_ampmut (
219
+ ClearCrossCrate :: Set ( mir :: BindingForm :: Var (
220
+ mir:: VarBindingForm {
221
+ binding_mode : ty :: BindingMode :: BindByValue ( _ ) ,
222
+ opt_ty_info ,
223
+ ..
224
+ } ,
225
+ ) ) => Some ( suggest_ampmut (
223
226
self . tcx ,
224
227
self . mir ,
225
228
local,
226
229
local_decl,
227
230
* opt_ty_info,
228
231
) ) ,
229
232
230
- ClearCrossCrate :: Set (
231
- mir:: BindingForm :: Var (
232
- mir:: VarBindingForm {
233
- binding_mode : ty:: BindingMode :: BindByReference ( _) ,
234
- ..
235
- } )
236
- ) => suggest_ref_mut ( self . tcx , local_decl. source_info . span ) ,
233
+ ClearCrossCrate :: Set ( mir:: BindingForm :: Var (
234
+ mir:: VarBindingForm {
235
+ binding_mode : ty:: BindingMode :: BindByReference ( _) ,
236
+ ..
237
+ } ,
238
+ ) ) => suggest_ref_mut ( self . tcx , local_decl. source_info . span ) ,
237
239
238
- ClearCrossCrate :: Set (
239
- mir :: BindingForm :: RefForGuard
240
- ) => unreachable ! ( ) ,
240
+ ClearCrossCrate :: Set ( mir :: BindingForm :: RefForGuard ) => {
241
+ unreachable ! ( )
242
+ }
241
243
242
244
ClearCrossCrate :: Clear => bug ! ( "saw cleared local state" ) ,
243
245
} ;
244
246
245
- let ( pointer_sigil, pointer_desc) =
246
- if local_decl . ty . is_region_ptr ( ) {
247
+ let ( pointer_sigil, pointer_desc) = if local_decl . ty . is_region_ptr ( )
248
+ {
247
249
( "&" , "reference" )
248
250
} else {
249
251
( "*const" , "pointer" )
@@ -265,7 +267,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
265
267
span,
266
268
format ! (
267
269
"`{NAME}` is a `{SIGIL}` {DESC}, \
268
- so the data it refers to cannot be {ACTED_ON}",
270
+ so the data it refers to cannot be {ACTED_ON}",
269
271
NAME = name,
270
272
SIGIL = pointer_sigil,
271
273
DESC = pointer_desc,
@@ -284,17 +286,17 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
284
286
) ;
285
287
}
286
288
}
287
- _ => { } ,
289
+ _ => { }
288
290
}
289
291
if local == Local :: new ( 1 ) && !self . mir . upvar_decls . is_empty ( ) {
290
292
err. span_label ( span, format ! ( "cannot {ACT}" , ACT = act) ) ;
291
293
err. span_help (
292
294
self . mir . span ,
293
- "consider changing this to accept closures that implement `FnMut`"
295
+ "consider changing this to accept closures that implement `FnMut`" ,
294
296
) ;
295
297
}
296
298
}
297
- } ,
299
+ }
298
300
ProjectionElem :: Field ( upvar_index, _) => {
299
301
// Suggest adding mut for upvars
300
302
debug_assert ! ( is_closure_or_generator(
@@ -314,16 +316,16 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
314
316
upvar_ident,
315
317
_,
316
318
) = pat. node
317
- {
318
- err. span_suggestion (
319
- upvar_ident. span ,
320
- "consider changing this to be mutable" ,
321
- format ! ( "mut {}" , upvar_ident. name) ,
322
- ) ;
323
- }
319
+ {
320
+ err. span_suggestion (
321
+ upvar_ident. span ,
322
+ "consider changing this to be mutable" ,
323
+ format ! ( "mut {}" , upvar_ident. name) ,
324
+ ) ;
325
+ }
324
326
}
325
327
}
326
- _ => { } ,
328
+ _ => { }
327
329
}
328
330
} else if let PlaceBase :: Local ( local) = the_place_err. base {
329
331
if self . mir . local_decls [ local] . can_be_made_mutable ( ) {
@@ -367,10 +369,9 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
367
369
is_closure_or_generator ( base_ty)
368
370
}
369
371
ProjectionElem :: Deref => {
370
- if let (
371
- ref base_place,
372
- Some ( ProjectionElem :: Field ( upvar_index, _) ) ,
373
- ) = base_place. split_projection ( self . tcx ) {
372
+ if let ( ref base_place, Some ( ProjectionElem :: Field ( upvar_index, _) ) ) =
373
+ base_place. split_projection ( self . tcx )
374
+ {
374
375
let base_ty = base_place. ty ( self . mir , self . tcx ) . to_ty ( self . tcx ) ;
375
376
is_closure_or_generator ( base_ty)
376
377
&& self . mir . upvar_decls [ upvar_index. index ( ) ] . by_ref
@@ -391,17 +392,20 @@ fn suggest_ampmut_self<'cx, 'gcx, 'tcx>(
391
392
local_decl : & mir:: LocalDecl < ' tcx > ,
392
393
) -> ( Span , String ) {
393
394
let sp = local_decl. source_info . span ;
394
- ( sp, match tcx. sess . codemap ( ) . span_to_snippet ( sp) {
395
- Ok ( snippet) => {
396
- let lt_pos = snippet. find ( '\'' ) ;
397
- if let Some ( lt_pos) = lt_pos {
398
- format ! ( "&{}mut self" , & snippet[ lt_pos..snippet. len( ) - 4 ] )
399
- } else {
400
- "&mut self" . to_string ( )
395
+ (
396
+ sp,
397
+ match tcx. sess . codemap ( ) . span_to_snippet ( sp) {
398
+ Ok ( snippet) => {
399
+ let lt_pos = snippet. find ( '\'' ) ;
400
+ if let Some ( lt_pos) = lt_pos {
401
+ format ! ( "&{}mut self" , & snippet[ lt_pos..snippet. len( ) - 4 ] )
402
+ } else {
403
+ "&mut self" . to_string ( )
404
+ }
401
405
}
402
- }
403
- _ => "&mut self" . to_string ( )
404
- } )
406
+ _ => "&mut self" . to_string ( ) ,
407
+ } ,
408
+ )
405
409
}
406
410
407
411
// When we want to suggest a user change a local variable to be a `&mut`, there
@@ -467,12 +471,14 @@ fn suggest_ampmut<'cx, 'gcx, 'tcx>(
467
471
468
472
let ty_mut = local_decl. ty . builtin_deref ( true ) . unwrap ( ) ;
469
473
assert_eq ! ( ty_mut. mutbl, hir:: MutImmutable ) ;
470
- ( highlight_span,
471
- if local_decl. ty . is_region_ptr ( ) {
472
- format ! ( "&mut {}" , ty_mut. ty)
473
- } else {
474
- format ! ( "*mut {}" , ty_mut. ty)
475
- } )
474
+ (
475
+ highlight_span,
476
+ if local_decl. ty . is_region_ptr ( ) {
477
+ format ! ( "&mut {}" , ty_mut. ty)
478
+ } else {
479
+ format ! ( "*mut {}" , ty_mut. ty)
480
+ } ,
481
+ )
476
482
}
477
483
478
484
fn is_closure_or_generator ( ty : ty:: Ty ) -> bool {
0 commit comments