@@ -119,6 +119,11 @@ pub struct InherentAssocCandidate {
119119 pub scope : DefId ,
120120}
121121
122+ pub struct ResolvedStructPath < ' tcx > {
123+ pub res : Result < Res , ErrorGuaranteed > ,
124+ pub ty : Ty < ' tcx > ,
125+ }
126+
122127/// A context which can lower type-system entities from the [HIR][hir] to
123128/// the [`rustc_middle::ty`] representation.
124129///
@@ -2618,38 +2623,36 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26182623 ty:: Const :: new_error ( tcx, e)
26192624 } ;
26202625
2621- let ( ty, variant_did) = match qpath {
2626+ let ResolvedStructPath { res : opt_res, ty } =
2627+ self . lower_path_for_struct_expr ( qpath, span, hir_id) ;
2628+
2629+ let variant_did = match qpath {
26222630 hir:: QPath :: Resolved ( maybe_qself, path) => {
26232631 debug ! ( ?maybe_qself, ?path) ;
2624- let opt_self_ty = maybe_qself. as_ref ( ) . map ( |qself| self . lower_ty ( qself) ) ;
2625- let ty =
2626- self . lower_resolved_ty_path ( opt_self_ty, path, hir_id, PermitVariants :: Yes ) ;
26272632 let variant_did = match path. res {
26282633 Res :: Def ( DefKind :: Variant | DefKind :: Struct , did) => did,
26292634 _ => return non_adt_or_variant_res ( ) ,
26302635 } ;
26312636
2632- ( ty , variant_did)
2637+ variant_did
26332638 }
26342639 hir:: QPath :: TypeRelative ( hir_self_ty, segment) => {
26352640 debug ! ( ?hir_self_ty, ?segment) ;
2636- let self_ty = self . lower_ty ( hir_self_ty) ;
2637- let opt_res = self . lower_type_relative_ty_path (
2638- self_ty,
2639- hir_self_ty,
2640- segment,
2641- hir_id,
2642- span,
2643- PermitVariants :: Yes ,
2644- ) ;
26452641
2646- let ( ty, _, res_def_id) = match opt_res {
2647- Ok ( r @ ( _, DefKind :: Variant | DefKind :: Struct , _) ) => r,
2642+ let res_def_id = match opt_res {
2643+ Ok ( r)
2644+ if matches ! (
2645+ tcx. def_kind( r. def_id( ) ) ,
2646+ DefKind :: Variant | DefKind :: Struct
2647+ ) =>
2648+ {
2649+ r. def_id ( )
2650+ }
26482651 Ok ( _) => return non_adt_or_variant_res ( ) ,
26492652 Err ( e) => return ty:: Const :: new_error ( tcx, e) ,
26502653 } ;
26512654
2652- ( ty , res_def_id)
2655+ res_def_id
26532656 }
26542657 } ;
26552658
@@ -2710,6 +2713,41 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
27102713 ty:: Const :: new_value ( tcx, valtree, ty)
27112714 }
27122715
2716+ pub fn lower_path_for_struct_expr (
2717+ & self ,
2718+ qpath : hir:: QPath < ' tcx > ,
2719+ path_span : Span ,
2720+ hir_id : HirId ,
2721+ ) -> ResolvedStructPath < ' tcx > {
2722+ match qpath {
2723+ hir:: QPath :: Resolved ( ref maybe_qself, path) => {
2724+ let self_ty = maybe_qself. as_ref ( ) . map ( |qself| self . lower_ty ( qself) ) ;
2725+ let ty = self . lower_resolved_ty_path ( self_ty, path, hir_id, PermitVariants :: Yes ) ;
2726+ ResolvedStructPath { res : Ok ( path. res ) , ty }
2727+ }
2728+ hir:: QPath :: TypeRelative ( hir_self_ty, segment) => {
2729+ let self_ty = self . lower_ty ( hir_self_ty) ;
2730+
2731+ let result = self . lower_type_relative_ty_path (
2732+ self_ty,
2733+ hir_self_ty,
2734+ segment,
2735+ hir_id,
2736+ path_span,
2737+ PermitVariants :: Yes ,
2738+ ) ;
2739+ let ty = result
2740+ . map ( |( ty, _, _) | ty)
2741+ . unwrap_or_else ( |guar| Ty :: new_error ( self . tcx ( ) , guar) ) ;
2742+
2743+ ResolvedStructPath {
2744+ res : result. map ( |( _, kind, def_id) | Res :: Def ( kind, def_id) ) ,
2745+ ty,
2746+ }
2747+ }
2748+ }
2749+ }
2750+
27132751 /// Lower a [resolved][hir::QPath::Resolved] path to a (type-level) constant.
27142752 fn lower_resolved_const_path (
27152753 & self ,
0 commit comments