@@ -1415,9 +1415,15 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14151415 let ct = self . check_param_uses_if_mcg ( ct, span, false ) ;
14161416 Ok ( ct)
14171417 }
1418- TypeRelativePath :: Ctor { ctor_def_id, args } => {
1419- return Ok ( ty:: Const :: zero_sized ( tcx, Ty :: new_fn_def ( tcx, ctor_def_id, args) ) ) ;
1420- }
1418+ TypeRelativePath :: Ctor { ctor_def_id, args } => match tcx. def_kind ( ctor_def_id) {
1419+ DefKind :: Ctor ( _, CtorKind :: Fn ) => {
1420+ Ok ( ty:: Const :: zero_sized ( tcx, Ty :: new_fn_def ( tcx, ctor_def_id, args) ) )
1421+ }
1422+ DefKind :: Ctor ( ctor_of, CtorKind :: Const ) => {
1423+ Ok ( self . construct_const_ctor_value ( ctor_def_id, ctor_of, args) )
1424+ }
1425+ _ => unreachable ! ( ) ,
1426+ } ,
14211427 // FIXME(mgca): implement support for this once ready to support all adt ctor expressions,
14221428 // not just const ctors
14231429 TypeRelativePath :: Variant { .. } => {
@@ -1452,7 +1458,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14521458 // FIXME(mgca): do we want constructor resolutions to take priority over
14531459 // other possible resolutions?
14541460 if matches ! ( mode, LowerTypeRelativePathMode :: Const )
1455- && let Some ( ( CtorKind :: Fn , ctor_def_id) ) = variant_def. ctor
1461+ && let Some ( ( _ , ctor_def_id) ) = variant_def. ctor
14561462 {
14571463 tcx. check_stability ( variant_def. def_id , Some ( qpath_hir_id) , span, None ) ;
14581464 let _ = self . prohibit_generic_args (
@@ -2597,14 +2603,29 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
25972603 ) ;
25982604 self . lower_const_param ( def_id, hir_id)
25992605 }
2600- Res :: Def ( DefKind :: Const | DefKind :: Ctor ( _ , CtorKind :: Const ) , did) => {
2606+ Res :: Def ( DefKind :: Const , did) => {
26012607 assert_eq ! ( opt_self_ty, None ) ;
26022608 let [ leading_segments @ .., segment] = path. segments else { bug ! ( ) } ;
26032609 let _ = self
26042610 . prohibit_generic_args ( leading_segments. iter ( ) , GenericsArgsErrExtend :: None ) ;
26052611 let args = self . lower_generic_args_of_path_segment ( span, did, segment) ;
26062612 ty:: Const :: new_unevaluated ( tcx, ty:: UnevaluatedConst :: new ( did, args) )
26072613 }
2614+ Res :: Def ( DefKind :: Ctor ( ctor_of, CtorKind :: Const ) , did) => {
2615+ assert_eq ! ( opt_self_ty, None ) ;
2616+ let [ leading_segments @ .., segment] = path. segments else { bug ! ( ) } ;
2617+ let _ = self
2618+ . prohibit_generic_args ( leading_segments. iter ( ) , GenericsArgsErrExtend :: None ) ;
2619+
2620+ let parent_did = tcx. parent ( did) ;
2621+ let generics_did = match ctor_of {
2622+ CtorOf :: Variant => tcx. parent ( parent_did) ,
2623+ CtorOf :: Struct => parent_did,
2624+ } ;
2625+ let args = self . lower_generic_args_of_path_segment ( span, generics_did, segment) ;
2626+
2627+ self . construct_const_ctor_value ( did, ctor_of, args)
2628+ }
26082629 Res :: Def ( DefKind :: Ctor ( _, CtorKind :: Fn ) , did) => {
26092630 assert_eq ! ( opt_self_ty, None ) ;
26102631 let [ leading_segments @ .., segment] = path. segments else { bug ! ( ) } ;
@@ -3174,4 +3195,31 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
31743195 }
31753196 Some ( r)
31763197 }
3198+
3199+ fn construct_const_ctor_value (
3200+ & self ,
3201+ ctor_def_id : DefId ,
3202+ ctor_of : CtorOf ,
3203+ args : GenericArgsRef < ' tcx > ,
3204+ ) -> Const < ' tcx > {
3205+ let tcx = self . tcx ( ) ;
3206+ let parent_did = tcx. parent ( ctor_def_id) ;
3207+
3208+ let adt_def = tcx. adt_def ( match ctor_of {
3209+ CtorOf :: Variant => tcx. parent ( parent_did) ,
3210+ CtorOf :: Struct => parent_did,
3211+ } ) ;
3212+
3213+ let variant_idx = adt_def. variant_index_with_id ( parent_did) ;
3214+
3215+ let valtree = if adt_def. is_enum ( ) {
3216+ let discr = ty:: ValTree :: from_scalar_int ( tcx, variant_idx. as_u32 ( ) . into ( ) ) ;
3217+ ty:: ValTree :: from_branches ( tcx, [ ty:: Const :: new_value ( tcx, discr, tcx. types . u32 ) ] )
3218+ } else {
3219+ ty:: ValTree :: zst ( tcx)
3220+ } ;
3221+
3222+ let adt_ty = Ty :: new_adt ( tcx, adt_def, args) ;
3223+ ty:: Const :: new_value ( tcx, valtree, adt_ty)
3224+ }
31773225}
0 commit comments