@@ -33,7 +33,7 @@ pub(in crate::build) enum PlaceBuilder<'tcx> {
33
33
/// Denotes the start of a `Place`.
34
34
///
35
35
/// We use `PlaceElem` since this has all `Field` types available.
36
- Local ( Local , Vec < PlaceElem < ' tcx > > ) ,
36
+ Local { local : Local , projection : Vec < PlaceElem < ' tcx > > } ,
37
37
38
38
/// When building place for an expression within a closure, the place might start off a
39
39
/// captured path. When `capture_disjoint_fields` is enabled, we might not know the capture
@@ -67,11 +67,11 @@ pub(in crate::build) enum PlaceBuilder<'tcx> {
67
67
///
68
68
/// Note: in contrast to `PlaceBuilder::Local` we have not yet determined all `Field` types
69
69
/// and will only do so once converting to `PlaceBuilder::Local`.
70
- UpVar ( UpVar , Vec < UpvarProjectionElem < ' tcx > > ) ,
70
+ Upvar { upvar : Upvar , projection : Vec < UpvarProjectionElem < ' tcx > > } ,
71
71
}
72
72
73
73
#[ derive( Copy , Clone , Debug , PartialEq ) ]
74
- pub ( crate ) struct UpVar {
74
+ pub ( crate ) struct Upvar {
75
75
var_hir_id : LocalVarId ,
76
76
closure_def_id : LocalDefId ,
77
77
}
@@ -222,36 +222,7 @@ fn to_upvars_resolved_place_builder<'tcx>(
222
222
upvar_projection,
223
223
) ;
224
224
225
- debug_assert ! ( {
226
- let builder = upvar_resolved_place_builder. clone( ) ;
227
- let mut valid_conversion = true ;
228
- match builder {
229
- PlaceBuilder :: Local ( _, projections) => {
230
- for proj in projections. iter( ) {
231
- match proj {
232
- ProjectionElem :: Field ( _, field_ty) => {
233
- if matches!( field_ty. kind( ) , ty:: Infer ( ..) ) {
234
- debug!(
235
- "field ty should have been converted for projection {:?} in PlaceBuilder {:?}" ,
236
- proj,
237
- upvar_resolved_place_builder. clone( )
238
- ) ;
239
-
240
- valid_conversion = false ;
241
- break ;
242
- }
243
- }
244
- _ => { }
245
- }
246
- }
247
- }
248
- PlaceBuilder :: UpVar ( ..) => {
249
- unreachable!( )
250
- }
251
- }
252
-
253
- valid_conversion
254
- } ) ;
225
+ assert ! ( matches!( upvar_resolved_place_builder, PlaceBuilder :: Local { .. } ) ) ;
255
226
256
227
Some ( upvar_resolved_place_builder)
257
228
}
@@ -269,9 +240,9 @@ fn strip_prefix<'a, 'tcx>(
269
240
) -> impl Iterator < Item = UpvarProjectionElem < ' tcx > > + ' a {
270
241
let mut iter = projections
271
242
. iter ( )
243
+ . copied ( )
272
244
// Filter out opaque casts, they are unnecessary in the prefix.
273
- . filter ( |elem| !matches ! ( elem, ProjectionElem :: OpaqueCast ( ..) ) )
274
- . map ( |elem| * elem) ;
245
+ . filter ( |elem| !matches ! ( elem, ProjectionElem :: OpaqueCast ( ..) ) ) ;
275
246
for projection in prefix_projections {
276
247
debug ! ( ?projection, ?projection. ty) ;
277
248
@@ -305,8 +276,8 @@ impl<'tcx> PlaceBuilder<'tcx> {
305
276
pub ( in crate :: build) fn try_to_place ( & self , cx : & Builder < ' _ , ' tcx > ) -> Option < Place < ' tcx > > {
306
277
let resolved = self . resolve_upvar ( cx) ;
307
278
let builder = resolved. as_ref ( ) . unwrap_or ( self ) ;
308
- let PlaceBuilder :: Local ( local, projection) = builder else { return None } ;
309
- let projection = cx. tcx . intern_place_elems ( & projection) ;
279
+ let PlaceBuilder :: Local { local, ref projection} = builder else { return None } ;
280
+ let projection = cx. tcx . intern_place_elems ( projection) ;
310
281
Some ( Place { local : * local, projection } )
311
282
}
312
283
@@ -324,40 +295,31 @@ impl<'tcx> PlaceBuilder<'tcx> {
324
295
& self ,
325
296
cx : & Builder < ' _ , ' tcx > ,
326
297
) -> Option < PlaceBuilder < ' tcx > > {
327
- let PlaceBuilder :: UpVar ( UpVar { var_hir_id, closure_def_id } , projection) = self else {
298
+ let PlaceBuilder :: Upvar { upvar : Upvar { var_hir_id, closure_def_id } , projection} = self else {
328
299
return None ;
329
300
} ;
330
301
331
302
to_upvars_resolved_place_builder ( cx, * var_hir_id, * closure_def_id, & projection)
332
303
}
333
304
334
- pub ( crate ) fn get_local_projection ( & self ) -> & [ PlaceElem < ' tcx > ] {
335
- match self {
336
- Self :: Local ( _, projection) => projection,
337
- Self :: UpVar ( ..) => {
338
- bug ! ( "get_local_projection_mut can only be called on PlaceBuilder::Local" )
339
- }
340
- }
341
- }
342
-
343
305
#[ instrument( skip( cx) , level = "debug" ) ]
344
306
pub ( crate ) fn field ( self , cx : & Builder < ' _ , ' tcx > , f : Field ) -> Self {
345
- let field_ty = match self . clone ( ) {
346
- PlaceBuilder :: Local ( local, projection) => {
347
- let base_place = PlaceBuilder :: Local ( local, projection) ;
307
+ match self . clone ( ) {
308
+ PlaceBuilder :: Local { local, projection } => {
309
+ let base_place = PlaceBuilder :: Local { local, projection } ;
348
310
let PlaceTy { ty, variant_index } =
349
311
base_place. to_place ( cx) . ty ( & cx. local_decls , cx. tcx ) ;
350
312
let base_ty = cx. tcx . normalize_erasing_regions ( cx. param_env , ty) ;
351
313
352
- PlaceBuilder :: compute_field_ty ( cx, f, base_ty, variant_index)
314
+ let field_ty = PlaceBuilder :: compute_field_ty ( cx, f, base_ty, variant_index) ;
315
+
316
+ self . project ( ProjectionElem :: Field ( f, field_ty) )
353
317
}
354
- PlaceBuilder :: UpVar ( .. ) => {
355
- let dummy_ty = cx . tcx . mk_ty_infer ( ty :: FreshTy ( 0 ) ) ;
356
- dummy_ty
318
+ PlaceBuilder :: Upvar { upvar , mut projection } => {
319
+ projection . push ( ProjectionElem :: Field ( f , ( ) ) ) ;
320
+ PlaceBuilder :: Upvar { upvar , projection }
357
321
}
358
- } ;
359
-
360
- self . project ( ProjectionElem :: Field ( f, field_ty) )
322
+ }
361
323
}
362
324
363
325
pub ( crate ) fn deref ( self ) -> Self {
@@ -375,13 +337,13 @@ impl<'tcx> PlaceBuilder<'tcx> {
375
337
#[ instrument( level = "debug" ) ]
376
338
pub ( crate ) fn project ( self , elem : PlaceElem < ' tcx > ) -> Self {
377
339
let result = match self {
378
- PlaceBuilder :: Local ( local, mut proj ) => {
379
- proj . push ( elem) ;
380
- PlaceBuilder :: Local ( local, proj )
340
+ PlaceBuilder :: Local { local, mut projection } => {
341
+ projection . push ( elem) ;
342
+ PlaceBuilder :: Local { local, projection }
381
343
}
382
- PlaceBuilder :: UpVar ( upvar, mut proj ) => {
383
- proj . push ( elem. into ( ) ) ;
384
- PlaceBuilder :: UpVar ( upvar, proj )
344
+ PlaceBuilder :: Upvar { upvar, mut projection } => {
345
+ projection . push ( elem. into ( ) ) ;
346
+ PlaceBuilder :: Upvar { upvar, projection }
385
347
}
386
348
} ;
387
349
@@ -392,14 +354,14 @@ impl<'tcx> PlaceBuilder<'tcx> {
392
354
/// Same as `.clone().project(..)` but more efficient
393
355
pub ( crate ) fn clone_project ( & self , elem : PlaceElem < ' tcx > ) -> Self {
394
356
match self {
395
- PlaceBuilder :: Local ( local, proj ) => PlaceBuilder :: Local (
396
- * local,
397
- Vec :: from_iter ( proj . iter ( ) . copied ( ) . chain ( [ elem. into ( ) ] ) ) ,
398
- ) ,
399
- PlaceBuilder :: UpVar ( upvar, proj ) => PlaceBuilder :: UpVar (
400
- * upvar,
401
- Vec :: from_iter ( proj . iter ( ) . copied ( ) . chain ( [ elem. into ( ) ] ) ) ,
402
- ) ,
357
+ PlaceBuilder :: Local { local, projection } => PlaceBuilder :: Local {
358
+ local : * local,
359
+ projection : Vec :: from_iter ( projection . iter ( ) . copied ( ) . chain ( [ elem. into ( ) ] ) ) ,
360
+ } ,
361
+ PlaceBuilder :: Upvar { upvar, projection } => PlaceBuilder :: Upvar {
362
+ upvar : * upvar,
363
+ projection : Vec :: from_iter ( projection . iter ( ) . copied ( ) . chain ( [ elem. into ( ) ] ) ) ,
364
+ } ,
403
365
}
404
366
}
405
367
@@ -463,7 +425,11 @@ impl<'tcx> PlaceBuilder<'tcx> {
463
425
f_ty
464
426
} else {
465
427
let Some ( f_ty) = substs. as_generator ( ) . prefix_tys ( ) . nth ( field. index ( ) ) else {
466
- bug ! ( "expected to take index {:?} in {:?}" , field. index( ) , substs. as_generator( ) . prefix_tys( ) . collect:: <Vec <_>>( ) ) ;
428
+ bug ! (
429
+ "expected to take index {:?} in {:?}" ,
430
+ field. index( ) ,
431
+ substs. as_generator( ) . prefix_tys( ) . collect:: <Vec <_>>( )
432
+ ) ;
467
433
} ;
468
434
469
435
f_ty
@@ -475,7 +441,7 @@ impl<'tcx> PlaceBuilder<'tcx> {
475
441
cx. tcx . normalize_erasing_regions ( cx. param_env , field_ty)
476
442
}
477
443
478
- /// Creates a `PlaceBuilder::Local` from a `PlaceBuilder::UpVar ` whose upvars
444
+ /// Creates a `PlaceBuilder::Local` from a `PlaceBuilder::Upvar ` whose upvars
479
445
/// are resolved. This function takes two kinds of projections: `local_projection`
480
446
/// contains the projections of the captured upvar and `upvar_projection` the
481
447
/// projections that are applied to the captured upvar. The main purpose of this
@@ -516,19 +482,19 @@ impl<'tcx> PlaceBuilder<'tcx> {
516
482
}
517
483
}
518
484
519
- PlaceBuilder :: Local ( local, local_projection)
485
+ PlaceBuilder :: Local { local, projection : local_projection }
520
486
}
521
487
}
522
488
523
489
impl < ' tcx > From < Local > for PlaceBuilder < ' tcx > {
524
490
fn from ( local : Local ) -> Self {
525
- Self :: Local ( local, Vec :: new ( ) )
491
+ Self :: Local { local, projection : Vec :: new ( ) }
526
492
}
527
493
}
528
494
529
495
impl < ' tcx > From < Place < ' tcx > > for PlaceBuilder < ' tcx > {
530
496
fn from ( p : Place < ' tcx > ) -> Self {
531
- Self :: Local ( p. local , p. projection . to_vec ( ) )
497
+ Self :: Local { local : p. local , projection : p. projection . to_vec ( ) }
532
498
}
533
499
}
534
500
@@ -564,13 +530,7 @@ fn project_ty<'tcx>(
564
530
( ty, None )
565
531
}
566
532
ProjectionElem :: Downcast ( _, variant_idx) => ( ty, Some ( variant_idx) ) ,
567
- ProjectionElem :: Field ( _, ty) => {
568
- if matches ! ( ty. kind( ) , ty:: Infer ( ..) ) {
569
- bug ! ( "Field ty should have been resolved" ) ;
570
- }
571
-
572
- ( ty, None )
573
- }
533
+ ProjectionElem :: Field ( _, ty) => ( ty, None ) ,
574
534
ProjectionElem :: OpaqueCast ( ..) => bug ! ( "didn't expect OpaqueCast" ) ,
575
535
}
576
536
}
@@ -836,15 +796,18 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
836
796
}
837
797
838
798
/// Lower a captured upvar. Note we might not know the actual capture index,
839
- /// so we create a place starting from `UpVar `, which will be resolved
799
+ /// so we create a place starting from `Upvar `, which will be resolved
840
800
/// once all projections that allow us to identify a capture have been applied.
841
801
fn lower_captured_upvar (
842
802
& mut self ,
843
803
block : BasicBlock ,
844
804
closure_def_id : LocalDefId ,
845
805
var_hir_id : LocalVarId ,
846
806
) -> BlockAnd < PlaceBuilder < ' tcx > > {
847
- block. and ( PlaceBuilder :: UpVar ( UpVar { var_hir_id, closure_def_id } , vec ! [ ] ) )
807
+ block. and ( PlaceBuilder :: Upvar {
808
+ upvar : Upvar { var_hir_id, closure_def_id } ,
809
+ projection : vec ! [ ] ,
810
+ } )
848
811
}
849
812
850
813
/// Lower an index expression
0 commit comments