@@ -25,14 +25,15 @@ use rustc_middle::mir::*;
2525use rustc_middle:: traits:: query:: NoSolution ;
2626use rustc_middle:: ty:: adjustment:: PointerCoercion ;
2727use rustc_middle:: ty:: cast:: CastTy ;
28+ use rustc_middle:: ty:: reborrow:: { self , CoerceSharedFieldPairError } ;
2829use rustc_middle:: ty:: {
2930 self , CanonicalUserTypeAnnotation , CanonicalUserTypeAnnotations , CoroutineArgsExt ,
3031 GenericArgsRef , Ty , TyCtxt , TypeVisitableExt , UserArgs , UserTypeAnnotationIndex , fold_regions,
3132} ;
3233use rustc_mir_dataflow:: move_paths:: MoveData ;
3334use rustc_mir_dataflow:: points:: DenseLocationMap ;
3435use rustc_span:: def_id:: CRATE_DEF_ID ;
35- use rustc_span:: { Span , Spanned , Symbol , sym} ;
36+ use rustc_span:: { Span , Spanned , sym} ;
3637use rustc_trait_selection:: infer:: InferCtxtExt ;
3738use rustc_trait_selection:: traits:: query:: type_op:: custom:: scrape_region_constraints;
3839use rustc_trait_selection:: traits:: query:: type_op:: { TypeOp , TypeOpOutput } ;
@@ -65,42 +66,6 @@ macro_rules! span_mirbug {
6566 } )
6667}
6768
68- #[ derive( Clone , Copy ) ]
69- struct ReborrowField < ' tcx > {
70- index : FieldIdx ,
71- name : Symbol ,
72- ty : Ty < ' tcx > ,
73- }
74-
75- fn reborrow_data_fields < ' tcx > (
76- tcx : TyCtxt < ' tcx > ,
77- def : ty:: AdtDef < ' tcx > ,
78- args : GenericArgsRef < ' tcx > ,
79- ) -> Vec < ReborrowField < ' tcx > > {
80- def. non_enum_variant ( )
81- . fields
82- . iter_enumerated ( )
83- . filter_map ( |( index, field) | {
84- let ty = field. ty ( tcx, args) . skip_norm_wip ( ) ;
85- ( !ty. is_phantom_data ( ) ) . then_some ( ReborrowField { index, name : field. name , ty } )
86- } )
87- . collect ( )
88- }
89-
90- fn reborrow_field_styles_match ( a : ty:: AdtDef < ' _ > , b : ty:: AdtDef < ' _ > ) -> bool {
91- a. non_enum_variant ( ) . ctor_kind ( ) == b. non_enum_variant ( ) . ctor_kind ( )
92- }
93-
94- fn find_corresponding_reborrow_field < ' a , ' tcx > (
95- source_fields : & ' a [ ReborrowField < ' tcx > ] ,
96- target : ReborrowField < ' tcx > ,
97- by_index : bool ,
98- ) -> Option < & ' a ReborrowField < ' tcx > > {
99- source_fields. iter ( ) . find ( |source| {
100- if by_index { source. index == target. index } else { source. name == target. name }
101- } )
102- }
103-
10469pub ( crate ) mod canonical;
10570pub ( crate ) mod constraint_conversion;
10671pub ( crate ) mod free_region_relations;
@@ -2607,13 +2572,15 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
26072572 category : ConstraintCategory < ' tcx > ,
26082573 ) -> Result < ( ) , ( ) > {
26092574 let tcx = self . tcx ( ) ;
2575+ let borrowed_ty = self . normalize ( ty:: Unnormalized :: new_wip ( borrowed_ty) , location) ;
2576+ let dest_ty = self . normalize ( ty:: Unnormalized :: new_wip ( dest_ty) , location) ;
26102577
26112578 if let (
26122579 ty:: Ref ( borrow_region, _, Mutability :: Mut ) ,
26132580 ty:: Ref ( ref_region, _, Mutability :: Not ) ,
26142581 ) = ( borrowed_ty. kind ( ) , dest_ty. kind ( ) )
26152582 {
2616- if let Err ( terr) = self . relate_types (
2583+ if let Err ( terr) = self . relate_types_structurally_relating_aliases (
26172584 borrowed_ty. peel_refs ( ) ,
26182585 ty:: Variance :: Covariant ,
26192586 dest_ty. peel_refs ( ) ,
@@ -2647,48 +2614,48 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
26472614 ( ty:: Adt ( borrowed_adt, borrowed_args) , ty:: Adt ( dest_adt, dest_args) )
26482615 if borrowed_adt. did ( ) != dest_adt. did ( ) =>
26492616 {
2650- let borrowed_fields = reborrow_data_fields ( tcx, * borrowed_adt, * borrowed_args) ;
2651- let dest_fields = reborrow_data_fields ( tcx, * dest_adt, * dest_args) ;
2652-
2653- if !reborrow_field_styles_match ( * borrowed_adt, * dest_adt) {
2654- span_mirbug ! (
2655- self ,
2656- borrowed_place,
2657- "generic shared reborrow has unsupported field structure: \
2658- {borrowed_ty:?} -> {dest_ty:?}"
2659- ) ;
2660- return Err ( ( ) ) ;
2661- }
2662-
2663- let by_index =
2664- matches ! ( dest_adt. non_enum_variant( ) . ctor_kind( ) , Some ( hir:: def:: CtorKind :: Fn ) ) ;
2665-
2666- for dest_field in dest_fields {
2667- let Some ( borrowed_field) =
2668- find_corresponding_reborrow_field ( & borrowed_fields, dest_field, by_index)
2669- else {
2617+ let field_pairs = match reborrow:: coerce_shared_field_pairs (
2618+ tcx,
2619+ * borrowed_adt,
2620+ * borrowed_args,
2621+ * dest_adt,
2622+ * dest_args,
2623+ ) {
2624+ Ok ( field_pairs) => field_pairs,
2625+ Err ( CoerceSharedFieldPairError :: FieldStyleMismatch ) => {
2626+ span_mirbug ! (
2627+ self ,
2628+ borrowed_place,
2629+ "generic shared reborrow has unsupported field structure: \
2630+ {borrowed_ty:?} -> {dest_ty:?}"
2631+ ) ;
2632+ return Err ( ( ) ) ;
2633+ }
2634+ Err ( CoerceSharedFieldPairError :: MissingSourceField { .. } ) => {
26702635 span_mirbug ! (
26712636 self ,
26722637 borrowed_place,
26732638 "generic shared reborrow is missing a source field: \
26742639 {borrowed_ty:?} -> {dest_ty:?}"
26752640 ) ;
26762641 return Err ( ( ) ) ;
2677- } ;
2642+ }
2643+ } ;
26782644
2645+ for field_pair in field_pairs {
26792646 self . add_generic_shared_reborrow_constraints (
26802647 location,
26812648 borrowed_place,
2682- borrowed_field . ty ,
2683- dest_field . ty ,
2649+ field_pair . source . ty ,
2650+ field_pair . target . ty ,
26842651 category,
26852652 ) ?;
26862653 }
26872654
26882655 Ok ( ( ) )
26892656 }
26902657 _ => {
2691- if let Err ( terr) = self . relate_types (
2658+ if let Err ( terr) = self . relate_types_structurally_relating_aliases (
26922659 borrowed_ty,
26932660 ty:: Variance :: Covariant ,
26942661 dest_ty,
0 commit comments